generated from Ed-Fi-Exchange-OSS/Template-for-GitHub
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathStaffAssignationDataTxtToEdFiAPI.ps1
262 lines (208 loc) · 9.07 KB
/
StaffAssignationDataTxtToEdFiAPI.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# 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.
$ISD = "Garland ISD"
$lastDataInfoFile = "lastDataInfo.json"
# Configuration: Set appopriate values
$Config = @{
CSVWorkingFile = "all_data.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, $endPoint) {
# Extract the requiered parameters from the config file.
$BaseApiUrl = $Config.BaseApiUrl
$EdFiUrl = $Config.EdFiUrl
$OAuthUrl = "$BaseApiUrl" + $Config.OAuthUrl
# $EndPoint= $Config.EndPoint
$EndPoint = $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 SchoolYear, SchoolLevel, SchoolNumber, SchoolName, Job,
PositionTitle, EmployeeID, FirstName, LastName, StartDate, EndDate,
ReasonEndDate, Age, TotYrsExp, Gender, Race, TRSYrs, RetElig | Select-Object -Skip 1 |
ForEach-Object {
[PSCustomObject]@{
SchoolYear = [System.Security.SecurityElement]::Escape($_.SchoolYear)
SchoolLevel = [System.Security.SecurityElement]::Escape($_.SchoolLevel)
SchoolNumber = [System.Security.SecurityElement]::Escape($_.SchoolNumber)
SchoolName = [System.Security.SecurityElement]::Escape($_.SchoolName)
Job = [System.Security.SecurityElement]::Escape($_.Job)
PositionTitle = [System.Security.SecurityElement]::Escape($_.PositionTitle)
StaffUniqueId = [System.Security.SecurityElement]::Escape($_.EmployeeID)
FirstName = [System.Security.SecurityElement]::Escape($_.FirstName)
LastSurname = [System.Security.SecurityElement]::Escape($_.LastName)
StartDate = [System.Security.SecurityElement]::Escape($_.StartDate)
EndDate = [System.Security.SecurityElement]::Escape($_.EndDate)
ReasonEndDate = [System.Security.SecurityElement]::Escape($_.ReasonEndDate)
Age = [System.Security.SecurityElement]::Escape($_.Age)
TotYrsExp = [System.Security.SecurityElement]::Escape($_.TotYrsExp)
Gender = [System.Security.SecurityElement]::Escape($_.Gender)
Race = [System.Security.SecurityElement]::Escape($_.Race)
TRSYrs = [System.Security.SecurityElement]::Escape($_.TRSYrs)
RetElig = [System.Security.SecurityElement]::Escape($_.RetElig)
# LicenseStageDescriptor= "$NamesPace/LicenseStageDescriptor#" + [System.Security.SecurityElement]::Escape($LicenseStage)
# LicenseStatusDescriptor="$NamesPace/LicenseStatusDescriptor#" + [System.Security.SecurityElement]::Escape($LicenseStatus)
}
})
$totalCount = ($dataJSON.length)
Write-Host "**** THERE ARE " ($dataJSON.length)
Write-Host "**** THIS ARE " $dataJSON
# PostToEdfi $dataJSON
ProcessStaff $dataJSON
ProcessSchools $dataJSON
ProcessAssignments $dataJSON
}
##Process Staff
Function ProcessStaff($jsonData) {
Write-Host "**** PROCESSING STAFF ***"
$staffRecords = ($jsonData | ForEach-Object {
[PSCustomObject]@{
StaffUniqueId = [System.Security.SecurityElement]::Escape($_.StaffUniqueId)
FirstName = [System.Security.SecurityElement]::Escape($_.FirstName)
LastSurname = [System.Security.SecurityElement]::Escape($_.LastSurname)
}
})
Write-Host "**** Posting STAFF *** $staffRecords"
$endPoint = "/ed-fi/staffs"
PostToEdfi $staffRecords $endPoint
}
##Process EducationOrganization
Function ProcessSchools($jsonData) {
Write-Host "**** PROCESSING STAFF ***"
$schoolRecords = ($jsonData | ForEach-Object {
[PSCustomObject]@{
EducationOrganizationId = [System.Security.SecurityElement]::Escape($_.SchoolNumber)
NameOfInstitution = [System.Security.SecurityElement]::Escape($_.SchoolName)
Discriminator = "edfi.School"
}
})
Write-Host "**** Posting STAFF *** $schoolRecords"
$endPoint = "/ed-fi/schools"
PostToEdfi $schoolRecords $endPoint
}
##Process EducationOrganizationAssignments
Function ProcessSchools($jsonData) {
Write-Host "**** PROCESSING STAFF ***"
$schoolRecords = ($jsonData | ForEach-Object {
[PSCustomObject]@{
EducationOrganizationId = [System.Security.SecurityElement]::Escape($_.SchoolNumber)
NameOfInstitution = [System.Security.SecurityElement]::Escape($_.SchoolName)
Discriminator = "edfi.School"
}
})
Write-Host "**** Posting STAFF *** $schoolRecords"
$endPoint = "/ed-fi/schools"
PostToEdfi $schoolRecords $endPoint
}
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 = "C:\\Users\\Juan\\Workspace\\DDN\\Leadership-Profile\\Logs"
# $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 UpdateLastDataInfoFile() {
Param (
[Parameter(Mandatory = $true)]
[string] $isd,
[Parameter(Mandatory = $true)]
[int] $ItemsProccessed,
[Parameter(Mandatory = $true)]
[string] $File
)
$lastDataInfo = @"
{
"ISD": "Garland ISD",
"Date": "$(Get-Date (Get-Date).ToUniversalTime() -UFormat '+%Y-%m-%dT%H:%M:%S.000Z')",
"ItemsProccessed": $ItemsProccessed
}
"@
Write-Host $lastDataInfo
$lastDataInfo | Out-File $File
}
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
Write-Host "Creating JSon file with ingest info"
UpdateLastDataInfoFile $ISD $totalCount $lastDataInfoFile
Stop-Transcript
$errorLogPath = RenameLogOnError $logPath
CopyErrorLogToDestination $errorLogPath
Clean20DayOldLogs
}
# Execute\Init the task
Init