Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix-mapping-issues #34

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions configuration.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@
"required": true
}
},
{
"key": "managerRoleCode",
"type": "input",
"defaultValue": "MGR",
"templateOptions": {
"label": "Manager Role Code",
"placeholder": "MGR",
"required": true
}
},
{
"key": "includeAssignments",
"type": "checkbox",
Expand Down
48 changes: 26 additions & 22 deletions departments.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ $WarningPreference = "Continue"
$clientId = $c.clientId
$clientSecret = $c.clientSecret
$tenantId = $c.tenantId
$managerRoleCode = $c.managerRoleCode

$Script:AuthenticationUri = "https://connect.visma.com/connect/token"
$Script:BaseUri = "https://api.youforce.com"
Expand Down Expand Up @@ -280,10 +281,21 @@ try {
Write-Verbose "Querying roleAssignments"

$roleAssignments = Invoke-RaetWebRequestList -Url "$BaseUri/iam/v1.0/roleAssignments"
# Sort Role assignments on personCode to make sure we always have the same manager with the same data
$roleAssignments = $roleAssignments | Sort-Object -Property { [int]$_.personCode }

Write-Information "Successfully queried roleAssignments. Result: $($roleAssignments.Count)"

# Filter Role assignments for only active and specific role, and sort descending on startDate and personCode to ensure consistent manager data
$currentDate = Get-Date
$roleAssignments = $roleAssignments | Where-Object {
$_.startDate -as [datetime] -le $currentDate -and
($_.endDate -eq $null -or $_.endDate -as [datetime] -ge $currentDate) -and
$_.shortName -eq $managerRoleCode
} | Sort-Object -Property { $_.startDate , [int]$_.personCode } -Descending

# Group on organizationUnit (to match to department)
$roleAssignmentsGrouped = $roleAssignments | Group-Object -Property organizationUnit -AsHashTable -AsString

Write-Information "Successfully filtered roleAssignments for only active and specific role [$($managerRoleCode)]. Result: $(@($roleAssignments).Count)"
}
catch {
$ex = $PSItem
Expand All @@ -303,30 +315,22 @@ try {
$managerActiveCompareDate = Get-Date

foreach ($organizationUnit in $organizationUnits) {
$ouRoleAssignments = $roleAssignments | Where-Object { $_.organizationUnit -eq $organizationUnit.id }
# Sort role assignments on person code to make sure the order is always the same (if the data is the same)
$ouRoleAssignments = $ouRoleAssignments | Sort-Object personCode

# Organizational units may contain multiple managers (per organizational unit). There's no way to specify which manager is primary
# We check of the manager assignment is active and then select the first one we come across that's valid
$managerId = $null
foreach ($roleAssignment in $ouRoleAssignments) {
if (![string]::IsNullOrEmpty($roleAssignment)) {
if ($roleAssignment.ShortName -eq 'MGR') {
$startDate = ([Datetime]::ParseExact($roleAssignment.startDate, 'yyyy-MM-dd', $null))
$endDate = ([Datetime]::ParseExact($roleAssignment.endDate, 'yyyy-MM-dd', $null))

if ($startDate -lt $managerActiveCompareDate -and $endDate -ge $managerActiveCompareDate ) {
$managerId = $roleAssignment.personCode
break
}
}
}
# Get manager from roleassignments
$ouRoleAssignments = $null
$ouRoleAssignments = $roleAssignmentsGrouped["$($organizationUnit.id)"]
if ($null -ne $ouRoleAssignments) {
# Organizational units may contain multiple managers (per organizational unit). There's no way to specify which manager is primary
# Therefore we always select the first one we encounter
$roleAssignment = $ouRoleAssignments | Select-Object -First 1

$managerId = $roleAssignment.personCode
}
else {
$managerId = $null
}

$department = [PSCustomObject]@{
ExternalId = $organizationUnit.id
ShortName = $organizationUnit.shortName
DisplayName = $organizationUnit.fullName
ManagerExternalId = $managerId
ParentExternalId = $organizationUnit.parentOrgUnit
Expand Down
6 changes: 3 additions & 3 deletions mapping.assignments.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@
{
"name": "Name.Convention",
"mode": "complex",
"value": "function getValue() {\r\n let returnValue = '';\r\n\r\n switch (source.nameAssembleOrder) {\r\n case \"P\": {\r\n returnValue = \"P\";\r\n break;\r\n }\r\n case \"E\": {\r\n returnValue = \"B\";\r\n break;\r\n }\r\n case \"B\": {\r\n returnValue = \"PB\";\r\n break;\r\n }\r\n case \"C\": {\r\n returnValue = \"BP\";\r\n break;\r\n }\r\n case \"D\": {\r\n returnValue = \"BP\";\r\n break;\r\n }\r\n default: {\r\n returnValue = \"B\";\r\n break;\r\n }\r\n }\r\n\r\n return returnValue;\r\n}\r\n\r\ngetValue();",
"value": "function getValue() {\r\n let returnValue = '';\r\n let convention = source.nameAssembleOrder\r\n\r\nif (typeof convention !== 'undefined' && convention ) { \r\n convention = convention.toUpperCase()\r\n \r\n switch (convention) {\r\n case \"P\": {\r\n returnValue = \"P\";\r\n break;\r\n }\r\n case \"E\": {\r\n returnValue = \"B\";\r\n break;\r\n }\r\n case \"B\": {\r\n returnValue = \"PB\";\r\n break;\r\n }\r\n case \"C\": {\r\n returnValue = \"BP\";\r\n break;\r\n }\r\n case \"D\": {\r\n returnValue = \"BP\";\r\n break;\r\n }\r\n default: {\r\n returnValue = \"B\";\r\n break;\r\n }\r\n }\r\n}else{\r\n //if convention is empty:\r\n returnValue = \"B\";\r\n}\r\n\r\n return returnValue;\r\n}\r\n\r\ngetValue();",
"validation": {
"required": false
"required": true
}
},
{
Expand Down Expand Up @@ -181,7 +181,7 @@
{
"name": "EndDate",
"mode": "complex",
"value": "function getValue(){\r\n returnValue = sourceContract.assignment_endDate;\r\n\r\n if(sourceContract.assignment_endDate == '9999-12-31'){\r\n returnValue = null;\r\n }\r\n return returnValue;\r\n}\r\n\r\ngetValue();",
"value": "function getValue(){\r\n let returnValue = sourceContract.assignment_endDate;\r\n\r\n if(sourceContract.assignment_endDate == '9999-12-31'){\r\n returnValue = null;\r\n }\r\n return returnValue;\r\n}\r\n\r\ngetValue();",
"validation": {
"required": false
}
Expand Down
2 changes: 1 addition & 1 deletion mapping.json
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@
{
"name": "EndDate",
"mode": "complex",
"value": "function getValue(){\r\n returnValue = sourceContract.assignment_endDate;\r\n\r\n if(sourceContract.assignment_endDate == '9999-12-31'){\r\n returnValue = null;\r\n }\r\n return returnValue;\r\n}\r\n\r\ngetValue();",
"value": "function getValue(){\r\n let returnValue = sourceContract.assignment_endDate;\r\n\r\n if(sourceContract.assignment_endDate == '9999-12-31'){\r\n returnValue = null;\r\n }\r\n return returnValue;\r\n}\r\n\r\ngetValue();",
"validation": {
"required": false
}
Expand Down
Loading