@oneblink/sdk-core 7.2.0 → 7.3.0-beta.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/paymentService.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { FormTypes, SubmissionEventTypes, SubmissionTypes } from '@oneblink/types';
|
|
2
|
+
import { ReplaceInjectablesFormatters } from './submissionService';
|
|
2
3
|
/**
|
|
3
4
|
* Examine a submission and its form definition to validate whether a payment
|
|
4
5
|
* workflow event needs to run.
|
|
@@ -17,3 +18,38 @@ export declare function checkForPaymentEvent(definition: FormTypes.Form, submiss
|
|
|
17
18
|
paymentSubmissionEvent: SubmissionEventTypes.FormPaymentEvent;
|
|
18
19
|
amount: number;
|
|
19
20
|
} | undefined;
|
|
21
|
+
type PaymentDisplayDetail = {
|
|
22
|
+
label: string;
|
|
23
|
+
value: string;
|
|
24
|
+
/** A key to identify the detail */
|
|
25
|
+
key: string;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Retrieve an array of detail items from a form submission payment.
|
|
29
|
+
*
|
|
30
|
+
* #### Example
|
|
31
|
+
*
|
|
32
|
+
* ```js
|
|
33
|
+
* const detailItems =
|
|
34
|
+
* paymentService.getDisplayDetailsFromFormSubmissionPayment(
|
|
35
|
+
* formSubmissionPayment,
|
|
36
|
+
* {
|
|
37
|
+
* formatCurrency,
|
|
38
|
+
* formatDateTime,
|
|
39
|
+
* formatDate,
|
|
40
|
+
* },
|
|
41
|
+
* )
|
|
42
|
+
* ```
|
|
43
|
+
*
|
|
44
|
+
* @param formSubmissionPayment
|
|
45
|
+
* @param options
|
|
46
|
+
* @returns
|
|
47
|
+
*/
|
|
48
|
+
export declare const getDisplayDetailsFromFormSubmissionPayment: (formSubmissionPayment: SubmissionTypes.FormSubmissionPayment, { formatCurrency, formatDateTime, formatDate, }: Pick<ReplaceInjectablesFormatters, 'formatCurrency' | 'formatDate' | 'formatDateTime'>) => {
|
|
49
|
+
amount: {
|
|
50
|
+
value: number;
|
|
51
|
+
formatted: string;
|
|
52
|
+
};
|
|
53
|
+
paymentDisplayDetails: PaymentDisplayDetail[];
|
|
54
|
+
} | undefined;
|
|
55
|
+
export {};
|
package/dist/paymentService.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.checkForPaymentEvent = void 0;
|
|
3
|
+
exports.getDisplayDetailsFromFormSubmissionPayment = exports.checkForPaymentEvent = void 0;
|
|
4
4
|
const _1 = require(".");
|
|
5
5
|
const submissionService_1 = require("./submissionService");
|
|
6
6
|
/**
|
|
@@ -55,4 +55,316 @@ function checkForPaymentEvent(definition, submission) {
|
|
|
55
55
|
return result;
|
|
56
56
|
}
|
|
57
57
|
exports.checkForPaymentEvent = checkForPaymentEvent;
|
|
58
|
+
/**
|
|
59
|
+
* Retrieve an array of detail items from a form submission payment.
|
|
60
|
+
*
|
|
61
|
+
* #### Example
|
|
62
|
+
*
|
|
63
|
+
* ```js
|
|
64
|
+
* const detailItems =
|
|
65
|
+
* paymentService.getDisplayDetailsFromFormSubmissionPayment(
|
|
66
|
+
* formSubmissionPayment,
|
|
67
|
+
* {
|
|
68
|
+
* formatCurrency,
|
|
69
|
+
* formatDateTime,
|
|
70
|
+
* formatDate,
|
|
71
|
+
* },
|
|
72
|
+
* )
|
|
73
|
+
* ```
|
|
74
|
+
*
|
|
75
|
+
* @param formSubmissionPayment
|
|
76
|
+
* @param options
|
|
77
|
+
* @returns
|
|
78
|
+
*/
|
|
79
|
+
const getDisplayDetailsFromFormSubmissionPayment = (
|
|
80
|
+
/** The form submission payment to get the details from */
|
|
81
|
+
formSubmissionPayment, { formatCurrency, formatDateTime, formatDate, }) => {
|
|
82
|
+
var _a, _b;
|
|
83
|
+
switch (formSubmissionPayment.type) {
|
|
84
|
+
case 'NSW_GOV_PAY': {
|
|
85
|
+
const { paymentTransaction } = formSubmissionPayment;
|
|
86
|
+
if (!paymentTransaction || !paymentTransaction.agencyCompletionPayment) {
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
const amount = {
|
|
90
|
+
value: paymentTransaction.agencyCompletionPayment.amount,
|
|
91
|
+
formatted: formatCurrency(paymentTransaction.agencyCompletionPayment.amount),
|
|
92
|
+
};
|
|
93
|
+
return {
|
|
94
|
+
amount,
|
|
95
|
+
paymentDisplayDetails: [
|
|
96
|
+
{
|
|
97
|
+
key: 'completionReference',
|
|
98
|
+
label: 'Completion Reference',
|
|
99
|
+
value: paymentTransaction.agencyCompletionPayment
|
|
100
|
+
.paymentCompletionReference,
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
key: 'paymentReference',
|
|
104
|
+
label: 'Payment Reference',
|
|
105
|
+
value: paymentTransaction.agencyCompletionPayment.paymentReference,
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
key: 'bankReference',
|
|
109
|
+
label: 'Bank Reference',
|
|
110
|
+
value: paymentTransaction.agencyCompletionPayment.bankReference,
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
key: 'paymentMethod',
|
|
114
|
+
label: 'Payment Method',
|
|
115
|
+
value: paymentTransaction.agencyCompletionPayment.paymentMethod,
|
|
116
|
+
},
|
|
117
|
+
...(paymentTransaction.agencyCompletionPayment.paymentMethod ===
|
|
118
|
+
'BPAY' &&
|
|
119
|
+
((_a = paymentTransaction.agencyCompletionPayment.bPay) === null || _a === void 0 ? void 0 : _a.billerCode)
|
|
120
|
+
? [
|
|
121
|
+
{
|
|
122
|
+
key: 'billerCode',
|
|
123
|
+
label: 'BPay Biller Code',
|
|
124
|
+
value: paymentTransaction.agencyCompletionPayment.bPay.billerCode,
|
|
125
|
+
},
|
|
126
|
+
]
|
|
127
|
+
: []),
|
|
128
|
+
...(paymentTransaction.agencyCompletionPayment.paymentMethod ===
|
|
129
|
+
'CARD'
|
|
130
|
+
? [
|
|
131
|
+
{
|
|
132
|
+
key: 'creditCardMask',
|
|
133
|
+
label: 'Card Number',
|
|
134
|
+
value: `xxxx xxxx xxxx ${(_b = paymentTransaction.agencyCompletionPayment.card) === null || _b === void 0 ? void 0 : _b.last4Digits}`,
|
|
135
|
+
},
|
|
136
|
+
]
|
|
137
|
+
: []),
|
|
138
|
+
{
|
|
139
|
+
key: 'amount',
|
|
140
|
+
label: 'Amount',
|
|
141
|
+
value: amount.formatted,
|
|
142
|
+
},
|
|
143
|
+
{
|
|
144
|
+
key: 'surchargeAmount',
|
|
145
|
+
label: 'Surcharge Amount',
|
|
146
|
+
value: formatCurrency(paymentTransaction.agencyCompletionPayment.surcharge),
|
|
147
|
+
},
|
|
148
|
+
{
|
|
149
|
+
key: 'surchargeGST',
|
|
150
|
+
label: 'Surcharge GST',
|
|
151
|
+
value: formatCurrency(paymentTransaction.agencyCompletionPayment.surchargeGst),
|
|
152
|
+
},
|
|
153
|
+
{
|
|
154
|
+
key: 'createdDateTime',
|
|
155
|
+
label: 'Created Date Time',
|
|
156
|
+
value: formatDateTime(formSubmissionPayment.createdAt),
|
|
157
|
+
},
|
|
158
|
+
],
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
case 'BPOINT': {
|
|
162
|
+
const { paymentTransaction } = formSubmissionPayment;
|
|
163
|
+
if (!paymentTransaction) {
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
const amountValue = paymentTransaction.Amount / 100;
|
|
167
|
+
const amount = {
|
|
168
|
+
value: amountValue,
|
|
169
|
+
formatted: formatCurrency(amountValue),
|
|
170
|
+
};
|
|
171
|
+
return {
|
|
172
|
+
amount,
|
|
173
|
+
paymentDisplayDetails: [
|
|
174
|
+
{
|
|
175
|
+
key: 'receiptNumber',
|
|
176
|
+
label: 'Receipt Number',
|
|
177
|
+
value: paymentTransaction.ReceiptNumber,
|
|
178
|
+
},
|
|
179
|
+
{
|
|
180
|
+
key: 'crn1',
|
|
181
|
+
label: 'CRN 1',
|
|
182
|
+
value: paymentTransaction.Crn1,
|
|
183
|
+
},
|
|
184
|
+
{
|
|
185
|
+
key: 'crn2',
|
|
186
|
+
label: 'CRN 2',
|
|
187
|
+
value: paymentTransaction.Crn2,
|
|
188
|
+
},
|
|
189
|
+
{
|
|
190
|
+
key: 'crn3',
|
|
191
|
+
label: 'CRN 3',
|
|
192
|
+
value: paymentTransaction.Crn3,
|
|
193
|
+
},
|
|
194
|
+
{
|
|
195
|
+
key: 'billerCode',
|
|
196
|
+
label: 'Biller Code',
|
|
197
|
+
value: paymentTransaction.BillerCode,
|
|
198
|
+
},
|
|
199
|
+
{
|
|
200
|
+
key: 'creditCardMask',
|
|
201
|
+
label: 'Card Number',
|
|
202
|
+
value: paymentTransaction.CardDetails.MaskedCardNumber,
|
|
203
|
+
},
|
|
204
|
+
{
|
|
205
|
+
key: 'amount',
|
|
206
|
+
label: 'Amount',
|
|
207
|
+
value: amount.formatted,
|
|
208
|
+
},
|
|
209
|
+
{
|
|
210
|
+
key: 'surchargeAmount',
|
|
211
|
+
label: 'Surcharge Amount',
|
|
212
|
+
value: formatCurrency(paymentTransaction.AmountSurcharge / 100),
|
|
213
|
+
},
|
|
214
|
+
{
|
|
215
|
+
key: 'processedDateTime',
|
|
216
|
+
label: 'Processed Date Time',
|
|
217
|
+
value: formatDateTime(paymentTransaction.ProcessedDateTime),
|
|
218
|
+
},
|
|
219
|
+
],
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
case 'CP_PAY': {
|
|
223
|
+
const { paymentTransaction } = formSubmissionPayment;
|
|
224
|
+
if (!paymentTransaction) {
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
const determineDetails = () => {
|
|
228
|
+
var _a, _b;
|
|
229
|
+
switch (paymentTransaction.cpPayVersion) {
|
|
230
|
+
case 'v2': {
|
|
231
|
+
return {
|
|
232
|
+
transactionId: paymentTransaction.result.id,
|
|
233
|
+
orderNumber: (_a = paymentTransaction.result.externalReferenceId) !== null && _a !== void 0 ? _a : undefined,
|
|
234
|
+
paymentType: paymentTransaction.result.paymentType,
|
|
235
|
+
creditCardMask: paymentTransaction.result.lastFour
|
|
236
|
+
? `xxxx xxxx xxxx ${paymentTransaction.result.lastFour}`
|
|
237
|
+
: undefined,
|
|
238
|
+
amount: paymentTransaction.result.amount !== undefined
|
|
239
|
+
? {
|
|
240
|
+
value: paymentTransaction.result.amount,
|
|
241
|
+
formatted: formatCurrency(paymentTransaction.result.amount),
|
|
242
|
+
}
|
|
243
|
+
: {
|
|
244
|
+
value: NaN,
|
|
245
|
+
formatted: 'Unknown',
|
|
246
|
+
},
|
|
247
|
+
createdDateTime: paymentTransaction.result.createdOnUtc,
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
default: {
|
|
251
|
+
return {
|
|
252
|
+
transactionId: paymentTransaction.transactionId,
|
|
253
|
+
orderNumber: (_b = paymentTransaction.orderNumber) !== null && _b !== void 0 ? _b : undefined,
|
|
254
|
+
paymentType: paymentTransaction.paymentTypeId === 1
|
|
255
|
+
? 'Credit/Debit Card'
|
|
256
|
+
: paymentTransaction.paymentTypeId === 2
|
|
257
|
+
? 'ACH'
|
|
258
|
+
: undefined,
|
|
259
|
+
creditCardMask: paymentTransaction.lastFour
|
|
260
|
+
? `xxxx xxxx xxxx ${paymentTransaction.lastFour}`
|
|
261
|
+
: undefined,
|
|
262
|
+
amount: {
|
|
263
|
+
value: paymentTransaction.amount,
|
|
264
|
+
formatted: formatCurrency(paymentTransaction.amount),
|
|
265
|
+
},
|
|
266
|
+
createdDateTime: paymentTransaction.createdAt,
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
const { transactionId, orderNumber, paymentType, creditCardMask, amount, createdDateTime, } = determineDetails();
|
|
272
|
+
const paymentDisplayDetails = [];
|
|
273
|
+
if (transactionId) {
|
|
274
|
+
paymentDisplayDetails.push({
|
|
275
|
+
key: 'transactionId',
|
|
276
|
+
label: 'Transaction Id',
|
|
277
|
+
value: transactionId,
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
if (orderNumber) {
|
|
281
|
+
paymentDisplayDetails.push({
|
|
282
|
+
key: 'orderNumber',
|
|
283
|
+
label: 'Order Number',
|
|
284
|
+
value: orderNumber,
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
if (paymentType) {
|
|
288
|
+
paymentDisplayDetails.push({
|
|
289
|
+
key: 'paymentType',
|
|
290
|
+
label: 'Payment Type',
|
|
291
|
+
value: paymentType,
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
if (creditCardMask) {
|
|
295
|
+
paymentDisplayDetails.push({
|
|
296
|
+
key: 'creditCardMask',
|
|
297
|
+
label: 'Card Number',
|
|
298
|
+
value: creditCardMask,
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
paymentDisplayDetails.push({
|
|
302
|
+
key: 'amount',
|
|
303
|
+
label: 'Amount',
|
|
304
|
+
value: amount.formatted,
|
|
305
|
+
});
|
|
306
|
+
if (createdDateTime) {
|
|
307
|
+
paymentDisplayDetails.push({
|
|
308
|
+
key: 'createdDateTime',
|
|
309
|
+
label: 'Created At',
|
|
310
|
+
value: formatDateTime(createdDateTime),
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
return {
|
|
314
|
+
amount,
|
|
315
|
+
paymentDisplayDetails,
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
case 'WESTPAC_QUICK_STREAM': {
|
|
319
|
+
const { paymentTransaction } = formSubmissionPayment;
|
|
320
|
+
if (!paymentTransaction) {
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
const amount = {
|
|
324
|
+
value: paymentTransaction.totalAmount.amount,
|
|
325
|
+
formatted: formatCurrency(parseFloat(paymentTransaction.totalAmount.amount.toString())),
|
|
326
|
+
};
|
|
327
|
+
return {
|
|
328
|
+
amount,
|
|
329
|
+
paymentDisplayDetails: [
|
|
330
|
+
{
|
|
331
|
+
key: 'receiptNumber',
|
|
332
|
+
label: 'Receipt Number',
|
|
333
|
+
value: paymentTransaction.receiptNumber,
|
|
334
|
+
},
|
|
335
|
+
{
|
|
336
|
+
key: 'paymentReferenceNumber',
|
|
337
|
+
label: 'Payment Reference',
|
|
338
|
+
value: paymentTransaction.paymentReferenceNumber,
|
|
339
|
+
},
|
|
340
|
+
{
|
|
341
|
+
key: 'customerReferenceNumber',
|
|
342
|
+
label: 'Customer Reference Number',
|
|
343
|
+
value: paymentTransaction.customerReferenceNumber,
|
|
344
|
+
},
|
|
345
|
+
{
|
|
346
|
+
key: 'amount',
|
|
347
|
+
label: 'Amount',
|
|
348
|
+
value: amount.formatted,
|
|
349
|
+
},
|
|
350
|
+
...(paymentTransaction.surchargeAmount.amount
|
|
351
|
+
? [
|
|
352
|
+
{
|
|
353
|
+
key: 'surchargeAmount',
|
|
354
|
+
label: 'Surcharge Amount',
|
|
355
|
+
value: formatCurrency(parseFloat(paymentTransaction.surchargeAmount.amount.toString())),
|
|
356
|
+
},
|
|
357
|
+
]
|
|
358
|
+
: []),
|
|
359
|
+
{
|
|
360
|
+
key: 'settlementDate',
|
|
361
|
+
label: 'Settlement Date',
|
|
362
|
+
value: formatDate(paymentTransaction.settlementDate),
|
|
363
|
+
},
|
|
364
|
+
],
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
};
|
|
369
|
+
exports.getDisplayDetailsFromFormSubmissionPayment = getDisplayDetailsFromFormSubmissionPayment;
|
|
58
370
|
//# sourceMappingURL=paymentService.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"paymentService.js","sourceRoot":"","sources":["../src/paymentService.ts"],"names":[],"mappings":";;;AAKA,wBAAgE;AAChE,2DAA6D;AAE7D;;;;;;;;;;;;;GAaG;AACH,SAAgB,oBAAoB,CAClC,UAA0B,EAC1B,UAA0D;IAO1D,MAAM,uBAAuB,GAAG,UAAU,CAAC,aAAa,IAAI,EAAE,CAAA;IAC9D,MA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,CACb,wKAAwK,CACzK,CAAA;KACF;IAED,MAAM,MAAM,GAAG;QACb,sBAAsB;QACtB,MAAM;KACP,CAAA;IAED,OAAO,MAAM,CAAA;AACf,CAAC;AA7ED,oDA6EC","sourcesContent":["import {\n FormTypes,\n SubmissionEventTypes,\n SubmissionTypes,\n} from '@oneblink/types'\nimport { conditionalLogicService, formElementsService } from '.'\nimport { getRootElementValueById } from './submissionService'\n\n/**\n * Examine a submission and its form definition to validate whether a payment\n * workflow event needs to run.\n *\n * #### Example\n *\n * ```js\n * const result = paymentService.checkForPaymentEvent(form, submission)\n * ```\n *\n * @param definition\n * @param submission\n * @returns\n */\nexport function checkForPaymentEvent(\n definition: FormTypes.Form,\n submission: SubmissionTypes.S3SubmissionData['submission'],\n):\n | {\n paymentSubmissionEvent: SubmissionEventTypes.FormPaymentEvent\n amount: number\n }\n | undefined {\n const paymentSubmissionEvents = definition.paymentEvents || []\n const paymentSubmissionEvent = paymentSubmissionEvents.find(\n (paymentSubmissionEvent) => {\n return (\n paymentSubmissionEvent &&\n conditionalLogicService.evaluateConditionalPredicates({\n isConditional: !!paymentSubmissionEvent.conditionallyExecute,\n requiresAllConditionalPredicates:\n !!paymentSubmissionEvent.requiresAllConditionallyExecutePredicates,\n conditionalPredicates:\n paymentSubmissionEvent.conditionallyExecutePredicates || [],\n submission: submission,\n formElements: definition.elements,\n })\n )\n },\n )\n\n if (!paymentSubmissionEvent) {\n return\n }\n console.log(\n 'Checking if submission with payment submission event needs processing',\n )\n\n const amountElement = formElementsService.findFormElement(\n definition.elements,\n (element) => element.id === paymentSubmissionEvent.configuration.elementId,\n )\n if (!amountElement || amountElement.type === 'page') {\n console.log(\n 'Form has a payment submission event but the amount element does not exist, throwing error',\n )\n throw new Error(\n 'We could not find the configuration required to make a payment. Please contact your administrator to ensure your application configuration has been completed successfully.',\n )\n }\n\n console.log('Found form element for payment submission event', amountElement)\n\n const amount = getRootElementValueById(\n amountElement.id,\n definition.elements,\n submission,\n )\n\n if (!amount) {\n console.log(\n 'Form has a payment submission event but the amount has been entered as 0 or not at all, finishing as normal submission',\n )\n return\n }\n\n if (typeof amount !== 'number') {\n console.log(\n 'Form has a payment submission event but the amount is not a number, throwing error',\n )\n throw new Error(\n 'The configuration required to make a payment is incorrect. Please contact your administrator to ensure your application configuration has been completed successfully.',\n )\n }\n\n const result = {\n paymentSubmissionEvent,\n amount,\n }\n\n return result\n}\n"]}
|
|
1
|
+
{"version":3,"file":"paymentService.js","sourceRoot":"","sources":["../src/paymentService.ts"],"names":[],"mappings":";;;AAKA,wBAAgE;AAChE,2DAG4B;AAE5B;;;;;;;;;;;;;GAaG;AACH,SAAgB,oBAAoB,CAClC,UAA0B,EAC1B,UAA0D;IAO1D,MAAM,uBAAuB,GAAG,UAAU,CAAC,aAAa,IAAI,EAAE,CAAA;IAC9D,MAAM,sBAAsB,GAAG,uBAAuB,CAAC,IAAI,CACzD,CAAC,sBAAsB,EAAE,EAAE;QACzB,OAAO,CACL,sBAAsB;YACtB,0BAAuB,CAAC,6BAA6B,CAAC;gBACpD,aAAa,EAAE,CAAC,CAAC,sBAAsB,CAAC,oBAAoB;gBAC5D,gCAAgC,EAC9B,CAAC,CAAC,sBAAsB,CAAC,yCAAyC;gBACpE,qBAAqB,EACnB,sBAAsB,CAAC,8BAA8B,IAAI,EAAE;gBAC7D,UAAU,EAAE,UAAU;gBACtB,YAAY,EAAE,UAAU,CAAC,QAAQ;aAClC,CAAC,CACH,CAAA;IACH,CAAC,CACF,CAAA;IAED,IAAI,CAAC,sBAAsB,EAAE;QAC3B,OAAM;KACP;IACD,OAAO,CAAC,GAAG,CACT,uEAAuE,CACxE,CAAA;IAED,MAAM,aAAa,GAAG,sBAAmB,CAAC,eAAe,CACvD,UAAU,CAAC,QAAQ,EACnB,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,EAAE,KAAK,sBAAsB,CAAC,aAAa,CAAC,SAAS,CAC3E,CAAA;IACD,IAAI,CAAC,aAAa,IAAI,aAA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AAE,wBAAwB;wBAC7B,KAAK,EAAE,mBAAmB;wBAC1B,KAAK,EAAE,kBAAkB,CAAC,sBAAsB;qBACjD;oBACD;wBACE,GAAG,EAAE,yBAAyB;wBAC9B,KAAK,EAAE,2BAA2B;wBAClC,KAAK,EAAE,kBAAkB,CAAC,uBAAuB;qBAClD;oBACD;wBACE,GAAG,EAAE,QAAQ;wBACb,KAAK,EAAE,QAAQ;wBACf,KAAK,EAAE,MAAM,CAAC,SAAS;qBACxB;oBACD,GAAG,CAAC,kBAAkB,CAAC,eAAe,CAAC,MAAM;wBAC3C,CAAC,CAAC;4BACE;gCACE,GAAG,EAAE,iBAAiB;gCACtB,KAAK,EAAE,kBAAkB;gCACzB,KAAK,EAAE,cAAc,CACnB,UAAU,CACR,kBAAkB,CAAC,eAAe,CAAC,MAAM,CAAC,QAAQ,EAAE,CACrD,CACF;6BACF;yBACF;wBACH,CAAC,CAAC,EAAE,CAAC;oBACP;wBACE,GAAG,EAAE,gBAAgB;wBACrB,KAAK,EAAE,iBAAiB;wBACxB,KAAK,EAAE,UAAU,CAAC,kBAAkB,CAAC,cAAc,CAAC;qBACrD;iBACF;aACF,CAAA;SACF;KACF;AACH,CAAC,CAAA;AAhVY,QAAA,0CAA0C,8CAgVtD","sourcesContent":["import {\n FormTypes,\n SubmissionEventTypes,\n SubmissionTypes,\n} from '@oneblink/types'\nimport { conditionalLogicService, formElementsService } from '.'\nimport {\n getRootElementValueById,\n ReplaceInjectablesFormatters,\n} from './submissionService'\n\n/**\n * Examine a submission and its form definition to validate whether a payment\n * workflow event needs to run.\n *\n * #### Example\n *\n * ```js\n * const result = paymentService.checkForPaymentEvent(form, submission)\n * ```\n *\n * @param definition\n * @param submission\n * @returns\n */\nexport function checkForPaymentEvent(\n definition: FormTypes.Form,\n submission: SubmissionTypes.S3SubmissionData['submission'],\n):\n | {\n paymentSubmissionEvent: SubmissionEventTypes.FormPaymentEvent\n amount: number\n }\n | undefined {\n const paymentSubmissionEvents = definition.paymentEvents || []\n const paymentSubmissionEvent = paymentSubmissionEvents.find(\n (paymentSubmissionEvent) => {\n return (\n paymentSubmissionEvent &&\n conditionalLogicService.evaluateConditionalPredicates({\n isConditional: !!paymentSubmissionEvent.conditionallyExecute,\n requiresAllConditionalPredicates:\n !!paymentSubmissionEvent.requiresAllConditionallyExecutePredicates,\n conditionalPredicates:\n paymentSubmissionEvent.conditionallyExecutePredicates || [],\n submission: submission,\n formElements: definition.elements,\n })\n )\n },\n )\n\n if (!paymentSubmissionEvent) {\n return\n }\n console.log(\n 'Checking if submission with payment submission event needs processing',\n )\n\n const amountElement = formElementsService.findFormElement(\n definition.elements,\n (element) => element.id === paymentSubmissionEvent.configuration.elementId,\n )\n if (!amountElement || amountElement.type === 'page') {\n console.log(\n 'Form has a payment submission event but the amount element does not exist, throwing error',\n )\n throw new Error(\n 'We could not find the configuration required to make a payment. Please contact your administrator to ensure your application configuration has been completed successfully.',\n )\n }\n\n console.log('Found form element for payment submission event', amountElement)\n\n const amount = getRootElementValueById(\n amountElement.id,\n definition.elements,\n submission,\n )\n\n if (!amount) {\n console.log(\n 'Form has a payment submission event but the amount has been entered as 0 or not at all, finishing as normal submission',\n )\n return\n }\n\n if (typeof amount !== 'number') {\n console.log(\n 'Form has a payment submission event but the amount is not a number, throwing error',\n )\n throw new Error(\n 'The configuration required to make a payment is incorrect. Please contact your administrator to ensure your application configuration has been completed successfully.',\n )\n }\n\n const result = {\n paymentSubmissionEvent,\n amount,\n }\n\n return result\n}\n\ntype PaymentDisplayDetail = {\n label: string\n value: string\n /** A key to identify the detail */\n key: string\n}\n\n/**\n * Retrieve an array of detail items from a form submission payment.\n *\n * #### Example\n *\n * ```js\n * const detailItems =\n * paymentService.getDisplayDetailsFromFormSubmissionPayment(\n * formSubmissionPayment,\n * {\n * formatCurrency,\n * formatDateTime,\n * formatDate,\n * },\n * )\n * ```\n *\n * @param formSubmissionPayment\n * @param options\n * @returns\n */\nexport const getDisplayDetailsFromFormSubmissionPayment = (\n /** The form submission payment to get the details from */\n formSubmissionPayment: SubmissionTypes.FormSubmissionPayment,\n {\n formatCurrency,\n formatDateTime,\n formatDate,\n }: Pick<\n ReplaceInjectablesFormatters,\n 'formatCurrency' | 'formatDate' | 'formatDateTime'\n >,\n):\n | {\n amount: {\n value: number\n formatted: string\n }\n paymentDisplayDetails: PaymentDisplayDetail[]\n }\n | undefined => {\n switch (formSubmissionPayment.type) {\n case 'NSW_GOV_PAY': {\n const { paymentTransaction } = formSubmissionPayment\n if (!paymentTransaction || !paymentTransaction.agencyCompletionPayment) {\n return\n }\n\n const amount = {\n value: paymentTransaction.agencyCompletionPayment.amount,\n formatted: formatCurrency(\n paymentTransaction.agencyCompletionPayment.amount,\n ),\n }\n return {\n amount,\n paymentDisplayDetails: [\n {\n key: 'completionReference',\n label: 'Completion Reference',\n value:\n paymentTransaction.agencyCompletionPayment\n .paymentCompletionReference,\n },\n {\n key: 'paymentReference',\n label: 'Payment Reference',\n value: paymentTransaction.agencyCompletionPayment.paymentReference,\n },\n {\n key: 'bankReference',\n label: 'Bank Reference',\n value: paymentTransaction.agencyCompletionPayment.bankReference,\n },\n {\n key: 'paymentMethod',\n label: 'Payment Method',\n value: paymentTransaction.agencyCompletionPayment.paymentMethod,\n },\n ...(paymentTransaction.agencyCompletionPayment.paymentMethod ===\n 'BPAY' &&\n paymentTransaction.agencyCompletionPayment.bPay?.billerCode\n ? [\n {\n key: 'billerCode',\n label: 'BPay Biller Code',\n value:\n paymentTransaction.agencyCompletionPayment.bPay.billerCode,\n },\n ]\n : []),\n ...(paymentTransaction.agencyCompletionPayment.paymentMethod ===\n 'CARD'\n ? [\n {\n key: 'creditCardMask',\n label: 'Card Number',\n value: `xxxx xxxx xxxx ${paymentTransaction.agencyCompletionPayment.card?.last4Digits}`,\n },\n ]\n : []),\n {\n key: 'amount',\n label: 'Amount',\n value: amount.formatted,\n },\n {\n key: 'surchargeAmount',\n label: 'Surcharge Amount',\n value: formatCurrency(\n paymentTransaction.agencyCompletionPayment.surcharge,\n ),\n },\n {\n key: 'surchargeGST',\n label: 'Surcharge GST',\n value: formatCurrency(\n paymentTransaction.agencyCompletionPayment.surchargeGst,\n ),\n },\n {\n key: 'createdDateTime',\n label: 'Created Date Time',\n value: formatDateTime(formSubmissionPayment.createdAt),\n },\n ],\n }\n }\n case 'BPOINT': {\n const { paymentTransaction } = formSubmissionPayment\n if (!paymentTransaction) {\n return\n }\n const amountValue = paymentTransaction.Amount / 100\n const amount = {\n value: amountValue,\n formatted: formatCurrency(amountValue),\n }\n return {\n amount,\n paymentDisplayDetails: [\n {\n key: 'receiptNumber',\n label: 'Receipt Number',\n value: paymentTransaction.ReceiptNumber,\n },\n {\n key: 'crn1',\n label: 'CRN 1',\n value: paymentTransaction.Crn1,\n },\n {\n key: 'crn2',\n label: 'CRN 2',\n value: paymentTransaction.Crn2,\n },\n {\n key: 'crn3',\n label: 'CRN 3',\n value: paymentTransaction.Crn3,\n },\n {\n key: 'billerCode',\n label: 'Biller Code',\n value: paymentTransaction.BillerCode,\n },\n {\n key: 'creditCardMask',\n label: 'Card Number',\n value: paymentTransaction.CardDetails.MaskedCardNumber,\n },\n {\n key: 'amount',\n label: 'Amount',\n value: amount.formatted,\n },\n {\n key: 'surchargeAmount',\n label: 'Surcharge Amount',\n value: formatCurrency(paymentTransaction.AmountSurcharge / 100),\n },\n {\n key: 'processedDateTime',\n label: 'Processed Date Time',\n value: formatDateTime(paymentTransaction.ProcessedDateTime),\n },\n ],\n }\n }\n case 'CP_PAY': {\n const { paymentTransaction } = formSubmissionPayment\n if (!paymentTransaction) {\n return\n }\n\n const determineDetails = () => {\n switch (paymentTransaction.cpPayVersion) {\n case 'v2': {\n return {\n transactionId: paymentTransaction.result.id,\n orderNumber:\n paymentTransaction.result.externalReferenceId ?? undefined,\n paymentType: paymentTransaction.result.paymentType,\n creditCardMask: paymentTransaction.result.lastFour\n ? `xxxx xxxx xxxx ${paymentTransaction.result.lastFour}`\n : undefined,\n amount:\n paymentTransaction.result.amount !== undefined\n ? {\n value: paymentTransaction.result.amount,\n formatted: formatCurrency(\n paymentTransaction.result.amount,\n ),\n }\n : {\n value: NaN,\n formatted: 'Unknown',\n },\n createdDateTime: paymentTransaction.result.createdOnUtc,\n }\n }\n default: {\n return {\n transactionId: paymentTransaction.transactionId,\n orderNumber: paymentTransaction.orderNumber ?? undefined,\n paymentType:\n paymentTransaction.paymentTypeId === 1\n ? 'Credit/Debit Card'\n : paymentTransaction.paymentTypeId === 2\n ? 'ACH'\n : undefined,\n creditCardMask: paymentTransaction.lastFour\n ? `xxxx xxxx xxxx ${paymentTransaction.lastFour}`\n : undefined,\n amount: {\n value: paymentTransaction.amount,\n formatted: formatCurrency(paymentTransaction.amount),\n },\n createdDateTime: paymentTransaction.createdAt,\n }\n }\n }\n }\n\n const {\n transactionId,\n orderNumber,\n paymentType,\n creditCardMask,\n amount,\n createdDateTime,\n } = determineDetails()\n const paymentDisplayDetails = []\n if (transactionId) {\n paymentDisplayDetails.push({\n key: 'transactionId',\n label: 'Transaction Id',\n value: transactionId,\n })\n }\n if (orderNumber) {\n paymentDisplayDetails.push({\n key: 'orderNumber',\n label: 'Order Number',\n value: orderNumber,\n })\n }\n if (paymentType) {\n paymentDisplayDetails.push({\n key: 'paymentType',\n label: 'Payment Type',\n value: paymentType,\n })\n }\n if (creditCardMask) {\n paymentDisplayDetails.push({\n key: 'creditCardMask',\n label: 'Card Number',\n value: creditCardMask,\n })\n }\n paymentDisplayDetails.push({\n key: 'amount',\n label: 'Amount',\n value: amount.formatted,\n })\n if (createdDateTime) {\n paymentDisplayDetails.push({\n key: 'createdDateTime',\n label: 'Created At',\n value: formatDateTime(createdDateTime),\n })\n }\n\n return {\n amount,\n paymentDisplayDetails,\n }\n }\n case 'WESTPAC_QUICK_STREAM': {\n const { paymentTransaction } = formSubmissionPayment\n if (!paymentTransaction) {\n return\n }\n\n const amount = {\n value: paymentTransaction.totalAmount.amount,\n formatted: formatCurrency(\n parseFloat(paymentTransaction.totalAmount.amount.toString()),\n ),\n }\n\n return {\n amount,\n paymentDisplayDetails: [\n {\n key: 'receiptNumber',\n label: 'Receipt Number',\n value: paymentTransaction.receiptNumber,\n },\n {\n key: 'paymentReferenceNumber',\n label: 'Payment Reference',\n value: paymentTransaction.paymentReferenceNumber,\n },\n {\n key: 'customerReferenceNumber',\n label: 'Customer Reference Number',\n value: paymentTransaction.customerReferenceNumber,\n },\n {\n key: 'amount',\n label: 'Amount',\n value: amount.formatted,\n },\n ...(paymentTransaction.surchargeAmount.amount\n ? [\n {\n key: 'surchargeAmount',\n label: 'Surcharge Amount',\n value: formatCurrency(\n parseFloat(\n paymentTransaction.surchargeAmount.amount.toString(),\n ),\n ),\n },\n ]\n : []),\n {\n key: 'settlementDate',\n label: 'Settlement Date',\n value: formatDate(paymentTransaction.settlementDate),\n },\n ],\n }\n }\n }\n}\n"]}
|
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
import { FormTypes, MiscTypes, ScheduledTasksTypes, SubmissionTypes } from '@oneblink/types';
|
|
2
2
|
export type ReplaceInjectablesFormatters = {
|
|
3
|
+
/** A function to format any date/times. */
|
|
3
4
|
formatDateTime: (value: string) => string;
|
|
5
|
+
/** A function to format any dates. */
|
|
4
6
|
formatDate: (value: string) => string;
|
|
7
|
+
/** A function to format any times. */
|
|
5
8
|
formatTime: (value: string) => string;
|
|
9
|
+
/** A function to format any numbers. */
|
|
6
10
|
formatNumber: (value: number) => string;
|
|
11
|
+
/** A function to format any numbers as currency. */
|
|
7
12
|
formatCurrency: (value: number) => string;
|
|
8
13
|
};
|
|
9
14
|
export type ReplaceInjectablesBaseOptions = ReplaceInjectablesFormatters & {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"replaceCustomValues.js","sourceRoot":"","sources":["../src/replaceCustomValues.ts"],"names":[],"mappings":";;;AAWA,6CAAwD;AACxD,+DAI8B;AAC9B,+DAA4E;AAoC5E,MAAM,iBAAiB,GAGlB;IACH;QACE,MAAM,EAAE,gBAA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;wBACA,KAAK,MAAM,KAAK,IAAI,OAAO,EAAE;4BAC3B,MAAM,gBAAgB,GAAG,wBAAwB,CAC/C,WAAW,EACX,CAAC,YAAY,EAAE,EAAE;gCACf,OAAO,YAAY,CAAC,UAAU,CAC5B,YAAY,wBAAwB,GAAG,EACvC,WAAW,CACZ,CAAA;4BACH,CAAC,CACF,CAAA;4BACD,MAAM,eAAe,GAAG,kCAAkC,CAAI;gCAC5D,QAAQ,EAAE,gBAAgB;gCAC1B,UAAU,EAAE,KAAK;gCACjB,YAAY,EAAE,oBAAoB,CAAC,QAAQ;gCAC3C,sBAAsB;gCACtB,wBAAwB;6BACzB,CAAC,CAAA;4BACF,IAAI,eAAe,CAAC,IAAI,EAAE;gCACxB,eAAe,CAAC,OAAO,CAAC,CAAC,cAAc,EAAE,iBAAiB,EAAE,EAAE;oCAC5D,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,iBAAiB,CAAC,EAAE;wCACxC,YAAY,CAAC,GAAG,CAAC,iBAAiB,EAAE,cAAc,CAAC,CAAA;qCACpD;gCACH,CAAC,CAAC,CAAA;6BACH;yBACF;qBACF;iBACF;aACF;QACH,CAAC,CAAC,CAAA;KACH;SAAM;QACL,YAAY,CAAC,GAAG,CAAC,WAAW,EAAE,WAAW,CAAC,CAAA;KAC3C;IAED,OAAO,YAAY,CAAA;AACrB,CAAC;AArID,gFAqIC","sourcesContent":["import {\n APINSWTypes,\n CivicaTypes,\n FormTypes,\n GeoscapeTypes,\n GoogleTypes,\n MiscTypes,\n PointTypes,\n ScheduledTasksTypes,\n SubmissionTypes,\n} from '@oneblink/types'\nimport { getABNNumberFromABNRecord } from './abnService'\nimport {\n RootElementRegex,\n NestedElementRegex,\n matchElementsTagRegex,\n} from './form-elements-regex'\nimport { findFormElement, flattenFormElements } from './formElementsService'\n\nexport type ReplaceInjectablesFormatters = {\n formatDateTime: (value: string) => string\n formatDate: (value: string) => string\n formatTime: (value: string) => string\n formatNumber: (value: number) => string\n formatCurrency: (value: number) => string\n}\n\nexport type ReplaceInjectablesBaseOptions = ReplaceInjectablesFormatters & {\n submission: SubmissionTypes.S3SubmissionData['submission']\n userProfile: MiscTypes.UserProfile | undefined\n task: ScheduledTasksTypes.Task | undefined\n taskGroup: ScheduledTasksTypes.TaskGroup | undefined\n taskGroupInstance: ScheduledTasksTypes.TaskGroupInstance | undefined\n /**\n * Determine if only root level elements should be replaced.\n *\n * `false` will replace `\"{ELEMENT:Parent_Name}\"` and\n * `\"{ELEMENT:Children|Name}\"`.\n *\n * `true` will replace `\"{ELEMENT:Parent_Name}\"` but will NOT replace\n * `{ELEMENT:Children|Name}`.\n */\n excludeNestedElements?: boolean\n}\n\nexport type ReplaceInjectablesOptions = ReplaceInjectablesBaseOptions & {\n form: FormTypes.Form\n submissionId: string\n submissionTimestamp: string\n externalId: string | undefined\n previousApprovalId: string | undefined\n}\n\nconst SUBMISSION_VALUES: Array<{\n string: string\n value: (options: ReplaceInjectablesOptions) => string | undefined\n}> = [\n {\n string: '{INFO_PAGE_ID}',\n value: ({ form }: ReplaceInjectablesOptions) => form.id.toString(),\n },\n {\n string: '{INFO_PAGE_NAME}',\n value: ({ form }: ReplaceInjectablesOptions) => form.name,\n },\n {\n string: '{FORM_ID}',\n value: ({ form }: ReplaceInjectablesOptions) => form.id.toString(),\n },\n {\n string: '{FORM_NAME}',\n value: ({ form }: ReplaceInjectablesOptions) => form.name,\n },\n {\n string: '{DATE}',\n value: ({\n submissionTimestamp,\n formatDateTime,\n }: ReplaceInjectablesOptions) => {\n if (!submissionTimestamp) {\n return undefined\n }\n return formatDateTime(submissionTimestamp)\n },\n },\n {\n string: '{TIMESTAMP}',\n value: ({ submissionTimestamp }: ReplaceInjectablesOptions) =>\n submissionTimestamp,\n },\n {\n string: '{SUBMISSION_ID}',\n value: ({ submissionId }: ReplaceInjectablesOptions) => submissionId,\n },\n {\n string: '{EXTERNAL_ID}',\n value: ({ externalId }: ReplaceInjectablesOptions) => externalId,\n },\n {\n string: '{PREVIOUS_APPROVAL_ID}',\n value: ({ previousApprovalId }: ReplaceInjectablesOptions) =>\n previousApprovalId,\n },\n]\n\nconst ELEMENT_VALUES: Array<{\n string: string\n value: (options: ReplaceInjectablesBaseOptions) => string | undefined\n}> = [\n {\n string: '{USER:email}',\n value: ({ userProfile }) => userProfile?.email,\n },\n {\n string: '{USER:username}',\n value: ({ userProfile }) => userProfile?.username,\n },\n {\n string: '{TASK_NAME}',\n value: ({ task }) => task?.name,\n },\n {\n string: '{TASK_GROUP_NAME}',\n value: ({ taskGroup }) => taskGroup?.name,\n },\n {\n string: '{TASK_GROUP_INSTANCE_LABEL}',\n value: ({ taskGroupInstance }) => taskGroupInstance?.label,\n },\n]\n\n/**\n * Function to get the display value of a property in submission, if elementId\n * is provided propertyName will be ignored\n *\n * #### Example\n *\n * ```typescript\n * const result = submissionService.getElementSubmissionValue({\n * propertyName: 'search',\n * submission: {\n * search: 'Entered By User',\n * },\n * formElements: [\n * {\n * id: 'd4135b47-9004-4d75-aeb3-d2f6232da111',\n * name: 'search',\n * type: 'text',\n * label: 'Search',\n * readOnly: false,\n * required: false,\n * conditionallyShow: false,\n * requiresAllConditionallyShowPredicates: false,\n * isElementLookup: false,\n * isDataLookup: false,\n * },\n * ],\n * formatDate: (value) => new Date(value).toDateString(),\n * formatDateTime: (value) => new Date(value).toString(),\n * formatTime: (value) => new Date(value).toTimeString(),\n * formatNumber: (value) => Number(value).toString(),\n * formatCurrency: (value) => Number(value).toFixed(2),\n * })\n * ```\n *\n * @param options\n * @returns\n */\nexport function getElementSubmissionValue({\n propertyName,\n submission,\n formElements,\n formatDate,\n formatDateTime,\n formatTime,\n formatNumber,\n formatCurrency,\n}: {\n propertyName: string\n formElements: FormTypes.FormElement[]\n submission: SubmissionTypes.S3SubmissionData['submission']\n} & ReplaceInjectablesFormatters):\n | {\n element: FormTypes.FormElement | undefined\n value: unknown\n }\n | undefined\nexport function getElementSubmissionValue({\n elementId,\n submission,\n formElements,\n formatDate,\n formatDateTime,\n formatTime,\n formatNumber,\n formatCurrency,\n}: {\n elementId: string\n formElements: FormTypes.FormElement[]\n submission: SubmissionTypes.S3SubmissionData['submission']\n} & ReplaceInjectablesFormatters):\n | {\n element: FormTypes.FormElement | undefined\n value: unknown\n }\n | undefined\nexport function getElementSubmissionValue({\n elementId,\n propertyName,\n submission,\n formElements,\n formatDate,\n formatDateTime,\n formatTime,\n formatNumber,\n formatCurrency,\n}: {\n elementId?: string\n propertyName?: string\n formElements: FormTypes.FormElement[]\n submission: SubmissionTypes.S3SubmissionData['submission']\n} & ReplaceInjectablesFormatters) {\n if (elementId === undefined && propertyName === undefined) {\n return undefined\n }\n\n let result: ReturnType<typeof getElementSubmissionValueByName> | undefined =\n undefined\n\n if (elementId) {\n result = getElementSubmissionValueById({\n elementId,\n formElements,\n submission,\n })\n } else if (propertyName) {\n result = getElementSubmissionValueByName({\n propertyName,\n formElements,\n submission,\n })\n }\n\n if (!result) {\n return\n }\n return formatValue({\n element: result.formElement,\n unknownValue: result.unknownValue,\n formatDate,\n formatTime,\n formatDateTime,\n formatCurrency,\n formatNumber,\n })\n}\n\nfunction getElementSubmissionValueByName({\n propertyName,\n formElements,\n submission,\n}: {\n propertyName: string\n formElements: FormTypes.FormElement[]\n submission: SubmissionTypes.S3SubmissionData['submission']\n}):\n | { formElement: FormTypes.FormElement | undefined; unknownValue: unknown }\n | undefined {\n const unknownValue = submission[propertyName]\n if (unknownValue === undefined || unknownValue === null) {\n return undefined\n }\n\n const formElement = findFormElement(\n formElements,\n (element) =>\n element.type !== 'page' &&\n element.type !== 'section' &&\n element.name === propertyName,\n )\n\n return { formElement, unknownValue }\n}\n\nfunction getElementSubmissionValueById({\n elementId,\n formElements,\n submission,\n}: {\n elementId: string\n formElements: FormTypes.FormElement[]\n submission: SubmissionTypes.S3SubmissionData['submission']\n}): { formElement: FormTypes.FormElement; unknownValue: unknown } | undefined {\n // make sure submission is an object\n if (Object(submission) !== submission) {\n return undefined\n }\n\n const flattenedElements = flattenFormElements(formElements)\n\n let unknown: unknown = undefined\n let formElement: FormTypes.FormElement | undefined = undefined\n\n for (const element of flattenedElements) {\n if (elementId === element.id) {\n if ('name' in element) {\n unknown = submission[element.name]\n formElement = element\n }\n break\n }\n if (element.type === 'form') {\n const newSubmissionData = submission[element.name]\n\n const result = getElementSubmissionValueById({\n elementId,\n formElements: element.elements ?? [],\n submission:\n newSubmissionData as SubmissionTypes.S3SubmissionData['submission'],\n })\n if (result) {\n unknown = result.unknownValue\n formElement = result.formElement\n break\n }\n }\n }\n\n if (unknown === undefined || unknown === null || formElement === undefined) {\n return undefined\n }\n\n return { unknownValue: unknown, formElement }\n}\n\nfunction formatValue({\n element,\n unknownValue,\n formatDate,\n formatTime,\n formatDateTime,\n formatCurrency,\n formatNumber,\n}: {\n element: FormTypes.FormElement | undefined\n unknownValue: unknown\n} & ReplaceInjectablesFormatters) {\n switch (element?.type) {\n case 'datetime': {\n const value = unknownValue as string\n return { element: element, value: formatDateTime(value) }\n }\n case 'date': {\n const value = unknownValue as string\n return { element: element, value: formatDate(value) }\n }\n case 'time': {\n const value = unknownValue as string\n return { element: element, value: formatTime(value) }\n }\n case 'radio':\n case 'autocomplete': {\n const value = unknownValue as string\n const option = element.options?.find((opt) => opt.value === value)\n return { element: element, value: option?.label || value }\n }\n\n case 'checkboxes': {\n const value = unknownValue as string[]\n const options = element.options\n const selectedOptionLabels: string[] = value.reduce(\n (labels: string[], selectedOption: string) => {\n const foundOption = options?.find((o) => o.value === selectedOption)\n if (foundOption) labels.push(foundOption.label)\n return labels\n },\n [],\n )\n return {\n element: element,\n value: selectedOptionLabels.length ? selectedOptionLabels : undefined,\n }\n }\n case 'compliance': {\n const value = unknownValue as {\n value?: string\n }\n const option = (element.options || []).find(\n (option: FormTypes.ChoiceElementOption) => option.value === value.value,\n )\n return {\n element: element,\n value: {\n ...value,\n value: option?.label || value.value,\n },\n }\n }\n case 'select': {\n if (element.multi) {\n const value = unknownValue as string[]\n const options = element.options\n const selectedOptionLabels: string[] = value.reduce(\n (labels: string[], selectedOption: string) => {\n const foundOption = options?.find((o) => o.value === selectedOption)\n if (foundOption) labels.push(foundOption.label)\n return labels\n },\n [],\n )\n return {\n element: element,\n value: selectedOptionLabels.length ? selectedOptionLabels : undefined,\n }\n } else {\n const value = unknownValue as string\n const option = element.options?.find((opt) => opt.value === value)\n return { element: element, value: option?.label }\n }\n }\n case 'boolean': {\n const value = unknownValue as boolean\n return { element: element, value: value ? 'Yes' : 'No' }\n }\n case 'calculation': {\n const value = unknownValue as number\n if (!Number.isNaN(value) && Number.isFinite(value)) {\n let text\n if (element.displayAsCurrency) {\n text = formatCurrency(value)\n } else {\n text = formatNumber(value)\n }\n return { element: element, value: text }\n }\n return undefined\n }\n case 'pointAddress':\n case 'geoscapeAddress': {\n const value = unknownValue as\n | PointTypes.PointAddress\n | GeoscapeTypes.GeoscapeAddress\n return {\n element: element,\n value: value?.addressDetails?.formattedAddress || value?.addressId,\n }\n }\n case 'googleAddress': {\n const value = unknownValue as GoogleTypes.GoogleMapsAddress\n return {\n element: element,\n value: value.formatted_address || value.place_id,\n }\n }\n case 'civicaStreetName': {\n const value = unknownValue as CivicaTypes.CivicaStreetName\n return { element: element, value: value?.formattedStreet }\n }\n case 'civicaNameRecord': {\n const value = unknownValue as CivicaTypes.CivicaNameRecord\n return {\n element: element,\n value:\n [value?.title, value?.givenName1, value?.familyName]\n .filter((t) => t)\n .join(' ') || value?.emailAddress,\n }\n }\n case 'abn': {\n const value = unknownValue as MiscTypes.ABNRecord\n return {\n element: element,\n value: value ? getABNNumberFromABNRecord(value) : undefined,\n }\n }\n case 'apiNSWLiquorLicence': {\n const value = unknownValue as APINSWTypes.LiquorLicenceDetails | undefined\n return {\n element: element,\n value:\n `${value?.licenceDetail?.licenceNumber} | ${value?.licenceDetail?.licenceName}`.trim(),\n }\n }\n default: {\n return { element: element, value: unknownValue }\n }\n }\n}\n\n/**\n * Replace the `{ELEMENT:<elementName>}` values in text while a form is being\n * filled out. The replacements are suppose to be user friendly and for display\n * purposes, e.g. dates should be displayed in the user's desired format and\n * timezone.\n *\n * #### Example\n *\n * ```js\n * const { text: url } =\n * submissionService.replaceInjectablesWithElementValues(\n * 'https://example.com/path?search{ELEMENT:search}',\n * {\n * formatDate: (value) => new Date(value).toDateString(),\n * formatDateTime: (value) => new Date(value).toString(),\n * formatTime: (value) => new Date(value).toTimeString(),\n * formatNumber: (value) => Number(value).toString(),\n * formatCurrency: (value) => Number(value).toFixed(2),\n * submission: {\n * search: 'Entered By User',\n * },\n * elements: [\n * {\n * id: 'd4135b47-9004-4d75-aeb3-d2f6232da111',\n * name: 'search',\n * type: 'text',\n * label: 'Search',\n * readOnly: false,\n * required: false,\n * conditionallyShow: false,\n * requiresAllConditionallyShowPredicates: false,\n * isElementLookup: false,\n * isDataLookup: false,\n * },\n * ],\n * userProfile: {\n * userId: 'abc123',\n * username: 'john-user',\n * email: 'john.user@domain.com',\n * },\n * },\n * )\n * ```\n *\n * @param text\n * @param options\n * @returns\n */\nexport function replaceInjectablesWithElementValues(\n text: string,\n options: {\n formElements: FormTypes.FormElement[]\n } & ReplaceInjectablesBaseOptions,\n): {\n /**\n * The text passed in with all injectables replaced with their corresponding\n * values or empty strings.\n */\n text: string\n /**\n * Determine if replaceable strings could all be replaced. The `text` property\n * returned will replace all injectable with empty strings if the value cannot\n * be found. Use this boolean to either use the replaced string with missing\n * data or implement some other logic to handle missing injectables.\n */\n hadAllInjectablesReplaced: boolean\n} {\n let hadAllInjectablesReplaced = true\n text = ELEMENT_VALUES.reduce((newString, customValue) => {\n if (!newString.includes(customValue.string)) {\n return newString\n }\n const replacedValue = customValue.value(options)\n if (!replacedValue) {\n hadAllInjectablesReplaced = false\n }\n return newString.replaceAll(customValue.string, replacedValue || '')\n }, text)\n\n const matchesElement = text.match(\n options.excludeNestedElements ? RootElementRegex : NestedElementRegex,\n )\n if (matchesElement) {\n matchElementsTagRegex(\n {\n text,\n excludeNestedElements: !!options.excludeNestedElements,\n },\n ({ elementName, elementMatch }) => {\n const result = getElementSubmissionValue({\n propertyName: elementName,\n ...options,\n })\n\n const hasNoValue = result === undefined || result.value === undefined\n if (hasNoValue) {\n hadAllInjectablesReplaced = false\n }\n\n text = text.replace(\n elementMatch,\n hasNoValue ? '' : (result.value as string),\n )\n },\n )\n }\n\n return {\n text,\n hadAllInjectablesReplaced,\n }\n}\n\n/**\n * Replace the `{ELEMENT:<elementName>}` values in text after a successful form\n * submission as well as other replaceable parameters e.g. `submissionId`. The\n * replacements are suppose to be user friendly and for display purposes, e.g.\n * dates should be displayed in the user's desired format and timezone.\n *\n * #### Example\n *\n * ```js\n * const result = submissionService.replaceInjectablesWithSubmissionValues(\n * 'https://example.com/path?submissionId={SUBMISSION_ID}&externalId={EXTERNAL_ID}&search{ELEMENT:search}',\n * {\n * submissionId: 'abc-123',\n * submissionTimestamp: '2021-07-02T02:19:13.670Z',\n * formatDate: (value) => new Date(value).toDateString(),\n * formatDateTime: (value) => new Date(value).toString(),\n * formatTime: (value) => new Date(value).toTimeString(),\n * submission: {\n * search: 'Entered By User',\n * },\n * form: {\n * id: 1,\n * name: 'Form',\n * organisationId: '',\n * formsAppEnvironmentId: 1,\n * formsAppIds: [],\n * isAuthenticated: false,\n * isMultiPage: false,\n * isInfoPage: false,\n * postSubmissionAction: 'FORMS_LIBRARY',\n * cancelAction: 'FORMS_LIBRARY',\n * submissionEvents: [],\n * tags: [],\n * elements: [\n * {\n * id: 'd4135b47-9004-4d75-aeb3-d2f6232da111',\n * name: 'search',\n * type: 'text',\n * label: 'Search',\n * readOnly: false,\n * required: false,\n * conditionallyShow: false,\n * requiresAllConditionallyShowPredicates: false,\n * isElementLookup: false,\n * isDataLookup: false,\n * },\n * ],\n * },\n * },\n * )\n * ```\n *\n * @param text\n * @param options\n * @returns\n */\nexport function replaceInjectablesWithSubmissionValues(\n text: string,\n options: ReplaceInjectablesOptions,\n): {\n /**\n * The text passed in with all injectables replaced with their corresponding\n * values or empty strings.\n */\n text: string\n /**\n * Determine if replaceable strings could all be replaced. The `text` property\n * returned will replace all injectable with empty strings if the value cannot\n * be found. Use this boolean to either use the replaced string with missing\n * data or implement some other logic to handle missing injectables.\n */\n hadAllInjectablesReplaced: boolean\n} {\n const result = replaceInjectablesWithElementValues(text, {\n formElements: options.form.elements,\n ...options,\n })\n let hadAllInjectablesReplaced = result.hadAllInjectablesReplaced\n const replacedText = SUBMISSION_VALUES.reduce((newString, customValue) => {\n if (!newString.includes(customValue.string)) {\n return newString\n }\n const replacedValue = customValue.value(options)\n if (!replacedValue) {\n hadAllInjectablesReplaced = false\n }\n return newString.replaceAll(customValue.string, replacedValue || '')\n }, result.text)\n\n return {\n text: replacedText,\n hadAllInjectablesReplaced,\n }\n}\n\n/**\n * Process a resource with injectable element values to turn a single resource\n * (could be a single) into multiple resources. e.g.\n * `\"{ELEMENT:Children|Child_Name} {ELEMENT:Family_Name}\"` with the following\n * submission data:\n *\n * ```json\n * {\n * \"Family_Name\": \"Smith\",\n * \"Children\": [\n * {\n * \"Child_Name\": \"John\"\n * },\n * {\n * \"Child_Name\": \"Jane\"\n * }\n * ]\n * }\n * ```\n *\n * Would result in the following resources:\n *\n * - `\"John Smith\"`\n * - `\"Jane Smith\"`\n *\n * #### Example\n *\n * ```js\n * const emailAddresses = processInjectablesInCustomResource({\n * resource: '{ELEMENT:People|Email_Address}',\n * submission: {\n * People: [\n * {\n * Email_Address: 'user@oneblink.io',\n * },\n * {\n * Email_Address: 'admin@oneblink.io',\n * },\n * ],\n * },\n * formElements: [\n * {\n * id: '18dcd3e0-6e2f-462e-803b-e24562d9fa6d',\n * type: 'repeatableSet',\n * name: 'People',\n * label: 'People',\n * elements: [\n * {\n * id: 'd0902113-3f77-4070-adbd-ca3ae95ce091',\n * type: 'email',\n * name: 'Email_Address',\n * label: 'Email_Address',\n * },\n * ],\n * },\n * ],\n * replaceRootInjectables: (resource, submission, formElements) => {\n * const { text } = replaceInjectablesWithElementValues(resource, {\n * submission,\n * formElements,\n * excludeNestedElements: true,\n * // other options\n * })\n * return text\n * },\n * })\n * // emailAddresses === [\"user@oneblink.io\", \"admin@oneblink.io\"]\n * ```\n *\n * @param options\n * @returns\n */\nexport function processInjectablesInCustomResource<T>({\n resource,\n submission,\n formElements,\n replaceRootInjectables,\n prepareNestedInjectables = (resource, prepare) =>\n prepare(String(resource)) as T,\n}: {\n /** The resource that contains properties that support injection or a string */\n resource: T\n /** The form submission data to process */\n submission: SubmissionTypes.S3SubmissionData['submission']\n /** The form elements to process */\n formElements: FormTypes.FormElement[]\n /**\n * A function to inject values, this allows custom formatters to be used.\n * Return `undefined` to prevent the injection from recursively continuing.\n *\n * @param resource The current resource that contains properties that support\n * injection or a string\n * @param submission The current form submission data to process (may be an\n * entry in a repeatable set)\n * @param formElements The current form elements to process (may be the\n * elements from a repeatable set)\n * @returns\n */\n replaceRootInjectables: (\n resource: T,\n submission: SubmissionTypes.S3SubmissionData['submission'],\n formElements: FormTypes.FormElement[],\n ) =>\n | [injectedText: string, resourceKey: string, newResource: T]\n | string\n | undefined\n /**\n * An optional function to replace nested injectables when creating multiple\n * resources from repeatable sets. Only required if the `resource` param is\n * not a `string`.\n *\n * @param resource The current resource that contains properties that support\n * injection or a string\n * @param prepare A function to prepare the resource string(s) for another\n * iteration\n * @returns\n */\n prepareNestedInjectables?: (\n resource: T,\n preparer: (resourceText: string) => string,\n ) => T\n}): Map<string, T> {\n const newResources: Map<string, T> = new Map<string, T>()\n\n const injectorResult = replaceRootInjectables(\n resource,\n submission,\n formElements,\n )\n if (!injectorResult) {\n return newResources\n }\n\n const [text, resourceKey, newResource] =\n typeof injectorResult === 'string'\n ? [injectorResult, injectorResult, injectorResult as T]\n : injectorResult\n\n // Find nested form elements\n const matches: Map<string, boolean> = new Map()\n matchElementsTagRegex(\n {\n text,\n excludeNestedElements: false,\n },\n ({ elementName }) => {\n const [repeatableSetElementName, ...elementNames] = elementName.split('|')\n matches.set(repeatableSetElementName, !!elementNames.length)\n },\n )\n\n if (matches.size) {\n matches.forEach((hasNestedFormElements, repeatableSetElementName) => {\n if (hasNestedFormElements) {\n // Attempt to create a new resource for each entry in the repeatable set.\n const entries = submission?.[repeatableSetElementName]\n if (Array.isArray(entries)) {\n const repeatableSetElement = findFormElement(\n formElements,\n (formElement) => {\n return (\n 'name' in formElement &&\n formElement.name === repeatableSetElementName\n )\n },\n )\n if (\n repeatableSetElement &&\n 'elements' in repeatableSetElement &&\n Array.isArray(repeatableSetElement.elements)\n ) {\n for (const entry of entries) {\n const replacedResource = prepareNestedInjectables(\n newResource,\n (resourceText) => {\n return resourceText.replaceAll(\n `{ELEMENT:${repeatableSetElementName}|`,\n '{ELEMENT:',\n )\n },\n )\n const nestedResources = processInjectablesInCustomResource<T>({\n resource: replacedResource,\n submission: entry,\n formElements: repeatableSetElement.elements,\n replaceRootInjectables,\n prepareNestedInjectables,\n })\n if (nestedResources.size) {\n nestedResources.forEach((nestedResource, nestedResourceKey) => {\n if (!newResources.has(nestedResourceKey)) {\n newResources.set(nestedResourceKey, nestedResource)\n }\n })\n }\n }\n }\n }\n }\n })\n } else {\n newResources.set(resourceKey, newResource)\n }\n\n return newResources\n}\n"]}
|
|
1
|
+
{"version":3,"file":"replaceCustomValues.js","sourceRoot":"","sources":["../src/replaceCustomValues.ts"],"names":[],"mappings":";;;AAWA,6CAAwD;AACxD,+DAI8B;AAC9B,+DAA4E;AAyC5E,MAAM,iBAAiB,GAGlB;IACH;QACE,MAAM,EAAE,gBAAgB;QACxB,KAAK,EAAE,CAAC,EAAE,IAAI,EAA6B,EAAE,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,QAAQ,EAAE;KACnE;IACD;QACE,MAAM,EAAE,kBAAkB;QAC1B,KAAK,EAAE,CAAC,EAAE,IAAI,EAA6B,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI;KAC1D;IACD;QACE,MAAM,EAAE,WAAW;QACnB,KAAK,EAAE,CAAC,EAAE,IAAI,EAA6B,EAAE,EAAE,CAAC,IAAI,CAAC,EAAE,C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,EAAE,oBAAoB,CAAC,QAAQ;gCAC3C,sBAAsB;gCACtB,wBAAwB;6BACzB,CAAC,CAAA;4BACF,IAAI,eAAe,CAAC,IAAI,EAAE;gCACxB,eAAe,CAAC,OAAO,CAAC,CAAC,cAAc,EAAE,iBAAiB,EAAE,EAAE;oCAC5D,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,iBAAiB,CAAC,EAAE;wCACxC,YAAY,CAAC,GAAG,CAAC,iBAAiB,EAAE,cAAc,CAAC,CAAA;qCACpD;gCACH,CAAC,CAAC,CAAA;6BACH;yBACF;qBACF;iBACF;aACF;QACH,CAAC,CAAC,CAAA;KACH;SAAM;QACL,YAAY,CAAC,GAAG,CAAC,WAAW,EAAE,WAAW,CAAC,CAAA;KAC3C;IAED,OAAO,YAAY,CAAA;AACrB,CAAC;AArID,gFAqIC","sourcesContent":["import {\n APINSWTypes,\n CivicaTypes,\n FormTypes,\n GeoscapeTypes,\n GoogleTypes,\n MiscTypes,\n PointTypes,\n ScheduledTasksTypes,\n SubmissionTypes,\n} from '@oneblink/types'\nimport { getABNNumberFromABNRecord } from './abnService'\nimport {\n RootElementRegex,\n NestedElementRegex,\n matchElementsTagRegex,\n} from './form-elements-regex'\nimport { findFormElement, flattenFormElements } from './formElementsService'\n\nexport type ReplaceInjectablesFormatters = {\n /** A function to format any date/times. */\n formatDateTime: (value: string) => string\n /** A function to format any dates. */\n formatDate: (value: string) => string\n /** A function to format any times. */\n formatTime: (value: string) => string\n /** A function to format any numbers. */\n formatNumber: (value: number) => string\n /** A function to format any numbers as currency. */\n formatCurrency: (value: number) => string\n}\n\nexport type ReplaceInjectablesBaseOptions = ReplaceInjectablesFormatters & {\n submission: SubmissionTypes.S3SubmissionData['submission']\n userProfile: MiscTypes.UserProfile | undefined\n task: ScheduledTasksTypes.Task | undefined\n taskGroup: ScheduledTasksTypes.TaskGroup | undefined\n taskGroupInstance: ScheduledTasksTypes.TaskGroupInstance | undefined\n /**\n * Determine if only root level elements should be replaced.\n *\n * `false` will replace `\"{ELEMENT:Parent_Name}\"` and\n * `\"{ELEMENT:Children|Name}\"`.\n *\n * `true` will replace `\"{ELEMENT:Parent_Name}\"` but will NOT replace\n * `{ELEMENT:Children|Name}`.\n */\n excludeNestedElements?: boolean\n}\n\nexport type ReplaceInjectablesOptions = ReplaceInjectablesBaseOptions & {\n form: FormTypes.Form\n submissionId: string\n submissionTimestamp: string\n externalId: string | undefined\n previousApprovalId: string | undefined\n}\n\nconst SUBMISSION_VALUES: Array<{\n string: string\n value: (options: ReplaceInjectablesOptions) => string | undefined\n}> = [\n {\n string: '{INFO_PAGE_ID}',\n value: ({ form }: ReplaceInjectablesOptions) => form.id.toString(),\n },\n {\n string: '{INFO_PAGE_NAME}',\n value: ({ form }: ReplaceInjectablesOptions) => form.name,\n },\n {\n string: '{FORM_ID}',\n value: ({ form }: ReplaceInjectablesOptions) => form.id.toString(),\n },\n {\n string: '{FORM_NAME}',\n value: ({ form }: ReplaceInjectablesOptions) => form.name,\n },\n {\n string: '{DATE}',\n value: ({\n submissionTimestamp,\n formatDateTime,\n }: ReplaceInjectablesOptions) => {\n if (!submissionTimestamp) {\n return undefined\n }\n return formatDateTime(submissionTimestamp)\n },\n },\n {\n string: '{TIMESTAMP}',\n value: ({ submissionTimestamp }: ReplaceInjectablesOptions) =>\n submissionTimestamp,\n },\n {\n string: '{SUBMISSION_ID}',\n value: ({ submissionId }: ReplaceInjectablesOptions) => submissionId,\n },\n {\n string: '{EXTERNAL_ID}',\n value: ({ externalId }: ReplaceInjectablesOptions) => externalId,\n },\n {\n string: '{PREVIOUS_APPROVAL_ID}',\n value: ({ previousApprovalId }: ReplaceInjectablesOptions) =>\n previousApprovalId,\n },\n]\n\nconst ELEMENT_VALUES: Array<{\n string: string\n value: (options: ReplaceInjectablesBaseOptions) => string | undefined\n}> = [\n {\n string: '{USER:email}',\n value: ({ userProfile }) => userProfile?.email,\n },\n {\n string: '{USER:username}',\n value: ({ userProfile }) => userProfile?.username,\n },\n {\n string: '{TASK_NAME}',\n value: ({ task }) => task?.name,\n },\n {\n string: '{TASK_GROUP_NAME}',\n value: ({ taskGroup }) => taskGroup?.name,\n },\n {\n string: '{TASK_GROUP_INSTANCE_LABEL}',\n value: ({ taskGroupInstance }) => taskGroupInstance?.label,\n },\n]\n\n/**\n * Function to get the display value of a property in submission, if elementId\n * is provided propertyName will be ignored\n *\n * #### Example\n *\n * ```typescript\n * const result = submissionService.getElementSubmissionValue({\n * propertyName: 'search',\n * submission: {\n * search: 'Entered By User',\n * },\n * formElements: [\n * {\n * id: 'd4135b47-9004-4d75-aeb3-d2f6232da111',\n * name: 'search',\n * type: 'text',\n * label: 'Search',\n * readOnly: false,\n * required: false,\n * conditionallyShow: false,\n * requiresAllConditionallyShowPredicates: false,\n * isElementLookup: false,\n * isDataLookup: false,\n * },\n * ],\n * formatDate: (value) => new Date(value).toDateString(),\n * formatDateTime: (value) => new Date(value).toString(),\n * formatTime: (value) => new Date(value).toTimeString(),\n * formatNumber: (value) => Number(value).toString(),\n * formatCurrency: (value) => Number(value).toFixed(2),\n * })\n * ```\n *\n * @param options\n * @returns\n */\nexport function getElementSubmissionValue({\n propertyName,\n submission,\n formElements,\n formatDate,\n formatDateTime,\n formatTime,\n formatNumber,\n formatCurrency,\n}: {\n propertyName: string\n formElements: FormTypes.FormElement[]\n submission: SubmissionTypes.S3SubmissionData['submission']\n} & ReplaceInjectablesFormatters):\n | {\n element: FormTypes.FormElement | undefined\n value: unknown\n }\n | undefined\nexport function getElementSubmissionValue({\n elementId,\n submission,\n formElements,\n formatDate,\n formatDateTime,\n formatTime,\n formatNumber,\n formatCurrency,\n}: {\n elementId: string\n formElements: FormTypes.FormElement[]\n submission: SubmissionTypes.S3SubmissionData['submission']\n} & ReplaceInjectablesFormatters):\n | {\n element: FormTypes.FormElement | undefined\n value: unknown\n }\n | undefined\nexport function getElementSubmissionValue({\n elementId,\n propertyName,\n submission,\n formElements,\n formatDate,\n formatDateTime,\n formatTime,\n formatNumber,\n formatCurrency,\n}: {\n elementId?: string\n propertyName?: string\n formElements: FormTypes.FormElement[]\n submission: SubmissionTypes.S3SubmissionData['submission']\n} & ReplaceInjectablesFormatters) {\n if (elementId === undefined && propertyName === undefined) {\n return undefined\n }\n\n let result: ReturnType<typeof getElementSubmissionValueByName> | undefined =\n undefined\n\n if (elementId) {\n result = getElementSubmissionValueById({\n elementId,\n formElements,\n submission,\n })\n } else if (propertyName) {\n result = getElementSubmissionValueByName({\n propertyName,\n formElements,\n submission,\n })\n }\n\n if (!result) {\n return\n }\n return formatValue({\n element: result.formElement,\n unknownValue: result.unknownValue,\n formatDate,\n formatTime,\n formatDateTime,\n formatCurrency,\n formatNumber,\n })\n}\n\nfunction getElementSubmissionValueByName({\n propertyName,\n formElements,\n submission,\n}: {\n propertyName: string\n formElements: FormTypes.FormElement[]\n submission: SubmissionTypes.S3SubmissionData['submission']\n}):\n | { formElement: FormTypes.FormElement | undefined; unknownValue: unknown }\n | undefined {\n const unknownValue = submission[propertyName]\n if (unknownValue === undefined || unknownValue === null) {\n return undefined\n }\n\n const formElement = findFormElement(\n formElements,\n (element) =>\n element.type !== 'page' &&\n element.type !== 'section' &&\n element.name === propertyName,\n )\n\n return { formElement, unknownValue }\n}\n\nfunction getElementSubmissionValueById({\n elementId,\n formElements,\n submission,\n}: {\n elementId: string\n formElements: FormTypes.FormElement[]\n submission: SubmissionTypes.S3SubmissionData['submission']\n}): { formElement: FormTypes.FormElement; unknownValue: unknown } | undefined {\n // make sure submission is an object\n if (Object(submission) !== submission) {\n return undefined\n }\n\n const flattenedElements = flattenFormElements(formElements)\n\n let unknown: unknown = undefined\n let formElement: FormTypes.FormElement | undefined = undefined\n\n for (const element of flattenedElements) {\n if (elementId === element.id) {\n if ('name' in element) {\n unknown = submission[element.name]\n formElement = element\n }\n break\n }\n if (element.type === 'form') {\n const newSubmissionData = submission[element.name]\n\n const result = getElementSubmissionValueById({\n elementId,\n formElements: element.elements ?? [],\n submission:\n newSubmissionData as SubmissionTypes.S3SubmissionData['submission'],\n })\n if (result) {\n unknown = result.unknownValue\n formElement = result.formElement\n break\n }\n }\n }\n\n if (unknown === undefined || unknown === null || formElement === undefined) {\n return undefined\n }\n\n return { unknownValue: unknown, formElement }\n}\n\nfunction formatValue({\n element,\n unknownValue,\n formatDate,\n formatTime,\n formatDateTime,\n formatCurrency,\n formatNumber,\n}: {\n element: FormTypes.FormElement | undefined\n unknownValue: unknown\n} & ReplaceInjectablesFormatters) {\n switch (element?.type) {\n case 'datetime': {\n const value = unknownValue as string\n return { element: element, value: formatDateTime(value) }\n }\n case 'date': {\n const value = unknownValue as string\n return { element: element, value: formatDate(value) }\n }\n case 'time': {\n const value = unknownValue as string\n return { element: element, value: formatTime(value) }\n }\n case 'radio':\n case 'autocomplete': {\n const value = unknownValue as string\n const option = element.options?.find((opt) => opt.value === value)\n return { element: element, value: option?.label || value }\n }\n\n case 'checkboxes': {\n const value = unknownValue as string[]\n const options = element.options\n const selectedOptionLabels: string[] = value.reduce(\n (labels: string[], selectedOption: string) => {\n const foundOption = options?.find((o) => o.value === selectedOption)\n if (foundOption) labels.push(foundOption.label)\n return labels\n },\n [],\n )\n return {\n element: element,\n value: selectedOptionLabels.length ? selectedOptionLabels : undefined,\n }\n }\n case 'compliance': {\n const value = unknownValue as {\n value?: string\n }\n const option = (element.options || []).find(\n (option: FormTypes.ChoiceElementOption) => option.value === value.value,\n )\n return {\n element: element,\n value: {\n ...value,\n value: option?.label || value.value,\n },\n }\n }\n case 'select': {\n if (element.multi) {\n const value = unknownValue as string[]\n const options = element.options\n const selectedOptionLabels: string[] = value.reduce(\n (labels: string[], selectedOption: string) => {\n const foundOption = options?.find((o) => o.value === selectedOption)\n if (foundOption) labels.push(foundOption.label)\n return labels\n },\n [],\n )\n return {\n element: element,\n value: selectedOptionLabels.length ? selectedOptionLabels : undefined,\n }\n } else {\n const value = unknownValue as string\n const option = element.options?.find((opt) => opt.value === value)\n return { element: element, value: option?.label }\n }\n }\n case 'boolean': {\n const value = unknownValue as boolean\n return { element: element, value: value ? 'Yes' : 'No' }\n }\n case 'calculation': {\n const value = unknownValue as number\n if (!Number.isNaN(value) && Number.isFinite(value)) {\n let text\n if (element.displayAsCurrency) {\n text = formatCurrency(value)\n } else {\n text = formatNumber(value)\n }\n return { element: element, value: text }\n }\n return undefined\n }\n case 'pointAddress':\n case 'geoscapeAddress': {\n const value = unknownValue as\n | PointTypes.PointAddress\n | GeoscapeTypes.GeoscapeAddress\n return {\n element: element,\n value: value?.addressDetails?.formattedAddress || value?.addressId,\n }\n }\n case 'googleAddress': {\n const value = unknownValue as GoogleTypes.GoogleMapsAddress\n return {\n element: element,\n value: value.formatted_address || value.place_id,\n }\n }\n case 'civicaStreetName': {\n const value = unknownValue as CivicaTypes.CivicaStreetName\n return { element: element, value: value?.formattedStreet }\n }\n case 'civicaNameRecord': {\n const value = unknownValue as CivicaTypes.CivicaNameRecord\n return {\n element: element,\n value:\n [value?.title, value?.givenName1, value?.familyName]\n .filter((t) => t)\n .join(' ') || value?.emailAddress,\n }\n }\n case 'abn': {\n const value = unknownValue as MiscTypes.ABNRecord\n return {\n element: element,\n value: value ? getABNNumberFromABNRecord(value) : undefined,\n }\n }\n case 'apiNSWLiquorLicence': {\n const value = unknownValue as APINSWTypes.LiquorLicenceDetails | undefined\n return {\n element: element,\n value:\n `${value?.licenceDetail?.licenceNumber} | ${value?.licenceDetail?.licenceName}`.trim(),\n }\n }\n default: {\n return { element: element, value: unknownValue }\n }\n }\n}\n\n/**\n * Replace the `{ELEMENT:<elementName>}` values in text while a form is being\n * filled out. The replacements are suppose to be user friendly and for display\n * purposes, e.g. dates should be displayed in the user's desired format and\n * timezone.\n *\n * #### Example\n *\n * ```js\n * const { text: url } =\n * submissionService.replaceInjectablesWithElementValues(\n * 'https://example.com/path?search{ELEMENT:search}',\n * {\n * formatDate: (value) => new Date(value).toDateString(),\n * formatDateTime: (value) => new Date(value).toString(),\n * formatTime: (value) => new Date(value).toTimeString(),\n * formatNumber: (value) => Number(value).toString(),\n * formatCurrency: (value) => Number(value).toFixed(2),\n * submission: {\n * search: 'Entered By User',\n * },\n * elements: [\n * {\n * id: 'd4135b47-9004-4d75-aeb3-d2f6232da111',\n * name: 'search',\n * type: 'text',\n * label: 'Search',\n * readOnly: false,\n * required: false,\n * conditionallyShow: false,\n * requiresAllConditionallyShowPredicates: false,\n * isElementLookup: false,\n * isDataLookup: false,\n * },\n * ],\n * userProfile: {\n * userId: 'abc123',\n * username: 'john-user',\n * email: 'john.user@domain.com',\n * },\n * },\n * )\n * ```\n *\n * @param text\n * @param options\n * @returns\n */\nexport function replaceInjectablesWithElementValues(\n text: string,\n options: {\n formElements: FormTypes.FormElement[]\n } & ReplaceInjectablesBaseOptions,\n): {\n /**\n * The text passed in with all injectables replaced with their corresponding\n * values or empty strings.\n */\n text: string\n /**\n * Determine if replaceable strings could all be replaced. The `text` property\n * returned will replace all injectable with empty strings if the value cannot\n * be found. Use this boolean to either use the replaced string with missing\n * data or implement some other logic to handle missing injectables.\n */\n hadAllInjectablesReplaced: boolean\n} {\n let hadAllInjectablesReplaced = true\n text = ELEMENT_VALUES.reduce((newString, customValue) => {\n if (!newString.includes(customValue.string)) {\n return newString\n }\n const replacedValue = customValue.value(options)\n if (!replacedValue) {\n hadAllInjectablesReplaced = false\n }\n return newString.replaceAll(customValue.string, replacedValue || '')\n }, text)\n\n const matchesElement = text.match(\n options.excludeNestedElements ? RootElementRegex : NestedElementRegex,\n )\n if (matchesElement) {\n matchElementsTagRegex(\n {\n text,\n excludeNestedElements: !!options.excludeNestedElements,\n },\n ({ elementName, elementMatch }) => {\n const result = getElementSubmissionValue({\n propertyName: elementName,\n ...options,\n })\n\n const hasNoValue = result === undefined || result.value === undefined\n if (hasNoValue) {\n hadAllInjectablesReplaced = false\n }\n\n text = text.replace(\n elementMatch,\n hasNoValue ? '' : (result.value as string),\n )\n },\n )\n }\n\n return {\n text,\n hadAllInjectablesReplaced,\n }\n}\n\n/**\n * Replace the `{ELEMENT:<elementName>}` values in text after a successful form\n * submission as well as other replaceable parameters e.g. `submissionId`. The\n * replacements are suppose to be user friendly and for display purposes, e.g.\n * dates should be displayed in the user's desired format and timezone.\n *\n * #### Example\n *\n * ```js\n * const result = submissionService.replaceInjectablesWithSubmissionValues(\n * 'https://example.com/path?submissionId={SUBMISSION_ID}&externalId={EXTERNAL_ID}&search{ELEMENT:search}',\n * {\n * submissionId: 'abc-123',\n * submissionTimestamp: '2021-07-02T02:19:13.670Z',\n * formatDate: (value) => new Date(value).toDateString(),\n * formatDateTime: (value) => new Date(value).toString(),\n * formatTime: (value) => new Date(value).toTimeString(),\n * submission: {\n * search: 'Entered By User',\n * },\n * form: {\n * id: 1,\n * name: 'Form',\n * organisationId: '',\n * formsAppEnvironmentId: 1,\n * formsAppIds: [],\n * isAuthenticated: false,\n * isMultiPage: false,\n * isInfoPage: false,\n * postSubmissionAction: 'FORMS_LIBRARY',\n * cancelAction: 'FORMS_LIBRARY',\n * submissionEvents: [],\n * tags: [],\n * elements: [\n * {\n * id: 'd4135b47-9004-4d75-aeb3-d2f6232da111',\n * name: 'search',\n * type: 'text',\n * label: 'Search',\n * readOnly: false,\n * required: false,\n * conditionallyShow: false,\n * requiresAllConditionallyShowPredicates: false,\n * isElementLookup: false,\n * isDataLookup: false,\n * },\n * ],\n * },\n * },\n * )\n * ```\n *\n * @param text\n * @param options\n * @returns\n */\nexport function replaceInjectablesWithSubmissionValues(\n text: string,\n options: ReplaceInjectablesOptions,\n): {\n /**\n * The text passed in with all injectables replaced with their corresponding\n * values or empty strings.\n */\n text: string\n /**\n * Determine if replaceable strings could all be replaced. The `text` property\n * returned will replace all injectable with empty strings if the value cannot\n * be found. Use this boolean to either use the replaced string with missing\n * data or implement some other logic to handle missing injectables.\n */\n hadAllInjectablesReplaced: boolean\n} {\n const result = replaceInjectablesWithElementValues(text, {\n formElements: options.form.elements,\n ...options,\n })\n let hadAllInjectablesReplaced = result.hadAllInjectablesReplaced\n const replacedText = SUBMISSION_VALUES.reduce((newString, customValue) => {\n if (!newString.includes(customValue.string)) {\n return newString\n }\n const replacedValue = customValue.value(options)\n if (!replacedValue) {\n hadAllInjectablesReplaced = false\n }\n return newString.replaceAll(customValue.string, replacedValue || '')\n }, result.text)\n\n return {\n text: replacedText,\n hadAllInjectablesReplaced,\n }\n}\n\n/**\n * Process a resource with injectable element values to turn a single resource\n * (could be a single) into multiple resources. e.g.\n * `\"{ELEMENT:Children|Child_Name} {ELEMENT:Family_Name}\"` with the following\n * submission data:\n *\n * ```json\n * {\n * \"Family_Name\": \"Smith\",\n * \"Children\": [\n * {\n * \"Child_Name\": \"John\"\n * },\n * {\n * \"Child_Name\": \"Jane\"\n * }\n * ]\n * }\n * ```\n *\n * Would result in the following resources:\n *\n * - `\"John Smith\"`\n * - `\"Jane Smith\"`\n *\n * #### Example\n *\n * ```js\n * const emailAddresses = processInjectablesInCustomResource({\n * resource: '{ELEMENT:People|Email_Address}',\n * submission: {\n * People: [\n * {\n * Email_Address: 'user@oneblink.io',\n * },\n * {\n * Email_Address: 'admin@oneblink.io',\n * },\n * ],\n * },\n * formElements: [\n * {\n * id: '18dcd3e0-6e2f-462e-803b-e24562d9fa6d',\n * type: 'repeatableSet',\n * name: 'People',\n * label: 'People',\n * elements: [\n * {\n * id: 'd0902113-3f77-4070-adbd-ca3ae95ce091',\n * type: 'email',\n * name: 'Email_Address',\n * label: 'Email_Address',\n * },\n * ],\n * },\n * ],\n * replaceRootInjectables: (resource, submission, formElements) => {\n * const { text } = replaceInjectablesWithElementValues(resource, {\n * submission,\n * formElements,\n * excludeNestedElements: true,\n * // other options\n * })\n * return text\n * },\n * })\n * // emailAddresses === [\"user@oneblink.io\", \"admin@oneblink.io\"]\n * ```\n *\n * @param options\n * @returns\n */\nexport function processInjectablesInCustomResource<T>({\n resource,\n submission,\n formElements,\n replaceRootInjectables,\n prepareNestedInjectables = (resource, prepare) =>\n prepare(String(resource)) as T,\n}: {\n /** The resource that contains properties that support injection or a string */\n resource: T\n /** The form submission data to process */\n submission: SubmissionTypes.S3SubmissionData['submission']\n /** The form elements to process */\n formElements: FormTypes.FormElement[]\n /**\n * A function to inject values, this allows custom formatters to be used.\n * Return `undefined` to prevent the injection from recursively continuing.\n *\n * @param resource The current resource that contains properties that support\n * injection or a string\n * @param submission The current form submission data to process (may be an\n * entry in a repeatable set)\n * @param formElements The current form elements to process (may be the\n * elements from a repeatable set)\n * @returns\n */\n replaceRootInjectables: (\n resource: T,\n submission: SubmissionTypes.S3SubmissionData['submission'],\n formElements: FormTypes.FormElement[],\n ) =>\n | [injectedText: string, resourceKey: string, newResource: T]\n | string\n | undefined\n /**\n * An optional function to replace nested injectables when creating multiple\n * resources from repeatable sets. Only required if the `resource` param is\n * not a `string`.\n *\n * @param resource The current resource that contains properties that support\n * injection or a string\n * @param prepare A function to prepare the resource string(s) for another\n * iteration\n * @returns\n */\n prepareNestedInjectables?: (\n resource: T,\n preparer: (resourceText: string) => string,\n ) => T\n}): Map<string, T> {\n const newResources: Map<string, T> = new Map<string, T>()\n\n const injectorResult = replaceRootInjectables(\n resource,\n submission,\n formElements,\n )\n if (!injectorResult) {\n return newResources\n }\n\n const [text, resourceKey, newResource] =\n typeof injectorResult === 'string'\n ? [injectorResult, injectorResult, injectorResult as T]\n : injectorResult\n\n // Find nested form elements\n const matches: Map<string, boolean> = new Map()\n matchElementsTagRegex(\n {\n text,\n excludeNestedElements: false,\n },\n ({ elementName }) => {\n const [repeatableSetElementName, ...elementNames] = elementName.split('|')\n matches.set(repeatableSetElementName, !!elementNames.length)\n },\n )\n\n if (matches.size) {\n matches.forEach((hasNestedFormElements, repeatableSetElementName) => {\n if (hasNestedFormElements) {\n // Attempt to create a new resource for each entry in the repeatable set.\n const entries = submission?.[repeatableSetElementName]\n if (Array.isArray(entries)) {\n const repeatableSetElement = findFormElement(\n formElements,\n (formElement) => {\n return (\n 'name' in formElement &&\n formElement.name === repeatableSetElementName\n )\n },\n )\n if (\n repeatableSetElement &&\n 'elements' in repeatableSetElement &&\n Array.isArray(repeatableSetElement.elements)\n ) {\n for (const entry of entries) {\n const replacedResource = prepareNestedInjectables(\n newResource,\n (resourceText) => {\n return resourceText.replaceAll(\n `{ELEMENT:${repeatableSetElementName}|`,\n '{ELEMENT:',\n )\n },\n )\n const nestedResources = processInjectablesInCustomResource<T>({\n resource: replacedResource,\n submission: entry,\n formElements: repeatableSetElement.elements,\n replaceRootInjectables,\n prepareNestedInjectables,\n })\n if (nestedResources.size) {\n nestedResources.forEach((nestedResource, nestedResourceKey) => {\n if (!newResources.has(nestedResourceKey)) {\n newResources.set(nestedResourceKey, nestedResource)\n }\n })\n }\n }\n }\n }\n }\n })\n } else {\n newResources.set(resourceKey, newResource)\n }\n\n return newResources\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oneblink/sdk-core",
|
|
3
3
|
"description": "OneBlink SDK for JavaScript (works in Browsers and NodeJS)",
|
|
4
|
-
"version": "7.
|
|
4
|
+
"version": "7.3.0-beta.2",
|
|
5
5
|
"author": "OneBlink <developers@oneblink.io> (https://oneblink.io)",
|
|
6
6
|
"bugs": {
|
|
7
7
|
"url": "https://github.com/oneblink/sdk-core-js/issues"
|