@worknice/js-sdk 0.0.5 → 0.0.7

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.
Files changed (71) hide show
  1. package/dist/{_types-kBsjg67t.d.ts → _types-Cobjq3B-.d.ts} +64 -0
  2. package/dist/api/ApiError.js +3 -3
  3. package/dist/api/ApiError.js.map +1 -1
  4. package/dist/api/WorkniceIntegrationLogger.d.ts +3 -1
  5. package/dist/api/WorkniceIntegrationLogger.js.map +1 -1
  6. package/dist/api/_generated.d.ts +261 -40
  7. package/dist/api/_generated.js +15 -12
  8. package/dist/api/_generated.js.map +1 -1
  9. package/dist/api/_types.d.ts +2 -2
  10. package/dist/api/createApiOperation.d.ts +1 -1
  11. package/dist/api/createWorkniceClient.d.ts +1 -1
  12. package/dist/api/createWorkniceClient.js +2 -0
  13. package/dist/api/createWorkniceClient.js.map +1 -1
  14. package/dist/api/operations/activateIntegration.d.ts +1 -1
  15. package/dist/api/operations/authorizeIntegration.d.ts +1 -1
  16. package/dist/api/operations/completeSync.d.ts +1 -1
  17. package/dist/api/operations/createApiToken.d.ts +1 -1
  18. package/dist/api/operations/createDataImport.d.ts +1 -1
  19. package/dist/api/operations/createPersonConnection.d.ts +1 -1
  20. package/dist/api/operations/createPersonDataImportLine.d.ts +1 -1
  21. package/dist/api/operations/deleteApiToken.d.ts +1 -1
  22. package/dist/api/operations/deletePersonConnection.d.ts +1 -1
  23. package/dist/api/operations/getApiTokens.d.ts +1 -1
  24. package/dist/api/operations/getIntegration.d.ts +1 -1
  25. package/dist/api/operations/getMyApps.d.ts +1 -1
  26. package/dist/api/operations/getPeople.d.ts +2 -1
  27. package/dist/api/operations/getPeople.js +1 -0
  28. package/dist/api/operations/getPeople.js.map +1 -1
  29. package/dist/api/operations/getPersonConnections.d.ts +1 -1
  30. package/dist/api/operations/initializeIntegration.d.ts +1 -1
  31. package/dist/api/operations/updatePerson.d.ts +70 -0
  32. package/dist/api/operations/updatePerson.js +90 -0
  33. package/dist/api/operations/updatePerson.js.map +1 -0
  34. package/dist/api/operations/updatePersonConnection.d.ts +1 -1
  35. package/dist/api/operations/updatePersonConnection.js.map +1 -1
  36. package/dist/employee-records/EQUALITY_CHECKS.js +5 -0
  37. package/dist/employee-records/EQUALITY_CHECKS.js.map +1 -1
  38. package/dist/employee-records/_types.d.ts +11 -5
  39. package/dist/employee-records/comparePersonDataTransferLines.js +14 -0
  40. package/dist/employee-records/comparePersonDataTransferLines.js.map +1 -1
  41. package/dist/employee-records/index.d.ts +1 -1
  42. package/dist/employee-records/mergePersonDataTransferLines.js +1 -0
  43. package/dist/employee-records/mergePersonDataTransferLines.js.map +1 -1
  44. package/dist/employee-records/validatePersonDataTransferLine.js +2 -0
  45. package/dist/employee-records/validatePersonDataTransferLine.js.map +1 -1
  46. package/dist/employee-records/validatePersonDataTransferLineProfile.d.ts +6 -0
  47. package/dist/employee-records/validatePersonDataTransferLineProfile.js +17 -0
  48. package/dist/employee-records/validatePersonDataTransferLineProfile.js.map +1 -0
  49. package/dist/helpers/_types.d.ts +1 -1
  50. package/dist/helpers/handleApproveLeaveRequestWebhook.d.ts +1 -1
  51. package/dist/helpers/handleCancelLeaveRequestWebhook.d.ts +1 -1
  52. package/dist/helpers/handleCreateIntegrationWebhook.d.ts +4 -2
  53. package/dist/helpers/handleCreateIntegrationWebhook.js +17 -2
  54. package/dist/helpers/handleCreateIntegrationWebhook.js.map +1 -1
  55. package/dist/helpers/handleCreateLeaveRequestWebhook.d.ts +1 -1
  56. package/dist/helpers/handleDenyLeaveRequestWebhook.d.ts +1 -1
  57. package/dist/helpers/handleGetAuthorizationUrlWebhook.d.ts +1 -1
  58. package/dist/helpers/handleGetLeaveBalancesWebhook.d.ts +1 -1
  59. package/dist/helpers/handleGetLeaveCategoriesWebhook.d.ts +1 -1
  60. package/dist/helpers/handleGetLeaveRequestHoursWebhook.d.ts +1 -1
  61. package/dist/helpers/handleProcessLeaveRequestWebhook.d.ts +1 -1
  62. package/dist/helpers/handleRequest.d.ts +1 -1
  63. package/dist/helpers/handleRequestWithWorknice.d.ts +1 -1
  64. package/dist/helpers/handleTriggerIntegrationSyncWebhook.d.ts +7 -8
  65. package/dist/helpers/handleTriggerIntegrationSyncWebhook.js +185 -117
  66. package/dist/helpers/handleTriggerIntegrationSyncWebhook.js.map +1 -1
  67. package/dist/helpers/index.d.ts +1 -1
  68. package/dist/index.d.ts +1 -1
  69. package/dist/utils/errorToString.js +6 -4
  70. package/dist/utils/errorToString.js.map +1 -1
  71. package/package.json +7 -4
@@ -1,14 +1,16 @@
1
- import { W as WorkniceClient } from '../_types-kBsjg67t.js';
2
- import { PartialPersonDataTransferLine } from '../employee-records/_types.js';
1
+ import { W as WorkniceClient } from '../_types-Cobjq3B-.js';
2
+ import { PersonDataTransferLine } from '../employee-records/_types.js';
3
3
  import { TriggerIntegrationSyncRequestPayload } from '../webhooks.js';
4
4
  import { HandlerOptions, TaskContext } from './_types.js';
5
- import '../api/_generated.js';
5
+ import { ConnectionStatus } from '../api/_generated.js';
6
6
  import '../api/WorkniceIntegrationLogger.js';
7
7
 
8
8
  type Context<Env, Req extends Request = Request> = TaskContext<TriggerIntegrationSyncRequestPayload, Env, Req> & {
9
9
  integration: Awaited<ReturnType<WorkniceClient["getIntegration"]>>;
10
10
  };
11
- type Person = PartialPersonDataTransferLine & {
11
+ type Person = Omit<PersonDataTransferLine, "profile"> & {
12
+ profile: NonNullable<PersonDataTransferLine["profile"]>;
13
+ } & {
12
14
  metadata: {
13
15
  deleted: boolean;
14
16
  employeeCode: string | null;
@@ -16,15 +18,12 @@ type Person = PartialPersonDataTransferLine & {
16
18
  targetId: string | null;
17
19
  updatedAt: string;
18
20
  };
19
- profile: {
20
- displayName: string;
21
- profileEmail: string | null;
22
- } | null;
23
21
  };
24
22
  type Tasks<Env, Req extends Request = Request> = {
25
23
  createRemotePerson?: (person: Person, context: Context<Env, Req>) => Promise<{
26
24
  id: string;
27
25
  name: string;
26
+ status?: ConnectionStatus.Connected | ConnectionStatus.Merged;
28
27
  }>;
29
28
  getApiToken: (context: Pick<Context<Env, Req>, "env" | "logger" | "payload" | "request">) => Promise<string>;
30
29
  getConfig: (context: Context<Env, Req>) => Promise<Config>;
@@ -4,14 +4,17 @@ import { Temporal } from "temporal-polyfill";
4
4
  import { ConnectionStatus } from "../api/_types.js";
5
5
  import comparePersonDataTransferLines from "../employee-records/comparePersonDataTransferLines.js";
6
6
  import mergePersonDataTransferLines from "../employee-records/mergePersonDataTransferLines.js";
7
+ import validatePersonDataTransferLine from "../employee-records/validatePersonDataTransferLine.js";
7
8
  import validatePersonDataTransferLineBankAccounts from "../employee-records/validatePersonDataTransferLineBankAccounts.js";
8
9
  import validatePersonDataTransferLineDateOfBirth from "../employee-records/validatePersonDataTransferLineDateOfBirth.js";
9
10
  import validatePersonDataTransferLineFullName from "../employee-records/validatePersonDataTransferLineFullName.js";
10
11
  import validatePersonDataTransferLineGender from "../employee-records/validatePersonDataTransferLineGender.js";
11
12
  import validatePersonDataTransferLinePersonalEmail from "../employee-records/validatePersonDataTransferLinePersonalEmail.js";
12
13
  import validatePersonDataTransferLinePersonalPhone from "../employee-records/validatePersonDataTransferLinePersonalPhone.js";
14
+ import validatePersonDataTransferLineProfile from "../employee-records/validatePersonDataTransferLineProfile.js";
13
15
  import validatePersonDataTransferLineResidentialAddress from "../employee-records/validatePersonDataTransferLineResidentialAddress.js";
14
16
  import handleRequestWithWorknice from "./handleRequestWithWorknice.js";
17
+ import validatePersonDataTransferLineTaxDetails from "../employee-records/validatePersonDataTransferLineTaxDetails.js";
15
18
  const handleTriggerIntegrationSyncWebhook = async (request, {
16
19
  createRemotePerson,
17
20
  getApiToken,
@@ -68,7 +71,13 @@ const handleTriggerIntegrationSyncWebhook = async (request, {
68
71
  logger.info(`Using a "${config.mode}" sync mode.`);
69
72
  logger.dedent("Finished loading configuration details.");
70
73
  logger.indent(`Retrieving ${config.appName} data\u2026`);
71
- const remotePeople = await getRemotePeople(contextWithIntegration);
74
+ let remotePeople;
75
+ try {
76
+ remotePeople = await getRemotePeople(contextWithIntegration);
77
+ } catch (error) {
78
+ logger.info(`An error occurred while retrieving data from ${config.appName}.`);
79
+ throw error;
80
+ }
72
81
  logger.info(`Retrieved ${remotePeople.length} people from ${config.appName}.`);
73
82
  logger.dedent(`Completed retrieving ${config.appName} data.`);
74
83
  logger.indent("Updating person connections\u2026");
@@ -84,7 +93,7 @@ const handleTriggerIntegrationSyncWebhook = async (request, {
84
93
  personConnectionId: remotePersonConnection.id
85
94
  });
86
95
  logger.info(
87
- `Deleted connection for "${remotePersonName}" in ${config.appName} because they have been terminated.`
96
+ `Deleted connection for ${config.appName} person "${remotePersonName}" because they have been terminated.`
88
97
  );
89
98
  }
90
99
  } else {
@@ -136,7 +145,7 @@ const handleTriggerIntegrationSyncWebhook = async (request, {
136
145
  );
137
146
  if (personConnection?.status === ConnectionStatus.Connected || personConnection?.status === ConnectionStatus.Merged) {
138
147
  logger.info(
139
- `Unable to automatically match person "${matchingWorknicePerson.displayName}" in Worknice to person "${remotePersonName}" in ${config.appName} because the Worknice person has already been matched to a different ${config.appName} person.`
148
+ `Unable to automatically match Worknice person "${matchingWorknicePerson.displayName}" to ${config.appName} person "${remotePersonName}" because the Worknice person has already been matched to a different ${config.appName} person.`
140
149
  );
141
150
  } else {
142
151
  if (personConnection) {
@@ -160,7 +169,7 @@ const handleTriggerIntegrationSyncWebhook = async (request, {
160
169
  updatedConnection
161
170
  );
162
171
  logger.info(
163
- `Automatically matched and combined person connections for person "${matchingWorknicePerson.displayName}" in Worknice and person "${remotePersonName}" in ${config.appName}.`
172
+ `Automatically matched and combined person connections for Worknice person "${matchingWorknicePerson.displayName}" and ${config.appName} person "${remotePersonName}".`
164
173
  );
165
174
  } else {
166
175
  const updatedConnection = await worknice.updatePersonConnection({
@@ -178,7 +187,7 @@ const handleTriggerIntegrationSyncWebhook = async (request, {
178
187
  updatedConnection
179
188
  );
180
189
  logger.info(
181
- `Automatically matched existing person connection for person "${matchingWorknicePerson.displayName}" in Worknice to person "${remotePersonName}" in ${config.appName}.`
190
+ `Automatically matched existing person connection for Worknice person "${matchingWorknicePerson.displayName}" to ${config.appName} person "${remotePersonName}".`
182
191
  );
183
192
  }
184
193
  } else {
@@ -198,7 +207,7 @@ const handleTriggerIntegrationSyncWebhook = async (request, {
198
207
  updatedConnection
199
208
  );
200
209
  logger.info(
201
- `Automatically matched existing person connection for person "${remotePersonName}" in ${config.appName} to person "${matchingWorknicePerson.displayName}" in Worknice.`
210
+ `Automatically matched existing person connection for ${config.appName} person "${remotePersonName}" to Worknice person "${matchingWorknicePerson.displayName}".`
202
211
  );
203
212
  } else {
204
213
  const newPersonConnection = await worknice.createPersonConnection({
@@ -212,7 +221,7 @@ const handleTriggerIntegrationSyncWebhook = async (request, {
212
221
  });
213
222
  personConnections.push(newPersonConnection);
214
223
  logger.info(
215
- `Created new person connection for person "${remotePersonName}" in ${config.appName} and automatically matched to person "${matchingWorknicePerson.displayName}" in Worknice.`
224
+ `Created new person connection for ${config.appName} person "${remotePersonName}" and automatically matched to Worknice person "${matchingWorknicePerson.displayName}".`
216
225
  );
217
226
  }
218
227
  }
@@ -228,16 +237,17 @@ const handleTriggerIntegrationSyncWebhook = async (request, {
228
237
  });
229
238
  personConnections.push(newPersonConnection);
230
239
  logger.info(
231
- `New person connection created for person "${remotePersonName}" in ${config.appName}.`
240
+ `New person connection created for ${config.appName} person "${remotePersonName}".`
232
241
  );
233
242
  }
234
243
  }
235
244
  }
236
245
  } catch (error) {
237
- logger.info(
238
- `Unable to update person connection for person "${remotePersonName}" in ${config.appName} because of the following error:`
246
+ logger.indent(
247
+ `Unable to update person connection for ${config.appName} person "${remotePersonName}" because of the following error:`
239
248
  );
240
249
  logger.error(error);
250
+ logger.dedent();
241
251
  }
242
252
  }
243
253
  logger.dedent("Finished updating person connections.");
@@ -253,15 +263,24 @@ const handleTriggerIntegrationSyncWebhook = async (request, {
253
263
  const remotePerson = remotePeople.find(
254
264
  (person) => person.metadata.sourceId === personConnection.remote.id
255
265
  );
256
- if (!worknicePerson) {
257
- throw Error(
258
- `Unable to find person in Worknice with the ID "${personConnection.person.id}".`
259
- );
260
- }
261
- if (!remotePerson) {
262
- throw Error(
263
- `Unable to find person in ${config.appName} with the ID "${personConnection.remote.id}".`
266
+ try {
267
+ if (!worknicePerson) {
268
+ throw Error(
269
+ `Unable to find person in Worknice with the ID "${personConnection.person.id}".`
270
+ );
271
+ }
272
+ if (!remotePerson) {
273
+ throw Error(
274
+ `Unable to find person in ${config.appName} with the ID "${personConnection.remote.id}".`
275
+ );
276
+ }
277
+ } catch (error) {
278
+ logger.indent(
279
+ `Unable to update Worknice person "${personConnection.person.displayName}" and/or ${config.appName} person "${personConnection.remote.name}" because of the following error:`
264
280
  );
281
+ logger.error(error);
282
+ logger.dedent();
283
+ continue;
265
284
  }
266
285
  const remotePersonName = personToName(remotePerson);
267
286
  const worknicePersonDataTransferLine = {
@@ -272,10 +291,6 @@ const handleTriggerIntegrationSyncWebhook = async (request, {
272
291
  sourceId: worknicePerson.id,
273
292
  targetId: remotePerson.metadata.sourceId,
274
293
  updatedAt: worknicePerson.updatedAt
275
- },
276
- profile: {
277
- displayName: worknicePerson.displayName,
278
- profileEmail: worknicePerson.profileEmail ?? null
279
294
  }
280
295
  };
281
296
  const remotePersonDataTransferLine = remotePerson;
@@ -283,7 +298,7 @@ const handleTriggerIntegrationSyncWebhook = async (request, {
283
298
  worknicePersonDataTransferLine,
284
299
  remotePersonDataTransferLine
285
300
  );
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)) {
301
+ 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 || config.syncFields?.taxDetails && comparison.sections.taxDetails.hasDifferences)) {
287
302
  const worknicePersonUpdatedAt = Temporal.Instant.from(worknicePerson.updatedAt);
288
303
  if (!isValidInstant(remotePerson.metadata.updatedAt)) {
289
304
  throw Error(
@@ -308,12 +323,13 @@ const handleTriggerIntegrationSyncWebhook = async (request, {
308
323
  worknicePersonDataTransferLine,
309
324
  contextWithIntegration
310
325
  );
311
- logger.info(`Updated person "${remotePersonName}" in ${config.appName}.`);
326
+ logger.info(`Updated ${config.appName} person "${remotePersonName}".`);
312
327
  } catch (error) {
313
- logger.info(
314
- `Unable to update person "${remotePersonName}" in ${config.appName} because of the following error:`
328
+ logger.indent(
329
+ `Unable to update ${config.appName} person "${remotePersonName}" because of the following error:`
315
330
  );
316
331
  logger.error(error);
332
+ logger.dedent();
317
333
  }
318
334
  }
319
335
  } else {
@@ -339,6 +355,9 @@ const handleTriggerIntegrationSyncWebhook = async (request, {
339
355
  const residentialAddress = validatePersonDataTransferLineResidentialAddress(
340
356
  remotePersonDataTransferLine.residentialAddress
341
357
  );
358
+ const taxDetails = validatePersonDataTransferLineTaxDetails(
359
+ remotePersonDataTransferLine.taxDetails
360
+ );
342
361
  const dataImport = await getDataImport();
343
362
  await worknice.createPersonDataImportLine({
344
363
  bankAccounts,
@@ -370,17 +389,17 @@ const handleTriggerIntegrationSyncWebhook = async (request, {
370
389
  residentialAddress,
371
390
  // TODO: Enable syncing super funds. See PROD-2216.
372
391
  superFunds: null,
373
- // TODO: Enable syncing tax details. See PROD-2375.
374
- taxDetails: null,
392
+ taxDetails,
375
393
  // TODO: Enable syncing tenure. See PROD-1371.
376
394
  tenure: null
377
395
  });
378
- logger.info(`Updated person "${worknicePerson.displayName}" in Worknice.`);
396
+ logger.info(`Updated Worknice person "${worknicePerson.displayName}".`);
379
397
  } catch (error) {
380
- logger.info(
381
- `Unable to update person "${worknicePerson.displayName}" in Worknice because of the following error:`
398
+ logger.indent(
399
+ `Unable to update Worknice person "${worknicePerson.displayName}" because of the following error:`
382
400
  );
383
401
  logger.error(error);
402
+ logger.dedent();
384
403
  }
385
404
  }
386
405
  }
@@ -392,21 +411,28 @@ const handleTriggerIntegrationSyncWebhook = async (request, {
392
411
  (personConnection) => personConnection.status === ConnectionStatus.Connected
393
412
  );
394
413
  for (const personConnection of connectedConnections) {
395
- const worknicePerson = people.find(
396
- (person) => person.id === personConnection.person.id
397
- );
414
+ let worknicePerson = people.find((person) => person.id === personConnection.person.id);
398
415
  const remotePerson = remotePeople.find(
399
416
  (person) => person.metadata.sourceId === personConnection.remote.id
400
417
  );
401
- if (!worknicePerson) {
402
- throw Error(
403
- `Unable to find person in Worknice with the ID "${personConnection.person.id}".`
404
- );
405
- }
406
- if (!remotePerson) {
407
- throw Error(
408
- `Unable to find person in ${config.appName} with the ID "${personConnection.remote.id}".`
418
+ try {
419
+ if (!worknicePerson) {
420
+ throw Error(
421
+ `Unable to find person in Worknice with the ID "${personConnection.person.id}".`
422
+ );
423
+ }
424
+ if (!remotePerson) {
425
+ throw Error(
426
+ `Unable to find person in ${config.appName} with the ID "${personConnection.remote.id}".`
427
+ );
428
+ }
429
+ } catch (error) {
430
+ logger.indent(
431
+ `Unable to merge Worknice person "${personConnection.person.displayName}" with ${config.appName} person "${personConnection.remote.name}" because of the following error:`
409
432
  );
433
+ logger.error(error);
434
+ logger.dedent();
435
+ continue;
410
436
  }
411
437
  const remotePersonName = personToName(remotePerson);
412
438
  logger.indent(
@@ -450,9 +476,15 @@ const handleTriggerIntegrationSyncWebhook = async (request, {
450
476
  const personalPhone = validatePersonDataTransferLinePersonalPhone(
451
477
  mergedDataTransferLine.personalPhone
452
478
  );
479
+ const profile = validatePersonDataTransferLineProfile(
480
+ mergedDataTransferLine.profile
481
+ );
453
482
  const residentialAddress = validatePersonDataTransferLineResidentialAddress(
454
483
  mergedDataTransferLine.residentialAddress
455
484
  );
485
+ const taxDetails = validatePersonDataTransferLineTaxDetails(
486
+ mergedDataTransferLine.taxDetails
487
+ );
456
488
  const dataImport = await getDataImport();
457
489
  await worknice.createPersonDataImportLine({
458
490
  bankAccounts,
@@ -484,12 +516,18 @@ const handleTriggerIntegrationSyncWebhook = async (request, {
484
516
  residentialAddress,
485
517
  // TODO: Enable syncing super funds. See PROD-2216.
486
518
  superFunds: null,
487
- // TODO: Enable syncing tax details. See PROD-2375.
488
- taxDetails: null,
519
+ taxDetails,
489
520
  // TODO: Enable syncing tenure. See PROD-1371.
490
521
  tenure: null
491
522
  });
492
- logger.info(`Updated person "${worknicePerson.displayName}" in Worknice.`);
523
+ worknicePerson = await worknice.updatePerson({
524
+ displayName: profile?.displayName,
525
+ employeeCode: remotePerson.metadata.employeeCode,
526
+ personId: worknicePerson.id,
527
+ profileEmail: profile?.profileEmail,
528
+ profilePhone: profile?.profilePhone
529
+ });
530
+ logger.info(`Updated Worknice person "${worknicePerson.displayName}".`);
493
531
  if (config.mode === "two-way") {
494
532
  if (!updateRemotePerson) {
495
533
  throw Error(
@@ -498,7 +536,11 @@ const handleTriggerIntegrationSyncWebhook = async (request, {
498
536
  }
499
537
  await updateRemotePerson(
500
538
  {
501
- ...mergedDataTransferLine,
539
+ bankAccounts,
540
+ dateOfBirth,
541
+ emergencyContacts: null,
542
+ fullName,
543
+ gender,
502
544
  metadata: {
503
545
  deleted: false,
504
546
  employeeCode: worknicePerson.employeeCode ?? null,
@@ -506,14 +548,24 @@ const handleTriggerIntegrationSyncWebhook = async (request, {
506
548
  targetId: remotePerson.metadata.sourceId,
507
549
  updatedAt: worknicePerson.updatedAt
508
550
  },
551
+ personalEmail,
552
+ personalPhone,
553
+ postalAddress: null,
509
554
  profile: {
510
555
  displayName: worknicePerson.displayName,
511
- profileEmail: worknicePerson.profileEmail ?? null
512
- }
556
+ profileEmail: worknicePerson.profileEmail ?? null,
557
+ profilePhone: worknicePerson.profilePhone ?? null
558
+ },
559
+ residentialAddress,
560
+ // TODO: Enable syncing super funds. See PROD-2216.
561
+ superFunds: null,
562
+ taxDetails,
563
+ // TODO: Enable syncing tenure. See PROD-1371.
564
+ tenure: null
513
565
  },
514
566
  contextWithIntegration
515
567
  );
516
- logger.info(`Updated person "${remotePersonName}" in ${config.appName}.`);
568
+ logger.info(`Updated ${config.appName} person "${remotePersonName}".`);
517
569
  }
518
570
  }
519
571
  await worknice.updatePersonConnection({
@@ -527,11 +579,12 @@ const handleTriggerIntegrationSyncWebhook = async (request, {
527
579
  });
528
580
  logger.dedent(`Finished merging.`);
529
581
  } catch (error) {
530
- logger.info(
582
+ logger.indent(
531
583
  `Unable to merge ${config.appName} person "${remotePersonName}" and Worknice person "${worknicePerson.displayName}" because of the following error:`
532
584
  );
533
585
  logger.error(error);
534
586
  logger.dedent();
587
+ logger.dedent();
535
588
  }
536
589
  }
537
590
  logger.dedent("Finished merging unmerged connections.");
@@ -555,7 +608,7 @@ const handleTriggerIntegrationSyncWebhook = async (request, {
555
608
  "No createRemotePerson function supplied to handleTriggerIntegrationSyncWebhook."
556
609
  );
557
610
  }
558
- const remote = await createRemotePerson(
611
+ const { status, ...remote } = await createRemotePerson(
559
612
  {
560
613
  ...worknicePersonToPersonDataTransferLine(worknicePerson),
561
614
  metadata: {
@@ -564,26 +617,28 @@ const handleTriggerIntegrationSyncWebhook = async (request, {
564
617
  sourceId: worknicePerson.id,
565
618
  targetId: null,
566
619
  updatedAt: worknicePerson.updatedAt
567
- },
568
- profile: {
569
- displayName: worknicePerson.displayName,
570
- profileEmail: worknicePerson.profileEmail ?? null
571
620
  }
572
621
  },
573
622
  contextWithIntegration
574
623
  );
624
+ if (personConnections.some((connection) => connection.remote?.id === remote.id)) {
625
+ throw Error(
626
+ `A connection already exists for the ${config.appName} person "${remote.name}".`
627
+ );
628
+ }
575
629
  await worknice.updatePersonConnection({
576
630
  personConnectionId: personConnection.id,
577
631
  personId: personConnection.person.id,
578
632
  remote,
579
- status: ConnectionStatus.Merged
633
+ status: status ?? ConnectionStatus.Merged
580
634
  });
581
635
  logger.info(`Added new person "${remote.name}" to ${config.appName}.`);
582
636
  } catch (error) {
583
- logger.info(
584
- `Unable to add person "${worknicePerson.displayName}" in Worknice to ${config.appName} because of the following error:`
637
+ logger.indent(
638
+ `Unable to add Worknice person "${worknicePerson.displayName}" to ${config.appName} because of the following error:`
585
639
  );
586
640
  logger.error(error);
641
+ logger.dedent();
587
642
  }
588
643
  }
589
644
  logger.dedent(`Finished adding new people to ${config.appName}.`);
@@ -615,65 +670,78 @@ const personToName = (person) => {
615
670
  return `(unnamed person ${person.metadata.sourceId})`;
616
671
  };
617
672
  const worknicePersonToPersonDataTransferLine = (worknicePerson) => ({
618
- bankAccounts: worknicePerson.bankAccount1AllocationMethod ? {
619
- bankAccount1Allocation: worknicePerson.bankAccount1Allocation ?? null,
620
- bankAccount1AllocationMethod: worknicePerson.bankAccount1AllocationMethod,
621
- bankAccount1Bsb: worknicePerson.bankAccount1Bsb ?? null,
622
- bankAccount1Name: worknicePerson.bankAccount1Name ?? null,
623
- bankAccount1Number: worknicePerson.bankAccount1Number ?? null,
624
- bankAccount2Allocation: worknicePerson.bankAccount2Allocation ?? null,
625
- bankAccount2AllocationMethod: worknicePerson.bankAccount2AllocationMethod ?? null,
626
- bankAccount2Bsb: worknicePerson.bankAccount2Bsb ?? null,
627
- bankAccount2Name: worknicePerson.bankAccount2Name ?? null,
628
- bankAccount2Number: worknicePerson.bankAccount2Number ?? null,
629
- bankAccount3Allocation: worknicePerson.bankAccount3Allocation ?? null,
630
- bankAccount3AllocationMethod: worknicePerson.bankAccount3AllocationMethod ?? null,
631
- bankAccount3Bsb: worknicePerson.bankAccount3Bsb ?? null,
632
- bankAccount3Name: worknicePerson.bankAccount3Name ?? null,
633
- bankAccount3Number: worknicePerson.bankAccount3Number ?? null,
634
- bankAccount4Allocation: worknicePerson.bankAccount4Allocation ?? null,
635
- bankAccount4AllocationMethod: worknicePerson.bankAccount4AllocationMethod ?? null,
636
- bankAccount4Bsb: worknicePerson.bankAccount4Bsb ?? null,
637
- bankAccount4Name: worknicePerson.bankAccount4Name ?? null,
638
- bankAccount4Number: worknicePerson.bankAccount4Number ?? null,
639
- bankAccount5Allocation: worknicePerson.bankAccount5Allocation ?? null,
640
- bankAccount5AllocationMethod: worknicePerson.bankAccount5AllocationMethod ?? null,
641
- bankAccount5Bsb: worknicePerson.bankAccount5Bsb ?? null,
642
- bankAccount5Name: worknicePerson.bankAccount5Name ?? null,
643
- bankAccount5Number: worknicePerson.bankAccount5Number ?? null
644
- } : null,
645
- dateOfBirth: worknicePerson.dateOfBirth ? { dateOfBirth: worknicePerson.dateOfBirth } : null,
646
- emergencyContacts: null,
647
- fullName: worknicePerson.givenName ? {
648
- familyName: worknicePerson.familyName ?? null,
649
- givenName: worknicePerson.givenName ?? null,
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
660
- } : null,
661
- postalAddress: null,
662
- residentialAddress: worknicePerson.residentialAddressLine1 ? {
663
- residentialAddressCity: worknicePerson.residentialAddressCity ?? null,
664
- // FIXME: This should not default to "Australia". See PROD-1085.
665
- residentialAddressCountry: worknicePerson.residentialAddressCountry ?? "Australia",
666
- residentialAddressLine1: worknicePerson.residentialAddressLine1 ?? null,
667
- residentialAddressLine2: worknicePerson.residentialAddressLine2 ?? null,
668
- residentialAddressPostcode: worknicePerson.residentialAddressPostcode ?? null,
669
- residentialAddressState: worknicePerson.residentialAddressState ?? null
670
- } : null,
671
- // TODO: Enable syncing super funds. See PROD-2216.
672
- superFunds: null,
673
- // TODO: Enable syncing tax details. See PROD-2375.
674
- taxDetails: null,
675
- // TODO: Enable syncing tenure. See PROD-1371.
676
- tenure: null
673
+ ...validatePersonDataTransferLine({
674
+ bankAccounts: worknicePerson.bankAccount1AllocationMethod ? {
675
+ bankAccount1Allocation: worknicePerson.bankAccount1Allocation ?? null,
676
+ bankAccount1AllocationMethod: worknicePerson.bankAccount1AllocationMethod,
677
+ bankAccount1Bsb: worknicePerson.bankAccount1Bsb ?? null,
678
+ bankAccount1Name: worknicePerson.bankAccount1Name ?? null,
679
+ bankAccount1Number: worknicePerson.bankAccount1Number ?? null,
680
+ bankAccount2Allocation: worknicePerson.bankAccount2Allocation ?? null,
681
+ bankAccount2AllocationMethod: worknicePerson.bankAccount2AllocationMethod ?? null,
682
+ bankAccount2Bsb: worknicePerson.bankAccount2Bsb ?? null,
683
+ bankAccount2Name: worknicePerson.bankAccount2Name ?? null,
684
+ bankAccount2Number: worknicePerson.bankAccount2Number ?? null,
685
+ bankAccount3Allocation: worknicePerson.bankAccount3Allocation ?? null,
686
+ bankAccount3AllocationMethod: worknicePerson.bankAccount3AllocationMethod ?? null,
687
+ bankAccount3Bsb: worknicePerson.bankAccount3Bsb ?? null,
688
+ bankAccount3Name: worknicePerson.bankAccount3Name ?? null,
689
+ bankAccount3Number: worknicePerson.bankAccount3Number ?? null,
690
+ bankAccount4Allocation: worknicePerson.bankAccount4Allocation ?? null,
691
+ bankAccount4AllocationMethod: worknicePerson.bankAccount4AllocationMethod ?? null,
692
+ bankAccount4Bsb: worknicePerson.bankAccount4Bsb ?? null,
693
+ bankAccount4Name: worknicePerson.bankAccount4Name ?? null,
694
+ bankAccount4Number: worknicePerson.bankAccount4Number ?? null,
695
+ bankAccount5Allocation: worknicePerson.bankAccount5Allocation ?? null,
696
+ bankAccount5AllocationMethod: worknicePerson.bankAccount5AllocationMethod ?? null,
697
+ bankAccount5Bsb: worknicePerson.bankAccount5Bsb ?? null,
698
+ bankAccount5Name: worknicePerson.bankAccount5Name ?? null,
699
+ bankAccount5Number: worknicePerson.bankAccount5Number ?? null
700
+ } : null,
701
+ dateOfBirth: worknicePerson.dateOfBirth ? { dateOfBirth: worknicePerson.dateOfBirth } : null,
702
+ emergencyContacts: null,
703
+ fullName: worknicePerson.givenName ? {
704
+ familyName: worknicePerson.familyName ?? null,
705
+ givenName: worknicePerson.givenName,
706
+ otherGivenNames: worknicePerson.otherGivenNames ?? null
707
+ } : null,
708
+ gender: worknicePerson.gender ? {
709
+ gender: worknicePerson.gender
710
+ } : null,
711
+ personalEmail: worknicePerson.personalEmail ? {
712
+ personalEmail: worknicePerson.personalEmail
713
+ } : null,
714
+ personalPhone: worknicePerson.personalPhone ? {
715
+ personalPhone: worknicePerson.personalPhone
716
+ } : null,
717
+ postalAddress: null,
718
+ profile: null,
719
+ residentialAddress: worknicePerson.residentialAddressLine1 ? {
720
+ residentialAddressCity: worknicePerson.residentialAddressCity ?? null,
721
+ // FIXME: This should not default to "Australia". See PROD-1085.
722
+ residentialAddressCountry: worknicePerson.residentialAddressCountry ?? "Australia",
723
+ residentialAddressLine1: worknicePerson.residentialAddressLine1,
724
+ residentialAddressLine2: worknicePerson.residentialAddressLine2 ?? null,
725
+ residentialAddressPostcode: worknicePerson.residentialAddressPostcode ?? null,
726
+ residentialAddressState: worknicePerson.residentialAddressState ?? null
727
+ } : null,
728
+ // TODO: Enable syncing super funds. See PROD-2216.
729
+ superFunds: null,
730
+ taxDetails: worknicePerson.taxFileNumberExemption ? {
731
+ hasLoanOrStudentDebt: worknicePerson.hasLoanOrStudentDebt ?? null,
732
+ residencyStatus: worknicePerson.residencyStatus ?? null,
733
+ taxFileNumber: worknicePerson.taxFileNumber ?? null,
734
+ taxFileNumberExemption: worknicePerson.taxFileNumberExemption ?? null,
735
+ taxFreeThresholdClaimed: worknicePerson.taxFreeThresholdClaimed ?? null
736
+ } : null,
737
+ // TODO: Enable syncing tenure. See PROD-1371.
738
+ tenure: null
739
+ }),
740
+ profile: {
741
+ displayName: worknicePerson.displayName,
742
+ profileEmail: worknicePerson.profileEmail ?? null,
743
+ profilePhone: worknicePerson.profilePhone ?? null
744
+ }
677
745
  });
678
746
  var handleTriggerIntegrationSyncWebhook_default = handleTriggerIntegrationSyncWebhook;
679
747
  export {