Skip to content

Commit 1314e29

Browse files
committed
Added configuration and fixed contract loading when person has no contracts
1 parent d277604 commit 1314e29

2 files changed

Lines changed: 65 additions & 23 deletions

File tree

configuration.json

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
[
2+
{
3+
"key": "server",
4+
"type": "input",
5+
"defaultValue": "",
6+
"templateOptions": {
7+
"label": "SQL Server",
8+
"placeholder": "tcp:[namehere].database.windows.net,1433",
9+
"description": "Please add a SQL server to connect with",
10+
"required": true
11+
}
12+
},
13+
{
14+
"key": "database",
15+
"type": "input",
16+
"defaultValue": "",
17+
"templateOptions": {
18+
"label": "SQL Database",
19+
"placeholder": "Please enter Database name",
20+
"description": "Please add a SQL database to connect with",
21+
"required": true
22+
}
23+
},
24+
{
25+
"key": "user",
26+
"type": "input",
27+
"defaultValue": "",
28+
"templateOptions": {
29+
"label": "SQL User",
30+
"placeholder": "Please enter SQL User",
31+
"description": "Please add a SQL user to connect with",
32+
"required": true
33+
}
34+
},
35+
{
36+
"key": "password",
37+
"type": "input",
38+
"defaultValue": "",
39+
"templateOptions": {
40+
"label": "Please add a SQL user password to connect with",
41+
"type": "password",
42+
"placeholder": "Please enter a password",
43+
"required": true
44+
}
45+
}
46+
]

persons.ps1

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function GetDataFromSqlDatabase($Query, $ConnectionString) {
1+
function PerfromQuery($Query, $ConnectionString) {
22
try {
33
# Initialize connection and query information
44
# Connect to the SQL server
@@ -8,33 +8,28 @@ function GetDataFromSqlDatabase($Query, $ConnectionString) {
88
$SqlCmd.Connection = $SqlConnection;
99
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter;
1010

11+
#Query to get all person information adjust to liking#
1112
$SqlCmd.CommandText = $query;
1213
$SqlAdapter.SelectCommand = $SqlCmd;
1314

1415
$DataSet = New-Object System.Data.DataSet;
1516
$SqlAdapter.Fill($DataSet) | out-null;
16-
return $DataSet.Tables[0] | Select-Object -Property * -ExcludeProperty RowError, RowState, Table, ItemArray, HasErrors;
17+
$sqlData = $DataSet.Tables[0];
18+
return $sqlData | Select-Object -Property * -ExcludeProperty RowError, RowState, Table, ItemArray, HasErrors;
1719
}
1820
catch {
1921
Write-Error "Something went wrong while connecting to the SQL server";
2022
Write-Error $_.Exception.Message;
2123
Exit;
2224
}
23-
finally
24-
{
25-
if($SqlConnection -and $SqlConnection.State -eq [System.Data.ConnectionState]::Open)
26-
{
27-
$SqlConnection.Close()
28-
}
29-
}
3025
}
31-
32-
$server = ""
33-
$database = ""
34-
$userId = ""
35-
$password = ""
26+
$config = ConvertFrom-Json $configuration
27+
$server = $config.server
28+
$database = $config.database
29+
$userId = $config.user
30+
$password = $config.password
3631
$connectionString = "Data Source=$server;Initial Catalog=$database;User Id=$userId;Password=$password;";
37-
$languageId = 2;
32+
$languageId = "(1,2)"; # 1=NL, 2=EN
3833

3934
#Query to get all person information adjust to liking#
4035
$personQuery = "SELECT [Medewerker] as [EmployeeId]
@@ -69,7 +64,7 @@ $personQuery = "SELECT [Medewerker] as [EmployeeId]
6964
,[Datum_in_dienst] as [EmploymentStartDate]
7065
,[Datum_uit_dienst] as [EmploymentEndDate]
7166
,[MultipleContracts] as [HasMultipleContracts]
72-
FROM [dbo].[T4E_IAM_Persons] WHERE [lang_id] = $languageId
67+
FROM [dbo].[T4E_IAM_Persons] WHERE [lang_id] in $languageId
7368
ORDER by [Medewerker] asc";
7469

7570

@@ -86,28 +81,28 @@ $contractsQuery = "SELECT [Medewerker] as [EmployeeId]
8681
,[Kostenplaats] as [CostCenter]
8782
,[Leidinggevende] as [PrimaryManagerId]
8883
,[Vervangende_leidinggevende] as [SecondaryManagerId]
89-
FROM [dbo].[T4E_IAM_Contracts] WHERE [lang_id] = $languageId"
84+
FROM [dbo].[T4E_IAM_Contracts] WHERE [lang_id] in $languageId"
9085

9186

9287
#Query to get all function information adjust to liking#
9388
$functionsQuery = "SELECT [Functie] as [Id]
9489
,[Omschrijving] as [Description]
95-
FROM [dbo].[T4E_IAM_OrganizationalFunctions] WHERE [lang_id] = $languageId"
90+
FROM [dbo].[T4E_IAM_OrganizationalFunctions] WHERE [lang_id] in $languageId"
9691

9792
#Query to get all department information adjust to liking#
9893
$departmentsQuery = "SELECT [Organisatorische_eenheid] as [Id]
9994
,[Omschrijving] as [DisplayName]
10095
,[Omschrijving] as [Name]
10196
,[Leidinggevende] as [ManagerId]
10297
,[Parent] as [ParentDepartmentId]
103-
FROM [dbo].[T4E_IAM_OrganizationalUnits] WHERE [lang_id] = $languageId"
98+
FROM [dbo].[T4E_IAM_OrganizationalUnits] WHERE [lang_id] in $languageId"
10499

105100

106-
$persons = GetDataFromSqlDatabase -Query $personQuery -ConnectionString $connectionString
101+
$persons = PerfromQuery -Query $personQuery -ConnectionString $connectionString
107102
#$personLookup = $persons | ForEach-Object { $_ } #copy persons
108-
$contracts = GetDataFromSqlDatabase -Query $contractsQuery -ConnectionString $connectionString
109-
$functions = GetDataFromSqlDatabase -Query $functionsQuery -ConnectionString $connectionString
110-
$departments = GetDataFromSqlDatabase -Query $departmentsQuery -ConnectionString $connectionString
103+
$contracts = PerfromQuery -Query $contractsQuery -ConnectionString $connectionString
104+
$functions = PerfromQuery -Query $functionsQuery -ConnectionString $connectionString
105+
$departments = PerfromQuery -Query $departmentsQuery -ConnectionString $connectionString
111106

112107

113108
Foreach ($person in $persons) {
@@ -118,6 +113,7 @@ Foreach ($person in $persons) {
118113

119114
#link contracts
120115
[array]$contractsOfPerson = $($contracts | Where-Object { $_.EmployeeId -eq $person.EmployeeId })
116+
if($null -eq $contractsOfPerson){$contractsOfPerson = @()}
121117
$person | Add-Member -Name "Contracts" -MemberType NoteProperty -Value $contractsOfPerson -Force;
122118

123119
foreach ($contract in $person.Contracts) {

0 commit comments

Comments
 (0)