@worknice/js-sdk 0.0.8-rc.1 → 0.0.8-rc.2
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-Cobjq3B-.d.ts +761 -0
- package/dist/api/ApiError.d.ts +13 -0
- package/dist/api/ApiError.js +29 -0
- package/dist/api/ApiError.js.map +1 -0
- package/dist/api/WorkniceIntegrationLogger.d.ts +125 -0
- package/dist/api/WorkniceIntegrationLogger.js +203 -0
- package/dist/api/WorkniceIntegrationLogger.js.map +1 -0
- package/dist/api/_generated.d.ts +5722 -0
- package/dist/api/_generated.js +402 -0
- package/dist/api/_generated.js.map +1 -0
- package/dist/api/_types.d.ts +2 -0
- package/dist/api/_types.js +2 -0
- package/dist/api/_types.js.map +1 -0
- package/dist/api/assertNotNull.d.ts +5 -0
- package/dist/api/assertNotNull.js +14 -0
- package/dist/api/assertNotNull.js.map +1 -0
- package/dist/api/createApiOperation.d.ts +11 -0
- package/dist/api/createApiOperation.js +6 -0
- package/dist/api/createApiOperation.js.map +1 -0
- package/dist/api/createWorkniceClient.d.ts +2 -0
- package/dist/api/createWorkniceClient.js +125 -0
- package/dist/api/createWorkniceClient.js.map +1 -0
- package/dist/api/operations/activateIntegration.d.ts +22 -0
- package/dist/api/operations/activateIntegration.js +29 -0
- package/dist/api/operations/activateIntegration.js.map +1 -0
- package/dist/api/operations/authorizeIntegration.d.ts +20 -0
- package/dist/api/operations/authorizeIntegration.js +23 -0
- package/dist/api/operations/authorizeIntegration.js.map +1 -0
- package/dist/api/operations/completeSync.d.ts +20 -0
- package/dist/api/operations/completeSync.js +23 -0
- package/dist/api/operations/completeSync.js.map +1 -0
- package/dist/api/operations/createApiToken.d.ts +14 -0
- package/dist/api/operations/createApiToken.js +31 -0
- package/dist/api/operations/createApiToken.js.map +1 -0
- package/dist/api/operations/createDataImport.d.ts +12 -0
- package/dist/api/operations/createDataImport.js +25 -0
- package/dist/api/operations/createDataImport.js.map +1 -0
- package/dist/api/operations/createPersonConnection.d.ts +139 -0
- package/dist/api/operations/createPersonConnection.js +66 -0
- package/dist/api/operations/createPersonConnection.js.map +1 -0
- package/dist/api/operations/createPersonDataImportLine.d.ts +26 -0
- package/dist/api/operations/createPersonDataImportLine.js +57 -0
- package/dist/api/operations/createPersonDataImportLine.js.map +1 -0
- package/dist/api/operations/deleteApiToken.d.ts +10 -0
- package/dist/api/operations/deleteApiToken.js +21 -0
- package/dist/api/operations/deleteApiToken.js.map +1 -0
- package/dist/api/operations/deletePersonConnection.d.ts +10 -0
- package/dist/api/operations/deletePersonConnection.js +21 -0
- package/dist/api/operations/deletePersonConnection.js.map +1 -0
- package/dist/api/operations/getApiTokens.d.ts +14 -0
- package/dist/api/operations/getApiTokens.js +39 -0
- package/dist/api/operations/getApiTokens.js.map +1 -0
- package/dist/api/operations/getIntegration.d.ts +65 -0
- package/dist/api/operations/getIntegration.js +32 -0
- package/dist/api/operations/getIntegration.js.map +1 -0
- 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 +66 -0
- package/dist/api/operations/getPeople.js +80 -0
- package/dist/api/operations/getPeople.js.map +1 -0
- package/dist/api/operations/getPersonConnections.d.ts +114 -0
- package/dist/api/operations/getPersonConnections.js +44 -0
- package/dist/api/operations/getPersonConnections.js.map +1 -0
- package/dist/api/operations/initializeIntegration.d.ts +20 -0
- package/dist/api/operations/initializeIntegration.js +23 -0
- package/dist/api/operations/initializeIntegration.js.map +1 -0
- package/dist/api/operations/updatePerson.d.ts +70 -0
- package/dist/api/operations/updatePerson.js +90 -0
- package/dist/api/operations/updatePerson.js.map +1 -0
- package/dist/api/operations/updatePersonConnection.d.ts +139 -0
- package/dist/api/operations/updatePersonConnection.js +66 -0
- package/dist/api/operations/updatePersonConnection.js.map +1 -0
- package/dist/employee-records/EQUALITY_CHECKS.d.ts +10 -0
- package/dist/employee-records/EQUALITY_CHECKS.js +116 -0
- package/dist/employee-records/EQUALITY_CHECKS.js.map +1 -0
- package/dist/employee-records/_types.d.ts +198 -0
- package/dist/employee-records/_types.js +1 -0
- package/dist/employee-records/_types.js.map +1 -0
- package/dist/employee-records/comparePersonDataTransferLines.d.ts +6 -0
- package/dist/employee-records/comparePersonDataTransferLines.js +397 -0
- package/dist/employee-records/comparePersonDataTransferLines.js.map +1 -0
- package/dist/employee-records/index.d.ts +20 -0
- package/dist/employee-records/index.js +33 -0
- package/dist/employee-records/index.js.map +1 -0
- package/dist/employee-records/mergePersonDataTransferLines.d.ts +6 -0
- package/dist/employee-records/mergePersonDataTransferLines.js +23 -0
- package/dist/employee-records/mergePersonDataTransferLines.js.map +1 -0
- package/dist/employee-records/validatePersonDataTransferLine.d.ts +6 -0
- package/dist/employee-records/validatePersonDataTransferLine.js +33 -0
- package/dist/employee-records/validatePersonDataTransferLine.js.map +1 -0
- package/dist/employee-records/validatePersonDataTransferLineBankAccounts.d.ts +6 -0
- package/dist/employee-records/validatePersonDataTransferLineBankAccounts.js +241 -0
- package/dist/employee-records/validatePersonDataTransferLineBankAccounts.js.map +1 -0
- 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/validatePersonDataTransferLineEmergencyContacts.d.ts +6 -0
- package/dist/employee-records/validatePersonDataTransferLineEmergencyContacts.js +6 -0
- package/dist/employee-records/validatePersonDataTransferLineEmergencyContacts.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/validatePersonDataTransferLinePostalAddress.d.ts +6 -0
- package/dist/employee-records/validatePersonDataTransferLinePostalAddress.js +39 -0
- package/dist/employee-records/validatePersonDataTransferLinePostalAddress.js.map +1 -0
- package/dist/employee-records/validatePersonDataTransferLineProfile.d.ts +6 -0
- package/dist/employee-records/validatePersonDataTransferLineProfile.js +17 -0
- package/dist/employee-records/validatePersonDataTransferLineProfile.js.map +1 -0
- package/dist/employee-records/validatePersonDataTransferLineResidentialAddress.d.ts +6 -0
- package/dist/employee-records/validatePersonDataTransferLineResidentialAddress.js +39 -0
- package/dist/employee-records/validatePersonDataTransferLineResidentialAddress.js.map +1 -0
- package/dist/employee-records/validatePersonDataTransferLineSuperFunds.d.ts +6 -0
- package/dist/employee-records/validatePersonDataTransferLineSuperFunds.js +46 -0
- package/dist/employee-records/validatePersonDataTransferLineSuperFunds.js.map +1 -0
- package/dist/employee-records/validatePersonDataTransferLineTaxDetails.d.ts +6 -0
- package/dist/employee-records/validatePersonDataTransferLineTaxDetails.js +46 -0
- package/dist/employee-records/validatePersonDataTransferLineTaxDetails.js.map +1 -0
- package/dist/employee-records/validatePersonDataTransferLineTenure.d.ts +6 -0
- package/dist/employee-records/validatePersonDataTransferLineTenure.js +20 -0
- package/dist/employee-records/validatePersonDataTransferLineTenure.js.map +1 -0
- package/dist/helpers/_types.d.ts +22 -0
- package/dist/helpers/_types.js +1 -0
- package/dist/helpers/_types.js.map +1 -0
- 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 +14 -0
- package/dist/helpers/handleCreateIntegrationWebhook.js +42 -0
- package/dist/helpers/handleCreateIntegrationWebhook.js.map +1 -0
- 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 +12 -0
- package/dist/helpers/handleGetAuthorizationUrlWebhook.js +27 -0
- package/dist/helpers/handleGetAuthorizationUrlWebhook.js.map +1 -0
- package/dist/helpers/handleGetLeaveBalancesWebhook.d.ts +13 -0
- package/dist/helpers/handleGetLeaveBalancesWebhook.js +27 -0
- package/dist/helpers/handleGetLeaveBalancesWebhook.js.map +1 -0
- 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 +23 -0
- package/dist/helpers/handleRequestWithWorknice.js +44 -0
- package/dist/helpers/handleRequestWithWorknice.js.map +1 -0
- package/dist/helpers/handleTriggerIntegrationSyncWebhook.d.ts +76 -0
- package/dist/helpers/handleTriggerIntegrationSyncWebhook.js +750 -0
- package/dist/helpers/handleTriggerIntegrationSyncWebhook.js.map +1 -0
- package/dist/helpers/index.d.ts +19 -0
- package/dist/helpers/index.js +29 -0
- package/dist/helpers/index.js.map +1 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -0
- package/dist/utils/disambiguatePersonConnections.d.ts +25 -0
- package/dist/utils/disambiguatePersonConnections.js +18 -0
- package/dist/utils/disambiguatePersonConnections.js.map +1 -0
- package/dist/utils/errorToString.d.ts +22 -0
- package/dist/utils/errorToString.js +30 -0
- package/dist/utils/errorToString.js.map +1 -0
- package/dist/utils/isNotNullable.d.ts +10 -0
- package/dist/utils/isNotNullable.js +6 -0
- package/dist/utils/isNotNullable.js.map +1 -0
- package/dist/utils/isSamePlainDate.d.ts +3 -0
- package/dist/utils/isSamePlainDate.js +5 -0
- package/dist/utils/isSamePlainDate.js.map +1 -0
- package/dist/utils/isValidPlainDate.d.ts +1 -0
- package/dist/utils/isValidPlainDate.js +5 -0
- package/dist/utils/isValidPlainDate.js.map +1 -0
- package/dist/utils/parsePlainDate.d.ts +1 -0
- package/dist/utils/parsePlainDate.js +5 -0
- package/dist/utils/parsePlainDate.js.map +1 -0
- package/dist/webhooks.d.ts +112 -0
- package/dist/webhooks.js +1 -0
- package/dist/webhooks.js.map +1 -0
- package/package.json +2 -2
|
@@ -0,0 +1,750 @@
|
|
|
1
|
+
import isInstantAfter from "@worknice/utils/temporal/isInstantAfter";
|
|
2
|
+
import isValidInstant from "@worknice/utils/temporal/isValidInstant";
|
|
3
|
+
import { Temporal } from "temporal-polyfill";
|
|
4
|
+
import { ConnectionStatus } from "../api/_types.js";
|
|
5
|
+
import comparePersonDataTransferLines from "../employee-records/comparePersonDataTransferLines.js";
|
|
6
|
+
import mergePersonDataTransferLines from "../employee-records/mergePersonDataTransferLines.js";
|
|
7
|
+
import validatePersonDataTransferLine from "../employee-records/validatePersonDataTransferLine.js";
|
|
8
|
+
import validatePersonDataTransferLineBankAccounts from "../employee-records/validatePersonDataTransferLineBankAccounts.js";
|
|
9
|
+
import validatePersonDataTransferLineDateOfBirth from "../employee-records/validatePersonDataTransferLineDateOfBirth.js";
|
|
10
|
+
import validatePersonDataTransferLineFullName from "../employee-records/validatePersonDataTransferLineFullName.js";
|
|
11
|
+
import validatePersonDataTransferLineGender from "../employee-records/validatePersonDataTransferLineGender.js";
|
|
12
|
+
import validatePersonDataTransferLinePersonalEmail from "../employee-records/validatePersonDataTransferLinePersonalEmail.js";
|
|
13
|
+
import validatePersonDataTransferLinePersonalPhone from "../employee-records/validatePersonDataTransferLinePersonalPhone.js";
|
|
14
|
+
import validatePersonDataTransferLineProfile from "../employee-records/validatePersonDataTransferLineProfile.js";
|
|
15
|
+
import validatePersonDataTransferLineResidentialAddress from "../employee-records/validatePersonDataTransferLineResidentialAddress.js";
|
|
16
|
+
import handleRequestWithWorknice from "./handleRequestWithWorknice.js";
|
|
17
|
+
import validatePersonDataTransferLineTaxDetails from "../employee-records/validatePersonDataTransferLineTaxDetails.js";
|
|
18
|
+
const handleTriggerIntegrationSyncWebhook = async (request, {
|
|
19
|
+
createRemotePerson,
|
|
20
|
+
getApiToken,
|
|
21
|
+
getConfig,
|
|
22
|
+
getEnv,
|
|
23
|
+
getRemotePeople,
|
|
24
|
+
updateRemotePerson
|
|
25
|
+
}, options) => handleRequestWithWorknice(
|
|
26
|
+
request,
|
|
27
|
+
{
|
|
28
|
+
getApiToken,
|
|
29
|
+
handleRequest: async (context) => {
|
|
30
|
+
const { logger, payload, worknice } = context;
|
|
31
|
+
let cachedDataImport;
|
|
32
|
+
const getDataImport = async () => {
|
|
33
|
+
if (!cachedDataImport) {
|
|
34
|
+
cachedDataImport = await worknice.createDataImport({
|
|
35
|
+
integrationId: payload.integrationId
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
return cachedDataImport;
|
|
39
|
+
};
|
|
40
|
+
try {
|
|
41
|
+
logger.connect(worknice, payload.integrationId);
|
|
42
|
+
logger.indent("Retrieving Worknice data\u2026");
|
|
43
|
+
const integration = await worknice.getIntegration({
|
|
44
|
+
integrationId: payload.integrationId
|
|
45
|
+
});
|
|
46
|
+
if (integration.archived) {
|
|
47
|
+
logger.info("Integration is archived. Skipping sync.");
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
if (integration.status !== "SYNCING") {
|
|
51
|
+
logger.info("Integration does not have a syncing status. Skipping sync.");
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
logger.info("Retrieved integration from Worknice.");
|
|
55
|
+
const personConnections = await worknice.getPersonConnections({
|
|
56
|
+
integrationId: payload.integrationId
|
|
57
|
+
});
|
|
58
|
+
logger.info(`Retrieved ${personConnections.length} person connection(s) from Worknice.`);
|
|
59
|
+
const people = await worknice.getPeople({ orgId: integration.org.id });
|
|
60
|
+
logger.info(`Retrieved ${people.length} person(s) from Worknice.`);
|
|
61
|
+
logger.dedent("Completed retrieving Worknice data.");
|
|
62
|
+
const contextWithIntegration = {
|
|
63
|
+
...context,
|
|
64
|
+
integration
|
|
65
|
+
};
|
|
66
|
+
logger.indent("Loading configuration details\u2026");
|
|
67
|
+
const config = await getConfig(contextWithIntegration);
|
|
68
|
+
logger.info(
|
|
69
|
+
config.automaticMatching ? "Automatic matching enabled." : "Automatic matching disabled."
|
|
70
|
+
);
|
|
71
|
+
logger.info(`Using a "${config.mode}" sync mode.`);
|
|
72
|
+
logger.dedent("Finished loading configuration details.");
|
|
73
|
+
logger.indent(`Retrieving ${config.appName} data\u2026`);
|
|
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
|
+
}
|
|
81
|
+
logger.info(`Retrieved ${remotePeople.length} people from ${config.appName}.`);
|
|
82
|
+
logger.dedent(`Completed retrieving ${config.appName} data.`);
|
|
83
|
+
logger.indent("Updating person connections\u2026");
|
|
84
|
+
for (const remotePerson of remotePeople) {
|
|
85
|
+
const remotePersonName = personToName(remotePerson);
|
|
86
|
+
try {
|
|
87
|
+
const remotePersonConnection = personConnections.find(
|
|
88
|
+
(connection) => connection.remote?.id === remotePerson.metadata.sourceId
|
|
89
|
+
);
|
|
90
|
+
if (remotePerson.metadata.deleted) {
|
|
91
|
+
if (remotePersonConnection) {
|
|
92
|
+
await worknice.deletePersonConnection({
|
|
93
|
+
personConnectionId: remotePersonConnection.id
|
|
94
|
+
});
|
|
95
|
+
logger.info(
|
|
96
|
+
`Deleted connection for ${config.appName} person "${remotePersonName}" because they have been terminated.`
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
} else {
|
|
100
|
+
if (remotePersonConnection && (remotePersonConnection.status === ConnectionStatus.Connected || remotePersonConnection.status === ConnectionStatus.Merged)) {
|
|
101
|
+
if (remotePersonConnection.remote?.name !== remotePersonName) {
|
|
102
|
+
const connection = await worknice.updatePersonConnection({
|
|
103
|
+
personConnectionId: remotePersonConnection.id,
|
|
104
|
+
personId: remotePersonConnection.person.id,
|
|
105
|
+
remote: {
|
|
106
|
+
id: remotePerson.metadata.sourceId,
|
|
107
|
+
name: remotePersonName
|
|
108
|
+
},
|
|
109
|
+
status: remotePersonConnection.status
|
|
110
|
+
});
|
|
111
|
+
personConnections.splice(
|
|
112
|
+
personConnections.indexOf(remotePersonConnection),
|
|
113
|
+
1,
|
|
114
|
+
connection
|
|
115
|
+
);
|
|
116
|
+
logger.info(
|
|
117
|
+
`Updated person connection remote name from "${remotePersonConnection.remote?.name}" to "${remotePersonName}".`
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
} else {
|
|
121
|
+
if (remotePersonConnection && remotePersonConnection.remote?.name !== remotePersonName) {
|
|
122
|
+
const connection = await worknice.updatePersonConnection({
|
|
123
|
+
personConnectionId: remotePersonConnection.id,
|
|
124
|
+
remote: {
|
|
125
|
+
id: remotePerson.metadata.sourceId,
|
|
126
|
+
name: remotePersonName
|
|
127
|
+
},
|
|
128
|
+
status: remotePersonConnection.status
|
|
129
|
+
});
|
|
130
|
+
personConnections.splice(
|
|
131
|
+
personConnections.indexOf(remotePersonConnection),
|
|
132
|
+
1,
|
|
133
|
+
connection
|
|
134
|
+
);
|
|
135
|
+
logger.info(
|
|
136
|
+
`Updated person connection remote name from "${remotePersonConnection.remote?.name}" to "${remotePersonName}".`
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
const matchingWorknicePerson = people.find(
|
|
140
|
+
(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
|
|
141
|
+
) ?? null;
|
|
142
|
+
if (config.automaticMatching === true && matchingWorknicePerson) {
|
|
143
|
+
const personConnection = personConnections.find(
|
|
144
|
+
(connection) => connection.person?.id === matchingWorknicePerson.id
|
|
145
|
+
);
|
|
146
|
+
if (personConnection?.status === ConnectionStatus.Connected || personConnection?.status === ConnectionStatus.Merged) {
|
|
147
|
+
logger.info(
|
|
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.`
|
|
149
|
+
);
|
|
150
|
+
} else {
|
|
151
|
+
if (personConnection) {
|
|
152
|
+
if (remotePersonConnection) {
|
|
153
|
+
await worknice.deletePersonConnection({
|
|
154
|
+
personConnectionId: personConnection.id
|
|
155
|
+
});
|
|
156
|
+
personConnections.splice(personConnections.indexOf(personConnection), 1);
|
|
157
|
+
const updatedConnection = await worknice.updatePersonConnection({
|
|
158
|
+
personConnectionId: remotePersonConnection.id,
|
|
159
|
+
personId: matchingWorknicePerson.id,
|
|
160
|
+
remote: {
|
|
161
|
+
id: remotePerson.metadata.sourceId,
|
|
162
|
+
name: remotePersonName
|
|
163
|
+
},
|
|
164
|
+
status: ConnectionStatus.Connected
|
|
165
|
+
});
|
|
166
|
+
personConnections.splice(
|
|
167
|
+
personConnections.indexOf(remotePersonConnection),
|
|
168
|
+
1,
|
|
169
|
+
updatedConnection
|
|
170
|
+
);
|
|
171
|
+
logger.info(
|
|
172
|
+
`Automatically matched and combined person connections for Worknice person "${matchingWorknicePerson.displayName}" and ${config.appName} person "${remotePersonName}".`
|
|
173
|
+
);
|
|
174
|
+
} else {
|
|
175
|
+
const updatedConnection = await worknice.updatePersonConnection({
|
|
176
|
+
personConnectionId: personConnection.id,
|
|
177
|
+
personId: personConnection.person.id,
|
|
178
|
+
remote: {
|
|
179
|
+
id: remotePerson.metadata.sourceId,
|
|
180
|
+
name: remotePersonName
|
|
181
|
+
},
|
|
182
|
+
status: ConnectionStatus.Connected
|
|
183
|
+
});
|
|
184
|
+
personConnections.splice(
|
|
185
|
+
personConnections.indexOf(personConnection),
|
|
186
|
+
1,
|
|
187
|
+
updatedConnection
|
|
188
|
+
);
|
|
189
|
+
logger.info(
|
|
190
|
+
`Automatically matched existing person connection for Worknice person "${matchingWorknicePerson.displayName}" to ${config.appName} person "${remotePersonName}".`
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
} else {
|
|
194
|
+
if (remotePersonConnection) {
|
|
195
|
+
const updatedConnection = await worknice.updatePersonConnection({
|
|
196
|
+
personConnectionId: remotePersonConnection.id,
|
|
197
|
+
personId: matchingWorknicePerson.id,
|
|
198
|
+
remote: {
|
|
199
|
+
...remotePersonConnection.remote,
|
|
200
|
+
name: remotePersonName
|
|
201
|
+
},
|
|
202
|
+
status: ConnectionStatus.Connected
|
|
203
|
+
});
|
|
204
|
+
personConnections.splice(
|
|
205
|
+
personConnections.indexOf(remotePersonConnection),
|
|
206
|
+
1,
|
|
207
|
+
updatedConnection
|
|
208
|
+
);
|
|
209
|
+
logger.info(
|
|
210
|
+
`Automatically matched existing person connection for ${config.appName} person "${remotePersonName}" to Worknice person "${matchingWorknicePerson.displayName}".`
|
|
211
|
+
);
|
|
212
|
+
} else {
|
|
213
|
+
const newPersonConnection = await worknice.createPersonConnection({
|
|
214
|
+
integrationId: payload.integrationId,
|
|
215
|
+
personId: matchingWorknicePerson.id,
|
|
216
|
+
remote: {
|
|
217
|
+
id: remotePerson.metadata.sourceId,
|
|
218
|
+
name: remotePersonName
|
|
219
|
+
},
|
|
220
|
+
status: ConnectionStatus.Connected
|
|
221
|
+
});
|
|
222
|
+
personConnections.push(newPersonConnection);
|
|
223
|
+
logger.info(
|
|
224
|
+
`Created new person connection for ${config.appName} person "${remotePersonName}" and automatically matched to Worknice person "${matchingWorknicePerson.displayName}".`
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
} else if (remotePersonConnection === void 0) {
|
|
230
|
+
const newPersonConnection = await worknice.createPersonConnection({
|
|
231
|
+
integrationId: payload.integrationId,
|
|
232
|
+
remote: {
|
|
233
|
+
id: remotePerson.metadata.sourceId,
|
|
234
|
+
name: remotePersonName
|
|
235
|
+
},
|
|
236
|
+
status: ConnectionStatus.RemoteOnly
|
|
237
|
+
});
|
|
238
|
+
personConnections.push(newPersonConnection);
|
|
239
|
+
logger.info(
|
|
240
|
+
`New person connection created for ${config.appName} person "${remotePersonName}".`
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
} catch (error) {
|
|
246
|
+
logger.indent(
|
|
247
|
+
`Unable to update person connection for ${config.appName} person "${remotePersonName}" because of the following error:`
|
|
248
|
+
);
|
|
249
|
+
logger.error(error);
|
|
250
|
+
logger.dedent();
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
logger.dedent("Finished updating person connections.");
|
|
254
|
+
if (config.mode === "one-way" || config.mode === "two-way") {
|
|
255
|
+
logger.indent("Updating people with connections marked as merged\u2026");
|
|
256
|
+
const mergedConnections = personConnections.filter(
|
|
257
|
+
(personConnection) => personConnection.status === ConnectionStatus.Merged
|
|
258
|
+
);
|
|
259
|
+
for (const personConnection of mergedConnections) {
|
|
260
|
+
const worknicePerson = people.find(
|
|
261
|
+
(person) => person.id === personConnection.person.id
|
|
262
|
+
);
|
|
263
|
+
const remotePerson = remotePeople.find(
|
|
264
|
+
(person) => person.metadata.sourceId === personConnection.remote.id
|
|
265
|
+
);
|
|
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:`
|
|
280
|
+
);
|
|
281
|
+
logger.error(error);
|
|
282
|
+
logger.dedent();
|
|
283
|
+
continue;
|
|
284
|
+
}
|
|
285
|
+
const remotePersonName = personToName(remotePerson);
|
|
286
|
+
const worknicePersonDataTransferLine = {
|
|
287
|
+
...worknicePersonToPersonDataTransferLine(worknicePerson),
|
|
288
|
+
metadata: {
|
|
289
|
+
deleted: false,
|
|
290
|
+
employeeCode: worknicePerson.employeeCode ?? null,
|
|
291
|
+
sourceId: worknicePerson.id,
|
|
292
|
+
targetId: remotePerson.metadata.sourceId,
|
|
293
|
+
updatedAt: worknicePerson.updatedAt
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
const remotePersonDataTransferLine = remotePerson;
|
|
297
|
+
const comparison = comparePersonDataTransferLines(
|
|
298
|
+
worknicePersonDataTransferLine,
|
|
299
|
+
remotePersonDataTransferLine
|
|
300
|
+
);
|
|
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)) {
|
|
302
|
+
const worknicePersonUpdatedAt = Temporal.Instant.from(worknicePerson.updatedAt);
|
|
303
|
+
if (!isValidInstant(remotePerson.metadata.updatedAt)) {
|
|
304
|
+
throw Error(
|
|
305
|
+
`Invalid updatedAt value for ${config.appName} person "${remotePersonName}". Must be in ISO 8601 format, including a date, a time, and a time zone offset.`,
|
|
306
|
+
{
|
|
307
|
+
cause: Error(`Unable to parse: ${remotePerson.metadata.updatedAt}`)
|
|
308
|
+
}
|
|
309
|
+
);
|
|
310
|
+
}
|
|
311
|
+
const remotePersonUpdatedAt = Temporal.Instant.from(
|
|
312
|
+
remotePerson.metadata.updatedAt
|
|
313
|
+
);
|
|
314
|
+
if (isInstantAfter(worknicePersonUpdatedAt, remotePersonUpdatedAt)) {
|
|
315
|
+
if (config.mode === "two-way") {
|
|
316
|
+
if (!updateRemotePerson) {
|
|
317
|
+
throw Error(
|
|
318
|
+
"No updateRemotePerson function supplied to handleTriggerIntegrationSyncWebhook."
|
|
319
|
+
);
|
|
320
|
+
}
|
|
321
|
+
try {
|
|
322
|
+
await updateRemotePerson(
|
|
323
|
+
worknicePersonDataTransferLine,
|
|
324
|
+
contextWithIntegration
|
|
325
|
+
);
|
|
326
|
+
logger.info(`Updated ${config.appName} person "${remotePersonName}".`);
|
|
327
|
+
} catch (error) {
|
|
328
|
+
logger.indent(
|
|
329
|
+
`Unable to update ${config.appName} person "${remotePersonName}" because of the following error:`
|
|
330
|
+
);
|
|
331
|
+
logger.error(error);
|
|
332
|
+
logger.dedent();
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
} else {
|
|
336
|
+
try {
|
|
337
|
+
const bankAccounts = validatePersonDataTransferLineBankAccounts(
|
|
338
|
+
remotePersonDataTransferLine.bankAccounts
|
|
339
|
+
);
|
|
340
|
+
const dateOfBirth = validatePersonDataTransferLineDateOfBirth(
|
|
341
|
+
remotePersonDataTransferLine.dateOfBirth
|
|
342
|
+
);
|
|
343
|
+
const fullName = validatePersonDataTransferLineFullName(
|
|
344
|
+
remotePersonDataTransferLine.fullName
|
|
345
|
+
);
|
|
346
|
+
const gender = validatePersonDataTransferLineGender(
|
|
347
|
+
remotePersonDataTransferLine.gender
|
|
348
|
+
);
|
|
349
|
+
const personalEmail = validatePersonDataTransferLinePersonalEmail(
|
|
350
|
+
remotePersonDataTransferLine.personalEmail
|
|
351
|
+
);
|
|
352
|
+
const personalPhone = validatePersonDataTransferLinePersonalPhone(
|
|
353
|
+
remotePersonDataTransferLine.personalPhone
|
|
354
|
+
);
|
|
355
|
+
const residentialAddress = validatePersonDataTransferLineResidentialAddress(
|
|
356
|
+
remotePersonDataTransferLine.residentialAddress
|
|
357
|
+
);
|
|
358
|
+
const taxDetails = validatePersonDataTransferLineTaxDetails(
|
|
359
|
+
remotePersonDataTransferLine.taxDetails
|
|
360
|
+
);
|
|
361
|
+
const dataImport = await getDataImport();
|
|
362
|
+
await worknice.createPersonDataImportLine({
|
|
363
|
+
bankAccounts,
|
|
364
|
+
config: {
|
|
365
|
+
bankAccounts: config.syncFields?.bankAccounts ?? false,
|
|
366
|
+
dateOfBirth: config.syncFields?.dateOfBirth ?? false,
|
|
367
|
+
emergencyContacts: config.syncFields?.emergencyContacts ?? false,
|
|
368
|
+
fullName: config.syncFields?.fullName ?? false,
|
|
369
|
+
gender: config.syncFields?.gender ?? false,
|
|
370
|
+
personalEmail: config.syncFields?.personalEmail ?? false,
|
|
371
|
+
personalPhone: config.syncFields?.personalPhone ?? false,
|
|
372
|
+
postalAddress: config.syncFields?.postalAddress ?? false,
|
|
373
|
+
profile: config.syncFields?.profile ?? false,
|
|
374
|
+
remuneration: config.syncFields?.remuneration ?? false,
|
|
375
|
+
residentialAddress: config.syncFields?.residentialAddress ?? false,
|
|
376
|
+
superFunds: config.syncFields?.superFunds ?? false,
|
|
377
|
+
taxDetails: config.syncFields?.taxDetails ?? false,
|
|
378
|
+
tenure: config.syncFields?.tenure ?? false
|
|
379
|
+
},
|
|
380
|
+
dataImportId: dataImport.id,
|
|
381
|
+
dateOfBirth,
|
|
382
|
+
emergencyContacts: null,
|
|
383
|
+
fullName,
|
|
384
|
+
gender,
|
|
385
|
+
personalEmail,
|
|
386
|
+
personalPhone,
|
|
387
|
+
personId: worknicePerson.id,
|
|
388
|
+
postalAddress: null,
|
|
389
|
+
residentialAddress,
|
|
390
|
+
// TODO: Enable syncing super funds. See PROD-2216.
|
|
391
|
+
superFunds: null,
|
|
392
|
+
taxDetails,
|
|
393
|
+
// TODO: Enable syncing tenure. See PROD-1371.
|
|
394
|
+
tenure: null
|
|
395
|
+
});
|
|
396
|
+
logger.info(`Updated Worknice person "${worknicePerson.displayName}".`);
|
|
397
|
+
} catch (error) {
|
|
398
|
+
logger.indent(
|
|
399
|
+
`Unable to update Worknice person "${worknicePerson.displayName}" because of the following error:`
|
|
400
|
+
);
|
|
401
|
+
logger.error(error);
|
|
402
|
+
logger.dedent();
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
logger.dedent("Finished updating people with connections marked as merged.");
|
|
408
|
+
}
|
|
409
|
+
logger.indent("Merging unmerged connections\u2026");
|
|
410
|
+
const connectedConnections = personConnections.filter(
|
|
411
|
+
(personConnection) => personConnection.status === ConnectionStatus.Connected
|
|
412
|
+
);
|
|
413
|
+
for (const personConnection of connectedConnections) {
|
|
414
|
+
let worknicePerson = people.find((person) => person.id === personConnection.person.id);
|
|
415
|
+
const remotePerson = remotePeople.find(
|
|
416
|
+
(person) => person.metadata.sourceId === personConnection.remote.id
|
|
417
|
+
);
|
|
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:`
|
|
432
|
+
);
|
|
433
|
+
logger.error(error);
|
|
434
|
+
logger.dedent();
|
|
435
|
+
continue;
|
|
436
|
+
}
|
|
437
|
+
const remotePersonName = personToName(remotePerson);
|
|
438
|
+
logger.indent(
|
|
439
|
+
`Merging Worknice person "${worknicePerson.displayName}" with ${config.appName} person "${remotePersonName}"\u2026`
|
|
440
|
+
);
|
|
441
|
+
try {
|
|
442
|
+
if (config.mode === "one-way" || config.mode === "two-way") {
|
|
443
|
+
const worknicePersonUpdatedAt = Temporal.Instant.from(worknicePerson.updatedAt);
|
|
444
|
+
if (!isValidInstant(remotePerson.metadata.updatedAt)) {
|
|
445
|
+
throw Error(
|
|
446
|
+
`Invalid updatedAt value for ${config.appName} person "${remotePersonName}". Must be in ISO 8601 format, including a date, a time, and a time zone offset.`,
|
|
447
|
+
{
|
|
448
|
+
cause: Error(`Unable to parse: ${remotePerson.metadata.updatedAt}`)
|
|
449
|
+
}
|
|
450
|
+
);
|
|
451
|
+
}
|
|
452
|
+
const remotePersonUpdatedAt = Temporal.Instant.from(
|
|
453
|
+
remotePerson.metadata.updatedAt
|
|
454
|
+
);
|
|
455
|
+
const [primaryDataTransferLine, secondaryDataTransferLine] = isInstantAfter(
|
|
456
|
+
worknicePersonUpdatedAt,
|
|
457
|
+
remotePersonUpdatedAt
|
|
458
|
+
) ? [worknicePersonToPersonDataTransferLine(worknicePerson), remotePerson] : [remotePerson, worknicePersonToPersonDataTransferLine(worknicePerson)];
|
|
459
|
+
const mergedDataTransferLine = mergePersonDataTransferLines(
|
|
460
|
+
primaryDataTransferLine,
|
|
461
|
+
secondaryDataTransferLine
|
|
462
|
+
);
|
|
463
|
+
const bankAccounts = validatePersonDataTransferLineBankAccounts(
|
|
464
|
+
mergedDataTransferLine.bankAccounts
|
|
465
|
+
);
|
|
466
|
+
const dateOfBirth = validatePersonDataTransferLineDateOfBirth(
|
|
467
|
+
mergedDataTransferLine.dateOfBirth
|
|
468
|
+
);
|
|
469
|
+
const fullName = validatePersonDataTransferLineFullName(
|
|
470
|
+
mergedDataTransferLine.fullName
|
|
471
|
+
);
|
|
472
|
+
const gender = validatePersonDataTransferLineGender(mergedDataTransferLine.gender);
|
|
473
|
+
const personalEmail = validatePersonDataTransferLinePersonalEmail(
|
|
474
|
+
mergedDataTransferLine.personalEmail
|
|
475
|
+
);
|
|
476
|
+
const personalPhone = validatePersonDataTransferLinePersonalPhone(
|
|
477
|
+
mergedDataTransferLine.personalPhone
|
|
478
|
+
);
|
|
479
|
+
const profile = validatePersonDataTransferLineProfile(
|
|
480
|
+
mergedDataTransferLine.profile
|
|
481
|
+
);
|
|
482
|
+
const residentialAddress = validatePersonDataTransferLineResidentialAddress(
|
|
483
|
+
mergedDataTransferLine.residentialAddress
|
|
484
|
+
);
|
|
485
|
+
const taxDetails = validatePersonDataTransferLineTaxDetails(
|
|
486
|
+
mergedDataTransferLine.taxDetails
|
|
487
|
+
);
|
|
488
|
+
const dataImport = await getDataImport();
|
|
489
|
+
await worknice.createPersonDataImportLine({
|
|
490
|
+
bankAccounts,
|
|
491
|
+
config: {
|
|
492
|
+
bankAccounts: config.syncFields?.bankAccounts ?? false,
|
|
493
|
+
dateOfBirth: config.syncFields?.dateOfBirth ?? false,
|
|
494
|
+
emergencyContacts: config.syncFields?.emergencyContacts ?? false,
|
|
495
|
+
fullName: config.syncFields?.fullName ?? false,
|
|
496
|
+
gender: config.syncFields?.gender ?? false,
|
|
497
|
+
personalEmail: config.syncFields?.personalEmail ?? false,
|
|
498
|
+
personalPhone: config.syncFields?.personalPhone ?? false,
|
|
499
|
+
postalAddress: config.syncFields?.postalAddress ?? false,
|
|
500
|
+
profile: config.syncFields?.profile ?? false,
|
|
501
|
+
remuneration: config.syncFields?.remuneration ?? false,
|
|
502
|
+
residentialAddress: config.syncFields?.residentialAddress ?? false,
|
|
503
|
+
superFunds: config.syncFields?.superFunds ?? false,
|
|
504
|
+
taxDetails: config.syncFields?.taxDetails ?? false,
|
|
505
|
+
tenure: config.syncFields?.tenure ?? false
|
|
506
|
+
},
|
|
507
|
+
dataImportId: dataImport.id,
|
|
508
|
+
dateOfBirth,
|
|
509
|
+
emergencyContacts: null,
|
|
510
|
+
fullName,
|
|
511
|
+
gender,
|
|
512
|
+
personalEmail,
|
|
513
|
+
personalPhone,
|
|
514
|
+
personId: worknicePerson.id,
|
|
515
|
+
postalAddress: null,
|
|
516
|
+
residentialAddress,
|
|
517
|
+
// TODO: Enable syncing super funds. See PROD-2216.
|
|
518
|
+
superFunds: null,
|
|
519
|
+
taxDetails,
|
|
520
|
+
// TODO: Enable syncing tenure. See PROD-1371.
|
|
521
|
+
tenure: null
|
|
522
|
+
});
|
|
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}".`);
|
|
531
|
+
if (config.mode === "two-way") {
|
|
532
|
+
if (!updateRemotePerson) {
|
|
533
|
+
throw Error(
|
|
534
|
+
"No updateRemotePerson function supplied to handleTriggerIntegrationSyncWebhook."
|
|
535
|
+
);
|
|
536
|
+
}
|
|
537
|
+
await updateRemotePerson(
|
|
538
|
+
{
|
|
539
|
+
bankAccounts,
|
|
540
|
+
dateOfBirth,
|
|
541
|
+
emergencyContacts: null,
|
|
542
|
+
fullName,
|
|
543
|
+
gender,
|
|
544
|
+
metadata: {
|
|
545
|
+
deleted: false,
|
|
546
|
+
employeeCode: worknicePerson.employeeCode ?? null,
|
|
547
|
+
sourceId: worknicePerson.id,
|
|
548
|
+
targetId: remotePerson.metadata.sourceId,
|
|
549
|
+
updatedAt: worknicePerson.updatedAt
|
|
550
|
+
},
|
|
551
|
+
personalEmail,
|
|
552
|
+
personalPhone,
|
|
553
|
+
postalAddress: null,
|
|
554
|
+
profile: {
|
|
555
|
+
displayName: worknicePerson.displayName,
|
|
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
|
|
565
|
+
},
|
|
566
|
+
contextWithIntegration
|
|
567
|
+
);
|
|
568
|
+
logger.info(`Updated ${config.appName} person "${remotePersonName}".`);
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
await worknice.updatePersonConnection({
|
|
572
|
+
personConnectionId: personConnection.id,
|
|
573
|
+
personId: personConnection.person.id,
|
|
574
|
+
remote: {
|
|
575
|
+
id: remotePerson.metadata.sourceId,
|
|
576
|
+
name: remotePersonName
|
|
577
|
+
},
|
|
578
|
+
status: ConnectionStatus.Merged
|
|
579
|
+
});
|
|
580
|
+
logger.dedent(`Finished merging.`);
|
|
581
|
+
} catch (error) {
|
|
582
|
+
logger.indent(
|
|
583
|
+
`Unable to merge ${config.appName} person "${remotePersonName}" and Worknice person "${worknicePerson.displayName}" because of the following error:`
|
|
584
|
+
);
|
|
585
|
+
logger.error(error);
|
|
586
|
+
logger.dedent();
|
|
587
|
+
logger.dedent();
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
logger.dedent("Finished merging unmerged connections.");
|
|
591
|
+
if (config.mode === "two-way") {
|
|
592
|
+
logger.indent(`Adding new people to ${config.appName}\u2026`);
|
|
593
|
+
const localOnlyConnections = personConnections.filter(
|
|
594
|
+
(personConnection) => personConnection.status === ConnectionStatus.LocalOnly
|
|
595
|
+
);
|
|
596
|
+
for (const personConnection of localOnlyConnections) {
|
|
597
|
+
const worknicePerson = people.find(
|
|
598
|
+
(person) => person.id === personConnection.person.id
|
|
599
|
+
);
|
|
600
|
+
if (!worknicePerson) {
|
|
601
|
+
throw Error(
|
|
602
|
+
`Unable to find person in Worknice with the ID "${personConnection.person.id}".`
|
|
603
|
+
);
|
|
604
|
+
}
|
|
605
|
+
try {
|
|
606
|
+
if (!createRemotePerson) {
|
|
607
|
+
throw Error(
|
|
608
|
+
"No createRemotePerson function supplied to handleTriggerIntegrationSyncWebhook."
|
|
609
|
+
);
|
|
610
|
+
}
|
|
611
|
+
const { status, ...remote } = await createRemotePerson(
|
|
612
|
+
{
|
|
613
|
+
...worknicePersonToPersonDataTransferLine(worknicePerson),
|
|
614
|
+
metadata: {
|
|
615
|
+
deleted: false,
|
|
616
|
+
employeeCode: worknicePerson.employeeCode ?? null,
|
|
617
|
+
sourceId: worknicePerson.id,
|
|
618
|
+
targetId: null,
|
|
619
|
+
updatedAt: worknicePerson.updatedAt
|
|
620
|
+
}
|
|
621
|
+
},
|
|
622
|
+
contextWithIntegration
|
|
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
|
+
}
|
|
629
|
+
await worknice.updatePersonConnection({
|
|
630
|
+
personConnectionId: personConnection.id,
|
|
631
|
+
personId: personConnection.person.id,
|
|
632
|
+
remote,
|
|
633
|
+
status: status ?? ConnectionStatus.Merged
|
|
634
|
+
});
|
|
635
|
+
logger.info(`Added new person "${remote.name}" to ${config.appName}.`);
|
|
636
|
+
} catch (error) {
|
|
637
|
+
logger.indent(
|
|
638
|
+
`Unable to add Worknice person "${worknicePerson.displayName}" to ${config.appName} because of the following error:`
|
|
639
|
+
);
|
|
640
|
+
logger.error(error);
|
|
641
|
+
logger.dedent();
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
logger.dedent(`Finished adding new people to ${config.appName}.`);
|
|
645
|
+
}
|
|
646
|
+
await worknice.completeSync({ integrationId: payload.integrationId });
|
|
647
|
+
logger.info("Sync completed.");
|
|
648
|
+
} catch (error) {
|
|
649
|
+
await worknice.completeSync({ integrationId: payload.integrationId });
|
|
650
|
+
throw error;
|
|
651
|
+
}
|
|
652
|
+
},
|
|
653
|
+
parseRequest: async (context) => {
|
|
654
|
+
const payload = await context.request.json();
|
|
655
|
+
return {
|
|
656
|
+
env: await getEnv({ ...context, payload }),
|
|
657
|
+
payload
|
|
658
|
+
};
|
|
659
|
+
}
|
|
660
|
+
},
|
|
661
|
+
options
|
|
662
|
+
);
|
|
663
|
+
const personToName = (person) => {
|
|
664
|
+
if ((person.fullName?.givenName ?? "").trim() !== "" && (person.fullName?.familyName ?? "").trim() !== "") {
|
|
665
|
+
return `${person.fullName?.givenName} ${person.fullName?.familyName}`;
|
|
666
|
+
}
|
|
667
|
+
if ((person.profile?.displayName ?? "").trim() !== "") {
|
|
668
|
+
return `${person.profile?.displayName}`;
|
|
669
|
+
}
|
|
670
|
+
return `(unnamed person ${person.metadata.sourceId})`;
|
|
671
|
+
};
|
|
672
|
+
const worknicePersonToPersonDataTransferLine = (worknicePerson) => ({
|
|
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
|
+
}
|
|
745
|
+
});
|
|
746
|
+
var handleTriggerIntegrationSyncWebhook_default = handleTriggerIntegrationSyncWebhook;
|
|
747
|
+
export {
|
|
748
|
+
handleTriggerIntegrationSyncWebhook_default as default
|
|
749
|
+
};
|
|
750
|
+
//# sourceMappingURL=handleTriggerIntegrationSyncWebhook.js.map
|