-
Notifications
You must be signed in to change notification settings - Fork 0
/
departments.ps1
122 lines (108 loc) · 4.56 KB
/
departments.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
######################################################
# HelloID-Conn-Prov-Source-Elanza-Departments
#
# Version: 1.0.0
######################################################
# Initialize default value's
$config = $Configuration | ConvertFrom-Json
# Set debug logging
switch ($($config.IsDebug)) {
$true { $VerbosePreference = 'Continue' }
$false { $VerbosePreference = 'SilentlyContinue' }
}
# Keep track of retrieved departments
$retrievedDepartments = [System.Collections.Generic.List[object]]::new()
#region functions
function Get-ElanzaDepartmentById {
[CmdletBinding()]
param (
[Parameter()]
$Id
)
if (-not ($retrievedDepartments | Where-Object { $_.uuid -eq $Id })) {
$departmentDetails = Invoke-ElanzaRestMethod -Uri "department/$Id"
$departmentDetails | Add-Member -MemberType 'NoteProperty' -Name 'ExternalId' -Value $departmentDetails.uuid
$departmentDetails | Add-Member -MemberType 'NoteProperty' -Name 'DisplayName' -Value $departmentDetails.name
$retrievedDepartments.Add($departmentDetails)
}
}
function Invoke-ElanzaRestMethod {
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]
$Uri
)
process {
try {
$headers = [System.Collections.Generic.Dictionary[[String], [String]]]::new()
$headers.Add("elanza-api-key-v1", "$($config.ApiKey)")
$splatParams = @{
Uri = "$($config.BaseUrl)/rest-api/v1/$Uri"
Method = 'GET'
ContentType = 'application/json'
Headers = $headers
}
Invoke-RestMethod @splatParams -Verbose:$false
} catch {
if (($Uri -like "department/*" -or $Uri -like "product/*") -and $_.Exception.Response.StatusCode -eq 'NotFound') {
$id = ($Uri -split "/")[-1]
Write-Warning "A product or department with id: [$id] cannot be found!"
} else {
$PSCmdlet.ThrowTerminatingError($_)
}
}
}
}
function Resolve-ElanzaError {
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[object]
$ErrorObject
)
process {
$httpErrorObj = [PSCustomObject]@{
ScriptLineNumber = $ErrorObject.InvocationInfo.ScriptLineNumber
Line = $ErrorObject.InvocationInfo.Line
ErrorDetails = $ErrorObject.Exception.Message
FriendlyMessage = $ErrorObject.Exception.Message
}
try {
$httpErrorObj.ErrorDetails = $ErrorObject.ErrorDetails.Message
$errorMessage = (($ErrorObject.ErrorDetails.Message | ConvertFrom-Json)).message
$httpErrorObj.FriendlyMessage = $errorMessage
} catch {
$httpErrorObj.FriendlyMessage = "Received an unexpected response. The JSON could not be converted, error: [$($_.Exception.Message)]. Original error from web service: [$($ErrorObject.Exception.Message)]"
}
Write-Output $httpErrorObj
}
}
#endregion
try {
$historicalDays = (Get-Date).ToUniversalTime().AddDays(-$($config.HistoricalDays))
$futureDays = (Get-Date).ToUniversalTime().AddDays($($config.FutureDays))
$response = Invoke-ElanzaRestMethod -Uri "plannedWorkers?from=$($historicalDays.ToString('yyyy-MM-ddTHH:mm:ssZ'))&to=$($futureDays.ToString('yyyy-MM-ddTHH:mm:ssZ'))"
foreach ($worker in $response.workers) {
foreach ($shift in $worker.shifts){
if (-not[string]::IsNullOrEmpty($shift.departmentUuid)){
Get-ElanzaDepartmentById -Id $shift.departmentUuid
}
}
foreach ($department in $retrievedDepartments){
Write-Output $department | ConvertTo-Json -Depth 10
}
}
} catch {
$ex = $PSItem
if ($($ex.Exception.GetType().FullName -eq 'Microsoft.PowerShell.Commands.HttpResponseException') -or
$($ex.Exception.GetType().FullName -eq 'System.Net.WebException')) {
$errorObj = Resolve-ElanzaError -ErrorObject $ex
Write-Verbose "Could not import Elanza departments. Error at Line '$($errorObj.ScriptLineNumber)': $($errorObj.Line). Error: $($errorObj.ErrorDetails)"
Throw "Could not import Elanza departments. Error: $($errorObj.FriendlyMessage)"
} else {
Write-Verbose "Could not import Elanza departments. Error at Line '$($ex.InvocationInfo.ScriptLineNumber)': $($ex.InvocationInfo.Line). Error: $($ex.Exception.Message)"
Throw "Could not import Elanza departments. Error: $($errorObj.FriendlyMessage)"
}
}