@oneblink/sdk-core
Advanced tools
Comparing version 6.0.0-beta.2 to 6.0.0-beta.3
@@ -34,3 +34,4 @@ import { FormTypes, MiscTypes, ScheduledTasksTypes, SubmissionTypes } from '@oneblink/types'; | ||
/** | ||
* Function to get the display value of a property in submission | ||
* Function to get the display value of a property in submission, if elementId | ||
* is provided propertyName will be ignored | ||
* | ||
@@ -70,7 +71,11 @@ * #### Example | ||
*/ | ||
export declare function getElementSubmissionValue({ propertyName, submission, formElements, formatDate, formatDateTime, formatTime, formatNumber, formatCurrency, }: { | ||
propertyName: string; | ||
export declare function getElementSubmissionValue({ elementId, propertyName, submission, formElements, formatDate, formatDateTime, formatTime, formatNumber, formatCurrency, }: { | ||
elementId?: string; | ||
propertyName?: string; | ||
formElements: FormTypes.FormElement[]; | ||
submission: SubmissionTypes.S3SubmissionData['submission']; | ||
} & ReplaceInjectablesFormatters): unknown; | ||
} & ReplaceInjectablesFormatters): { | ||
element: FormTypes.FormElement | undefined; | ||
value: unknown; | ||
} | undefined; | ||
/** | ||
@@ -77,0 +82,0 @@ * Replace the `{ELEMENT:<elementName>}` values in text while a form is being |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.replaceInjectablesWithSubmissionValues = exports.replaceInjectablesWithElementValues = exports.getElementSubmissionValue = void 0; | ||
const formElementsService_1 = require("./formElementsService"); | ||
const abnService_1 = require("./abnService"); | ||
@@ -73,3 +72,4 @@ const form_elements_regex_1 = require("./form-elements-regex"); | ||
/** | ||
* Function to get the display value of a property in submission | ||
* Function to get the display value of a property in submission, if elementId | ||
* is provided propertyName will be ignored | ||
* | ||
@@ -109,8 +109,49 @@ * #### Example | ||
*/ | ||
function getElementSubmissionValue({ propertyName, submission, formElements, formatDate, formatDateTime, formatTime, formatNumber, formatCurrency, }) { | ||
function getElementSubmissionValue({ elementId, propertyName, submission, formElements, formatDate, formatDateTime, formatTime, formatNumber, formatCurrency, }) { | ||
var _a, _b, _c, _d, _e; | ||
const formElement = (0, formElementsService_1.findFormElement)(formElements, (element) => element.type !== 'page' && | ||
element.type !== 'section' && | ||
element.name === propertyName); | ||
const unknown = submission[propertyName]; | ||
if (elementId === undefined && propertyName === undefined) { | ||
return undefined; | ||
} | ||
// make sure submission is an object | ||
if (Object(submission) !== submission) { | ||
return undefined; | ||
} | ||
// initialise if propertyName provided and not elementId | ||
let unknown = elementId ? undefined : submission[propertyName !== null && propertyName !== void 0 ? propertyName : '']; | ||
let formElement = undefined; | ||
const matchFn = (element) => { | ||
if (elementId) { | ||
return elementId === element.id; | ||
} | ||
return 'name' in element && element.name === propertyName; | ||
}; | ||
for (const element of formElements) { | ||
if (matchFn(element)) { | ||
if ('name' in element) { | ||
unknown = submission[element.name]; | ||
formElement = element; | ||
break; | ||
} | ||
} | ||
if ('elements' in element) { | ||
const newSubmissionData = 'name' in element ? submission[element.name] : submission; | ||
if (Array.isArray(newSubmissionData) || !element.elements) { | ||
continue; | ||
} | ||
const result = getElementSubmissionValue({ | ||
elementId, | ||
formElements: element.elements, | ||
submission: newSubmissionData, | ||
formatDate, | ||
formatDateTime, | ||
formatTime, | ||
formatNumber, | ||
formatCurrency, | ||
}); | ||
if (result) { | ||
unknown = result.value; | ||
formElement = result.element; | ||
} | ||
} | ||
} | ||
if (unknown === undefined || unknown === null) { | ||
@@ -122,11 +163,11 @@ return undefined; | ||
const value = unknown; | ||
return formatDateTime(value); | ||
return { element: formElement, value: formatDateTime(value) }; | ||
} | ||
case 'date': { | ||
const value = unknown; | ||
return formatDate(value); | ||
return { element: formElement, value: formatDate(value) }; | ||
} | ||
case 'time': { | ||
const value = unknown; | ||
return formatTime(value); | ||
return { element: formElement, value: formatTime(value) }; | ||
} | ||
@@ -137,9 +178,9 @@ case 'radio': | ||
const option = (_a = formElement.options) === null || _a === void 0 ? void 0 : _a.find((opt) => opt.value === value); | ||
return (option === null || option === void 0 ? void 0 : option.label) || value; | ||
return { element: formElement, value: (option === null || option === void 0 ? void 0 : option.label) || value }; | ||
} | ||
case 'checkboxes': { | ||
const value = unknown; | ||
const options = formElement.options; | ||
const selectedOptionLabels = value.reduce((labels, selectedOption) => { | ||
var _a; | ||
const foundOption = (_a = formElement.options) === null || _a === void 0 ? void 0 : _a.find((o) => o.value === selectedOption); | ||
const foundOption = options === null || options === void 0 ? void 0 : options.find((o) => o.value === selectedOption); | ||
if (foundOption) | ||
@@ -149,3 +190,6 @@ labels.push(foundOption.label); | ||
}, []); | ||
return selectedOptionLabels.length ? selectedOptionLabels : undefined; | ||
return { | ||
element: formElement, | ||
value: selectedOptionLabels.length ? selectedOptionLabels : undefined, | ||
}; | ||
} | ||
@@ -156,4 +200,7 @@ case 'compliance': { | ||
return { | ||
...value, | ||
value: (option === null || option === void 0 ? void 0 : option.label) || value.value, | ||
element: formElement, | ||
value: { | ||
...value, | ||
value: (option === null || option === void 0 ? void 0 : option.label) || value.value, | ||
}, | ||
}; | ||
@@ -164,5 +211,5 @@ } | ||
const value = unknown; | ||
const options = formElement.options; | ||
const selectedOptionLabels = value.reduce((labels, selectedOption) => { | ||
var _a; | ||
const foundOption = (_a = formElement.options) === null || _a === void 0 ? void 0 : _a.find((o) => o.value === selectedOption); | ||
const foundOption = options === null || options === void 0 ? void 0 : options.find((o) => o.value === selectedOption); | ||
if (foundOption) | ||
@@ -172,3 +219,6 @@ labels.push(foundOption.label); | ||
}, []); | ||
return selectedOptionLabels.length ? selectedOptionLabels : undefined; | ||
return { | ||
element: formElement, | ||
value: selectedOptionLabels.length ? selectedOptionLabels : undefined, | ||
}; | ||
} | ||
@@ -178,3 +228,3 @@ else { | ||
const option = (_b = formElement.options) === null || _b === void 0 ? void 0 : _b.find((opt) => opt.value === value); | ||
return option === null || option === void 0 ? void 0 : option.label; | ||
return { element: formElement, value: option === null || option === void 0 ? void 0 : option.label }; | ||
} | ||
@@ -184,3 +234,3 @@ } | ||
const value = unknown; | ||
return value ? 'Yes' : 'No'; | ||
return { element: formElement, value: value ? 'Yes' : 'No' }; | ||
} | ||
@@ -197,3 +247,3 @@ case 'calculation': { | ||
} | ||
return text; | ||
return { element: formElement, value: text }; | ||
} | ||
@@ -205,24 +255,36 @@ return undefined; | ||
const value = unknown; | ||
return ((_c = value === null || value === void 0 ? void 0 : value.addressDetails) === null || _c === void 0 ? void 0 : _c.formattedAddress) || (value === null || value === void 0 ? void 0 : value.addressId); | ||
return { | ||
element: formElement, | ||
value: ((_c = value === null || value === void 0 ? void 0 : value.addressDetails) === null || _c === void 0 ? void 0 : _c.formattedAddress) || (value === null || value === void 0 ? void 0 : value.addressId), | ||
}; | ||
} | ||
case 'civicaStreetName': { | ||
const value = unknown; | ||
return value === null || value === void 0 ? void 0 : value.formattedStreet; | ||
return { element: formElement, value: value === null || value === void 0 ? void 0 : value.formattedStreet }; | ||
} | ||
case 'civicaNameRecord': { | ||
const value = unknown; | ||
return ([value === null || value === void 0 ? void 0 : value.title, value === null || value === void 0 ? void 0 : value.givenName1, value === null || value === void 0 ? void 0 : value.familyName] | ||
.filter((t) => t) | ||
.join(' ') || (value === null || value === void 0 ? void 0 : value.emailAddress)); | ||
return { | ||
element: formElement, | ||
value: [value === null || value === void 0 ? void 0 : value.title, value === null || value === void 0 ? void 0 : value.givenName1, value === null || value === void 0 ? void 0 : value.familyName] | ||
.filter((t) => t) | ||
.join(' ') || (value === null || value === void 0 ? void 0 : value.emailAddress), | ||
}; | ||
} | ||
case 'abn': { | ||
const value = unknown; | ||
return value ? (0, abnService_1.getABNNumberFromABNRecord)(value) : undefined; | ||
return { | ||
element: formElement, | ||
value: value ? (0, abnService_1.getABNNumberFromABNRecord)(value) : undefined, | ||
}; | ||
} | ||
case 'apiNSWLiquorLicence': { | ||
const value = unknown; | ||
return `${(_d = value === null || value === void 0 ? void 0 : value.licenceDetail) === null || _d === void 0 ? void 0 : _d.licenceNumber} | ${(_e = value === null || value === void 0 ? void 0 : value.licenceDetail) === null || _e === void 0 ? void 0 : _e.licenceName}`.trim(); | ||
return { | ||
element: formElement, | ||
value: `${(_d = value === null || value === void 0 ? void 0 : value.licenceDetail) === null || _d === void 0 ? void 0 : _d.licenceNumber} | ${(_e = value === null || value === void 0 ? void 0 : value.licenceDetail) === null || _e === void 0 ? void 0 : _e.licenceName}`.trim(), | ||
}; | ||
} | ||
default: { | ||
return unknown; | ||
return { element: formElement, value: unknown }; | ||
} | ||
@@ -298,11 +360,11 @@ } | ||
}, ({ elementName, elementMatch }) => { | ||
const value = getElementSubmissionValue({ | ||
const result = getElementSubmissionValue({ | ||
propertyName: elementName, | ||
...options, | ||
}); | ||
const hasNoValue = value === undefined; | ||
const hasNoValue = result === undefined || result.value === undefined; | ||
if (hasNoValue) { | ||
hadAllInjectablesReplaced = false; | ||
} | ||
text = text.replace(elementMatch, hasNoValue ? '' : value); | ||
text = text.replace(elementMatch, hasNoValue ? '' : result.value); | ||
}); | ||
@@ -309,0 +371,0 @@ } |
{ | ||
"name": "@oneblink/sdk-core", | ||
"description": "OneBlink SDK for JavaScript (works in Browsers and NodeJS)", | ||
"version": "6.0.0-beta.2", | ||
"version": "6.0.0-beta.3", | ||
"author": "OneBlink <developers@oneblink.io> (https://oneblink.io)", | ||
@@ -6,0 +6,0 @@ "bugs": { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
279862
3194