generated from Ed-Fi-Exchange-OSS/Template-for-GitHub
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathStaffTxtToEdFiAPI.ps1
224 lines (186 loc) · 7.65 KB
/
StaffTxtToEdFiAPI.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# SPDX-License-Identifier: Apache-2.0
# Licensed to the Ed-Fi Alliance under one or more agreements.
# The Ed-Fi Alliance licenses this file to you under the Apache License, Version 2.0.
# See the LICENSE and NOTICES files in the project root for more information.
# Configuration: Set appopriate values
$Config = @{
CSVWorkingFile="all_data_simple.csv"
OAuthUrl="/oauth/token"
BaseApiUrl='https://api.ed-fi.org/v5.3/api'
EdFiUrl="/data/v3"
EndPoint="/ed-fi/staffs"
Key="RvcohKz9zHI4"
Secret="E1iEFusaNf81xzCxwHfbolkC"
NamesPace="uri://mybps.org"
logRootPath="Logs"
}
# Configuration: Set appopriate values
$totalCount = -1
Function PostToEdfi($dataJSON)
{
# extract the requiered parameters from the config file.
$BaseApiUrl= $Config.BaseApiUrl
$EdFiUrl= $Config.EdFiUrl
$OAuthUrl= "$BaseApiUrl" + $Config.OAuthUrl
$EndPoint= $Config.EndPoint
Write-Host " *** Getting Token ******** "
# * Get a token *
$FormData = @{
Client_id = $Config.Key
Client_secret = $Config.Secret
Grant_type = 'client_credentials'
}
$OAuthResponse = Invoke-RestMethod -Uri "$OAuthUrl" -Method Post -Body $FormData
$token = $OAuthResponse.access_token
$Headers = @{
"Accept" = "application/json"
"Authorization" = "Bearer $token"
"Content-Type" = "application/json"
}
$uri = "$BaseApiUrl" + "$EdFiUrl$EndPoint"
Write-Host "OAuthUrl *** $OAuthUrl"
Write-Host "url ***********$uri"
$i=0
foreach ($rowJSOn in $dataJSON)
{
$i++
$staffRecord = ConvertTo-Json $rowJSOn
try {
$result = Invoke-RestMethod -Uri $uri -Method Post -Headers $Headers -Body $staffRecord
Write-Host "*** RESULT: $result ***"
if($i % 500 -eq 0) {
Write-Host " Processing $i of $totalCount total Staff Assignments... "
}
}
catch {
Write-Host "$i) An error occurred: $uri"
Write-Host "$staffRecord"
Write-Host $_
}
}
Write-Host "*** DONE ***"
}
Function Create-Json()
{
Write-Host "Working file '" $Config.CSVWorkingFile "'"
$NamesPace=$Config.NamesPace
$dataJSON = (
Import-Csv $Config.CSVWorkingFile -Header EmployeeID,
FirstName,LastName|Select-Object -Skip 1|
ForEach-Object {
# $GradeLevel =NormalizeGradeLevels $_.'GradeLevel'
# $LicenseStage =NormalizeStage $_.Stage
# $LicenseStatus =NormalizeStatus $_.LicenseStatus
# $LicenseDescription =NormalizeLicenseDescription $_.Description
[PSCustomObject]@{
# StaffReference= @{staffUniqueId=[System.Security.SecurityElement]::Escape($_.StaffId)}
# SchoolYear =[System.Security.SecurityElement]::Escape($_.SchoolYear)
# EmployeeID =[System.Security.SecurityElement]::Escape($_.EmployeeID)
StaffUniqueId =[System.Security.SecurityElement]::Escape($_.EmployeeID)
FirstName = [System.Security.SecurityElement]::Escape($_.FirstName)
LastSurname = [System.Security.SecurityElement]::Escape($_.LastName)
# LicenseNumber=[System.Security.SecurityElement]::Escape($_.LicenseNo)
# LicenseIssueDate = [System.Security.SecurityElement]::Escape($_.IssueDate)
# LicenseStateIdentifier = [System.Security.SecurityElement]::Escape('MA')
# LicenseExpirationDate = [System.Security.SecurityElement]::Escape($_.ExpireDate)
# LicenseEffectiveDate= [System.Security.SecurityElement]::Escape($_.IssueDate)
# LicenseTitle= [System.Security.SecurityElement]::Escape($_.Description)
# LicenseDescription=[System.Security.SecurityElement]::Escape($LicenseDescription)
# Accomp=[System.Security.SecurityElement]::Escape($_.Accomp)
# LicenseStageDescriptor= "$NamesPace/LicenseStageDescriptor#" + [System.Security.SecurityElement]::Escape($LicenseStage)
# LicenseStatusDescriptor="$NamesPace/LicenseStatusDescriptor#" + [System.Security.SecurityElement]::Escape($LicenseStatus)
# LicensingOrganization= [System.Security.SecurityElement]::Escape("org")
}
})
$totalCount = ($dataJSON.length)
Write-Host "**** THERE ARE " ($dataJSON.length)
Write-Host "**** THIS ARE " $dataJSON
PostToEdfi $dataJSON
}
##Normalize Sheltered English Immersion - Teacher
Function NormalizeLicenseDescription($LicenseDescription)
{
if($LicenseDescription -Contains "Sheltered Eng Immersion - Tch"){
$LicenseDescription ="Sheltered English Immersion - Teacher"
}
return $LicenseDescription
}
#NormalizeGradeLevels funtion converts Grade to Grades
Function NormalizeGradeLevels($GradeLevel)
{
if($GradeLevel -Contains "Grade") {
if( -not($GradeLevel.Contains("Grades"))) {
$GradeLevel -replace "Grade", "Grades"
}
}
return $GradeLevel
}
#NormalizeStage converts Initial - Extension to Initial Extension
Function NormalizeStage($Stage)
{
if($Stage -Contains "Initial - Extension"){
$Stage ="Initial Extension"
}
if($Stage -Contains "Emergency - Extension"){
$Stage ="Emergency"
}
return $Stage
}
Function NormalizeStatus($Status)
{
if(($Status -Contains "Invalid: RETELL/SEI Restricted") -or ($Status -Contains "Invalid: RETELL/SEI Restricted") ){
$Status ="Invalid: RETELL/SEI R"
}
if(($Status -Contains "Licensed: RETELL/SEI Restricte")-or ($Status -Contains "Licensed: RETELL/SEI Restricted") ){
$Status ="Licensed: RETELL/SEI R"
}
if(($Status -Contains "Inactive/Invalid: RETELL/SEI Restricted") ){
$Status ="Inactive/Invalid: RETELL/SEI R"
}
return $Status
}
Function RenameLogOnError($logPath)
{
if($error){
Write-Host "*** An ERROR occured. Renaming Log file... ***"
$date = Get-Date -Format "MM-dd-yyyy-H-m-s"
$errorLogPath = Join-Path -Path $Config.logRootPath -ChildPath "ERROR_StaffLicenses_Log_$date.log"
#Rename-Item -Path $logPath -NewName $errorLogPath
Move-Item -Path $logPath -Destination $errorLogPath
return $errorLogPath
}
}
Function Clean20DayOldLogs()
{
$limit = (Get-Date).AddDays(-20)
$path = "Logs"
Get-ChildItem -Path $path -Recurse -Force -Include *.log | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force
}
Function CopyErrorLogToDestination($errorLogPath){
if($error){
$destination = "D:\\BPS Pub\\ftproot\\PeopleSoftFiles\\Logs\\"
Copy-Item $errorLogPath -Destination $destination
#Clean files older than 5 days in the destination.
$limit = (Get-Date).AddDays(-5)
Get-ChildItem -Path $path -Recurse -Force -Include ERROR_StaffLicenses_Log_*.log | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force
}
}
Function Init()
{
$error.clear()
# Enable Logging
New-Item -ItemType Directory -Force -Path $Config.logRootPath
$date = Get-Date -Format "MM-dd-yyyy-H-m-s"
$logPath = Join-Path -Path $Config.logRootPath -ChildPath "StaffLicenses_Log_$date.log"
Start-Transcript -Path $logPath
Write-Host "*** Initializing Ed-Fi > Staff CSV Processing. ***" -ForegroundColor Cyan
Write-Host "Cheking if the required permisions exists"
Write-Host "Creating a Json Object"
Create-Json
Stop-Transcript
$errorLogPath = RenameLogOnError $logPath
CopyErrorLogToDestination $errorLogPath
Clean20DayOldLogs
}
# Execute\Init the task
Init