@worknice/js-sdk 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{_types-BXw9D0Qi.d.ts → _types-kBsjg67t.d.ts} +57 -2
- package/dist/api/ApiError.d.ts +2 -1
- package/dist/api/ApiError.js +4 -2
- package/dist/api/ApiError.js.map +1 -1
- package/dist/api/WorkniceIntegrationLogger.d.ts +41 -17
- package/dist/api/WorkniceIntegrationLogger.js +58 -28
- package/dist/api/WorkniceIntegrationLogger.js.map +1 -1
- package/dist/api/_generated.d.ts +183 -68
- package/dist/api/_generated.js +2 -0
- package/dist/api/_generated.js.map +1 -1
- package/dist/api/_types.d.ts +2 -2
- package/dist/api/createApiOperation.d.ts +1 -1
- package/dist/api/createWorkniceClient.d.ts +1 -1
- package/dist/api/createWorkniceClient.js +2 -0
- package/dist/api/createWorkniceClient.js.map +1 -1
- package/dist/api/operations/activateIntegration.d.ts +1 -1
- package/dist/api/operations/authorizeIntegration.d.ts +1 -1
- package/dist/api/operations/completeSync.d.ts +1 -1
- package/dist/api/operations/createApiToken.d.ts +1 -1
- package/dist/api/operations/createDataImport.d.ts +1 -1
- package/dist/api/operations/createPersonConnection.d.ts +8 -1
- package/dist/api/operations/createPersonConnection.js +1 -0
- package/dist/api/operations/createPersonConnection.js.map +1 -1
- package/dist/api/operations/createPersonDataImportLine.d.ts +7 -3
- package/dist/api/operations/createPersonDataImportLine.js +10 -2
- package/dist/api/operations/createPersonDataImportLine.js.map +1 -1
- package/dist/api/operations/deleteApiToken.d.ts +1 -1
- package/dist/api/operations/deletePersonConnection.d.ts +1 -1
- package/dist/api/operations/getApiTokens.d.ts +1 -1
- package/dist/api/operations/getApiTokens.js +7 -1
- package/dist/api/operations/getApiTokens.js.map +1 -1
- package/dist/api/operations/getIntegration.d.ts +1 -1
- package/dist/api/operations/getMyApps.d.ts +37 -0
- package/dist/api/operations/getMyApps.js +62 -0
- package/dist/api/operations/getMyApps.js.map +1 -0
- package/dist/api/operations/getPeople.d.ts +1 -1
- package/dist/api/operations/getPersonConnections.d.ts +8 -1
- package/dist/api/operations/getPersonConnections.js +1 -0
- package/dist/api/operations/getPersonConnections.js.map +1 -1
- package/dist/api/operations/initializeIntegration.d.ts +1 -1
- package/dist/api/operations/updatePersonConnection.d.ts +8 -1
- package/dist/api/operations/updatePersonConnection.js +1 -0
- package/dist/api/operations/updatePersonConnection.js.map +1 -1
- package/dist/employee-records/EQUALITY_CHECKS.js +23 -11
- package/dist/employee-records/EQUALITY_CHECKS.js.map +1 -1
- package/dist/employee-records/_types.d.ts +169 -149
- package/dist/employee-records/comparePersonDataTransferLines.js +61 -13
- package/dist/employee-records/comparePersonDataTransferLines.js.map +1 -1
- package/dist/employee-records/index.d.ts +9 -6
- package/dist/employee-records/index.js +22 -14
- package/dist/employee-records/index.js.map +1 -1
- package/dist/employee-records/mergePersonDataTransferLines.js +5 -9
- package/dist/employee-records/mergePersonDataTransferLines.js.map +1 -1
- package/dist/employee-records/validatePersonDataTransferLine.js +10 -2
- package/dist/employee-records/validatePersonDataTransferLine.js.map +1 -1
- package/dist/employee-records/validatePersonDataTransferLineDateOfBirth.d.ts +6 -0
- package/dist/employee-records/validatePersonDataTransferLineDateOfBirth.js +17 -0
- package/dist/employee-records/validatePersonDataTransferLineDateOfBirth.js.map +1 -0
- package/dist/employee-records/validatePersonDataTransferLineFullName.d.ts +6 -0
- package/dist/employee-records/validatePersonDataTransferLineFullName.js +20 -0
- package/dist/employee-records/validatePersonDataTransferLineFullName.js.map +1 -0
- package/dist/employee-records/validatePersonDataTransferLineGender.d.ts +6 -0
- package/dist/employee-records/validatePersonDataTransferLineGender.js +13 -0
- package/dist/employee-records/validatePersonDataTransferLineGender.js.map +1 -0
- package/dist/employee-records/validatePersonDataTransferLinePersonalEmail.d.ts +6 -0
- package/dist/employee-records/validatePersonDataTransferLinePersonalEmail.js +15 -0
- package/dist/employee-records/validatePersonDataTransferLinePersonalEmail.js.map +1 -0
- package/dist/employee-records/validatePersonDataTransferLinePersonalPhone.d.ts +6 -0
- package/dist/employee-records/validatePersonDataTransferLinePersonalPhone.js +15 -0
- package/dist/employee-records/validatePersonDataTransferLinePersonalPhone.js.map +1 -0
- package/dist/employee-records/validatePersonDataTransferLineTenure.js +1 -1
- package/dist/employee-records/validatePersonDataTransferLineTenure.js.map +1 -1
- package/dist/helpers/_types.d.ts +7 -3
- package/dist/helpers/handleApproveLeaveRequestWebhook.d.ts +15 -0
- package/dist/helpers/handleApproveLeaveRequestWebhook.js +21 -0
- package/dist/helpers/handleApproveLeaveRequestWebhook.js.map +1 -0
- package/dist/helpers/handleCancelLeaveRequestWebhook.d.ts +15 -0
- package/dist/helpers/handleCancelLeaveRequestWebhook.js +21 -0
- package/dist/helpers/handleCancelLeaveRequestWebhook.js.map +1 -0
- package/dist/helpers/handleCreateIntegrationWebhook.d.ts +4 -4
- package/dist/helpers/handleCreateIntegrationWebhook.js +7 -2
- package/dist/helpers/handleCreateIntegrationWebhook.js.map +1 -1
- package/dist/helpers/handleCreateLeaveRequestWebhook.d.ts +15 -0
- package/dist/helpers/handleCreateLeaveRequestWebhook.js +21 -0
- package/dist/helpers/handleCreateLeaveRequestWebhook.js.map +1 -0
- package/dist/helpers/handleDenyLeaveRequestWebhook.d.ts +15 -0
- package/dist/helpers/handleDenyLeaveRequestWebhook.js +21 -0
- package/dist/helpers/handleDenyLeaveRequestWebhook.js.map +1 -0
- package/dist/helpers/handleGetAuthorizationUrlWebhook.d.ts +4 -4
- package/dist/helpers/handleGetAuthorizationUrlWebhook.js +7 -2
- package/dist/helpers/handleGetAuthorizationUrlWebhook.js.map +1 -1
- package/dist/helpers/handleGetLeaveBalancesWebhook.d.ts +6 -7
- package/dist/helpers/handleGetLeaveBalancesWebhook.js +7 -2
- package/dist/helpers/handleGetLeaveBalancesWebhook.js.map +1 -1
- package/dist/helpers/handleGetLeaveCategoriesWebhook.d.ts +15 -0
- package/dist/helpers/handleGetLeaveCategoriesWebhook.js +27 -0
- package/dist/helpers/handleGetLeaveCategoriesWebhook.js.map +1 -0
- package/dist/helpers/handleGetLeaveRequestHoursWebhook.d.ts +13 -0
- package/dist/helpers/handleGetLeaveRequestHoursWebhook.js +27 -0
- package/dist/helpers/handleGetLeaveRequestHoursWebhook.js.map +1 -0
- package/dist/helpers/handleProcessLeaveRequestWebhook.d.ts +15 -0
- package/dist/helpers/handleProcessLeaveRequestWebhook.js +21 -0
- package/dist/helpers/handleProcessLeaveRequestWebhook.js.map +1 -0
- package/dist/helpers/handleRequest.d.ts +12 -0
- package/dist/helpers/handleRequest.js +58 -0
- package/dist/helpers/handleRequest.js.map +1 -0
- package/dist/helpers/handleRequestWithWorknice.d.ts +17 -8
- package/dist/helpers/handleRequestWithWorknice.js +35 -58
- package/dist/helpers/handleRequestWithWorknice.js.map +1 -1
- package/dist/helpers/handleTriggerIntegrationSyncWebhook.d.ts +15 -11
- package/dist/helpers/handleTriggerIntegrationSyncWebhook.js +211 -141
- package/dist/helpers/handleTriggerIntegrationSyncWebhook.js.map +1 -1
- package/dist/helpers/index.d.ts +9 -1
- package/dist/helpers/index.js +26 -10
- package/dist/helpers/index.js.map +1 -1
- package/dist/index.d.ts +9 -6
- package/dist/index.js +6 -8
- package/dist/index.js.map +1 -1
- package/dist/utils/isSamePlainDate.d.ts +3 -1
- package/dist/utils/isSamePlainDate.js +1 -1
- package/dist/utils/isSamePlainDate.js.map +1 -1
- package/dist/utils/isValidPlainDate.d.ts +1 -1
- package/dist/utils/isValidPlainDate.js +1 -1
- package/dist/utils/isValidPlainDate.js.map +1 -1
- package/dist/utils/parsePlainDate.d.ts +1 -1
- package/dist/utils/parsePlainDate.js +1 -1
- package/dist/utils/parsePlainDate.js.map +1 -1
- package/dist/webhooks.d.ts +71 -1
- package/package.json +4 -4
- package/dist/employee-records/validatePersonDataTransferLinePersonalDetails.d.ts +0 -6
- package/dist/employee-records/validatePersonDataTransferLinePersonalDetails.js +0 -38
- package/dist/employee-records/validatePersonDataTransferLinePersonalDetails.js.map +0 -1
- package/dist/utils/isAfter.d.ts +0 -4
- package/dist/utils/isAfter.js +0 -5
- package/dist/utils/isAfter.js.map +0 -1
|
@@ -1,11 +1,16 @@
|
|
|
1
|
+
import isInstantAfter from "@worknice/utils/temporal/isInstantAfter";
|
|
2
|
+
import isValidInstant from "@worknice/utils/temporal/isValidInstant";
|
|
1
3
|
import { Temporal } from "temporal-polyfill";
|
|
2
4
|
import { ConnectionStatus } from "../api/_types.js";
|
|
3
5
|
import comparePersonDataTransferLines from "../employee-records/comparePersonDataTransferLines.js";
|
|
4
6
|
import mergePersonDataTransferLines from "../employee-records/mergePersonDataTransferLines.js";
|
|
5
7
|
import validatePersonDataTransferLineBankAccounts from "../employee-records/validatePersonDataTransferLineBankAccounts.js";
|
|
6
|
-
import
|
|
8
|
+
import validatePersonDataTransferLineDateOfBirth from "../employee-records/validatePersonDataTransferLineDateOfBirth.js";
|
|
9
|
+
import validatePersonDataTransferLineFullName from "../employee-records/validatePersonDataTransferLineFullName.js";
|
|
10
|
+
import validatePersonDataTransferLineGender from "../employee-records/validatePersonDataTransferLineGender.js";
|
|
11
|
+
import validatePersonDataTransferLinePersonalEmail from "../employee-records/validatePersonDataTransferLinePersonalEmail.js";
|
|
12
|
+
import validatePersonDataTransferLinePersonalPhone from "../employee-records/validatePersonDataTransferLinePersonalPhone.js";
|
|
7
13
|
import validatePersonDataTransferLineResidentialAddress from "../employee-records/validatePersonDataTransferLineResidentialAddress.js";
|
|
8
|
-
import isAfter from "../utils/isAfter.js";
|
|
9
14
|
import handleRequestWithWorknice from "./handleRequestWithWorknice.js";
|
|
10
15
|
const handleTriggerIntegrationSyncWebhook = async (request, {
|
|
11
16
|
createRemotePerson,
|
|
@@ -18,9 +23,17 @@ const handleTriggerIntegrationSyncWebhook = async (request, {
|
|
|
18
23
|
request,
|
|
19
24
|
{
|
|
20
25
|
getApiToken,
|
|
21
|
-
getEnv,
|
|
22
26
|
handleRequest: async (context) => {
|
|
23
27
|
const { logger, payload, worknice } = context;
|
|
28
|
+
let cachedDataImport;
|
|
29
|
+
const getDataImport = async () => {
|
|
30
|
+
if (!cachedDataImport) {
|
|
31
|
+
cachedDataImport = await worknice.createDataImport({
|
|
32
|
+
integrationId: payload.integrationId
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
return cachedDataImport;
|
|
36
|
+
};
|
|
24
37
|
try {
|
|
25
38
|
logger.connect(worknice, payload.integrationId);
|
|
26
39
|
logger.indent("Retrieving Worknice data\u2026");
|
|
@@ -115,7 +128,7 @@ const handleTriggerIntegrationSyncWebhook = async (request, {
|
|
|
115
128
|
);
|
|
116
129
|
}
|
|
117
130
|
const matchingWorknicePerson = people.find(
|
|
118
|
-
(person) => person.employeeCode !== null && person.employeeCode === remotePerson.metadata.employeeCode || person.personalEmail !== null && person.personalEmail === remotePerson.
|
|
131
|
+
(person) => person.id === remotePerson.metadata.targetId || person.employeeCode !== null && person.employeeCode === remotePerson.metadata.employeeCode || person.personalEmail !== null && person.personalEmail === remotePerson.personalEmail?.personalEmail || person.profileEmail !== null && person.profileEmail === remotePerson.profile?.profileEmail
|
|
119
132
|
) ?? null;
|
|
120
133
|
if (config.automaticMatching === true && matchingWorknicePerson) {
|
|
121
134
|
const personConnection = personConnections.find(
|
|
@@ -222,18 +235,13 @@ const handleTriggerIntegrationSyncWebhook = async (request, {
|
|
|
222
235
|
}
|
|
223
236
|
} catch (error) {
|
|
224
237
|
logger.info(
|
|
225
|
-
`Unable to update person connection for person "${remotePersonName}" in ${config.appName} because of the following error
|
|
226
|
-
|
|
227
|
-
${error instanceof Error ? error.message : error}
|
|
228
|
-
`
|
|
238
|
+
`Unable to update person connection for person "${remotePersonName}" in ${config.appName} because of the following error:`
|
|
229
239
|
);
|
|
240
|
+
logger.error(error);
|
|
230
241
|
}
|
|
231
242
|
}
|
|
232
243
|
logger.dedent("Finished updating person connections.");
|
|
233
244
|
if (config.mode === "one-way" || config.mode === "two-way") {
|
|
234
|
-
const dataImport = await worknice.createDataImport({
|
|
235
|
-
integrationId: payload.integrationId
|
|
236
|
-
});
|
|
237
245
|
logger.indent("Updating people with connections marked as merged\u2026");
|
|
238
246
|
const mergedConnections = personConnections.filter(
|
|
239
247
|
(personConnection) => personConnection.status === ConnectionStatus.Merged
|
|
@@ -270,17 +278,25 @@ ${error instanceof Error ? error.message : error}
|
|
|
270
278
|
profileEmail: worknicePerson.profileEmail ?? null
|
|
271
279
|
}
|
|
272
280
|
};
|
|
273
|
-
const
|
|
281
|
+
const remotePersonDataTransferLine = remotePerson;
|
|
274
282
|
const comparison = comparePersonDataTransferLines(
|
|
275
283
|
worknicePersonDataTransferLine,
|
|
276
|
-
|
|
284
|
+
remotePersonDataTransferLine
|
|
277
285
|
);
|
|
278
|
-
if (comparison.hasDifferences && (comparison.sections.bankAccounts.hasDifferences || comparison.sections.
|
|
286
|
+
if (comparison.hasDifferences && (config.syncFields?.bankAccounts && comparison.sections.bankAccounts.hasDifferences || config.syncFields?.dateOfBirth && comparison.sections.dateOfBirth.hasDifferences || config.syncFields?.fullName && comparison.sections.fullName.hasDifferences || config.syncFields?.gender && comparison.sections.gender.hasDifferences || config.syncFields?.personalEmail && comparison.sections.personalEmail.hasDifferences || config.syncFields?.personalPhone && comparison.sections.personalPhone.hasDifferences || config.syncFields?.residentialAddress && comparison.sections.residentialAddress.hasDifferences)) {
|
|
279
287
|
const worknicePersonUpdatedAt = Temporal.Instant.from(worknicePerson.updatedAt);
|
|
280
|
-
|
|
288
|
+
if (!isValidInstant(remotePerson.metadata.updatedAt)) {
|
|
289
|
+
throw Error(
|
|
290
|
+
`Invalid updatedAt value for ${config.appName} person "${remotePersonName}". Must be in ISO 8601 format, including a date, a time, and a time zone offset.`,
|
|
291
|
+
{
|
|
292
|
+
cause: Error(`Unable to parse: ${remotePerson.metadata.updatedAt}`)
|
|
293
|
+
}
|
|
294
|
+
);
|
|
295
|
+
}
|
|
296
|
+
const remotePersonUpdatedAt = Temporal.Instant.from(
|
|
281
297
|
remotePerson.metadata.updatedAt
|
|
282
|
-
)
|
|
283
|
-
if (
|
|
298
|
+
);
|
|
299
|
+
if (isInstantAfter(worknicePersonUpdatedAt, remotePersonUpdatedAt)) {
|
|
284
300
|
if (config.mode === "two-way") {
|
|
285
301
|
if (!updateRemotePerson) {
|
|
286
302
|
throw Error(
|
|
@@ -295,31 +311,46 @@ ${error instanceof Error ? error.message : error}
|
|
|
295
311
|
logger.info(`Updated person "${remotePersonName}" in ${config.appName}.`);
|
|
296
312
|
} catch (error) {
|
|
297
313
|
logger.info(
|
|
298
|
-
`Unable to update person "${remotePersonName}" in ${config.appName} because of the following error
|
|
299
|
-
|
|
300
|
-
${error instanceof Error ? error.message : error}
|
|
301
|
-
`
|
|
314
|
+
`Unable to update person "${remotePersonName}" in ${config.appName} because of the following error:`
|
|
302
315
|
);
|
|
316
|
+
logger.error(error);
|
|
303
317
|
}
|
|
304
318
|
}
|
|
305
319
|
} else {
|
|
306
320
|
try {
|
|
307
|
-
const personalDetails = validatePersonDataTransferLinePersonalDetails(
|
|
308
|
-
myobPersonDataTransferLine.personalDetails
|
|
309
|
-
);
|
|
310
321
|
const bankAccounts = validatePersonDataTransferLineBankAccounts(
|
|
311
|
-
|
|
322
|
+
remotePersonDataTransferLine.bankAccounts
|
|
323
|
+
);
|
|
324
|
+
const dateOfBirth = validatePersonDataTransferLineDateOfBirth(
|
|
325
|
+
remotePersonDataTransferLine.dateOfBirth
|
|
326
|
+
);
|
|
327
|
+
const fullName = validatePersonDataTransferLineFullName(
|
|
328
|
+
remotePersonDataTransferLine.fullName
|
|
329
|
+
);
|
|
330
|
+
const gender = validatePersonDataTransferLineGender(
|
|
331
|
+
remotePersonDataTransferLine.gender
|
|
332
|
+
);
|
|
333
|
+
const personalEmail = validatePersonDataTransferLinePersonalEmail(
|
|
334
|
+
remotePersonDataTransferLine.personalEmail
|
|
335
|
+
);
|
|
336
|
+
const personalPhone = validatePersonDataTransferLinePersonalPhone(
|
|
337
|
+
remotePersonDataTransferLine.personalPhone
|
|
312
338
|
);
|
|
313
339
|
const residentialAddress = validatePersonDataTransferLineResidentialAddress(
|
|
314
|
-
|
|
340
|
+
remotePersonDataTransferLine.residentialAddress
|
|
315
341
|
);
|
|
342
|
+
const dataImport = await getDataImport();
|
|
316
343
|
await worknice.createPersonDataImportLine({
|
|
317
344
|
bankAccounts,
|
|
318
345
|
config: {
|
|
319
346
|
bankAccounts: config.syncFields?.bankAccounts ?? false,
|
|
347
|
+
dateOfBirth: config.syncFields?.dateOfBirth ?? false,
|
|
320
348
|
emergencyContacts: config.syncFields?.emergencyContacts ?? false,
|
|
321
|
-
|
|
322
|
-
|
|
349
|
+
fullName: config.syncFields?.fullName ?? false,
|
|
350
|
+
gender: config.syncFields?.gender ?? false,
|
|
351
|
+
personalEmail: config.syncFields?.personalEmail ?? false,
|
|
352
|
+
personalPhone: config.syncFields?.personalPhone ?? false,
|
|
353
|
+
postalAddress: config.syncFields?.postalAddress ?? false,
|
|
323
354
|
profile: config.syncFields?.profile ?? false,
|
|
324
355
|
remuneration: config.syncFields?.remuneration ?? false,
|
|
325
356
|
residentialAddress: config.syncFields?.residentialAddress ?? false,
|
|
@@ -328,8 +359,12 @@ ${error instanceof Error ? error.message : error}
|
|
|
328
359
|
tenure: config.syncFields?.tenure ?? false
|
|
329
360
|
},
|
|
330
361
|
dataImportId: dataImport.id,
|
|
362
|
+
dateOfBirth,
|
|
331
363
|
emergencyContacts: null,
|
|
332
|
-
|
|
364
|
+
fullName,
|
|
365
|
+
gender,
|
|
366
|
+
personalEmail,
|
|
367
|
+
personalPhone,
|
|
333
368
|
personId: worknicePerson.id,
|
|
334
369
|
postalAddress: null,
|
|
335
370
|
residentialAddress,
|
|
@@ -343,70 +378,93 @@ ${error instanceof Error ? error.message : error}
|
|
|
343
378
|
logger.info(`Updated person "${worknicePerson.displayName}" in Worknice.`);
|
|
344
379
|
} catch (error) {
|
|
345
380
|
logger.info(
|
|
346
|
-
`Unable to update person "${worknicePerson.displayName}" in Worknice because of the following error
|
|
347
|
-
|
|
348
|
-
${error instanceof Error ? error.message : error}
|
|
349
|
-
`
|
|
381
|
+
`Unable to update person "${worknicePerson.displayName}" in Worknice because of the following error:`
|
|
350
382
|
);
|
|
383
|
+
logger.error(error);
|
|
351
384
|
}
|
|
352
385
|
}
|
|
353
386
|
}
|
|
354
387
|
}
|
|
355
388
|
logger.dedent("Finished updating people with connections marked as merged.");
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
389
|
+
}
|
|
390
|
+
logger.indent("Merging unmerged connections\u2026");
|
|
391
|
+
const connectedConnections = personConnections.filter(
|
|
392
|
+
(personConnection) => personConnection.status === ConnectionStatus.Connected
|
|
393
|
+
);
|
|
394
|
+
for (const personConnection of connectedConnections) {
|
|
395
|
+
const worknicePerson = people.find(
|
|
396
|
+
(person) => person.id === personConnection.person.id
|
|
359
397
|
);
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
398
|
+
const remotePerson = remotePeople.find(
|
|
399
|
+
(person) => person.metadata.sourceId === personConnection.remote.id
|
|
400
|
+
);
|
|
401
|
+
if (!worknicePerson) {
|
|
402
|
+
throw Error(
|
|
403
|
+
`Unable to find person in Worknice with the ID "${personConnection.person.id}".`
|
|
366
404
|
);
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
}
|
|
372
|
-
if (!remotePerson) {
|
|
373
|
-
throw Error(
|
|
374
|
-
`Unable to find person in ${config.appName} with the ID "${personConnection.remote.id}".`
|
|
375
|
-
);
|
|
376
|
-
}
|
|
377
|
-
const remotePersonName = personToName(remotePerson);
|
|
378
|
-
logger.indent(
|
|
379
|
-
`Merging Worknice person "${worknicePerson.displayName}" with ${config.appName} person "${remotePersonName}"\u2026`
|
|
405
|
+
}
|
|
406
|
+
if (!remotePerson) {
|
|
407
|
+
throw Error(
|
|
408
|
+
`Unable to find person in ${config.appName} with the ID "${personConnection.remote.id}".`
|
|
380
409
|
);
|
|
381
|
-
|
|
410
|
+
}
|
|
411
|
+
const remotePersonName = personToName(remotePerson);
|
|
412
|
+
logger.indent(
|
|
413
|
+
`Merging Worknice person "${worknicePerson.displayName}" with ${config.appName} person "${remotePersonName}"\u2026`
|
|
414
|
+
);
|
|
415
|
+
try {
|
|
416
|
+
if (config.mode === "one-way" || config.mode === "two-way") {
|
|
382
417
|
const worknicePersonUpdatedAt = Temporal.Instant.from(worknicePerson.updatedAt);
|
|
383
|
-
|
|
418
|
+
if (!isValidInstant(remotePerson.metadata.updatedAt)) {
|
|
419
|
+
throw Error(
|
|
420
|
+
`Invalid updatedAt value for ${config.appName} person "${remotePersonName}". Must be in ISO 8601 format, including a date, a time, and a time zone offset.`,
|
|
421
|
+
{
|
|
422
|
+
cause: Error(`Unable to parse: ${remotePerson.metadata.updatedAt}`)
|
|
423
|
+
}
|
|
424
|
+
);
|
|
425
|
+
}
|
|
426
|
+
const remotePersonUpdatedAt = Temporal.Instant.from(
|
|
384
427
|
remotePerson.metadata.updatedAt
|
|
385
|
-
)
|
|
386
|
-
const [primaryDataTransferLine, secondaryDataTransferLine] =
|
|
428
|
+
);
|
|
429
|
+
const [primaryDataTransferLine, secondaryDataTransferLine] = isInstantAfter(
|
|
387
430
|
worknicePersonUpdatedAt,
|
|
388
|
-
|
|
431
|
+
remotePersonUpdatedAt
|
|
389
432
|
) ? [worknicePersonToPersonDataTransferLine(worknicePerson), remotePerson] : [remotePerson, worknicePersonToPersonDataTransferLine(worknicePerson)];
|
|
390
433
|
const mergedDataTransferLine = mergePersonDataTransferLines(
|
|
391
434
|
primaryDataTransferLine,
|
|
392
435
|
secondaryDataTransferLine
|
|
393
436
|
);
|
|
394
|
-
const personalDetails = validatePersonDataTransferLinePersonalDetails(
|
|
395
|
-
mergedDataTransferLine.personalDetails
|
|
396
|
-
);
|
|
397
437
|
const bankAccounts = validatePersonDataTransferLineBankAccounts(
|
|
398
438
|
mergedDataTransferLine.bankAccounts
|
|
399
439
|
);
|
|
440
|
+
const dateOfBirth = validatePersonDataTransferLineDateOfBirth(
|
|
441
|
+
mergedDataTransferLine.dateOfBirth
|
|
442
|
+
);
|
|
443
|
+
const fullName = validatePersonDataTransferLineFullName(
|
|
444
|
+
mergedDataTransferLine.fullName
|
|
445
|
+
);
|
|
446
|
+
const gender = validatePersonDataTransferLineGender(mergedDataTransferLine.gender);
|
|
447
|
+
const personalEmail = validatePersonDataTransferLinePersonalEmail(
|
|
448
|
+
mergedDataTransferLine.personalEmail
|
|
449
|
+
);
|
|
450
|
+
const personalPhone = validatePersonDataTransferLinePersonalPhone(
|
|
451
|
+
mergedDataTransferLine.personalPhone
|
|
452
|
+
);
|
|
400
453
|
const residentialAddress = validatePersonDataTransferLineResidentialAddress(
|
|
401
454
|
mergedDataTransferLine.residentialAddress
|
|
402
455
|
);
|
|
456
|
+
const dataImport = await getDataImport();
|
|
403
457
|
await worknice.createPersonDataImportLine({
|
|
404
458
|
bankAccounts,
|
|
405
459
|
config: {
|
|
406
460
|
bankAccounts: config.syncFields?.bankAccounts ?? false,
|
|
461
|
+
dateOfBirth: config.syncFields?.dateOfBirth ?? false,
|
|
407
462
|
emergencyContacts: config.syncFields?.emergencyContacts ?? false,
|
|
408
|
-
|
|
409
|
-
|
|
463
|
+
fullName: config.syncFields?.fullName ?? false,
|
|
464
|
+
gender: config.syncFields?.gender ?? false,
|
|
465
|
+
personalEmail: config.syncFields?.personalEmail ?? false,
|
|
466
|
+
personalPhone: config.syncFields?.personalPhone ?? false,
|
|
467
|
+
postalAddress: config.syncFields?.postalAddress ?? false,
|
|
410
468
|
profile: config.syncFields?.profile ?? false,
|
|
411
469
|
remuneration: config.syncFields?.remuneration ?? false,
|
|
412
470
|
residentialAddress: config.syncFields?.residentialAddress ?? false,
|
|
@@ -415,8 +473,12 @@ ${error instanceof Error ? error.message : error}
|
|
|
415
473
|
tenure: config.syncFields?.tenure ?? false
|
|
416
474
|
},
|
|
417
475
|
dataImportId: dataImport.id,
|
|
476
|
+
dateOfBirth,
|
|
418
477
|
emergencyContacts: null,
|
|
419
|
-
|
|
478
|
+
fullName,
|
|
479
|
+
gender,
|
|
480
|
+
personalEmail,
|
|
481
|
+
personalPhone,
|
|
420
482
|
personId: worknicePerson.id,
|
|
421
483
|
postalAddress: null,
|
|
422
484
|
residentialAddress,
|
|
@@ -453,82 +515,78 @@ ${error instanceof Error ? error.message : error}
|
|
|
453
515
|
);
|
|
454
516
|
logger.info(`Updated person "${remotePersonName}" in ${config.appName}.`);
|
|
455
517
|
}
|
|
456
|
-
await worknice.updatePersonConnection({
|
|
457
|
-
personConnectionId: personConnection.id,
|
|
458
|
-
personId: personConnection.person.id,
|
|
459
|
-
remote: {
|
|
460
|
-
id: remotePerson.metadata.sourceId,
|
|
461
|
-
name: remotePersonName
|
|
462
|
-
},
|
|
463
|
-
status: ConnectionStatus.Merged
|
|
464
|
-
});
|
|
465
|
-
logger.dedent(`Finished merging.`);
|
|
466
|
-
} catch (error) {
|
|
467
|
-
logger.info(
|
|
468
|
-
`Unable to merge ${config.appName} person "${remotePersonName}" and Worknice person "${worknicePerson.displayName}" because of the following error:
|
|
469
|
-
|
|
470
|
-
${error instanceof Error ? error.message : error}
|
|
471
|
-
`
|
|
472
|
-
);
|
|
473
|
-
logger.dedent();
|
|
474
518
|
}
|
|
519
|
+
await worknice.updatePersonConnection({
|
|
520
|
+
personConnectionId: personConnection.id,
|
|
521
|
+
personId: personConnection.person.id,
|
|
522
|
+
remote: {
|
|
523
|
+
id: remotePerson.metadata.sourceId,
|
|
524
|
+
name: remotePersonName
|
|
525
|
+
},
|
|
526
|
+
status: ConnectionStatus.Merged
|
|
527
|
+
});
|
|
528
|
+
logger.dedent(`Finished merging.`);
|
|
529
|
+
} catch (error) {
|
|
530
|
+
logger.info(
|
|
531
|
+
`Unable to merge ${config.appName} person "${remotePersonName}" and Worknice person "${worknicePerson.displayName}" because of the following error:`
|
|
532
|
+
);
|
|
533
|
+
logger.error(error);
|
|
534
|
+
logger.dedent();
|
|
475
535
|
}
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
536
|
+
}
|
|
537
|
+
logger.dedent("Finished merging unmerged connections.");
|
|
538
|
+
if (config.mode === "two-way") {
|
|
539
|
+
logger.indent(`Adding new people to ${config.appName}\u2026`);
|
|
540
|
+
const localOnlyConnections = personConnections.filter(
|
|
541
|
+
(personConnection) => personConnection.status === ConnectionStatus.LocalOnly
|
|
542
|
+
);
|
|
543
|
+
for (const personConnection of localOnlyConnections) {
|
|
544
|
+
const worknicePerson = people.find(
|
|
545
|
+
(person) => person.id === personConnection.person.id
|
|
481
546
|
);
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
547
|
+
if (!worknicePerson) {
|
|
548
|
+
throw Error(
|
|
549
|
+
`Unable to find person in Worknice with the ID "${personConnection.person.id}".`
|
|
485
550
|
);
|
|
486
|
-
|
|
551
|
+
}
|
|
552
|
+
try {
|
|
553
|
+
if (!createRemotePerson) {
|
|
487
554
|
throw Error(
|
|
488
|
-
|
|
555
|
+
"No createRemotePerson function supplied to handleTriggerIntegrationSyncWebhook."
|
|
489
556
|
);
|
|
490
557
|
}
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
metadata: {
|
|
501
|
-
deleted: false,
|
|
502
|
-
employeeCode: worknicePerson.employeeCode ?? null,
|
|
503
|
-
sourceId: worknicePerson.id,
|
|
504
|
-
targetId: null,
|
|
505
|
-
updatedAt: worknicePerson.updatedAt
|
|
506
|
-
},
|
|
507
|
-
profile: {
|
|
508
|
-
displayName: worknicePerson.displayName,
|
|
509
|
-
profileEmail: worknicePerson.profileEmail ?? null
|
|
510
|
-
}
|
|
558
|
+
const remote = await createRemotePerson(
|
|
559
|
+
{
|
|
560
|
+
...worknicePersonToPersonDataTransferLine(worknicePerson),
|
|
561
|
+
metadata: {
|
|
562
|
+
deleted: false,
|
|
563
|
+
employeeCode: worknicePerson.employeeCode ?? null,
|
|
564
|
+
sourceId: worknicePerson.id,
|
|
565
|
+
targetId: null,
|
|
566
|
+
updatedAt: worknicePerson.updatedAt
|
|
511
567
|
},
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
568
|
+
profile: {
|
|
569
|
+
displayName: worknicePerson.displayName,
|
|
570
|
+
profileEmail: worknicePerson.profileEmail ?? null
|
|
571
|
+
}
|
|
572
|
+
},
|
|
573
|
+
contextWithIntegration
|
|
574
|
+
);
|
|
575
|
+
await worknice.updatePersonConnection({
|
|
576
|
+
personConnectionId: personConnection.id,
|
|
577
|
+
personId: personConnection.person.id,
|
|
578
|
+
remote,
|
|
579
|
+
status: ConnectionStatus.Merged
|
|
580
|
+
});
|
|
581
|
+
logger.info(`Added new person "${remote.name}" to ${config.appName}.`);
|
|
582
|
+
} catch (error) {
|
|
583
|
+
logger.info(
|
|
584
|
+
`Unable to add person "${worknicePerson.displayName}" in Worknice to ${config.appName} because of the following error:`
|
|
585
|
+
);
|
|
586
|
+
logger.error(error);
|
|
529
587
|
}
|
|
530
|
-
logger.dedent(`Finished adding new people to ${config.appName}.`);
|
|
531
588
|
}
|
|
589
|
+
logger.dedent(`Finished adding new people to ${config.appName}.`);
|
|
532
590
|
}
|
|
533
591
|
await worknice.completeSync({ integrationId: payload.integrationId });
|
|
534
592
|
logger.info("Sync completed.");
|
|
@@ -537,13 +595,19 @@ ${error instanceof Error ? error.message : error}
|
|
|
537
595
|
throw error;
|
|
538
596
|
}
|
|
539
597
|
},
|
|
540
|
-
|
|
598
|
+
parseRequest: async (context) => {
|
|
599
|
+
const payload = await context.request.json();
|
|
600
|
+
return {
|
|
601
|
+
env: await getEnv({ ...context, payload }),
|
|
602
|
+
payload
|
|
603
|
+
};
|
|
604
|
+
}
|
|
541
605
|
},
|
|
542
606
|
options
|
|
543
607
|
);
|
|
544
608
|
const personToName = (person) => {
|
|
545
|
-
if ((person.
|
|
546
|
-
return `${person.
|
|
609
|
+
if ((person.fullName?.givenName ?? "").trim() !== "" && (person.fullName?.familyName ?? "").trim() !== "") {
|
|
610
|
+
return `${person.fullName?.givenName} ${person.fullName?.familyName}`;
|
|
547
611
|
}
|
|
548
612
|
if ((person.profile?.displayName ?? "").trim() !== "") {
|
|
549
613
|
return `${person.profile?.displayName}`;
|
|
@@ -578,15 +642,21 @@ const worknicePersonToPersonDataTransferLine = (worknicePerson) => ({
|
|
|
578
642
|
bankAccount5Name: worknicePerson.bankAccount5Name ?? null,
|
|
579
643
|
bankAccount5Number: worknicePerson.bankAccount5Number ?? null
|
|
580
644
|
} : null,
|
|
645
|
+
dateOfBirth: worknicePerson.dateOfBirth ? { dateOfBirth: worknicePerson.dateOfBirth } : null,
|
|
581
646
|
emergencyContacts: null,
|
|
582
|
-
|
|
583
|
-
dateOfBirth: worknicePerson.dateOfBirth ?? null,
|
|
647
|
+
fullName: worknicePerson.givenName ? {
|
|
584
648
|
familyName: worknicePerson.familyName ?? null,
|
|
585
|
-
gender: worknicePerson.gender ?? null,
|
|
586
649
|
givenName: worknicePerson.givenName ?? null,
|
|
587
|
-
otherGivenNames: worknicePerson.otherGivenNames ?? null
|
|
588
|
-
|
|
589
|
-
|
|
650
|
+
otherGivenNames: worknicePerson.otherGivenNames ?? null
|
|
651
|
+
} : null,
|
|
652
|
+
gender: worknicePerson.gender ? {
|
|
653
|
+
gender: worknicePerson.gender
|
|
654
|
+
} : null,
|
|
655
|
+
personalEmail: worknicePerson.personalEmail ? {
|
|
656
|
+
personalEmail: worknicePerson.personalEmail
|
|
657
|
+
} : null,
|
|
658
|
+
personalPhone: worknicePerson.personalPhone ? {
|
|
659
|
+
personalPhone: worknicePerson.personalPhone
|
|
590
660
|
} : null,
|
|
591
661
|
postalAddress: null,
|
|
592
662
|
residentialAddress: worknicePerson.residentialAddressLine1 ? {
|