@worknice/js-sdk 0.0.2 → 0.0.4
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 +642 -0
- package/dist/api/WorkniceIntegrationLogger.d.ts +99 -0
- package/dist/api/WorkniceIntegrationLogger.js +173 -0
- package/dist/api/WorkniceIntegrationLogger.js.map +1 -0
- package/dist/api/_generated.d.ts +357 -22
- package/dist/api/_generated.js.map +1 -1
- package/dist/api/_types.d.ts +2 -2
- package/dist/api/_types.js +1 -0
- package/dist/api/_types.js.map +1 -1
- package/dist/api/createApiOperation.d.ts +1 -1
- package/dist/api/createWorkniceClient.d.ts +1 -1
- package/dist/api/createWorkniceClient.js +21 -1
- package/dist/api/createWorkniceClient.js.map +1 -1
- package/dist/api/operations/activateIntegration.d.ts +1 -1
- 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 +1 -1
- 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 +132 -0
- package/dist/api/operations/createPersonConnection.js +65 -0
- package/dist/api/operations/createPersonConnection.js.map +1 -0
- package/dist/api/operations/createPersonDataImportLine.d.ts +22 -0
- package/dist/api/operations/createPersonDataImportLine.js +49 -0
- package/dist/api/operations/createPersonDataImportLine.js.map +1 -0
- package/dist/api/operations/deleteApiToken.d.ts +1 -1
- 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 +1 -1
- 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/getPeople.d.ts +65 -0
- package/dist/api/operations/getPeople.js +79 -0
- package/dist/api/operations/getPeople.js.map +1 -0
- package/dist/api/operations/getPersonConnections.d.ts +107 -0
- package/dist/api/operations/getPersonConnections.js +43 -0
- package/dist/api/operations/getPersonConnections.js.map +1 -0
- package/dist/api/operations/initializeIntegration.d.ts +1 -1
- package/dist/api/operations/updatePersonConnection.d.ts +132 -0
- package/dist/api/operations/updatePersonConnection.js +65 -0
- package/dist/api/operations/updatePersonConnection.js.map +1 -0
- package/dist/employee-records/EQUALITY_CHECKS.d.ts +1 -0
- package/dist/employee-records/EQUALITY_CHECKS.js +0 -1
- package/dist/employee-records/EQUALITY_CHECKS.js.map +1 -1
- package/dist/employee-records/_types.d.ts +13 -12
- package/dist/employee-records/comparePersonDataTransferLines.d.ts +1 -0
- package/dist/employee-records/comparePersonDataTransferLines.js +0 -3
- package/dist/employee-records/comparePersonDataTransferLines.js.map +1 -1
- package/dist/employee-records/index.d.ts +1 -0
- package/dist/employee-records/mergePersonDataTransferLines.d.ts +1 -0
- package/dist/employee-records/mergePersonDataTransferLines.js +0 -1
- package/dist/employee-records/mergePersonDataTransferLines.js.map +1 -1
- package/dist/employee-records/validatePersonDataTransferLine.d.ts +1 -0
- package/dist/employee-records/validatePersonDataTransferLineBankAccounts.d.ts +1 -0
- package/dist/employee-records/validatePersonDataTransferLineBankAccounts.js +10 -9
- package/dist/employee-records/validatePersonDataTransferLineBankAccounts.js.map +1 -1
- package/dist/employee-records/validatePersonDataTransferLineEmergencyContacts.d.ts +1 -0
- package/dist/employee-records/validatePersonDataTransferLinePersonalDetails.d.ts +1 -0
- package/dist/employee-records/validatePersonDataTransferLinePersonalDetails.js +0 -2
- package/dist/employee-records/validatePersonDataTransferLinePersonalDetails.js.map +1 -1
- package/dist/employee-records/validatePersonDataTransferLinePostalAddress.d.ts +1 -0
- package/dist/employee-records/validatePersonDataTransferLineResidentialAddress.d.ts +1 -0
- package/dist/employee-records/validatePersonDataTransferLineSuperFunds.d.ts +1 -0
- package/dist/employee-records/validatePersonDataTransferLineTaxDetails.d.ts +1 -0
- package/dist/employee-records/validatePersonDataTransferLineTenure.d.ts +1 -0
- package/dist/helpers/_types.d.ts +7 -6
- package/dist/helpers/handleCreateIntegrationWebhook.d.ts +2 -1
- package/dist/helpers/handleCreateIntegrationWebhook.js +6 -4
- package/dist/helpers/handleCreateIntegrationWebhook.js.map +1 -1
- package/dist/helpers/handleGetAuthorizationUrlWebhook.d.ts +2 -1
- package/dist/helpers/handleGetAuthorizationUrlWebhook.js +4 -1
- package/dist/helpers/handleGetAuthorizationUrlWebhook.js.map +1 -1
- package/dist/helpers/handleGetLeaveBalancesWebhook.d.ts +14 -0
- package/dist/helpers/handleGetLeaveBalancesWebhook.js +22 -0
- package/dist/helpers/handleGetLeaveBalancesWebhook.js.map +1 -0
- package/dist/helpers/handleRequestWithWorknice.d.ts +9 -14
- package/dist/helpers/handleRequestWithWorknice.js +40 -19
- package/dist/helpers/handleRequestWithWorknice.js.map +1 -1
- package/dist/helpers/handleTriggerIntegrationSyncWebhook.d.ts +73 -0
- package/dist/helpers/handleTriggerIntegrationSyncWebhook.js +612 -0
- package/dist/helpers/handleTriggerIntegrationSyncWebhook.js.map +1 -0
- package/dist/helpers/index.d.ts +7 -1
- package/dist/helpers/index.js +9 -1
- package/dist/helpers/index.js.map +1 -1
- package/dist/index.d.ts +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/isNotNullable.d.ts +10 -0
- package/dist/utils/isNotNullable.js +6 -0
- package/dist/utils/isNotNullable.js.map +1 -0
- package/dist/webhooks.d.ts +13 -1
- package/package.json +8 -4
- package/dist/_types-DsB4q143.d.ts +0 -127
|
@@ -0,0 +1,612 @@
|
|
|
1
|
+
import { Temporal } from "temporal-polyfill";
|
|
2
|
+
import { ConnectionStatus } from "../api/_types.js";
|
|
3
|
+
import comparePersonDataTransferLines from "../employee-records/comparePersonDataTransferLines.js";
|
|
4
|
+
import mergePersonDataTransferLines from "../employee-records/mergePersonDataTransferLines.js";
|
|
5
|
+
import validatePersonDataTransferLineBankAccounts from "../employee-records/validatePersonDataTransferLineBankAccounts.js";
|
|
6
|
+
import validatePersonDataTransferLinePersonalDetails from "../employee-records/validatePersonDataTransferLinePersonalDetails.js";
|
|
7
|
+
import validatePersonDataTransferLineResidentialAddress from "../employee-records/validatePersonDataTransferLineResidentialAddress.js";
|
|
8
|
+
import isAfter from "../utils/isAfter.js";
|
|
9
|
+
import handleRequestWithWorknice from "./handleRequestWithWorknice.js";
|
|
10
|
+
const handleTriggerIntegrationSyncWebhook = async (request, {
|
|
11
|
+
createRemotePerson,
|
|
12
|
+
getApiToken,
|
|
13
|
+
getConfig,
|
|
14
|
+
getEnv,
|
|
15
|
+
getRemotePeople,
|
|
16
|
+
updateRemotePerson
|
|
17
|
+
}, options) => handleRequestWithWorknice(
|
|
18
|
+
request,
|
|
19
|
+
{
|
|
20
|
+
getApiToken,
|
|
21
|
+
getEnv,
|
|
22
|
+
handleRequest: async (context) => {
|
|
23
|
+
const { logger, payload, worknice } = context;
|
|
24
|
+
try {
|
|
25
|
+
logger.connect(worknice, payload.integrationId);
|
|
26
|
+
logger.indent("Retrieving Worknice data\u2026");
|
|
27
|
+
const integration = await worknice.getIntegration({
|
|
28
|
+
integrationId: payload.integrationId
|
|
29
|
+
});
|
|
30
|
+
if (integration.archived) {
|
|
31
|
+
logger.info("Integration is archived. Skipping sync.");
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
if (integration.status !== "SYNCING") {
|
|
35
|
+
logger.info("Integration does not have a syncing status. Skipping sync.");
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
logger.info("Retrieved integration from Worknice.");
|
|
39
|
+
const personConnections = await worknice.getPersonConnections({
|
|
40
|
+
integrationId: payload.integrationId
|
|
41
|
+
});
|
|
42
|
+
logger.info(`Retrieved ${personConnections.length} person connection(s) from Worknice.`);
|
|
43
|
+
const people = await worknice.getPeople({ orgId: integration.org.id });
|
|
44
|
+
logger.info(`Retrieved ${people.length} person(s) from Worknice.`);
|
|
45
|
+
logger.dedent("Completed retrieving Worknice data.");
|
|
46
|
+
const contextWithIntegration = {
|
|
47
|
+
...context,
|
|
48
|
+
integration
|
|
49
|
+
};
|
|
50
|
+
logger.indent("Loading configuration details\u2026");
|
|
51
|
+
const config = await getConfig(contextWithIntegration);
|
|
52
|
+
logger.info(
|
|
53
|
+
config.automaticMatching ? "Automatic matching enabled." : "Automatic matching disabled."
|
|
54
|
+
);
|
|
55
|
+
logger.info(`Using a "${config.mode}" sync mode.`);
|
|
56
|
+
logger.dedent("Finished loading configuration details.");
|
|
57
|
+
logger.indent(`Retrieving ${config.appName} data\u2026`);
|
|
58
|
+
const remotePeople = await getRemotePeople(contextWithIntegration);
|
|
59
|
+
logger.info(`Retrieved ${remotePeople.length} people from ${config.appName}.`);
|
|
60
|
+
logger.dedent(`Completed retrieving ${config.appName} data.`);
|
|
61
|
+
logger.indent("Updating person connections\u2026");
|
|
62
|
+
for (const remotePerson of remotePeople) {
|
|
63
|
+
const remotePersonName = personToName(remotePerson);
|
|
64
|
+
try {
|
|
65
|
+
const remotePersonConnection = personConnections.find(
|
|
66
|
+
(connection) => connection.remote?.id === remotePerson.metadata.sourceId
|
|
67
|
+
);
|
|
68
|
+
if (remotePerson.metadata.deleted) {
|
|
69
|
+
if (remotePersonConnection) {
|
|
70
|
+
await worknice.deletePersonConnection({
|
|
71
|
+
personConnectionId: remotePersonConnection.id
|
|
72
|
+
});
|
|
73
|
+
logger.info(
|
|
74
|
+
`Deleted connection for "${remotePersonName}" in ${config.appName} because they have been terminated.`
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
} else {
|
|
78
|
+
if (remotePersonConnection && (remotePersonConnection.status === ConnectionStatus.Connected || remotePersonConnection.status === ConnectionStatus.Merged)) {
|
|
79
|
+
if (remotePersonConnection.remote?.name !== remotePersonName) {
|
|
80
|
+
const connection = await worknice.updatePersonConnection({
|
|
81
|
+
personConnectionId: remotePersonConnection.id,
|
|
82
|
+
personId: remotePersonConnection.person.id,
|
|
83
|
+
remote: {
|
|
84
|
+
id: remotePerson.metadata.sourceId,
|
|
85
|
+
name: remotePersonName
|
|
86
|
+
},
|
|
87
|
+
status: remotePersonConnection.status
|
|
88
|
+
});
|
|
89
|
+
personConnections.splice(
|
|
90
|
+
personConnections.indexOf(remotePersonConnection),
|
|
91
|
+
1,
|
|
92
|
+
connection
|
|
93
|
+
);
|
|
94
|
+
logger.info(
|
|
95
|
+
`Updated person connection remote name from "${remotePersonConnection.remote?.name}" to "${remotePersonName}".`
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
} else {
|
|
99
|
+
if (remotePersonConnection && remotePersonConnection.remote?.name !== remotePersonName) {
|
|
100
|
+
const connection = await worknice.updatePersonConnection({
|
|
101
|
+
personConnectionId: remotePersonConnection.id,
|
|
102
|
+
remote: {
|
|
103
|
+
id: remotePerson.metadata.sourceId,
|
|
104
|
+
name: remotePersonName
|
|
105
|
+
},
|
|
106
|
+
status: remotePersonConnection.status
|
|
107
|
+
});
|
|
108
|
+
personConnections.splice(
|
|
109
|
+
personConnections.indexOf(remotePersonConnection),
|
|
110
|
+
1,
|
|
111
|
+
connection
|
|
112
|
+
);
|
|
113
|
+
logger.info(
|
|
114
|
+
`Updated person connection remote name from "${remotePersonConnection.remote?.name}" to "${remotePersonName}".`
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
const matchingWorknicePerson = people.find(
|
|
118
|
+
(person) => person.employeeCode !== null && person.employeeCode === remotePerson.metadata.employeeCode || person.personalEmail !== null && person.personalEmail === remotePerson.personalDetails?.personalEmail || person.profileEmail !== null && person.profileEmail === remotePerson.profile?.profileEmail
|
|
119
|
+
) ?? null;
|
|
120
|
+
if (config.automaticMatching === true && matchingWorknicePerson) {
|
|
121
|
+
const personConnection = personConnections.find(
|
|
122
|
+
(connection) => connection.person?.id === matchingWorknicePerson.id
|
|
123
|
+
);
|
|
124
|
+
if (personConnection?.status === ConnectionStatus.Connected || personConnection?.status === ConnectionStatus.Merged) {
|
|
125
|
+
logger.info(
|
|
126
|
+
`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.`
|
|
127
|
+
);
|
|
128
|
+
} else {
|
|
129
|
+
if (personConnection) {
|
|
130
|
+
if (remotePersonConnection) {
|
|
131
|
+
await worknice.deletePersonConnection({
|
|
132
|
+
personConnectionId: personConnection.id
|
|
133
|
+
});
|
|
134
|
+
personConnections.splice(personConnections.indexOf(personConnection), 1);
|
|
135
|
+
const updatedConnection = await worknice.updatePersonConnection({
|
|
136
|
+
personConnectionId: remotePersonConnection.id,
|
|
137
|
+
personId: matchingWorknicePerson.id,
|
|
138
|
+
remote: {
|
|
139
|
+
id: remotePerson.metadata.sourceId,
|
|
140
|
+
name: remotePersonName
|
|
141
|
+
},
|
|
142
|
+
status: ConnectionStatus.Connected
|
|
143
|
+
});
|
|
144
|
+
personConnections.splice(
|
|
145
|
+
personConnections.indexOf(remotePersonConnection),
|
|
146
|
+
1,
|
|
147
|
+
updatedConnection
|
|
148
|
+
);
|
|
149
|
+
logger.info(
|
|
150
|
+
`Automatically matched and combined person connections for person "${matchingWorknicePerson.displayName}" in Worknice and person "${remotePersonName}" in ${config.appName}.`
|
|
151
|
+
);
|
|
152
|
+
} else {
|
|
153
|
+
const updatedConnection = await worknice.updatePersonConnection({
|
|
154
|
+
personConnectionId: personConnection.id,
|
|
155
|
+
personId: personConnection.person.id,
|
|
156
|
+
remote: {
|
|
157
|
+
id: remotePerson.metadata.sourceId,
|
|
158
|
+
name: remotePersonName
|
|
159
|
+
},
|
|
160
|
+
status: ConnectionStatus.Connected
|
|
161
|
+
});
|
|
162
|
+
personConnections.splice(
|
|
163
|
+
personConnections.indexOf(personConnection),
|
|
164
|
+
1,
|
|
165
|
+
updatedConnection
|
|
166
|
+
);
|
|
167
|
+
logger.info(
|
|
168
|
+
`Automatically matched existing person connection for person "${matchingWorknicePerson.displayName}" in Worknice to person "${remotePersonName}" in ${config.appName}.`
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
} else {
|
|
172
|
+
if (remotePersonConnection) {
|
|
173
|
+
const updatedConnection = await worknice.updatePersonConnection({
|
|
174
|
+
personConnectionId: remotePersonConnection.id,
|
|
175
|
+
personId: matchingWorknicePerson.id,
|
|
176
|
+
remote: {
|
|
177
|
+
...remotePersonConnection.remote,
|
|
178
|
+
name: remotePersonName
|
|
179
|
+
},
|
|
180
|
+
status: ConnectionStatus.Connected
|
|
181
|
+
});
|
|
182
|
+
personConnections.splice(
|
|
183
|
+
personConnections.indexOf(remotePersonConnection),
|
|
184
|
+
1,
|
|
185
|
+
updatedConnection
|
|
186
|
+
);
|
|
187
|
+
logger.info(
|
|
188
|
+
`Automatically matched existing person connection for person "${remotePersonName}" in ${config.appName} to person "${matchingWorknicePerson.displayName}" in Worknice.`
|
|
189
|
+
);
|
|
190
|
+
} else {
|
|
191
|
+
const newPersonConnection = await worknice.createPersonConnection({
|
|
192
|
+
integrationId: payload.integrationId,
|
|
193
|
+
personId: matchingWorknicePerson.id,
|
|
194
|
+
remote: {
|
|
195
|
+
id: remotePerson.metadata.sourceId,
|
|
196
|
+
name: remotePersonName
|
|
197
|
+
},
|
|
198
|
+
status: ConnectionStatus.Connected
|
|
199
|
+
});
|
|
200
|
+
personConnections.push(newPersonConnection);
|
|
201
|
+
logger.info(
|
|
202
|
+
`Created new person connection for person "${remotePersonName}" in ${config.appName} and automatically matched to person "${matchingWorknicePerson.displayName}" in Worknice.`
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
} else if (remotePersonConnection === void 0) {
|
|
208
|
+
const newPersonConnection = await worknice.createPersonConnection({
|
|
209
|
+
integrationId: payload.integrationId,
|
|
210
|
+
remote: {
|
|
211
|
+
id: remotePerson.metadata.sourceId,
|
|
212
|
+
name: remotePersonName
|
|
213
|
+
},
|
|
214
|
+
status: ConnectionStatus.RemoteOnly
|
|
215
|
+
});
|
|
216
|
+
personConnections.push(newPersonConnection);
|
|
217
|
+
logger.info(
|
|
218
|
+
`New person connection created for person "${remotePersonName}" in ${config.appName}.`
|
|
219
|
+
);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
} catch (error) {
|
|
224
|
+
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
|
+
`
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
logger.dedent("Finished updating person connections.");
|
|
233
|
+
if (config.mode === "one-way" || config.mode === "two-way") {
|
|
234
|
+
const dataImport = await worknice.createDataImport({
|
|
235
|
+
integrationId: payload.integrationId
|
|
236
|
+
});
|
|
237
|
+
logger.indent("Updating people with connections marked as merged\u2026");
|
|
238
|
+
const mergedConnections = personConnections.filter(
|
|
239
|
+
(personConnection) => personConnection.status === ConnectionStatus.Merged
|
|
240
|
+
);
|
|
241
|
+
for (const personConnection of mergedConnections) {
|
|
242
|
+
const worknicePerson = people.find(
|
|
243
|
+
(person) => person.id === personConnection.person.id
|
|
244
|
+
);
|
|
245
|
+
const remotePerson = remotePeople.find(
|
|
246
|
+
(person) => person.metadata.sourceId === personConnection.remote.id
|
|
247
|
+
);
|
|
248
|
+
if (!worknicePerson) {
|
|
249
|
+
throw Error(
|
|
250
|
+
`Unable to find person in Worknice with the ID "${personConnection.person.id}".`
|
|
251
|
+
);
|
|
252
|
+
}
|
|
253
|
+
if (!remotePerson) {
|
|
254
|
+
throw Error(
|
|
255
|
+
`Unable to find person in ${config.appName} with the ID "${personConnection.remote.id}".`
|
|
256
|
+
);
|
|
257
|
+
}
|
|
258
|
+
const remotePersonName = personToName(remotePerson);
|
|
259
|
+
const worknicePersonDataTransferLine = {
|
|
260
|
+
...worknicePersonToPersonDataTransferLine(worknicePerson),
|
|
261
|
+
metadata: {
|
|
262
|
+
deleted: false,
|
|
263
|
+
employeeCode: worknicePerson.employeeCode ?? null,
|
|
264
|
+
sourceId: worknicePerson.id,
|
|
265
|
+
targetId: remotePerson.metadata.sourceId,
|
|
266
|
+
updatedAt: worknicePerson.updatedAt
|
|
267
|
+
},
|
|
268
|
+
profile: {
|
|
269
|
+
displayName: worknicePerson.displayName,
|
|
270
|
+
profileEmail: worknicePerson.profileEmail ?? null
|
|
271
|
+
}
|
|
272
|
+
};
|
|
273
|
+
const myobPersonDataTransferLine = remotePerson;
|
|
274
|
+
const comparison = comparePersonDataTransferLines(
|
|
275
|
+
worknicePersonDataTransferLine,
|
|
276
|
+
myobPersonDataTransferLine
|
|
277
|
+
);
|
|
278
|
+
if (comparison.hasDifferences && (comparison.sections.bankAccounts.hasDifferences || comparison.sections.personalDetails.hasDifferences || comparison.sections.residentialAddress.hasDifferences)) {
|
|
279
|
+
const worknicePersonUpdatedAt = Temporal.Instant.from(worknicePerson.updatedAt);
|
|
280
|
+
const myobEmployeeUpdatedAt = Temporal.PlainDateTime.from(
|
|
281
|
+
remotePerson.metadata.updatedAt
|
|
282
|
+
).toZonedDateTime(Temporal.TimeZone.from("UTC")).toInstant();
|
|
283
|
+
if (isAfter(worknicePersonUpdatedAt, myobEmployeeUpdatedAt)) {
|
|
284
|
+
if (config.mode === "two-way") {
|
|
285
|
+
if (!updateRemotePerson) {
|
|
286
|
+
throw Error(
|
|
287
|
+
"No updateRemotePerson function supplied to handleTriggerIntegrationSyncWebhook."
|
|
288
|
+
);
|
|
289
|
+
}
|
|
290
|
+
try {
|
|
291
|
+
await updateRemotePerson(
|
|
292
|
+
worknicePersonDataTransferLine,
|
|
293
|
+
contextWithIntegration
|
|
294
|
+
);
|
|
295
|
+
logger.info(`Updated person "${remotePersonName}" in ${config.appName}.`);
|
|
296
|
+
} catch (error) {
|
|
297
|
+
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
|
+
`
|
|
302
|
+
);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
} else {
|
|
306
|
+
try {
|
|
307
|
+
const personalDetails = validatePersonDataTransferLinePersonalDetails(
|
|
308
|
+
myobPersonDataTransferLine.personalDetails
|
|
309
|
+
);
|
|
310
|
+
const bankAccounts = validatePersonDataTransferLineBankAccounts(
|
|
311
|
+
myobPersonDataTransferLine.bankAccounts
|
|
312
|
+
);
|
|
313
|
+
const residentialAddress = validatePersonDataTransferLineResidentialAddress(
|
|
314
|
+
myobPersonDataTransferLine.residentialAddress
|
|
315
|
+
);
|
|
316
|
+
await worknice.createPersonDataImportLine({
|
|
317
|
+
bankAccounts,
|
|
318
|
+
config: {
|
|
319
|
+
bankAccounts: config.syncFields?.bankAccounts ?? false,
|
|
320
|
+
emergencyContacts: config.syncFields?.emergencyContacts ?? false,
|
|
321
|
+
personalDetails: config.syncFields?.personalDetails ?? false,
|
|
322
|
+
postalAddress: config.syncFields?.personalDetails ?? false,
|
|
323
|
+
profile: config.syncFields?.profile ?? false,
|
|
324
|
+
remuneration: config.syncFields?.remuneration ?? false,
|
|
325
|
+
residentialAddress: config.syncFields?.residentialAddress ?? false,
|
|
326
|
+
superFunds: config.syncFields?.superFunds ?? false,
|
|
327
|
+
taxDetails: config.syncFields?.taxDetails ?? false,
|
|
328
|
+
tenure: config.syncFields?.tenure ?? false
|
|
329
|
+
},
|
|
330
|
+
dataImportId: dataImport.id,
|
|
331
|
+
emergencyContacts: null,
|
|
332
|
+
personalDetails,
|
|
333
|
+
personId: worknicePerson.id,
|
|
334
|
+
postalAddress: null,
|
|
335
|
+
residentialAddress,
|
|
336
|
+
// TODO: Enable syncing super funds. See PROD-2216.
|
|
337
|
+
superFunds: null,
|
|
338
|
+
// TODO: Enable syncing tax details. See PROD-2375.
|
|
339
|
+
taxDetails: null,
|
|
340
|
+
// TODO: Enable syncing tenure. See PROD-1371.
|
|
341
|
+
tenure: null
|
|
342
|
+
});
|
|
343
|
+
logger.info(`Updated person "${worknicePerson.displayName}" in Worknice.`);
|
|
344
|
+
} catch (error) {
|
|
345
|
+
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
|
+
`
|
|
350
|
+
);
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
logger.dedent("Finished updating people with connections marked as merged.");
|
|
356
|
+
logger.indent("Merging unmerged connections\u2026");
|
|
357
|
+
const connectedConnections = personConnections.filter(
|
|
358
|
+
(personConnection) => personConnection.status === ConnectionStatus.Connected
|
|
359
|
+
);
|
|
360
|
+
for (const personConnection of connectedConnections) {
|
|
361
|
+
const worknicePerson = people.find(
|
|
362
|
+
(person) => person.id === personConnection.person.id
|
|
363
|
+
);
|
|
364
|
+
const remotePerson = remotePeople.find(
|
|
365
|
+
(person) => person.metadata.sourceId === personConnection.remote.id
|
|
366
|
+
);
|
|
367
|
+
if (!worknicePerson) {
|
|
368
|
+
throw Error(
|
|
369
|
+
`Unable to find person in Worknice with the ID "${personConnection.person.id}".`
|
|
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`
|
|
380
|
+
);
|
|
381
|
+
try {
|
|
382
|
+
const worknicePersonUpdatedAt = Temporal.Instant.from(worknicePerson.updatedAt);
|
|
383
|
+
const myobEmployeeUpdatedAt = Temporal.PlainDateTime.from(
|
|
384
|
+
remotePerson.metadata.updatedAt
|
|
385
|
+
).toZonedDateTime(Temporal.TimeZone.from("UTC")).toInstant();
|
|
386
|
+
const [primaryDataTransferLine, secondaryDataTransferLine] = isAfter(
|
|
387
|
+
worknicePersonUpdatedAt,
|
|
388
|
+
myobEmployeeUpdatedAt
|
|
389
|
+
) ? [worknicePersonToPersonDataTransferLine(worknicePerson), remotePerson] : [remotePerson, worknicePersonToPersonDataTransferLine(worknicePerson)];
|
|
390
|
+
const mergedDataTransferLine = mergePersonDataTransferLines(
|
|
391
|
+
primaryDataTransferLine,
|
|
392
|
+
secondaryDataTransferLine
|
|
393
|
+
);
|
|
394
|
+
const personalDetails = validatePersonDataTransferLinePersonalDetails(
|
|
395
|
+
mergedDataTransferLine.personalDetails
|
|
396
|
+
);
|
|
397
|
+
const bankAccounts = validatePersonDataTransferLineBankAccounts(
|
|
398
|
+
mergedDataTransferLine.bankAccounts
|
|
399
|
+
);
|
|
400
|
+
const residentialAddress = validatePersonDataTransferLineResidentialAddress(
|
|
401
|
+
mergedDataTransferLine.residentialAddress
|
|
402
|
+
);
|
|
403
|
+
await worknice.createPersonDataImportLine({
|
|
404
|
+
bankAccounts,
|
|
405
|
+
config: {
|
|
406
|
+
bankAccounts: config.syncFields?.bankAccounts ?? false,
|
|
407
|
+
emergencyContacts: config.syncFields?.emergencyContacts ?? false,
|
|
408
|
+
personalDetails: config.syncFields?.personalDetails ?? false,
|
|
409
|
+
postalAddress: config.syncFields?.personalDetails ?? false,
|
|
410
|
+
profile: config.syncFields?.profile ?? false,
|
|
411
|
+
remuneration: config.syncFields?.remuneration ?? false,
|
|
412
|
+
residentialAddress: config.syncFields?.residentialAddress ?? false,
|
|
413
|
+
superFunds: config.syncFields?.superFunds ?? false,
|
|
414
|
+
taxDetails: config.syncFields?.taxDetails ?? false,
|
|
415
|
+
tenure: config.syncFields?.tenure ?? false
|
|
416
|
+
},
|
|
417
|
+
dataImportId: dataImport.id,
|
|
418
|
+
emergencyContacts: null,
|
|
419
|
+
personalDetails,
|
|
420
|
+
personId: worknicePerson.id,
|
|
421
|
+
postalAddress: null,
|
|
422
|
+
residentialAddress,
|
|
423
|
+
// TODO: Enable syncing super funds. See PROD-2216.
|
|
424
|
+
superFunds: null,
|
|
425
|
+
// TODO: Enable syncing tax details. See PROD-2375.
|
|
426
|
+
taxDetails: null,
|
|
427
|
+
// TODO: Enable syncing tenure. See PROD-1371.
|
|
428
|
+
tenure: null
|
|
429
|
+
});
|
|
430
|
+
logger.info(`Updated person "${worknicePerson.displayName}" in Worknice.`);
|
|
431
|
+
if (config.mode === "two-way") {
|
|
432
|
+
if (!updateRemotePerson) {
|
|
433
|
+
throw Error(
|
|
434
|
+
"No updateRemotePerson function supplied to handleTriggerIntegrationSyncWebhook."
|
|
435
|
+
);
|
|
436
|
+
}
|
|
437
|
+
await updateRemotePerson(
|
|
438
|
+
{
|
|
439
|
+
...mergedDataTransferLine,
|
|
440
|
+
metadata: {
|
|
441
|
+
deleted: false,
|
|
442
|
+
employeeCode: worknicePerson.employeeCode ?? null,
|
|
443
|
+
sourceId: worknicePerson.id,
|
|
444
|
+
targetId: remotePerson.metadata.sourceId,
|
|
445
|
+
updatedAt: worknicePerson.updatedAt
|
|
446
|
+
},
|
|
447
|
+
profile: {
|
|
448
|
+
displayName: worknicePerson.displayName,
|
|
449
|
+
profileEmail: worknicePerson.profileEmail ?? null
|
|
450
|
+
}
|
|
451
|
+
},
|
|
452
|
+
contextWithIntegration
|
|
453
|
+
);
|
|
454
|
+
logger.info(`Updated person "${remotePersonName}" in ${config.appName}.`);
|
|
455
|
+
}
|
|
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
|
+
}
|
|
475
|
+
}
|
|
476
|
+
logger.dedent("Finished merging unmerged connections.");
|
|
477
|
+
if (config.mode === "two-way") {
|
|
478
|
+
logger.indent(`Adding new people to ${config.appName}\u2026`);
|
|
479
|
+
const localOnlyConnections = personConnections.filter(
|
|
480
|
+
(personConnection) => personConnection.status === ConnectionStatus.LocalOnly
|
|
481
|
+
);
|
|
482
|
+
for (const personConnection of localOnlyConnections) {
|
|
483
|
+
const worknicePerson = people.find(
|
|
484
|
+
(person) => person.id === personConnection.person.id
|
|
485
|
+
);
|
|
486
|
+
if (!worknicePerson) {
|
|
487
|
+
throw Error(
|
|
488
|
+
`Unable to find person in Worknice with the ID "${personConnection.person.id}".`
|
|
489
|
+
);
|
|
490
|
+
}
|
|
491
|
+
try {
|
|
492
|
+
if (!createRemotePerson) {
|
|
493
|
+
throw Error(
|
|
494
|
+
"No createRemotePerson function supplied to handleTriggerIntegrationSyncWebhook."
|
|
495
|
+
);
|
|
496
|
+
}
|
|
497
|
+
const remote = await createRemotePerson(
|
|
498
|
+
{
|
|
499
|
+
...worknicePersonToPersonDataTransferLine(worknicePerson),
|
|
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
|
+
}
|
|
511
|
+
},
|
|
512
|
+
contextWithIntegration
|
|
513
|
+
);
|
|
514
|
+
await worknice.updatePersonConnection({
|
|
515
|
+
personConnectionId: personConnection.id,
|
|
516
|
+
personId: personConnection.person.id,
|
|
517
|
+
remote,
|
|
518
|
+
status: ConnectionStatus.Merged
|
|
519
|
+
});
|
|
520
|
+
logger.info(`Added new person "${remote.name}" to ${config.appName}.`);
|
|
521
|
+
} catch (error) {
|
|
522
|
+
logger.info(
|
|
523
|
+
`Unable to add person "${worknicePerson.displayName}" in Worknice to ${config.appName} because of the following error:
|
|
524
|
+
|
|
525
|
+
${error instanceof Error ? error.message : error}
|
|
526
|
+
`
|
|
527
|
+
);
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
logger.dedent(`Finished adding new people to ${config.appName}.`);
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
await worknice.completeSync({ integrationId: payload.integrationId });
|
|
534
|
+
logger.info("Sync completed.");
|
|
535
|
+
} catch (error) {
|
|
536
|
+
await worknice.completeSync({ integrationId: payload.integrationId });
|
|
537
|
+
throw error;
|
|
538
|
+
}
|
|
539
|
+
},
|
|
540
|
+
parsePayload: async ({ request: request2 }) => request2.json()
|
|
541
|
+
},
|
|
542
|
+
options
|
|
543
|
+
);
|
|
544
|
+
const personToName = (person) => {
|
|
545
|
+
if ((person.personalDetails?.givenName ?? "").trim() !== "" && (person.personalDetails?.familyName ?? "").trim() !== "") {
|
|
546
|
+
return `${person.personalDetails?.givenName} ${person.personalDetails?.familyName}`;
|
|
547
|
+
}
|
|
548
|
+
if ((person.profile?.displayName ?? "").trim() !== "") {
|
|
549
|
+
return `${person.profile?.displayName}`;
|
|
550
|
+
}
|
|
551
|
+
return `(unnamed person ${person.metadata.sourceId})`;
|
|
552
|
+
};
|
|
553
|
+
const worknicePersonToPersonDataTransferLine = (worknicePerson) => ({
|
|
554
|
+
bankAccounts: worknicePerson.bankAccount1AllocationMethod ? {
|
|
555
|
+
bankAccount1Allocation: worknicePerson.bankAccount1Allocation ?? null,
|
|
556
|
+
bankAccount1AllocationMethod: worknicePerson.bankAccount1AllocationMethod,
|
|
557
|
+
bankAccount1Bsb: worknicePerson.bankAccount1Bsb ?? null,
|
|
558
|
+
bankAccount1Name: worknicePerson.bankAccount1Name ?? null,
|
|
559
|
+
bankAccount1Number: worknicePerson.bankAccount1Number ?? null,
|
|
560
|
+
bankAccount2Allocation: worknicePerson.bankAccount2Allocation ?? null,
|
|
561
|
+
bankAccount2AllocationMethod: worknicePerson.bankAccount2AllocationMethod ?? null,
|
|
562
|
+
bankAccount2Bsb: worknicePerson.bankAccount2Bsb ?? null,
|
|
563
|
+
bankAccount2Name: worknicePerson.bankAccount2Name ?? null,
|
|
564
|
+
bankAccount2Number: worknicePerson.bankAccount2Number ?? null,
|
|
565
|
+
bankAccount3Allocation: worknicePerson.bankAccount3Allocation ?? null,
|
|
566
|
+
bankAccount3AllocationMethod: worknicePerson.bankAccount3AllocationMethod ?? null,
|
|
567
|
+
bankAccount3Bsb: worknicePerson.bankAccount3Bsb ?? null,
|
|
568
|
+
bankAccount3Name: worknicePerson.bankAccount3Name ?? null,
|
|
569
|
+
bankAccount3Number: worknicePerson.bankAccount3Number ?? null,
|
|
570
|
+
bankAccount4Allocation: worknicePerson.bankAccount4Allocation ?? null,
|
|
571
|
+
bankAccount4AllocationMethod: worknicePerson.bankAccount4AllocationMethod ?? null,
|
|
572
|
+
bankAccount4Bsb: worknicePerson.bankAccount4Bsb ?? null,
|
|
573
|
+
bankAccount4Name: worknicePerson.bankAccount4Name ?? null,
|
|
574
|
+
bankAccount4Number: worknicePerson.bankAccount4Number ?? null,
|
|
575
|
+
bankAccount5Allocation: worknicePerson.bankAccount5Allocation ?? null,
|
|
576
|
+
bankAccount5AllocationMethod: worknicePerson.bankAccount5AllocationMethod ?? null,
|
|
577
|
+
bankAccount5Bsb: worknicePerson.bankAccount5Bsb ?? null,
|
|
578
|
+
bankAccount5Name: worknicePerson.bankAccount5Name ?? null,
|
|
579
|
+
bankAccount5Number: worknicePerson.bankAccount5Number ?? null
|
|
580
|
+
} : null,
|
|
581
|
+
emergencyContacts: null,
|
|
582
|
+
personalDetails: worknicePerson.givenName ? {
|
|
583
|
+
dateOfBirth: worknicePerson.dateOfBirth ?? null,
|
|
584
|
+
familyName: worknicePerson.familyName ?? null,
|
|
585
|
+
gender: worknicePerson.gender ?? null,
|
|
586
|
+
givenName: worknicePerson.givenName ?? null,
|
|
587
|
+
otherGivenNames: worknicePerson.otherGivenNames ?? null,
|
|
588
|
+
personalEmail: worknicePerson.personalEmail ?? null,
|
|
589
|
+
personalPhone: worknicePerson.personalPhone ?? null
|
|
590
|
+
} : null,
|
|
591
|
+
postalAddress: null,
|
|
592
|
+
residentialAddress: worknicePerson.residentialAddressLine1 ? {
|
|
593
|
+
residentialAddressCity: worknicePerson.residentialAddressCity ?? null,
|
|
594
|
+
// FIXME: This should not default to "Australia". See PROD-1085.
|
|
595
|
+
residentialAddressCountry: worknicePerson.residentialAddressCountry ?? "Australia",
|
|
596
|
+
residentialAddressLine1: worknicePerson.residentialAddressLine1 ?? null,
|
|
597
|
+
residentialAddressLine2: worknicePerson.residentialAddressLine2 ?? null,
|
|
598
|
+
residentialAddressPostcode: worknicePerson.residentialAddressPostcode ?? null,
|
|
599
|
+
residentialAddressState: worknicePerson.residentialAddressState ?? null
|
|
600
|
+
} : null,
|
|
601
|
+
// TODO: Enable syncing super funds. See PROD-2216.
|
|
602
|
+
superFunds: null,
|
|
603
|
+
// TODO: Enable syncing tax details. See PROD-2375.
|
|
604
|
+
taxDetails: null,
|
|
605
|
+
// TODO: Enable syncing tenure. See PROD-1371.
|
|
606
|
+
tenure: null
|
|
607
|
+
});
|
|
608
|
+
var handleTriggerIntegrationSyncWebhook_default = handleTriggerIntegrationSyncWebhook;
|
|
609
|
+
export {
|
|
610
|
+
handleTriggerIntegrationSyncWebhook_default as default
|
|
611
|
+
};
|
|
612
|
+
//# sourceMappingURL=handleTriggerIntegrationSyncWebhook.js.map
|