11# #################################################
22# HelloID-Conn-Prov-Source-Inplanning-Persons
33#
4- # Version: 1.0 .0
4+ # Version: 1.1 .0
55# #################################################
66# Initialize default value's
77$config = $configuration | ConvertFrom-Json
88
9+ # region functions
910function Resolve-InplanningError {
1011 [CmdletBinding ()]
1112 param (
@@ -32,7 +33,7 @@ function Resolve-InplanningError {
3233 }
3334}
3435
35- try {
36+ function Retrieve-AccessToken {
3637 $pair = " $ ( $config.Username ) :$ ( $config.Password ) "
3738 $bytes = [System.Text.Encoding ]::ASCII.GetBytes($pair )
3839 $base64 = [System.Convert ]::ToBase64String($bytes )
@@ -49,48 +50,86 @@ try {
4950 Body = ' grant_type=client_credentials'
5051 }
5152
52- $accessToken = (Invoke-RestMethod @splatGetToken ).access_token
53+ $result = (Invoke-RestMethod @splatGetToken )
54+ $script :expirationTimeAccessToken = (Get-Date ).AddSeconds($result.expires_in )
55+
56+ return $result.access_token
57+ }
58+
59+ function Confirm-AccessTokenIsValid {
60+ if ($null -ne $Script :expirationTimeAccessToken ) {
61+ if ((Get-Date ) -le $Script :expirationTimeAccessToken ) {
62+ return $true
63+ }
64+ }
65+ return $false
66+ }
67+ # endregion functions
5368
69+ try {
70+ $accessToken = Retrieve- AccessToken
5471 $headers = @ {
5572 Authorization = " Bearer $ ( $accessToken ) "
5673 Accept = ' application/json; charset=utf-8'
5774 }
5875
5976 $splatGetUsers = @ {
60- Uri = " $ ( $config.BaseUrl ) /humanresources"
77+ Uri = " $ ( $config.BaseUrl ) /users?limit=0"
78+ Headers = $headers
79+ Method = ' GET'
80+ }
81+
82+ $splatGetUsers = @ {
83+ Uri = " $ ( $config.BaseUrl ) /users?limit=0"
6184 Headers = $headers
6285 Method = ' GET'
6386 }
6487
6588 $personsWebRequest = Invoke-WebRequest @splatGetUsers
6689 $personsCorrected = [Text.Encoding ]::UTF8.GetString([Text.Encoding ]::UTF8.GetBytes($personsWebRequest.content ))
67- $persons = $personsCorrected | ConvertFrom-Json
68-
69- Write-Information " Retrieved $ ( $ persons.count ) persons from the source system. "
90+ $personObjects = $personsCorrected | ConvertFrom-Json
91+ $persons = $personObjects | Where-Object active -eq " True "
92+ $persons = $ persons | Sort-Object resource - Unique
7093
7194 $today = Get-Date
7295 $startDate = $today.AddDays ( - $ ($config.HistoricalDays )).ToString(' yyyy-MM-dd' )
7396 $endDate = $today.AddDays ($ ($config.FutureDays )).ToString(' yyyy-MM-dd' )
7497
7598 foreach ($person in $persons ) {
7699 try {
100+ If (($person.resource.Length -gt 0 ) -Or ($null -ne $person.resource )){
101+
77102 # Create an empty list that will hold all shifts (contracts)
78103 $contracts = [System.Collections.Generic.List [object ]]::new()
79104
105+ # Check if token is still valid
106+ if (-not (Confirm-AccessTokenIsValid )){
107+ $accessToken = Retrieve- AccessToken
108+
109+ $headers = @ {
110+ Authorization = " Bearer $ ( $accessToken ) "
111+ Accept = ' application/json; charset=utf-8'
112+ }
113+ }
114+
80115 $splatGetUsersShifts = @ {
81- Uri = " $ ( $config.BaseUrl ) /roster/resourceRoster?resource=$ ( $person.uname ) &startDate=$ ( $startDate ) &endDate=$ ( $endDate ) "
116+ Uri = " $ ( $config.BaseUrl ) /roster/resourceRoster?resource=$ ( $person.resource ) &startDate=$ ( $startDate ) &endDate=$ ( $endDate ) "
82117 Headers = $headers
83118 Method = ' GET'
84119 }
85120
86121 $personShifts = Invoke-RestMethod @splatGetUsersShifts
87122
123+ If ($personshifts.count -gt 0 ){
124+ $counter = 0
88125 foreach ($day in $personShifts.days ) {
126+
89127 # Removes days when person has vacation
90128 if ((-not ($day.parts.count -eq 0 )) -and ($null -eq $day.absence )) {
91129
92130 $rosterDate = $day.rosterDate
93131 foreach ($part in $day.parts ) {
132+ $counter = ($counter + 1 )
94133 # Define the pattern for hh:mm-hh:mm
95134 $pattern = ' ^\d{2}:\d{2}-\d{2}:\d{2}'
96135 $time = [regex ]::Match($part.shift.uname , $pattern )
@@ -104,12 +143,22 @@ try {
104143 $endTime = ' 00:00'
105144 }
106145
146+ if ($part.prop ){
147+ $functioncode = $part.prop.uname
148+ $function = $part.prop.name
149+ } else {
150+ $functioncode = " "
151+ $function = " "
152+ }
153+
107154 $ShiftContract = @ {
108- externalId = " $ ( $person.uname ) $ ( $rosterDate ) $ ( $time ) $ ( $part.group.externalId ) "
155+ externalId = " $ ( $person.resource ) $ ( $rosterDate ) $ ( $time ) $ ( $counter ) $ ( $part.group.externalId ) "
109156 labourHist = $part.labourHist
110157 labourHistGroup = $part.labourHistGroup
111158 shift = $part.shift
112159 group = $part.group
160+ functioncode = $functioncode
161+ functionname = $function
113162 # Add the same fields as for shift. Otherwise, the HelloID mapping will fail
114163 # The value of both the 'startAt' and 'endAt' cannot be null. If empty, HelloID is unable
115164 # to determine the start/end date, resulting in the contract marked as 'active'.
@@ -124,17 +173,16 @@ try {
124173
125174 if ($contracts.Count -gt 0 ) {
126175 $personObj = [PSCustomObject ]@ {
127- ExternalId = $person.uname
176+ ExternalId = $person.resource
128177 DisplayName = " $ ( $person.firstName ) $ ( $person.lastName ) " .Trim(' ' )
129178 FirstName = $person.firstName
130179 LastName = $person.lastName
131180 Email = $person.email
132- PhoneNumber = $person.phone
133181 Contracts = $contracts
134182 }
135183 Write-Output $personObj | ConvertTo-Json - Depth 20
136- }
137- } catch {
184+ }}
185+ }} catch {
138186 $ex = $PSItem
139187 if ($ ($ex.Exception.GetType ().FullName -eq ' Microsoft.PowerShell.Commands.HttpResponseException' )) {
140188 $errorObj = Resolve-InplanningError - ErrorObject $ex
0 commit comments