This repository was archived by the owner on Sep 2, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImportUsersToTravelSystem.cs
More file actions
145 lines (124 loc) · 5.8 KB
/
ImportUsersToTravelSystem.cs
File metadata and controls
145 lines (124 loc) · 5.8 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Oracle.ManagedDataAccess.Client;
using OracleClientExtensions;
using TravelSystemIntegration.Helpers;
using TravelSystemIntegration.Models;
namespace TravelSystemIntegration
{
public static class ImportUsersToTravelSystem
{
private static string _oracleConnectionString;
private static string _serviceRoot;
private static string _base64UsernamePassword;
private static OracleConnection _conn;
private static string _employeeSql;
[FunctionName("ImportUsersToTravelSystem")]
public static async Task Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, ILogger log)
{
log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
ConfigOracleConnection(log);
try
{
await _conn.OpenAsync();
SetOracleSessionProperties();
var employeeDbs = await _conn.ExecuteEntitiesAsync<EmployeeDB>(_employeeSql);
await CreateAndUpdateEmployeesAsync(employeeDbs, log);
}
catch (OracleException ex)
{
log.LogError(ex.Message);
}
catch (Exception ex)
{
log.LogError(ex.Message);
}
finally
{
log.LogInformation($"Import Done");
await _conn.CloseAsync();
await _conn.DisposeAsync();
}
}
private static void ConfigOracleConnection(ILogger log)
{
var config = new ConfigurationBuilder().AddEnvironmentVariables().Build();
_oracleConnectionString = config["connectionString"];
_serviceRoot = config["serviceRoot"];
_base64UsernamePassword = config["base64UsernamePassword"];
_employeeSql = config["employeeSql"];
log.LogInformation($"_connectionString: {_oracleConnectionString ?? null}");
log.LogInformation($"_serviceRoot: {_serviceRoot ?? null}");
log.LogInformation($"_base64UsernamePassword: {_base64UsernamePassword ?? null}");
_conn = new OracleConnection(connectionString: _oracleConnectionString);
}
private static void SetOracleSessionProperties()
{
// Get session info from connection object
OracleGlobalization info = _conn.GetSessionInfo();
// Set the session's DateFormat for output
info.DateFormat = "DD.MM.RRRR";
_conn.SetSessionInfo(info);
}
private static async Task UpdateTimestampEmployee(string companyCode, string vismaUsername)
{
await _conn.ExecuteNonQueryAsync($"BEGIN agresso5.set_res_date_loaded('{companyCode}', '{vismaUsername}', CURRENT_DATE); END; ");
}
private static async Task CreateAndUpdateEmployeesAsync(IEnumerable<EmployeeDB> aviRrEmployees, ILogger log)
{
var travelSystemApi = new TravelSystemApi(_serviceRoot, _base64UsernamePassword);
var companies = await travelSystemApi.GetCompaniesEntityAsync();
foreach (var aviRrEmployee in aviRrEmployees)
{
var employee = await travelSystemApi.GetEmployeesEntityFilteredByEmailAsync(aviRrEmployee.E_MAIL);
if (employee == null)
{
log.LogInformation($"Couldn't find employee with email {aviRrEmployee.E_MAIL}");
continue;
}
var company = companies.value.FirstOrDefault(c => c.CompanyRefImport == aviRrEmployee.FIRMA);
if (company == null)
{
log.LogInformation($"Couldn't find company with id {aviRrEmployee.FIRMA}. Employee Id:{aviRrEmployee.E_MAIL}");
continue;
}
if (employee.value.Count > 0)
{
await UpdateEmployeeInTravelSystem(aviRrEmployee, employee, company, travelSystemApi);
}
else
{
await CreateEmployeeInTravelSystem(aviRrEmployee, company, travelSystemApi);
}
}
}
private static async Task CreateEmployeeInTravelSystem(EmployeeDB aviRrEmployee, CompanyDto.Company company,
TravelSystemApi travelSystemApi)
{
//create
var empl = aviRrEmployee.ToEmployee(company.CompanyId.Value);
var ret = await travelSystemApi.PostEmployeeEntityWithExtraFieldsAsync(empl);
if (ret?.EmployeeId != null)
{
await travelSystemApi.PatchEmployeeEntityWithEmployeeAccessAsync(empl);
await UpdateTimestampEmployee(aviRrEmployee.FIRMA, aviRrEmployee.RESSNR);
}
}
private static async Task UpdateEmployeeInTravelSystem(EmployeeDB aviRrEmployee, EmployeeEntityDto employee,
CompanyDto.Company company, TravelSystemApi travelSystemApi)
{
//Update
var empl = aviRrEmployee.ToEmployee(employee.value.FirstOrDefault(), company.CompanyId.Value);
var retPut = await travelSystemApi.PutEmployeeEntityWithExtraFieldsAsync(empl);
//BUG UNIT4: temporary workaround: NOT ABLE TO CHANGE IsActive field with PUT Method (PutEmployeeEntityWithExtraFieldsAsync)
var retPatch = await travelSystemApi.PatchEmployeeEntityWithExtraFieldsAsync(empl.ToEmployeeEntityPatch());
await travelSystemApi.PatchEmployeeEntityWithEmployeeAccessAsync(empl);
await UpdateTimestampEmployee(aviRrEmployee.FIRMA, aviRrEmployee.RESSNR);
}
}
}