@oneblink/sdk-core 2.0.1-beta.2 → 3.0.0-beta.1

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.
@@ -28,16 +28,15 @@ const createLegalName = ({ familyName, givenName, otherGivenName, }) => {
28
28
  * @returns
29
29
  */
30
30
  const getBusinessNameFromABNRecord = (abnRecord) => {
31
- var _a, _b;
32
31
  if (abnRecord.entityType.entityTypeCode === 'IND' && abnRecord.legalName) {
33
32
  const legalName = createLegalName(abnRecord.legalName);
34
33
  if (legalName)
35
34
  return legalName.trim();
36
35
  }
37
- if ((_a = abnRecord.mainName) === null || _a === void 0 ? void 0 : _a.organisationName) {
36
+ if (abnRecord.mainName?.organisationName) {
38
37
  return abnRecord.mainName.organisationName;
39
38
  }
40
- if ((_b = abnRecord.mainTradingName) === null || _b === void 0 ? void 0 : _b.organisationName) {
39
+ if (abnRecord.mainTradingName?.organisationName) {
41
40
  return abnRecord.mainTradingName.organisationName;
42
41
  }
43
42
  if (abnRecord.legalName) {
@@ -84,7 +83,7 @@ const getABNNumberFromABNRecord = (abnRecord) => {
84
83
  return;
85
84
  if (Array.isArray(abnRecord.ABN)) {
86
85
  const relevantABN = abnRecord.ABN.find((abn) => abn.isCurrentIndicator === 'Y');
87
- return relevantABN === null || relevantABN === void 0 ? void 0 : relevantABN.identifierValue;
86
+ return relevantABN?.identifierValue;
88
87
  }
89
88
  return abnRecord.ABN.identifierValue;
90
89
  };
@@ -1 +1 @@
1
- {"version":3,"file":"abnService.js","sourceRoot":"","sources":["../src/abnService.ts"],"names":[],"mappings":";;;AAEA,MAAM,eAAe,GAAG,CAAC,EACvB,UAAU,EACV,SAAS,EACT,cAAc,GACsB,EAAE,EAAE;IACxC,IAAI,SAAS,GAAG,EAAE,CAAA;IAClB,mBAAmB;IACnB,IAAI,UAAU;QAAE,SAAS,IAAI,GAAG,UAAU,IAAI,CAAA;IAC9C,oBAAoB;IACpB,IAAI,SAAS;QAAE,SAA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AM;IAC1B,IAAI,KAAK,CAAC,OAAO,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE;QAChC,MAAM,WAAW,GAAG,SAAS,CAAC,GAAG,CAAC,IAAI,CACpC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,kBAAkB,KAAK,GAAG,CACxC,CAAA;QACD,OAAO,WAAW,aAAX,WAAW,uBAAX,WAAW,CAAE,eAAe,CAAA;KACpC;IACD,OAAO,SAAS,CAAC,GAAG,CAAC,eAAe,CAAA;AACtC,CAAC,CAAA;AAXY,QAAA,yBAAyB,6BAWrC;AAED;;;;;;;;;;;;GAYG;AACI,MAAM,6BAA6B,GAAG,CAAC,SAAoB,EAAU,EAAE;IAC5E,MAAM,SAAS,GAAG,IAAA,iCAAyB,EAAC,SAAS,CAAC,CAAA;IACtD,OAAO,SAAS,IAAI,oBAAoB,CAAA;AAC1C,CAAC,CAAA;AAHY,QAAA,6BAA6B,iCAGzC","sourcesContent":["import { ABNRecord } from '@oneblink/types/typescript/misc'\n\nconst createLegalName = ({\n familyName,\n givenName,\n otherGivenName,\n}: NonNullable<ABNRecord['legalName']>) => {\n let legalName = ''\n // Concat last name\n if (familyName) legalName += `${familyName}, `\n // Concat first name\n if (givenName) legalName += `${givenName} `\n // Concat middle name\n if (otherGivenName) legalName += `${otherGivenName}`\n\n return legalName\n}\n\n/**\n * Attempts to get the most relevant business name from the data returned in an\n * `ABNRecord`. Will return `undefined` if a suitable name is not found.\n *\n * #### Example\n *\n * ```js\n * const businessName = abnService.getBusinessNameFromABNRecord(abnRecord)\n * ```\n *\n * @param abnRecord\n * @returns\n */\nexport const getBusinessNameFromABNRecord = (\n abnRecord: ABNRecord,\n): string | undefined => {\n if (abnRecord.entityType.entityTypeCode === 'IND' && abnRecord.legalName) {\n const legalName = createLegalName(abnRecord.legalName)\n if (legalName) return legalName.trim()\n }\n\n if (abnRecord.mainName?.organisationName) {\n return abnRecord.mainName.organisationName\n }\n if (abnRecord.mainTradingName?.organisationName) {\n return abnRecord.mainTradingName.organisationName\n }\n if (abnRecord.legalName) {\n const legalName = createLegalName(abnRecord.legalName)\n if (legalName) return legalName.trim()\n }\n}\n\n/**\n * Get the most relevant business name from the data returned in an `ABNRecord`.\n * Will return \"Unknown Business Name\" if a suitable name is not found.\n *\n * #### Example\n *\n * ```js\n * const businessName =\n * abnService.displayBusinessNameFromABNRecord(abnRecord)\n * ```\n *\n * @param abnRecord\n * @returns\n */\nexport const displayBusinessNameFromABNRecord = (\n abnRecord: ABNRecord,\n): string => {\n const businessName = getBusinessNameFromABNRecord(abnRecord)\n return businessName || 'ABN Details Suppressed'\n}\n\n/**\n * Attempts to get the most recent ABN number from the data returned in an\n * `ABNRecord`. Will return `undefined` if an ABN is not found.\n *\n * #### Example\n *\n * ```js\n * const ABN = abnService.getABNNumberFromABNRecord(abnRecord)\n * ```\n *\n * @param abnRecord\n * @returns\n */\nexport const getABNNumberFromABNRecord = (\n abnRecord: ABNRecord,\n): string | undefined => {\n if (!abnRecord.ABN) return\n if (Array.isArray(abnRecord.ABN)) {\n const relevantABN = abnRecord.ABN.find(\n (abn) => abn.isCurrentIndicator === 'Y',\n )\n return relevantABN?.identifierValue\n }\n return abnRecord.ABN.identifierValue\n}\n\n/**\n * Attempts to get the most recent ABN number from the data returned in an\n * `ABNRecord`. Will return \"Unknown ABN Number\" if an ABN is not found.\n *\n * #### Example\n *\n * ```js\n * const ABN = abnService.displayABNNumberFromABNRecord(abnRecord)\n * ```\n *\n * @param abnRecord\n * @returns\n */\nexport const displayABNNumberFromABNRecord = (abnRecord: ABNRecord): string => {\n const abnNumber = getABNNumberFromABNRecord(abnRecord)\n return abnNumber || 'Unknown ABN Number'\n}\n"]}
1
+ {"version":3,"file":"abnService.js","sourceRoot":"","sources":["../src/abnService.ts"],"names":[],"mappings":";;;AAEA,MAAM,eAAe,GAAG,CAAC,EACvB,UAAU,EACV,SAAS,EACT,cAAc,GACsB,EAAE,EAAE;IACxC,IAAI,SAAS,GAAG,EAAE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;;;;;;;;GAYG;AACI,MAAM,yBAAyB,GAAG,CACvC,SAAoB,EACA,EAAE;IACtB,IAAI,CAAC,SAAS,CAAC,GAAG;QAAE,OAAM;IAC1B,IAAI,KAAK,CAAC,OAAO,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE;QAChC,MAAM,WAAW,GAAG,SAAS,CAAC,GAAG,CAAC,IAAI,CACpC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,kBAAkB,KAAK,GAAG,CACxC,CAAA;QACD,OAAO,WAAW,EAAE,eAAe,CAAA;KACpC;IACD,OAAO,SAAS,CAAC,GAAG,CAAC,eAAe,CAAA;AACtC,CAAC,CAAA;AAXY,QAAA,yBAAyB,6BAWrC;AAED;;;;;;;;;;;;GAYG;AACI,MAAM,6BAA6B,GAAG,CAAC,SAAoB,EAAU,EAAE;IAC5E,MAAM,SAAS,GAAG,IAAA,iCAAyB,EAAC,SAAS,CAAC,CAAA;IACtD,OAAO,SAAS,IAAI,oBAAoB,CAAA;AAC1C,CAAC,CAAA;AAHY,QAAA,6BAA6B,iCAGzC","sourcesContent":["import { ABNRecord } from '@oneblink/types/typescript/misc'\n\nconst createLegalName = ({\n familyName,\n givenName,\n otherGivenName,\n}: NonNullable<ABNRecord['legalName']>) => {\n let legalName = ''\n // Concat last name\n if (familyName) legalName += `${familyName}, `\n // Concat first name\n if (givenName) legalName += `${givenName} `\n // Concat middle name\n if (otherGivenName) legalName += `${otherGivenName}`\n\n return legalName\n}\n\n/**\n * Attempts to get the most relevant business name from the data returned in an\n * `ABNRecord`. Will return `undefined` if a suitable name is not found.\n *\n * #### Example\n *\n * ```js\n * const businessName = abnService.getBusinessNameFromABNRecord(abnRecord)\n * ```\n *\n * @param abnRecord\n * @returns\n */\nexport const getBusinessNameFromABNRecord = (\n abnRecord: ABNRecord,\n): string | undefined => {\n if (abnRecord.entityType.entityTypeCode === 'IND' && abnRecord.legalName) {\n const legalName = createLegalName(abnRecord.legalName)\n if (legalName) return legalName.trim()\n }\n\n if (abnRecord.mainName?.organisationName) {\n return abnRecord.mainName.organisationName\n }\n if (abnRecord.mainTradingName?.organisationName) {\n return abnRecord.mainTradingName.organisationName\n }\n if (abnRecord.legalName) {\n const legalName = createLegalName(abnRecord.legalName)\n if (legalName) return legalName.trim()\n }\n}\n\n/**\n * Get the most relevant business name from the data returned in an `ABNRecord`.\n * Will return \"Unknown Business Name\" if a suitable name is not found.\n *\n * #### Example\n *\n * ```js\n * const businessName =\n * abnService.displayBusinessNameFromABNRecord(abnRecord)\n * ```\n *\n * @param abnRecord\n * @returns\n */\nexport const displayBusinessNameFromABNRecord = (\n abnRecord: ABNRecord,\n): string => {\n const businessName = getBusinessNameFromABNRecord(abnRecord)\n return businessName || 'ABN Details Suppressed'\n}\n\n/**\n * Attempts to get the most recent ABN number from the data returned in an\n * `ABNRecord`. Will return `undefined` if an ABN is not found.\n *\n * #### Example\n *\n * ```js\n * const ABN = abnService.getABNNumberFromABNRecord(abnRecord)\n * ```\n *\n * @param abnRecord\n * @returns\n */\nexport const getABNNumberFromABNRecord = (\n abnRecord: ABNRecord,\n): string | undefined => {\n if (!abnRecord.ABN) return\n if (Array.isArray(abnRecord.ABN)) {\n const relevantABN = abnRecord.ABN.find(\n (abn) => abn.isCurrentIndicator === 'Y',\n )\n return relevantABN?.identifierValue\n }\n return abnRecord.ABN.identifierValue\n}\n\n/**\n * Attempts to get the most recent ABN number from the data returned in an\n * `ABNRecord`. Will return \"Unknown ABN Number\" if an ABN is not found.\n *\n * #### Example\n *\n * ```js\n * const ABN = abnService.displayABNNumberFromABNRecord(abnRecord)\n * ```\n *\n * @param abnRecord\n * @returns\n */\nexport const displayABNNumberFromABNRecord = (abnRecord: ABNRecord): string => {\n const abnNumber = getABNNumberFromABNRecord(abnRecord)\n return abnNumber || 'Unknown ABN Number'\n}\n"]}
@@ -67,8 +67,7 @@ const conditionallyShowOptionByPredicate = (formElementsCtrl, predicate, element
67
67
  }
68
68
  }
69
69
  const everyOptionIsShowing = predicate.optionIds.every((id) => {
70
- var _a;
71
- const predicateOption = (_a = optionsPredicateElement.options) === null || _a === void 0 ? void 0 : _a.find((o) => o.id === id);
70
+ const predicateOption = optionsPredicateElement.options?.find((o) => o.id === id);
72
71
  if (!predicateOption)
73
72
  return 'HIDE';
74
73
  return conditionallyShowOption({ model: formElementsCtrl.model, flattenedElements: [] }, optionsPredicateElement, predicateOption, elementsEvaluated);
@@ -1 +1 @@
1
- {"version":3,"file":"conditionallyShowOption.js","sourceRoot":"","sources":["../../src/conditionalLogicService/conditionallyShowOption.ts"],"names":[],"mappings":";;;;;AACA,gHAAuF;AAEvF,0BAAoC;AACpC,0FAAiE;AAQjE,MAAM,wBAAwB,GAAG,CAC/B,SAAiD,EACjD,KAAqD,EACrD,gBAAkD,EAClD,EAAE;IACF,MAAM,MAAM,GAAG,KAAK,CAAC,gBAAgB,CAAC,IAAI,CAAC,CAAA;IAC3C,IAAI,CAAC,MAAM;QAAE,OAAO,IAAI,CAAA;IAExB,IACE,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC;QACrB,CAAC,CAAC,MAAM,CAAC,MAAM;YACb,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,OAAO,KAAK,KAAK,WAAW,CAAC,CAAC,MAAM,CAAC,EACjE;QACA,OAAO,IAAI,CAAA;KACZ;IAED,OAAO,IAAA,6CAAmC,EAAC;QACzC,SAAS,EAAE;YACT,IAAI,EAAE,SAAS;YACf,SAAS,EAAE,SAAS,CAAC,SAAS;YAC9B,SAAS,EAAE,SAAS,CAAC,SAAS;SAC/B;QACD,cAAc,EAAE,MAAM;QACtB,gBAAgB;KACjB,CAAC,CAAA;AACJ,CAAC,CAAA;AAED,MAAM,kCAAkC,GAAG,CACzC,gBAAkC,EAClC,SAAiD,EACjD,iBAA2B,EACT,EAAE;IACpB,iDAAiD;IACjD,gCAAgC;IAChC,IACE,CAAC,SAAS;QACV,CAAC,SAAS,CAAC,SAAS;QACpB,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,CAAA;IACD,IAAI,CAAC,eAAe,CAAC,MAAM;QAAE,OAAO,MAAM,CAAA;IAE1C,IAAI,0BAA0B,GAAG,KAAK,CAAA;IACtC,IAAI,yBAAyB,GAAG,KAAK,CAAA;IACrC,KAAK,MAAM,SAAS,IAAI,eAAe,EAAE;QACvC,MAAM,eAAe,GAAG,kCAAkC,CACxD,gBAAgB,EAChB,SAAS,EACT,gBAAgB,CACjB,CAAA;QACD,QAAQ,eAAe,EAAE;YACvB,KAAK,MAAM,CAAC,CAAC;gBACX,OAAO,MAAM,CAAA;aACd;YACD,KAAK,4BAA4B,CAAC,CAAC;gBACjC,0BAA0B,GAAG,IAAI,CAAA;gBACjC,MAAK;aACN;YACD,KAAK,2BAA2B,CAAC,CAAC;gBAChC,yBAAyB,GAAG,IAAI,CAAA;gBAChC,MAAK;aACN;SACF;KACF;IACD,IAAI,0BAA0B,EAAE;QAC9B,OAAO,4BAA4B,CAAA;KACpC;SAAM,IAAI,yBAAyB,EAAE;QACpC,OAAO,2BAA2B,CAAA;KACnC;SAAM;QACL,OAAO,MAAM,CAAA;KACd;AACH,CAAC;AApED,0CAoEC","sourcesContent":["import { FormTypes, SubmissionTypes } from '@oneblink/types'\nimport evaluateConditionalOptionsPredicate from './evaluateConditionalOptionsPredicate'\nimport { FormElementsCtrl } from '../types'\nimport { typeCastService } from '..'\nimport conditionallyShowElement from './conditionallyShowElement'\n\nexport type ShouldShowOption =\n | 'SHOW'\n | 'HIDE'\n | 'LOADING_DYNAMIC_DEPENDENCY'\n | 'LOADING_STATIC_DEPENDENCY'\n\nconst handleAttributePredicate = (\n predicate: FormTypes.ChoiceElementOptionAttribute,\n model: SubmissionTypes.S3SubmissionData['submission'],\n predicateElement: FormTypes.FormElementWithOptions,\n) => {\n const values = model[predicateElement.name]\n if (!values) return true\n\n if (\n Array.isArray(values) &&\n (!values.length ||\n !values.filter((value) => typeof value !== 'undefined').length)\n ) {\n return true\n }\n\n return evaluateConditionalOptionsPredicate({\n predicate: {\n type: 'OPTIONS',\n elementId: predicate.elementId,\n optionIds: predicate.optionIds,\n },\n predicateValue: values,\n predicateElement,\n })\n}\n\nconst conditionallyShowOptionByPredicate = (\n formElementsCtrl: FormElementsCtrl,\n predicate: FormTypes.ChoiceElementOptionAttribute,\n elementsEvaluated: string[],\n): ShouldShowOption => {\n // Validate the predicate data, if it is invalid,\n // we will always show the field\n if (\n !predicate ||\n !predicate.elementId ||\n !predicate.optionIds ||\n !predicate.optionIds.length\n ) {\n return 'SHOW'\n }\n const predicateElement = formElementsCtrl.flattenedElements.find(\n (element) => {\n return element.id === predicate.elementId\n },\n )\n\n // If we cant find the element used for the predicate,\n // we can check to see if the element being evaluated\n // is in a repeatable set and the predicate element is\n // in a parent list of elements.\n if (!predicateElement) {\n if (formElementsCtrl.parentFormElementsCtrl) {\n return conditionallyShowOptionByPredicate(\n formElementsCtrl.parentFormElementsCtrl,\n predicate,\n elementsEvaluated,\n )\n } else {\n return 'HIDE'\n }\n }\n\n const optionsPredicateElement =\n typeCastService.formElements.toOptionsElement(predicateElement)\n if (!optionsPredicateElement) {\n return 'HIDE'\n }\n // If the predicate element does not have any options to evaluate,\n // we will show the element.\n // Unless the predicate element has dynamic options and\n // options have not been fetched yet.\n const isLoadingPredicateElementsOptions = !Array.isArray(\n optionsPredicateElement.options,\n )\n if (isLoadingPredicateElementsOptions) {\n if (optionsPredicateElement.optionsType === 'DYNAMIC') {\n return 'LOADING_DYNAMIC_DEPENDENCY'\n } else {\n return 'LOADING_STATIC_DEPENDENCY'\n }\n }\n\n const everyOptionIsShowing = predicate.optionIds.every((id) => {\n const predicateOption = optionsPredicateElement.options?.find(\n (o) => o.id === id,\n )\n if (!predicateOption) return 'HIDE'\n\n return conditionallyShowOption(\n { model: formElementsCtrl.model, flattenedElements: [] },\n optionsPredicateElement,\n predicateOption,\n elementsEvaluated,\n )\n })\n\n if (!everyOptionIsShowing) {\n return 'HIDE'\n }\n\n // Check to see if the model has one of the valid values to show the element\n const shouldShow = handleAttributePredicate(\n predicate,\n formElementsCtrl.model,\n optionsPredicateElement,\n )\n return shouldShow ? 'SHOW' : 'HIDE'\n}\n\nconst isAttributeFilterValid = (\n formElementsCtrl: FormElementsCtrl,\n predicate: FormTypes.ChoiceElementOptionAttribute,\n elementsEvaluated: string[],\n): boolean => {\n const predicateElement = formElementsCtrl.flattenedElements.find(\n (element) => {\n return element.id === predicate.elementId\n },\n )\n\n // If we cant find the element used for the predicate,\n // we can check to see if the element being evaluated\n // is in a repeatable set and the predicate element is\n // in a parent list of elements.\n if (!predicateElement) {\n if (formElementsCtrl.parentFormElementsCtrl) {\n return isAttributeFilterValid(\n formElementsCtrl.parentFormElementsCtrl,\n predicate,\n elementsEvaluated,\n )\n } else {\n return false\n }\n }\n\n // now we have the model and predicate element, verify that the predicate element\n // is not hidden\n if (\n // Will never be a page, just making typescript happy :)\n predicateElement.type === 'page' ||\n predicateElement.type === 'section' ||\n !conditionallyShowElement(formElementsCtrl, predicateElement, [])\n ) {\n return false\n }\n\n // verify that at least one option is selected\n const values = formElementsCtrl.model[predicateElement.name]\n if (!values) return false\n // if the model value is an array, verify that it has a selection\n if (\n Array.isArray(values) &&\n (!values.length ||\n !values.filter((value) => typeof value !== 'undefined').length)\n ) {\n return false\n }\n\n return true\n}\n\nexport default function conditionallyShowOption(\n formElementsCtrl: FormElementsCtrl,\n elementToEvaluate: FormTypes.FormElementWithOptions,\n optionToEvaluate: FormTypes.ChoiceElementOption,\n optionsEvaluated: string[],\n): ShouldShowOption {\n // If the element does not have the `conditionallyShow` flag set,\n // we can always show the element.\n\n if (\n !elementToEvaluate.conditionallyShowOptions ||\n !optionToEvaluate ||\n !optionToEvaluate.attributes ||\n !Array.isArray(optionToEvaluate.attributes) ||\n !optionToEvaluate.attributes.length\n ) {\n return 'SHOW'\n }\n\n // Check to see if this element has already been used to evaluate\n // if the element should be shown based on parent element conditional logic\n if (optionsEvaluated.some((optionId) => optionId === optionToEvaluate.id)) {\n throw new Error(\n 'Your conditional logic has caused an infinite loop. Check the following Fields to ensure element A does not rely on element B if element B also relies on element A.',\n )\n } else {\n optionsEvaluated.push(optionToEvaluate.id)\n }\n const validPredicates = (optionToEvaluate.attributes || []).filter(\n (predicate) => {\n return isAttributeFilterValid(\n formElementsCtrl,\n predicate,\n optionsEvaluated,\n )\n },\n )\n if (!validPredicates.length) return 'SHOW'\n\n let isLoadingDynamicDEPENDENCY = false\n let isLoadingStaticDEPENDENCY = false\n for (const predicate of validPredicates) {\n const predicateResult = conditionallyShowOptionByPredicate(\n formElementsCtrl,\n predicate,\n optionsEvaluated,\n )\n switch (predicateResult) {\n case 'SHOW': {\n return 'SHOW'\n }\n case 'LOADING_DYNAMIC_DEPENDENCY': {\n isLoadingDynamicDEPENDENCY = true\n break\n }\n case 'LOADING_STATIC_DEPENDENCY': {\n isLoadingStaticDEPENDENCY = true\n break\n }\n }\n }\n if (isLoadingDynamicDEPENDENCY) {\n return 'LOADING_DYNAMIC_DEPENDENCY'\n } else if (isLoadingStaticDEPENDENCY) {\n return 'LOADING_STATIC_DEPENDENCY'\n } else {\n return 'HIDE'\n }\n}\n"]}
1
+ {"version":3,"file":"conditionallyShowOption.js","sourceRoot":"","sources":["../../src/conditionalLogicService/conditionallyShowOption.ts"],"names":[],"mappings":";;;;;AACA,gHAAuF;AAEvF,0BAAoC;AACpC,0FAAiE;AAQjE,MAAM,wBAAwB,GAAG,CAC/B,SAAiD,EACjD,KAAqD,EACrD,gBAAkD,EAClD,EAAE;IACF,MAAM,MAAM,GAAG,KAAK,CAAC,gBAAgB,CAAC,IAAI,CAAC,CAAA;IAC3C,IAAI,CAAC,MAAM;QAAE,OAAO,IAAI,CAAA;IAExB,IACE,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC;QACrB,CAAC,CAAC,MAAM,CAAC,MAAM;YACb,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,OAAO,KAAK,KAAK,WAAW,CAAC,CAAC,MAAM,CAAC,EACjE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CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,KAAK,gBAAgB,CAAC,EAAE,CAAC,EAAE;QACzE,MAAM,IAAI,KAAK,CACb,sKAAsK,CACvK,CAAA;KACF;SAAM;QACL,gBAAgB,CAAC,IAAI,CAAC,gBAAgB,CAAC,EAAE,CAAC,CAAA;KAC3C;IACD,MAAM,eAAe,GAAG,CAAC,gBAAgB,CAAC,UAAU,IAAI,EAAE,CAAC,CAAC,MAAM,CAChE,CAAC,SAAS,EAAE,EAAE;QACZ,OAAO,sBAAsB,CAC3B,gBAAgB,EAChB,SAAS,EACT,gBAAgB,CACjB,CAAA;IACH,CAAC,CACF,CAAA;IACD,IAAI,CAAC,eAAe,CAAC,MAAM;QAAE,OAAO,MAAM,CAAA;IAE1C,IAAI,0BAA0B,GAAG,KAAK,CAAA;IACtC,IAAI,yBAAyB,GAAG,KAAK,CAAA;IACrC,KAAK,MAAM,SAAS,IAAI,eAAe,EAAE;QACvC,MAAM,eAAe,GAAG,kCAAkC,CACxD,gBAAgB,EAChB,SAAS,EACT,gBAAgB,CACjB,CAAA;QACD,QAAQ,eAAe,EAAE;YACvB,KAAK,MAAM,CAAC,CAAC;gBACX,OAAO,MAAM,CAAA;aACd;YACD,KAAK,4BAA4B,CAAC,CAAC;gBACjC,0BAA0B,GAAG,IAAI,CAAA;gBACjC,MAAK;aACN;YACD,KAAK,2BAA2B,CAAC,CAAC;gBAChC,yBAAyB,GAAG,IAAI,CAAA;gBAChC,MAAK;aACN;SACF;KACF;IACD,IAAI,0BAA0B,EAAE;QAC9B,OAAO,4BAA4B,CAAA;KACpC;SAAM,IAAI,yBAAyB,EAAE;QACpC,OAAO,2BAA2B,CAAA;KACnC;SAAM;QACL,OAAO,MAAM,CAAA;KACd;AACH,CAAC;AApED,0CAoEC","sourcesContent":["import { FormTypes, SubmissionTypes } from '@oneblink/types'\nimport evaluateConditionalOptionsPredicate from './evaluateConditionalOptionsPredicate'\nimport { FormElementsCtrl } from '../types'\nimport { typeCastService } from '..'\nimport conditionallyShowElement from './conditionallyShowElement'\n\nexport type ShouldShowOption =\n | 'SHOW'\n | 'HIDE'\n | 'LOADING_DYNAMIC_DEPENDENCY'\n | 'LOADING_STATIC_DEPENDENCY'\n\nconst handleAttributePredicate = (\n predicate: FormTypes.ChoiceElementOptionAttribute,\n model: SubmissionTypes.S3SubmissionData['submission'],\n predicateElement: FormTypes.FormElementWithOptions,\n) => {\n const values = model[predicateElement.name]\n if (!values) return true\n\n if (\n Array.isArray(values) &&\n (!values.length ||\n !values.filter((value) => typeof value !== 'undefined').length)\n ) {\n return true\n }\n\n return evaluateConditionalOptionsPredicate({\n predicate: {\n type: 'OPTIONS',\n elementId: predicate.elementId,\n optionIds: predicate.optionIds,\n },\n predicateValue: values,\n predicateElement,\n })\n}\n\nconst conditionallyShowOptionByPredicate = (\n formElementsCtrl: FormElementsCtrl,\n predicate: FormTypes.ChoiceElementOptionAttribute,\n elementsEvaluated: string[],\n): ShouldShowOption => {\n // Validate the predicate data, if it is invalid,\n // we will always show the field\n if (\n !predicate ||\n !predicate.elementId ||\n !predicate.optionIds ||\n !predicate.optionIds.length\n ) {\n return 'SHOW'\n }\n const predicateElement = formElementsCtrl.flattenedElements.find(\n (element) => {\n return element.id === predicate.elementId\n },\n )\n\n // If we cant find the element used for the predicate,\n // we can check to see if the element being evaluated\n // is in a repeatable set and the predicate element is\n // in a parent list of elements.\n if (!predicateElement) {\n if (formElementsCtrl.parentFormElementsCtrl) {\n return conditionallyShowOptionByPredicate(\n formElementsCtrl.parentFormElementsCtrl,\n predicate,\n elementsEvaluated,\n )\n } else {\n return 'HIDE'\n }\n }\n\n const optionsPredicateElement =\n typeCastService.formElements.toOptionsElement(predicateElement)\n if (!optionsPredicateElement) {\n return 'HIDE'\n }\n // If the predicate element does not have any options to evaluate,\n // we will show the element.\n // Unless the predicate element has dynamic options and\n // options have not been fetched yet.\n const isLoadingPredicateElementsOptions = !Array.isArray(\n optionsPredicateElement.options,\n )\n if (isLoadingPredicateElementsOptions) {\n if (optionsPredicateElement.optionsType === 'DYNAMIC') {\n return 'LOADING_DYNAMIC_DEPENDENCY'\n } else {\n return 'LOADING_STATIC_DEPENDENCY'\n }\n }\n\n const everyOptionIsShowing = predicate.optionIds.every((id) => {\n const predicateOption = optionsPredicateElement.options?.find(\n (o) => o.id === id,\n )\n if (!predicateOption) return 'HIDE'\n\n return conditionallyShowOption(\n { model: formElementsCtrl.model, flattenedElements: [] },\n optionsPredicateElement,\n predicateOption,\n elementsEvaluated,\n )\n })\n\n if (!everyOptionIsShowing) {\n return 'HIDE'\n }\n\n // Check to see if the model has one of the valid values to show the element\n const shouldShow = handleAttributePredicate(\n predicate,\n formElementsCtrl.model,\n optionsPredicateElement,\n )\n return shouldShow ? 'SHOW' : 'HIDE'\n}\n\nconst isAttributeFilterValid = (\n formElementsCtrl: FormElementsCtrl,\n predicate: FormTypes.ChoiceElementOptionAttribute,\n elementsEvaluated: string[],\n): boolean => {\n const predicateElement = formElementsCtrl.flattenedElements.find(\n (element) => {\n return element.id === predicate.elementId\n },\n )\n\n // If we cant find the element used for the predicate,\n // we can check to see if the element being evaluated\n // is in a repeatable set and the predicate element is\n // in a parent list of elements.\n if (!predicateElement) {\n if (formElementsCtrl.parentFormElementsCtrl) {\n return isAttributeFilterValid(\n formElementsCtrl.parentFormElementsCtrl,\n predicate,\n elementsEvaluated,\n )\n } else {\n return false\n }\n }\n\n // now we have the model and predicate element, verify that the predicate element\n // is not hidden\n if (\n // Will never be a page, just making typescript happy :)\n predicateElement.type === 'page' ||\n predicateElement.type === 'section' ||\n !conditionallyShowElement(formElementsCtrl, predicateElement, [])\n ) {\n return false\n }\n\n // verify that at least one option is selected\n const values = formElementsCtrl.model[predicateElement.name]\n if (!values) return false\n // if the model value is an array, verify that it has a selection\n if (\n Array.isArray(values) &&\n (!values.length ||\n !values.filter((value) => typeof value !== 'undefined').length)\n ) {\n return false\n }\n\n return true\n}\n\nexport default function conditionallyShowOption(\n formElementsCtrl: FormElementsCtrl,\n elementToEvaluate: FormTypes.FormElementWithOptions,\n optionToEvaluate: FormTypes.ChoiceElementOption,\n optionsEvaluated: string[],\n): ShouldShowOption {\n // If the element does not have the `conditionallyShow` flag set,\n // we can always show the element.\n\n if (\n !elementToEvaluate.conditionallyShowOptions ||\n !optionToEvaluate ||\n !optionToEvaluate.attributes ||\n !Array.isArray(optionToEvaluate.attributes) ||\n !optionToEvaluate.attributes.length\n ) {\n return 'SHOW'\n }\n\n // Check to see if this element has already been used to evaluate\n // if the element should be shown based on parent element conditional logic\n if (optionsEvaluated.some((optionId) => optionId === optionToEvaluate.id)) {\n throw new Error(\n 'Your conditional logic has caused an infinite loop. Check the following Fields to ensure element A does not rely on element B if element B also relies on element A.',\n )\n } else {\n optionsEvaluated.push(optionToEvaluate.id)\n }\n const validPredicates = (optionToEvaluate.attributes || []).filter(\n (predicate) => {\n return isAttributeFilterValid(\n formElementsCtrl,\n predicate,\n optionsEvaluated,\n )\n },\n )\n if (!validPredicates.length) return 'SHOW'\n\n let isLoadingDynamicDEPENDENCY = false\n let isLoadingStaticDEPENDENCY = false\n for (const predicate of validPredicates) {\n const predicateResult = conditionallyShowOptionByPredicate(\n formElementsCtrl,\n predicate,\n optionsEvaluated,\n )\n switch (predicateResult) {\n case 'SHOW': {\n return 'SHOW'\n }\n case 'LOADING_DYNAMIC_DEPENDENCY': {\n isLoadingDynamicDEPENDENCY = true\n break\n }\n case 'LOADING_STATIC_DEPENDENCY': {\n isLoadingStaticDEPENDENCY = true\n break\n }\n }\n }\n if (isLoadingDynamicDEPENDENCY) {\n return 'LOADING_DYNAMIC_DEPENDENCY'\n } else if (isLoadingStaticDEPENDENCY) {\n return 'LOADING_STATIC_DEPENDENCY'\n } else {\n return 'HIDE'\n }\n}\n"]}
@@ -2,8 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  function evaluateConditionalOptionsPredicate({ predicate, predicateElement, predicateValue: value, }) {
4
4
  return predicate.optionIds.some((optionId) => {
5
- var _a;
6
- const option = (_a = predicateElement.options) === null || _a === void 0 ? void 0 : _a.find((o) => o.id === optionId);
5
+ const option = predicateElement.options?.find((o) => o.id === optionId);
7
6
  if (option) {
8
7
  if (Array.isArray(value)) {
9
8
  return value.some((modelValue) => {
@@ -1 +1 @@
1
- {"version":3,"file":"evaluateConditionalOptionsPredicate.js","sourceRoot":"","sources":["../../src/conditionalLogicService/evaluateConditionalOptionsPredicate.ts"],"names":[],"mappings":";;AAEA,SAAwB,mCAAmC,CAAC,EAC1D,SAAS,EACT,gBAAgB,EAChB,cAAc,EAAE,KAAK,GAKtB;IACC,OAAO,SAAS,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,QAAQ,EAAE,EAAE;;QAC3C,MAAM,MAAM,GAAG,MAAA,gBAAgB,CAAC,OAAO,0CAAE,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,QAAQ,CAAC,CAAA;QACvE,IAAI,MAAM,EAAE;YACV,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE;gBACxB,OAAO,KAAK,CAAC,IAAI,CAAC,CAAC,UAAU,EAAE,EAAE;oBAC/B,OAAO,UAAU,KAAK,MAAM,CAAC,KAAK,CAAA;gBACpC,CAAC,CAAC,CAAA;aACH;iBAAM,IAAI,gBAAgB,CAAC,IAAI,KAAK,YAAY,IAAI,KAAK,EAAE;gBAC1D,OAAO,MAAM,CAAC,KAAK,KAAM,KAA4B,CAAC,KAAK,CAAA;aAC5D;iBAAM;gBACL,OAAO,MAAM,CAAC,KAAK,KAAK,KAAK,CAAA;aAC9B;SACF;aAAM;YACL,OAAO,KAAK,CAAA;SACb;IACH,CAAC,CAAC,CAAA;AACJ,CAAC;AAzBD,sDAyBC","sourcesContent":["import { FormTypes, ConditionTypes } from '@oneblink/types'\n\nexport default function evaluateConditionalOptionsPredicate({\n predicate,\n predicateElement,\n predicateValue: value,\n}: {\n predicate: ConditionTypes.ConditionalPredicateOptions\n predicateValue: unknown\n predicateElement: FormTypes.FormElementWithOptions\n}): boolean {\n return predicate.optionIds.some((optionId) => {\n const option = predicateElement.options?.find((o) => o.id === optionId)\n if (option) {\n if (Array.isArray(value)) {\n return value.some((modelValue) => {\n return modelValue === option.value\n })\n } else if (predicateElement.type === 'compliance' && value) {\n return option.value === (value as { value: unknown }).value\n } else {\n return option.value === value\n }\n } else {\n return false\n }\n })\n}\n"]}
1
+ {"version":3,"file":"evaluateConditionalOptionsPredicate.js","sourceRoot":"","sources":["../../src/conditionalLogicService/evaluateConditionalOptionsPredicate.ts"],"names":[],"mappings":";;AAEA,SAAwB,mCAAmC,CAAC,EAC1D,SAAS,EACT,gBAAgB,EAChB,cAAc,EAAE,KAAK,GAKtB;IACC,OAAO,SAAS,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,QAAQ,EAAE,EAAE;QAC3C,MAAM,MAAM,GAAG,gBAAgB,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,QAAQ,CAAC,CAAA;QACvE,IAAI,MAAM,EAAE;YACV,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE;gBACxB,OAAO,KAAK,CAAC,IAAI,CAAC,CAAC,UAAU,EAAE,EAAE;oBAC/B,OAAO,UAAU,KAAK,MAAM,CAAC,KAAK,CAAA;gBACpC,CAAC,CAAC,CAAA;aACH;iBAAM,IAAI,gBAAgB,CAAC,IAAI,KAAK,YAAY,IAAI,KAAK,EAAE;gBAC1D,OAAO,MAAM,CAAC,KAAK,KAAM,KAA4B,CAAC,KAAK,CAAA;aAC5D;iBAAM;gBACL,OAAO,MAAM,CAAC,KAAK,KAAK,KAAK,CAAA;aAC9B;SACF;aAAM;YACL,OAAO,KAAK,CAAA;SACb;IACH,CAAC,CAAC,CAAA;AACJ,CAAC;AAzBD,sDAyBC","sourcesContent":["import { FormTypes, ConditionTypes } from '@oneblink/types'\n\nexport default function evaluateConditionalOptionsPredicate({\n predicate,\n predicateElement,\n predicateValue: value,\n}: {\n predicate: ConditionTypes.ConditionalPredicateOptions\n predicateValue: unknown\n predicateElement: FormTypes.FormElementWithOptions\n}): boolean {\n return predicate.optionIds.some((optionId) => {\n const option = predicateElement.options?.find((o) => o.id === optionId)\n if (option) {\n if (Array.isArray(value)) {\n return value.some((modelValue) => {\n return modelValue === option.value\n })\n } else if (predicateElement.type === 'compliance' && value) {\n return option.value === (value as { value: unknown }).value\n } else {\n return option.value === value\n }\n } else {\n return false\n }\n })\n}\n"]}
@@ -72,7 +72,8 @@ declare function findFormElement(elements: FormTypes.FormElement[], predicate: (
72
72
  */
73
73
  declare function parseFormElementOptionsSet(data: unknown): FormTypes.ChoiceElementOption[];
74
74
  /**
75
- * Takes the nested definition structure and returns all form elements as 1d array.
75
+ * Takes the nested definition structure and returns all form elements as 1d
76
+ * array.
76
77
  *
77
78
  * #### Example
78
79
  *
@@ -86,7 +87,10 @@ declare function parseFormElementOptionsSet(data: unknown): FormTypes.ChoiceElem
86
87
  * @returns
87
88
  */
88
89
  declare function flattenFormElements(elements: FormTypes.FormElement[]): FormTypes.FormElement[];
89
- /** The regex used for matching `{ELEMENT:<elementName>}` tags in the OneBlink platform. */
90
+ /**
91
+ * The regex used for matching `{ELEMENT:<elementName>}` tags in the OneBlink
92
+ * platform.
93
+ */
90
94
  declare const ElementWYSIWYGRegex: RegExp;
91
95
  /**
92
96
  * Takes a string and calls a provided handler function for each found instance
@@ -113,28 +117,4 @@ declare const matchElementsTagRegex: (data: string, matchHandler: (options: {
113
117
  elementName: string;
114
118
  elementMatch: string;
115
119
  }) => void) => void;
116
- /** The regex used for matching the `{USER:email}` tag in the OneBlink platform. */
117
- declare const UserWYSIWYGRegex: RegExp;
118
- /**
119
- * Takes a string and calls a provided handler function for each found instance
120
- * of `{ELEMENT:<elementName>}` in the string. Used to replace values in
121
- * OneBlink calculation and info (HTML) elements.
122
- *
123
- * #### Example
124
- *
125
- * ```js
126
- * formElementsService.matchElementsTagRegex(
127
- * myString,
128
- * ({ elementName, elementMatch }) => {
129
- * const v = submission[elementName]
130
- * myString = myString.replace(elementMatch, v)
131
- * },
132
- * )
133
- * ```
134
- *
135
- * @param data
136
- * @param handler
137
- * @returns
138
- */
139
- declare const matchUserTagRegex: (data: string, matchHandler: (userMatch: string) => void) => void;
140
- export { forEachFormElement, forEachFormElementWithOptions, findFormElement, parseFormElementOptionsSet, flattenFormElements, ElementWYSIWYGRegex, UserWYSIWYGRegex, matchElementsTagRegex, matchUserTagRegex, };
120
+ export { forEachFormElement, forEachFormElementWithOptions, findFormElement, parseFormElementOptionsSet, flattenFormElements, ElementWYSIWYGRegex, matchElementsTagRegex, };
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.matchUserTagRegex = exports.matchElementsTagRegex = exports.UserWYSIWYGRegex = exports.ElementWYSIWYGRegex = exports.flattenFormElements = exports.parseFormElementOptionsSet = exports.findFormElement = exports.forEachFormElementWithOptions = exports.forEachFormElement = void 0;
3
+ exports.matchElementsTagRegex = exports.ElementWYSIWYGRegex = exports.flattenFormElements = exports.parseFormElementOptionsSet = exports.findFormElement = exports.forEachFormElementWithOptions = exports.forEachFormElement = void 0;
4
4
  const _1 = require(".");
5
5
  /**
6
6
  * Iterate over all form elements, also iterating over nested form element (e.g.
@@ -146,7 +146,8 @@ function parseFormElementOptionsSet(data) {
146
146
  }
147
147
  exports.parseFormElementOptionsSet = parseFormElementOptionsSet;
148
148
  /**
149
- * Takes the nested definition structure and returns all form elements as 1d array.
149
+ * Takes the nested definition structure and returns all form elements as 1d
150
+ * array.
150
151
  *
151
152
  * #### Example
152
153
  *
@@ -172,7 +173,10 @@ function flattenFormElements(elements) {
172
173
  }, []);
173
174
  }
174
175
  exports.flattenFormElements = flattenFormElements;
175
- /** The regex used for matching `{ELEMENT:<elementName>}` tags in the OneBlink platform. */
176
+ /**
177
+ * The regex used for matching `{ELEMENT:<elementName>}` tags in the OneBlink
178
+ * platform.
179
+ */
176
180
  const ElementWYSIWYGRegex = /({ELEMENT:)([^}]+)(})/g;
177
181
  exports.ElementWYSIWYGRegex = ElementWYSIWYGRegex;
178
182
  /**
@@ -199,44 +203,11 @@ exports.ElementWYSIWYGRegex = ElementWYSIWYGRegex;
199
203
  const matchElementsTagRegex = (data, matchHandler) => {
200
204
  let matches;
201
205
  while ((matches = ElementWYSIWYGRegex.exec(data)) !== null) {
202
- if ((matches === null || matches === void 0 ? void 0 : matches.length) < 3)
206
+ if (matches?.length < 3)
203
207
  continue;
204
208
  const elementName = matches[2];
205
209
  matchHandler({ elementName, elementMatch: matches[0] });
206
210
  }
207
211
  };
208
212
  exports.matchElementsTagRegex = matchElementsTagRegex;
209
- /** The regex used for matching the `{USER:email}` tag in the OneBlink platform. */
210
- const UserWYSIWYGRegex = /{USER:email}/g;
211
- exports.UserWYSIWYGRegex = UserWYSIWYGRegex;
212
- /**
213
- * Takes a string and calls a provided handler function for each found instance
214
- * of `{ELEMENT:<elementName>}` in the string. Used to replace values in
215
- * OneBlink calculation and info (HTML) elements.
216
- *
217
- * #### Example
218
- *
219
- * ```js
220
- * formElementsService.matchElementsTagRegex(
221
- * myString,
222
- * ({ elementName, elementMatch }) => {
223
- * const v = submission[elementName]
224
- * myString = myString.replace(elementMatch, v)
225
- * },
226
- * )
227
- * ```
228
- *
229
- * @param data
230
- * @param handler
231
- * @returns
232
- */
233
- const matchUserTagRegex = (data, matchHandler) => {
234
- let matches;
235
- while ((matches = UserWYSIWYGRegex.exec(data)) !== null) {
236
- if ((matches === null || matches === void 0 ? void 0 : matches.length) < 1)
237
- continue;
238
- matchHandler(matches[0]);
239
- }
240
- };
241
- exports.matchUserTagRegex = matchUserTagRegex;
242
213
  //# sourceMappingURL=formElementsService.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"formElementsService.js","sourceRoot":"","sources":["../src/formElementsService.ts"],"names":[],"mappings":";;;AACA,wBAA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A;AA8CC,sDAAqB;AA5CvB,mFAAmF;AACnF,MAAM,gBAAgB,GAAG,eAAe,CAAA;AA0CtC,4CAAgB;AAxClB;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,iBAAiB,GAAG,CACxB,IAAY,EACZ,YAAyC,EACzC,EAAE;IACF,IAAI,OAAO,CAAA;IACX,OAAO,CAAC,OAAO,GAAG,gBAAgB,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,KAAK,IAAI,EAAE;QACvD,IAAI,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,MAAM,IAAG,CAAC;YAAE,SAAQ;QAEjC,YAAY,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAA;KACzB;AACH,CAAC,CAAA;AAWC,8CAAiB","sourcesContent":["import { FormTypes } from '@oneblink/types'\nimport { typeCastService } from '.'\n\n/**\n * Iterate over all form elements, also iterating over nested form element (e.g.\n * page elements).\n *\n * #### Example\n *\n * ```js\n * formElementsService.forEachFormElement(form.elements, (formElement) => {\n * // do something with formElement\n * })\n * ```\n *\n * @param elements The form elements to iterate over\n * @param forEach Function to execute on each form element\n */\nfunction forEachFormElement(\n elements: FormTypes.FormElement[],\n forEach: (\n element: FormTypes.FormElement,\n elements: FormTypes.FormElement[],\n ) => void,\n): void {\n findFormElement(elements, (formElement, parentElements) => {\n forEach(formElement, parentElements)\n return false\n })\n}\n\n/**\n * Iterate over all form elements that have options (e.g. `'select'` type\n * elements), also iterating over nested form element (e.g. page elements).\n *\n * #### Example\n *\n * ```js\n * formElementsService.forEachFormElementWithOptions(\n * form.elements,\n * (formElementWithOptions) => {\n * // do something with formElementWithOptions\n * },\n * )\n * ```\n *\n * @param elements The form elements to iterate over\n * @param forEach Function to execute on each form element with options\n */\nfunction forEachFormElementWithOptions(\n elements: FormTypes.FormElement[],\n forEach: (\n elementWithOptions: FormTypes.FormElementWithOptions,\n elements: FormTypes.FormElement[],\n ) => void,\n): void {\n findFormElement(elements, (formElement, parentElements) => {\n const optionsFormElement =\n typeCastService.formElements.toOptionsElement(formElement)\n if (optionsFormElement) {\n forEach(optionsFormElement, parentElements)\n }\n return false\n })\n}\n\n/**\n * Iterate over all form elements and return an element that matches a\n * predicate, also iterating over nested form element (e.g. page elements). Will\n * return `undefined` if no matching element is found.\n *\n * #### Example\n *\n * ```js\n * const formElement = formElementsService.findFormElement(\n * form.elements,\n * (formElement) => {\n * return formElement.id === '123-abc'\n * },\n * )\n * ```\n *\n * @param elements The form elements to iterate over\n * @param predicate Predicate function to execute on each form element\n * @param parentElements\n * @returns\n */\nfunction findFormElement(\n elements: FormTypes.FormElement[],\n predicate: (\n element: FormTypes.FormElement,\n elements: FormTypes.FormElement[],\n ) => boolean,\n parentElements: FormTypes.FormElement[] = [],\n): FormTypes.FormElement | void {\n for (const element of elements) {\n if (predicate(element, parentElements)) {\n return element\n }\n\n if (\n (element.type === 'repeatableSet' ||\n element.type === 'page' ||\n element.type === 'form' ||\n element.type === 'infoPage' ||\n element.type === 'section') &&\n Array.isArray(element.elements)\n ) {\n const nestedElement = findFormElement(element.elements, predicate, [\n ...parentElements,\n element,\n ])\n\n if (nestedElement) {\n return nestedElement\n }\n }\n }\n}\n\n/**\n * Parse unknown data as valid options for a forms element. This will always\n * return an Array of valid options.\n *\n * #### Example\n *\n * ```js\n * const options = formElementsService.parseFormElementOptionsSet(data)\n * // \"options\" are valid for a form element\n * ```\n *\n * @param data\n * @returns\n */\nfunction parseFormElementOptionsSet(\n data: unknown,\n): FormTypes.ChoiceElementOption[] {\n if (!Array.isArray(data)) {\n return []\n }\n return data.reduce(\n (\n options: FormTypes.ChoiceElementOption[],\n record: unknown,\n index: number,\n ) => {\n if (typeof record === 'string') {\n options.push({\n id: index.toString(),\n value: record,\n label: record,\n })\n } else if (typeof record === 'object') {\n const option = record as Record<string, unknown>\n const value =\n typeof option.value === 'string' && option.value\n ? option.value\n : index.toString()\n const id =\n typeof option.id === 'string' && option.id ? option.id : value\n const label =\n typeof option.label === 'string' && option.label\n ? option.label\n : value\n const colour =\n typeof option.colour === 'string' && option.colour\n ? option.colour\n : undefined\n options.push({\n ...option,\n id,\n value,\n label,\n colour,\n })\n }\n return options\n },\n [],\n )\n}\n\n/**\n * Takes the nested definition structure and returns all form elements as 1d array.\n *\n * #### Example\n *\n * ```js\n * const flattenedElements = formElementsService.flattenFormElements(\n * form.elements,\n * )\n * ```\n *\n * @param elements\n * @returns\n */\nfunction flattenFormElements(\n elements: FormTypes.FormElement[],\n): FormTypes.FormElement[] {\n return elements.reduce<FormTypes.FormElement[]>(\n (flattenedElements, element) => {\n flattenedElements.push(element)\n switch (element.type) {\n case 'section':\n case 'page': {\n flattenedElements.push(...flattenFormElements(element.elements))\n }\n }\n return flattenedElements\n },\n [],\n )\n}\n\n/** The regex used for matching `{ELEMENT:<elementName>}` tags in the OneBlink platform. */\nconst ElementWYSIWYGRegex = /({ELEMENT:)([^}]+)(})/g\n\n/**\n * Takes a string and calls a provided handler function for each found instance\n * of `{ELEMENT:<elementName>}` in the string. Used to replace values in\n * OneBlink calculation and info (HTML) elements.\n *\n * #### Example\n *\n * ```js\n * formElementsService.matchElementsTagRegex(\n * myString,\n * ({ elementName, elementMatch }) => {\n * const v = submission[elementName]\n * myString = myString.replace(elementMatch, v)\n * },\n * )\n * ```\n *\n * @param data\n * @param handler\n * @returns\n */\nconst matchElementsTagRegex = (\n data: string,\n matchHandler: (options: {\n elementName: string\n elementMatch: string\n }) => void,\n) => {\n let matches\n while ((matches = ElementWYSIWYGRegex.exec(data)) !== null) {\n if (matches?.length < 3) continue\n\n const elementName = matches[2]\n matchHandler({ elementName, elementMatch: matches[0] })\n }\n}\n\n/** The regex used for matching the `{USER:email}` tag in the OneBlink platform. */\nconst UserWYSIWYGRegex = /{USER:email}/g\n\n/**\n * Takes a string and calls a provided handler function for each found instance\n * of `{ELEMENT:<elementName>}` in the string. Used to replace values in\n * OneBlink calculation and info (HTML) elements.\n *\n * #### Example\n *\n * ```js\n * formElementsService.matchElementsTagRegex(\n * myString,\n * ({ elementName, elementMatch }) => {\n * const v = submission[elementName]\n * myString = myString.replace(elementMatch, v)\n * },\n * )\n * ```\n *\n * @param data\n * @param handler\n * @returns\n */\nconst matchUserTagRegex = (\n data: string,\n matchHandler: (userMatch: string) => void,\n) => {\n let matches\n while ((matches = UserWYSIWYGRegex.exec(data)) !== null) {\n if (matches?.length < 1) continue\n\n matchHandler(matches[0])\n }\n}\n\nexport {\n forEachFormElement,\n forEachFormElementWithOptions,\n findFormElement,\n parseFormElementOptionsSet,\n flattenFormElements,\n ElementWYSIWYGRegex,\n UserWYSIWYGRegex,\n matchElementsTagRegex,\n matchUserTagRegex,\n}\n"]}
1
+ {"version":3,"file":"formElementsService.js","sourceRoot":"","sources":["../src/formElementsService.ts"],"names":[],"mappings":";;;AACA,wBAAmC;AAEnC;;;;;;;;;;;;;;GAcG;AACH,SAAS,kBAAkB,CACzB,QAAiC,EACjC,OAGS;IAET,eAAe,CAAC,QAAQ,EAAE,CAAC,WAAW,EAAE,cAAc,EAAE,EAAE;QACxD,OAAO,CAAC,WAAW,EAAE,cAAc,CAAC,CAAA;QACpC,OAAO,KAAK,CAAA;IACd,CAAC,CAAC,CAAA;AACJ,CAAC;AAsOC,gDAAkB;AApOpB;;;;;;;;;;;;;;;;;GAiBG;AACH,SAAS,6BAA6B,CACpC,QAAiC,EACjC,OAGS;IAET,eAAe,CAAC,QAAQ,EAAE,CAAC,WAAW,EAAE,cAAc,EAAE,EAAE;QACxD,MAAM,kBAAkB,GACtB,kBAAe,CAAC,YAAY,CAAC,gBAAgB,CAAC,WAAW,CAAC,CAAA;QAC5D,IAAI,kBAAkB,EAAE;YACtB,OAAO,CAAC,kBAAkB,EAAE,cAAc,CAAC,CAAA;SAC5C;QACD,OAAO,KAAK,CAAA;IACd,CAAC,CAAC,CAAA;AACJ,CAAC;AAoMC,sEAA6B;AAlM/B;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,SAAS,eAAe,CACtB,QAAiC,EACjC,SA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wBAAwB,CAAA;AA6ClD,kDAAmB;AA3CrB;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,qBAAqB,GAAG,CAC5B,IAAY,EACZ,YAGU,EACV,EAAE;IACF,IAAI,OAAO,CAAA;IACX,OAAO,CAAC,OAAO,GAAG,mBAAmB,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,KAAK,IAAI,EAAE;QAC1D,IAAI,OAAO,EAAE,MAAM,GAAG,CAAC;YAAE,SAAQ;QAEjC,MAAM,WAAW,GAAG,OAAO,CAAC,CAAC,CAAC,CAAA;QAC9B,YAAY,CAAC,EAAE,WAAW,EAAE,YAAY,EAAE,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC,CAAA;KACxD;AACH,CAAC,CAAA;AASC,sDAAqB","sourcesContent":["import { FormTypes } from '@oneblink/types'\nimport { typeCastService } from '.'\n\n/**\n * Iterate over all form elements, also iterating over nested form element (e.g.\n * page elements).\n *\n * #### Example\n *\n * ```js\n * formElementsService.forEachFormElement(form.elements, (formElement) => {\n * // do something with formElement\n * })\n * ```\n *\n * @param elements The form elements to iterate over\n * @param forEach Function to execute on each form element\n */\nfunction forEachFormElement(\n elements: FormTypes.FormElement[],\n forEach: (\n element: FormTypes.FormElement,\n elements: FormTypes.FormElement[],\n ) => void,\n): void {\n findFormElement(elements, (formElement, parentElements) => {\n forEach(formElement, parentElements)\n return false\n })\n}\n\n/**\n * Iterate over all form elements that have options (e.g. `'select'` type\n * elements), also iterating over nested form element (e.g. page elements).\n *\n * #### Example\n *\n * ```js\n * formElementsService.forEachFormElementWithOptions(\n * form.elements,\n * (formElementWithOptions) => {\n * // do something with formElementWithOptions\n * },\n * )\n * ```\n *\n * @param elements The form elements to iterate over\n * @param forEach Function to execute on each form element with options\n */\nfunction forEachFormElementWithOptions(\n elements: FormTypes.FormElement[],\n forEach: (\n elementWithOptions: FormTypes.FormElementWithOptions,\n elements: FormTypes.FormElement[],\n ) => void,\n): void {\n findFormElement(elements, (formElement, parentElements) => {\n const optionsFormElement =\n typeCastService.formElements.toOptionsElement(formElement)\n if (optionsFormElement) {\n forEach(optionsFormElement, parentElements)\n }\n return false\n })\n}\n\n/**\n * Iterate over all form elements and return an element that matches a\n * predicate, also iterating over nested form element (e.g. page elements). Will\n * return `undefined` if no matching element is found.\n *\n * #### Example\n *\n * ```js\n * const formElement = formElementsService.findFormElement(\n * form.elements,\n * (formElement) => {\n * return formElement.id === '123-abc'\n * },\n * )\n * ```\n *\n * @param elements The form elements to iterate over\n * @param predicate Predicate function to execute on each form element\n * @param parentElements\n * @returns\n */\nfunction findFormElement(\n elements: FormTypes.FormElement[],\n predicate: (\n element: FormTypes.FormElement,\n elements: FormTypes.FormElement[],\n ) => boolean,\n parentElements: FormTypes.FormElement[] = [],\n): FormTypes.FormElement | void {\n for (const element of elements) {\n if (predicate(element, parentElements)) {\n return element\n }\n\n if (\n (element.type === 'repeatableSet' ||\n element.type === 'page' ||\n element.type === 'form' ||\n element.type === 'infoPage' ||\n element.type === 'section') &&\n Array.isArray(element.elements)\n ) {\n const nestedElement = findFormElement(element.elements, predicate, [\n ...parentElements,\n element,\n ])\n\n if (nestedElement) {\n return nestedElement\n }\n }\n }\n}\n\n/**\n * Parse unknown data as valid options for a forms element. This will always\n * return an Array of valid options.\n *\n * #### Example\n *\n * ```js\n * const options = formElementsService.parseFormElementOptionsSet(data)\n * // \"options\" are valid for a form element\n * ```\n *\n * @param data\n * @returns\n */\nfunction parseFormElementOptionsSet(\n data: unknown,\n): FormTypes.ChoiceElementOption[] {\n if (!Array.isArray(data)) {\n return []\n }\n return data.reduce(\n (\n options: FormTypes.ChoiceElementOption[],\n record: unknown,\n index: number,\n ) => {\n if (typeof record === 'string') {\n options.push({\n id: index.toString(),\n value: record,\n label: record,\n })\n } else if (typeof record === 'object') {\n const option = record as Record<string, unknown>\n const value =\n typeof option.value === 'string' && option.value\n ? option.value\n : index.toString()\n const id =\n typeof option.id === 'string' && option.id ? option.id : value\n const label =\n typeof option.label === 'string' && option.label\n ? option.label\n : value\n const colour =\n typeof option.colour === 'string' && option.colour\n ? option.colour\n : undefined\n options.push({\n ...option,\n id,\n value,\n label,\n colour,\n })\n }\n return options\n },\n [],\n )\n}\n\n/**\n * Takes the nested definition structure and returns all form elements as 1d\n * array.\n *\n * #### Example\n *\n * ```js\n * const flattenedElements = formElementsService.flattenFormElements(\n * form.elements,\n * )\n * ```\n *\n * @param elements\n * @returns\n */\nfunction flattenFormElements(\n elements: FormTypes.FormElement[],\n): FormTypes.FormElement[] {\n return elements.reduce<FormTypes.FormElement[]>(\n (flattenedElements, element) => {\n flattenedElements.push(element)\n switch (element.type) {\n case 'section':\n case 'page': {\n flattenedElements.push(...flattenFormElements(element.elements))\n }\n }\n return flattenedElements\n },\n [],\n )\n}\n\n/**\n * The regex used for matching `{ELEMENT:<elementName>}` tags in the OneBlink\n * platform.\n */\nconst ElementWYSIWYGRegex = /({ELEMENT:)([^}]+)(})/g\n\n/**\n * Takes a string and calls a provided handler function for each found instance\n * of `{ELEMENT:<elementName>}` in the string. Used to replace values in\n * OneBlink calculation and info (HTML) elements.\n *\n * #### Example\n *\n * ```js\n * formElementsService.matchElementsTagRegex(\n * myString,\n * ({ elementName, elementMatch }) => {\n * const v = submission[elementName]\n * myString = myString.replace(elementMatch, v)\n * },\n * )\n * ```\n *\n * @param data\n * @param handler\n * @returns\n */\nconst matchElementsTagRegex = (\n data: string,\n matchHandler: (options: {\n elementName: string\n elementMatch: string\n }) => void,\n) => {\n let matches\n while ((matches = ElementWYSIWYGRegex.exec(data)) !== null) {\n if (matches?.length < 3) continue\n\n const elementName = matches[2]\n matchHandler({ elementName, elementMatch: matches[0] })\n }\n}\n\nexport {\n forEachFormElement,\n forEachFormElementWithOptions,\n findFormElement,\n parseFormElementOptionsSet,\n flattenFormElements,\n ElementWYSIWYGRegex,\n matchElementsTagRegex,\n}\n"]}
@@ -59,7 +59,8 @@ export declare function getElementSubmissionValue({ propertyName, submission, fo
59
59
  /**
60
60
  * Replace the `{ELEMENT:<elementName>}` values in text while a form is being
61
61
  * filled out. The replacements are suppose to be user friendly and for display
62
- * purposes, e.g. dates should be displayed in the user's desired format and timezone.
62
+ * purposes, e.g. dates should be displayed in the user's desired format and
63
+ * timezone.
63
64
  *
64
65
  * #### Example
65
66
  *
@@ -89,6 +90,11 @@ export declare function getElementSubmissionValue({ propertyName, submission, fo
89
90
  * isDataLookup: false,
90
91
  * },
91
92
  * ],
93
+ * userProfile: {
94
+ * userId: 'abc123',
95
+ * username: 'john-user',
96
+ * email: 'john.user@domain.com',
97
+ * },
92
98
  * },
93
99
  * )
94
100
  * ```
@@ -83,7 +83,6 @@ const CUSTOM_VALUES = [
83
83
  * @returns
84
84
  */
85
85
  function getElementSubmissionValue({ propertyName, submission, formElements, formatDate, formatDateTime, formatTime, formatNumber, formatCurrency, }) {
86
- var _a, _b, _c;
87
86
  const formElement = (0, formElementsService_1.findFormElement)(formElements, (element) => element.type !== 'page' &&
88
87
  element.type !== 'section' &&
89
88
  element.name === propertyName);
@@ -91,7 +90,7 @@ function getElementSubmissionValue({ propertyName, submission, formElements, for
91
90
  if (unknown === undefined || unknown === null) {
92
91
  return undefined;
93
92
  }
94
- switch (formElement === null || formElement === void 0 ? void 0 : formElement.type) {
93
+ switch (formElement?.type) {
95
94
  case 'datetime': {
96
95
  const value = unknown;
97
96
  return formatDateTime(value);
@@ -107,14 +106,13 @@ function getElementSubmissionValue({ propertyName, submission, formElements, for
107
106
  case 'radio':
108
107
  case 'autocomplete': {
109
108
  const value = unknown;
110
- const option = (_a = formElement.options) === null || _a === void 0 ? void 0 : _a.find((opt) => opt.value === value);
111
- return (option === null || option === void 0 ? void 0 : option.label) || value;
109
+ const option = formElement.options?.find((opt) => opt.value === value);
110
+ return option?.label || value;
112
111
  }
113
112
  case 'checkboxes': {
114
113
  const value = unknown;
115
114
  const selectedOptionLabels = value.reduce((labels, selectedOption) => {
116
- var _a;
117
- const foundOption = (_a = formElement.options) === null || _a === void 0 ? void 0 : _a.find((o) => o.value === selectedOption);
115
+ const foundOption = formElement.options?.find((o) => o.value === selectedOption);
118
116
  if (foundOption)
119
117
  labels.push(foundOption.label);
120
118
  return labels;
@@ -126,15 +124,14 @@ function getElementSubmissionValue({ propertyName, submission, formElements, for
126
124
  const option = (formElement.options || []).find((option) => option.value === value.value);
127
125
  return {
128
126
  ...value,
129
- value: (option === null || option === void 0 ? void 0 : option.label) || value.value,
127
+ value: option?.label || value.value,
130
128
  };
131
129
  }
132
130
  case 'select': {
133
131
  if (formElement.multi) {
134
132
  const value = unknown;
135
133
  const selectedOptionLabels = value.reduce((labels, selectedOption) => {
136
- var _a;
137
- const foundOption = (_a = formElement.options) === null || _a === void 0 ? void 0 : _a.find((o) => o.value === selectedOption);
134
+ const foundOption = formElement.options?.find((o) => o.value === selectedOption);
138
135
  if (foundOption)
139
136
  labels.push(foundOption.label);
140
137
  return labels;
@@ -143,8 +140,8 @@ function getElementSubmissionValue({ propertyName, submission, formElements, for
143
140
  }
144
141
  else {
145
142
  const value = unknown;
146
- const option = (_b = formElement.options) === null || _b === void 0 ? void 0 : _b.find((opt) => opt.value === value);
147
- return (option === null || option === void 0 ? void 0 : option.label) || '';
143
+ const option = formElement.options?.find((opt) => opt.value === value);
144
+ return option?.label || '';
148
145
  }
149
146
  }
150
147
  case 'boolean': {
@@ -168,17 +165,17 @@ function getElementSubmissionValue({ propertyName, submission, formElements, for
168
165
  case 'pointAddress':
169
166
  case 'geoscapeAddress': {
170
167
  const value = unknown;
171
- 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);
168
+ return value?.addressDetails?.formattedAddress || value?.addressId;
172
169
  }
173
170
  case 'civicaStreetName': {
174
171
  const value = unknown;
175
- return value === null || value === void 0 ? void 0 : value.formattedStreet;
172
+ return value?.formattedStreet;
176
173
  }
177
174
  case 'civicaNameRecord': {
178
175
  const value = unknown;
179
- 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]
176
+ return ([value?.title, value?.givenName1, value?.familyName]
180
177
  .filter((t) => t)
181
- .join(' ') || (value === null || value === void 0 ? void 0 : value.emailAddress));
178
+ .join(' ') || value?.emailAddress);
182
179
  }
183
180
  case 'abn': {
184
181
  const value = unknown;
@@ -193,7 +190,8 @@ exports.getElementSubmissionValue = getElementSubmissionValue;
193
190
  /**
194
191
  * Replace the `{ELEMENT:<elementName>}` values in text while a form is being
195
192
  * filled out. The replacements are suppose to be user friendly and for display
196
- * purposes, e.g. dates should be displayed in the user's desired format and timezone.
193
+ * purposes, e.g. dates should be displayed in the user's desired format and
194
+ * timezone.
197
195
  *
198
196
  * #### Example
199
197
  *
@@ -223,6 +221,11 @@ exports.getElementSubmissionValue = getElementSubmissionValue;
223
221
  * isDataLookup: false,
224
222
  * },
225
223
  * ],
224
+ * userProfile: {
225
+ * userId: 'abc123',
226
+ * username: 'john-user',
227
+ * email: 'john.user@domain.com',
228
+ * },
226
229
  * },
227
230
  * )
228
231
  * ```
@@ -232,28 +235,23 @@ exports.getElementSubmissionValue = getElementSubmissionValue;
232
235
  * @returns
233
236
  */
234
237
  function replaceInjectablesWithElementValues(text, options) {
238
+ const keys = ['email'];
239
+ // User based values should be replaced with an empty string if
240
+ // there is no user profile or if the property does not have a value
241
+ keys.forEach((key) => {
242
+ text = text.replaceAll(`{USER:${key}}`, options.userProfile?.[key]?.toString() || '');
243
+ });
235
244
  const matchesElement = text.match(formElementsService_1.ElementWYSIWYGRegex);
236
- const matchesUser = text.match(formElementsService_1.UserWYSIWYGRegex);
237
- if (!matchesElement && !matchesUser) {
245
+ if (!matchesElement) {
238
246
  return text;
239
247
  }
240
- if (matchesElement) {
241
- (0, formElementsService_1.matchElementsTagRegex)(text, ({ elementName, elementMatch }) => {
242
- const value = getElementSubmissionValue({
243
- propertyName: elementName,
244
- ...options,
245
- });
246
- text = text.replace(elementMatch, value === undefined ? '' : value);
248
+ (0, formElementsService_1.matchElementsTagRegex)(text, ({ elementName, elementMatch }) => {
249
+ const value = getElementSubmissionValue({
250
+ propertyName: elementName,
251
+ ...options,
247
252
  });
248
- }
249
- if (matchesUser) {
250
- (0, formElementsService_1.matchUserTagRegex)(text, (userMatch) => {
251
- const { userProfile } = options;
252
- if (userProfile === null || userProfile === void 0 ? void 0 : userProfile.email) {
253
- text = text.replace(userMatch, userProfile.email);
254
- }
255
- });
256
- }
253
+ text = text.replace(elementMatch, value === undefined ? '' : value);
254
+ });
257
255
  return text;
258
256
  }
259
257
  exports.replaceInjectablesWithElementValues = replaceInjectablesWithElementValues;
@@ -325,7 +323,7 @@ function replaceInjectablesWithSubmissionValues(text, { form, submission, extern
325
323
  userProfile,
326
324
  });
327
325
  return CUSTOM_VALUES.reduce((newString, customValue) => {
328
- return newString.replace(new RegExp(customValue.string, 'g'), customValue.value({
326
+ return newString.replaceAll(customValue.string, customValue.value({
329
327
  form,
330
328
  submissionTimestamp,
331
329
  externalId,
@@ -1 +1 @@
1
- {"version":3,"file":"replaceCustomValues.js","sourceRoot":"","sources":["../src/replaceCustomValues.ts"],"names":[],"mappings":";;;AAQA,+DAM8B;AAC9B,6CAAwD;AAoBxD,MAAM,aAAa,GAAG;IACpB;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,CAAC,QAAQ,EAAE;KACnE;IACD;QACE,MAAM,EAAE,aAAa;QACrB,KAAK,EAAE,CAAC,EAAE,I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,EAAE,GAAG,OAAO,CAAA;YAC/B,IAAI,WAAW,aAAX,WAAW,uBAAX,WAAW,CAAE,KAAK,EAAE;gBACtB,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE,WAAW,CAAC,KAAK,CAAC,CAAA;aAClD;QACH,CAAC,CAAC,CAAA;KACH;IACD,OAAO,IAAI,CAAA;AACb,CAAC;AArCD,kFAqCC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuDG;AACH,SAAgB,sCAAsC,CACpD,IAAY,EACZ,EACE,IAAI,EACJ,UAAU,EACV,UAAU,EACV,YAAY,EACZ,mBAAmB,EACnB,UAAU,EACV,cAAc,EACd,UAAU,EACV,YAAY,EACZ,cAAc,EACd,kBAAkB,EAClB,WAAW,GACe;IAE5B,MAAM,MAAM,GAAG,mCAAmC,CAAC,IAAI,EAAE;QACvD,YAAY,EAAE,IAAI,CAAC,QAAQ;QAC3B,UAAU;QACV,UAAU;QACV,cAAc;QACd,UAAU;QACV,YAAY;QACZ,cAAc;QACd,WAAW;KACZ,CAAC,CAAA;IACF,OAAO,aAAa,CAAC,MAAM,CAAC,CAAC,SAAS,EAAE,WAAW,EAAE,EAAE;QACrD,OAAO,SAAS,CAAC,OAAO,CACtB,IAAI,MAAM,CAAC,WAAW,CAAC,MAAM,EAAE,GAAG,CAAC,EACnC,WAAW,CAAC,KAAK,CAAC;YAChB,IAAI;YACJ,mBAAmB;YACnB,UAAU;YACV,YAAY;YACZ,UAAU;YACV,cAAc;YACd,UAAU;YACV,YAAY;YACZ,cAAc;YACd,kBAAkB;YAClB,UAAU;YACV,WAAW;SACZ,CAAC,CACH,CAAA;IACH,CAAC,EAAE,MAAM,CAAC,CAAA;AACZ,CAAC;AA9CD,wFA8CC","sourcesContent":["import {\n CivicaTypes,\n FormTypes,\n GeoscapeTypes,\n MiscTypes,\n PointTypes,\n SubmissionTypes,\n} from '@oneblink/types'\nimport {\n findFormElement,\n matchElementsTagRegex,\n matchUserTagRegex,\n ElementWYSIWYGRegex,\n UserWYSIWYGRegex,\n} from './formElementsService'\nimport { getABNNumberFromABNRecord } from './abnService'\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 ReplaceInjectablesOptions = ReplaceInjectablesFormatters & {\n form: FormTypes.Form\n submissionId: string\n submissionTimestamp: string\n externalId: string | undefined\n previousApprovalId: string | undefined\n submission: SubmissionTypes.S3SubmissionData['submission']\n userProfile: MiscTypes.UserProfile | undefined\n}\n\nconst CUSTOM_VALUES = [\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 ''\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\n/**\n * Function to get the display value of a property in submission\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): unknown {\n const formElement = findFormElement(\n formElements,\n (element) =>\n element.type !== 'page' &&\n element.type !== 'section' &&\n element.name === propertyName,\n )\n\n const unknown = submission[propertyName]\n if (unknown === undefined || unknown === null) {\n return undefined\n }\n\n switch (formElement?.type) {\n case 'datetime': {\n const value = unknown as string\n return formatDateTime(value)\n }\n case 'date': {\n const value = unknown as string\n return formatDate(value)\n }\n case 'time': {\n const value = unknown as string\n return formatTime(value)\n }\n case 'radio':\n case 'autocomplete': {\n const value = unknown as string\n const option = formElement.options?.find((opt) => opt.value === value)\n return option?.label || value\n }\n\n case 'checkboxes': {\n const value = unknown as string[]\n const selectedOptionLabels: string[] = value.reduce(\n (labels: string[], selectedOption: string) => {\n const foundOption = formElement.options?.find(\n (o) => o.value === selectedOption,\n )\n if (foundOption) labels.push(foundOption.label)\n return labels\n },\n [],\n )\n return selectedOptionLabels.length ? selectedOptionLabels : undefined\n }\n case 'compliance': {\n const value = unknown as {\n value?: string\n }\n const option = (formElement.options || []).find(\n (option: FormTypes.ChoiceElementOption) => option.value === value.value,\n )\n return {\n ...value,\n value: option?.label || value.value,\n }\n }\n case 'select': {\n if (formElement.multi) {\n const value = unknown as string[]\n const selectedOptionLabels: string[] = value.reduce(\n (labels: string[], selectedOption: string) => {\n const foundOption = formElement.options?.find(\n (o) => o.value === selectedOption,\n )\n if (foundOption) labels.push(foundOption.label)\n return labels\n },\n [],\n )\n return selectedOptionLabels.length ? selectedOptionLabels : undefined\n } else {\n const value = unknown as string\n const option = formElement.options?.find((opt) => opt.value === value)\n return option?.label || ''\n }\n }\n case 'boolean': {\n const value = unknown as boolean\n return value ? 'Yes' : 'No'\n }\n case 'calculation': {\n const value = unknown as number\n if (!Number.isNaN(value) && Number.isFinite(value)) {\n let text\n if (formElement.displayAsCurrency) {\n text = formatCurrency(value)\n } else {\n text = formatNumber(value)\n }\n return text\n }\n return undefined\n }\n case 'pointAddress':\n case 'geoscapeAddress': {\n const value = unknown as\n | PointTypes.PointAddress\n | GeoscapeTypes.GeoscapeAddress\n return value?.addressDetails?.formattedAddress || value?.addressId\n }\n case 'civicaStreetName': {\n const value = unknown as CivicaTypes.CivicaStreetName\n return value?.formattedStreet\n }\n case 'civicaNameRecord': {\n const value = unknown as CivicaTypes.CivicaNameRecord\n return (\n [value?.title, value?.givenName1, value?.familyName]\n .filter((t) => t)\n .join(' ') || value?.emailAddress\n )\n }\n case 'abn': {\n const value = unknown as MiscTypes.ABNRecord\n return value ? getABNNumberFromABNRecord(value) : undefined\n }\n default: {\n return unknown\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 timezone.\n *\n * #### Example\n *\n * ```js\n * const result = 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 * },\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 submission: SubmissionTypes.S3SubmissionData['submission']\n userProfile: MiscTypes.UserProfile | undefined\n } & ReplaceInjectablesFormatters,\n): string {\n const matchesElement = text.match(ElementWYSIWYGRegex)\n const matchesUser = text.match(UserWYSIWYGRegex)\n if (!matchesElement && !matchesUser) {\n return text\n }\n\n if (matchesElement) {\n matchElementsTagRegex(text, ({ elementName, elementMatch }) => {\n const value = getElementSubmissionValue({\n propertyName: elementName,\n ...options,\n })\n\n text = text.replace(\n elementMatch,\n value === undefined ? '' : (value as string),\n )\n })\n }\n\n if (matchesUser) {\n matchUserTagRegex(text, (userMatch) => {\n const { userProfile } = options\n if (userProfile?.email) {\n text = text.replace(userMatch, userProfile.email)\n }\n })\n }\n return text\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 {\n form,\n submission,\n externalId,\n submissionId,\n submissionTimestamp,\n formatDate,\n formatDateTime,\n formatTime,\n formatNumber,\n formatCurrency,\n previousApprovalId,\n userProfile,\n }: ReplaceInjectablesOptions,\n): string {\n const string = replaceInjectablesWithElementValues(text, {\n formElements: form.elements,\n submission,\n formatDate,\n formatDateTime,\n formatTime,\n formatNumber,\n formatCurrency,\n userProfile,\n })\n return CUSTOM_VALUES.reduce((newString, customValue) => {\n return newString.replace(\n new RegExp(customValue.string, 'g'),\n customValue.value({\n form,\n submissionTimestamp,\n externalId,\n submissionId,\n formatDate,\n formatDateTime,\n formatTime,\n formatNumber,\n formatCurrency,\n previousApprovalId,\n submission,\n userProfile,\n }),\n )\n }, string)\n}\n"]}
1
+ {"version":3,"file":"replaceCustomValues.js","sourceRoot":"","sources":["../src/replaceCustomValues.ts"],"names":[],"mappings":";;;AAQA,+DAI8B;AAC9B,6CAAwD;AAoBxD,MAAM,aAAa,GAAG;IACpB;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,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A;IACH,CAAC,CAAC,CAAA;IAEF,OAAO,IAAI,CAAA;AACb,CAAC;AApCD,kFAoCC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuDG;AACH,SAAgB,sCAAsC,CACpD,IAAY,EACZ,EACE,IAAI,EACJ,UAAU,EACV,UAAU,EACV,YAAY,EACZ,mBAAmB,EACnB,UAAU,EACV,cAAc,EACd,UAAU,EACV,YAAY,EACZ,cAAc,EACd,kBAAkB,EAClB,WAAW,GACe;IAE5B,MAAM,MAAM,GAAG,mCAAmC,CAAC,IAAI,EAAE;QACvD,YAAY,EAAE,IAAI,CAAC,QAAQ;QAC3B,UAAU;QACV,UAAU;QACV,cAAc;QACd,UAAU;QACV,YAAY;QACZ,cAAc;QACd,WAAW;KACZ,CAAC,CAAA;IACF,OAAO,aAAa,CAAC,MAAM,CAAC,CAAC,SAAS,EAAE,WAAW,EAAE,EAAE;QACrD,OAAO,SAAS,CAAC,UAAU,CACzB,WAAW,CAAC,MAAM,EAClB,WAAW,CAAC,KAAK,CAAC;YAChB,IAAI;YACJ,mBAAmB;YACnB,UAAU;YACV,YAAY;YACZ,UAAU;YACV,cAAc;YACd,UAAU;YACV,YAAY;YACZ,cAAc;YACd,kBAAkB;YAClB,UAAU;YACV,WAAW;SACZ,CAAC,CACH,CAAA;IACH,CAAC,EAAE,MAAM,CAAC,CAAA;AACZ,CAAC;AA9CD,wFA8CC","sourcesContent":["import {\n CivicaTypes,\n FormTypes,\n GeoscapeTypes,\n MiscTypes,\n PointTypes,\n SubmissionTypes,\n} from '@oneblink/types'\nimport {\n findFormElement,\n matchElementsTagRegex,\n ElementWYSIWYGRegex,\n} from './formElementsService'\nimport { getABNNumberFromABNRecord } from './abnService'\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 ReplaceInjectablesOptions = ReplaceInjectablesFormatters & {\n form: FormTypes.Form\n submissionId: string\n submissionTimestamp: string\n externalId: string | undefined\n previousApprovalId: string | undefined\n submission: SubmissionTypes.S3SubmissionData['submission']\n userProfile: MiscTypes.UserProfile | undefined\n}\n\nconst CUSTOM_VALUES = [\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 ''\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\n/**\n * Function to get the display value of a property in submission\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): unknown {\n const formElement = findFormElement(\n formElements,\n (element) =>\n element.type !== 'page' &&\n element.type !== 'section' &&\n element.name === propertyName,\n )\n\n const unknown = submission[propertyName]\n if (unknown === undefined || unknown === null) {\n return undefined\n }\n\n switch (formElement?.type) {\n case 'datetime': {\n const value = unknown as string\n return formatDateTime(value)\n }\n case 'date': {\n const value = unknown as string\n return formatDate(value)\n }\n case 'time': {\n const value = unknown as string\n return formatTime(value)\n }\n case 'radio':\n case 'autocomplete': {\n const value = unknown as string\n const option = formElement.options?.find((opt) => opt.value === value)\n return option?.label || value\n }\n\n case 'checkboxes': {\n const value = unknown as string[]\n const selectedOptionLabels: string[] = value.reduce(\n (labels: string[], selectedOption: string) => {\n const foundOption = formElement.options?.find(\n (o) => o.value === selectedOption,\n )\n if (foundOption) labels.push(foundOption.label)\n return labels\n },\n [],\n )\n return selectedOptionLabels.length ? selectedOptionLabels : undefined\n }\n case 'compliance': {\n const value = unknown as {\n value?: string\n }\n const option = (formElement.options || []).find(\n (option: FormTypes.ChoiceElementOption) => option.value === value.value,\n )\n return {\n ...value,\n value: option?.label || value.value,\n }\n }\n case 'select': {\n if (formElement.multi) {\n const value = unknown as string[]\n const selectedOptionLabels: string[] = value.reduce(\n (labels: string[], selectedOption: string) => {\n const foundOption = formElement.options?.find(\n (o) => o.value === selectedOption,\n )\n if (foundOption) labels.push(foundOption.label)\n return labels\n },\n [],\n )\n return selectedOptionLabels.length ? selectedOptionLabels : undefined\n } else {\n const value = unknown as string\n const option = formElement.options?.find((opt) => opt.value === value)\n return option?.label || ''\n }\n }\n case 'boolean': {\n const value = unknown as boolean\n return value ? 'Yes' : 'No'\n }\n case 'calculation': {\n const value = unknown as number\n if (!Number.isNaN(value) && Number.isFinite(value)) {\n let text\n if (formElement.displayAsCurrency) {\n text = formatCurrency(value)\n } else {\n text = formatNumber(value)\n }\n return text\n }\n return undefined\n }\n case 'pointAddress':\n case 'geoscapeAddress': {\n const value = unknown as\n | PointTypes.PointAddress\n | GeoscapeTypes.GeoscapeAddress\n return value?.addressDetails?.formattedAddress || value?.addressId\n }\n case 'civicaStreetName': {\n const value = unknown as CivicaTypes.CivicaStreetName\n return value?.formattedStreet\n }\n case 'civicaNameRecord': {\n const value = unknown as CivicaTypes.CivicaNameRecord\n return (\n [value?.title, value?.givenName1, value?.familyName]\n .filter((t) => t)\n .join(' ') || value?.emailAddress\n )\n }\n case 'abn': {\n const value = unknown as MiscTypes.ABNRecord\n return value ? getABNNumberFromABNRecord(value) : undefined\n }\n default: {\n return unknown\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 result = 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 submission: SubmissionTypes.S3SubmissionData['submission']\n userProfile: MiscTypes.UserProfile | undefined\n } & ReplaceInjectablesFormatters,\n): string {\n const keys: Array<keyof MiscTypes.UserProfile> = ['email']\n // User based values should be replaced with an empty string if\n // there is no user profile or if the property does not have a value\n keys.forEach((key) => {\n text = text.replaceAll(\n `{USER:${key}}`,\n options.userProfile?.[key]?.toString() || '',\n )\n })\n\n const matchesElement = text.match(ElementWYSIWYGRegex)\n if (!matchesElement) {\n return text\n }\n\n matchElementsTagRegex(text, ({ elementName, elementMatch }) => {\n const value = getElementSubmissionValue({\n propertyName: elementName,\n ...options,\n })\n\n text = text.replace(\n elementMatch,\n value === undefined ? '' : (value as string),\n )\n })\n\n return text\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 {\n form,\n submission,\n externalId,\n submissionId,\n submissionTimestamp,\n formatDate,\n formatDateTime,\n formatTime,\n formatNumber,\n formatCurrency,\n previousApprovalId,\n userProfile,\n }: ReplaceInjectablesOptions,\n): string {\n const string = replaceInjectablesWithElementValues(text, {\n formElements: form.elements,\n submission,\n formatDate,\n formatDateTime,\n formatTime,\n formatNumber,\n formatCurrency,\n userProfile,\n })\n return CUSTOM_VALUES.reduce((newString, customValue) => {\n return newString.replaceAll(\n customValue.string,\n customValue.value({\n form,\n submissionTimestamp,\n externalId,\n submissionId,\n formatDate,\n formatDateTime,\n formatTime,\n formatNumber,\n formatCurrency,\n previousApprovalId,\n submission,\n userProfile,\n }),\n )\n }, string)\n}\n"]}
@@ -20,7 +20,6 @@ exports.getUserFriendlyName = exports.parseUserProfile = void 0;
20
20
  * @param data An object containing all parameters to be passed to the function
21
21
  */
22
22
  function parseUserProfile(data) {
23
- var _a, _b, _c;
24
23
  if (typeof data !== 'object' || !data) {
25
24
  return;
26
25
  }
@@ -67,9 +66,9 @@ function parseUserProfile(data) {
67
66
  ? jwtPayload['custom:phone_number_verified']
68
67
  : undefined,
69
68
  };
70
- if (!((_a = userProfile.supervisor) === null || _a === void 0 ? void 0 : _a.fullName) &&
71
- !((_b = userProfile.supervisor) === null || _b === void 0 ? void 0 : _b.email) &&
72
- !((_c = userProfile.supervisor) === null || _c === void 0 ? void 0 : _c.providerUserId)) {
69
+ if (!userProfile.supervisor?.fullName &&
70
+ !userProfile.supervisor?.email &&
71
+ !userProfile.supervisor?.providerUserId) {
73
72
  userProfile.supervisor = undefined;
74
73
  }
75
74
  if (jwtPayload.identities &&
@@ -1 +1 @@
1
- {"version":3,"file":"userService.js","sourceRoot":"","sources":["../src/userService.ts"],"names":[],"mappings":";;;AAEA;;;;;;;;;;;;;;;;;GAiBG;AACH,SAAgB,gBAAgB,CAC9B,IAAa;;IAEb,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,CAAC,IAAI,EAAE;QACrC,OAAM;KACP;IACD,MAAM,UAAU,GAAG,IAA+B,CAAA;IAClD,IAAI,OAAO,UAAU,CAAC,GAAG,KAAK,QAAQ,EAAE;QACtC,OAAM;KACP;IACD,MAAM,WAAW,GAA0B;QACzC,UAAU,EAAE,KAAK;QACjB,YAAY,EAAE,SAAS;QACvB,cAAc,EAAE,UAAU,CAAC,GAAG;QAC9B,MAAM,EAAE,UAAU,CAAC,GAAG;QACtB,KAAK,EAAE,OAAO,UAAU,CAAC,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS;QAC1E,aAAa,EACX,OAAO,UAAU,CAAC,cAAc,KAAK,SAAS;YAC5C,CAAC,CAAC,UAAU,CAAC,cAAc;YAC3B,CAAC,CAAC,SAAS;QACf,SAAS,EACP,OAAO,UAAU,CAAC,UAAU,KAAK,QAAQ;YACvC,CAAC,CAAC,UAAU,CAAC,UAAU;YACvB,CAAC,CAAC,SAAS;QACf,QAAQ,EACN,OAAO,UAAU,CAAC,WAAW,KAAK,QAAQ;YACxC,CAAC,CAAC,UAAU,CAAC,WAAW;YACxB,CAAC,CAAC,SAAS;QACf,QAAQ,EAAE,OAAO,UAAU,CAAC,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS;QAC3E,OAAO,EACL,OAAO,UAAU,CAAC,OAAO,KAAK,QAAQ,CAAC,CAAC,CAAC,UAAU,CAAC,OAAO,CAAC,CAA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AC,UAAU;QACrB,KAAK,CAAC,OAAO,CAAC,UAAU,CAAC,UAAU,CAAC;QACpC,UAAU,CAAC,UAAU,CAAC,MAAM,EAC5B;QACA,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,GAAG,UAAU,CAAC,UAAU,CAAC,CAAC,CAAC,CAAA;QACzD,WAAW,CAAC,YAAY,GAAG,YAAY,CAAA;QACvC,WAAW,CAAC,cAAc,GAAG,MAAM,CAAA;QACnC,WAAW,CAAC,UAAU,GAAG,YAAY,KAAK,MAAM,CAAA;QAChD,IAAI,WAAW,CAAC,UAAU,EAAE;YAC1B,WAAW,CAAC,QAAQ,GAAG,UAAU,CAAC,kBAAkB,IAAI,MAAM,CAAA;SAC/D;KACF;IAED,OAAO,WAAW,CAAA;AACpB,CAAC;AApFD,4CAoFC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,SAAgB,mBAAmB,CACjC,WAAkC;IAElC,IAAI,WAAW,CAAC,QAAQ,EAAE;QACxB,OAAO,WAAW,CAAC,QAAQ,CAAA;KAC5B;IAED,IAAI,WAAW,CAAC,SAAS,IAAI,WAAW,CAAC,QAAQ,EAAE;QACjD,OAAO,CAAC,WAAW,CAAC,SAAS,EAAE,WAAW,CAAC,QAAQ,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,CAAA;KACtE;IAED,IAAI,WAAW,CAAC,KAAK,EAAE;QACrB,OAAO,WAAW,CAAC,KAAK,CAAA;KACzB;IAED,IAAI,WAAW,CAAC,QAAQ,EAAE;QACxB,OAAO,WAAW,CAAC,QAAQ,CAAA;KAC5B;IAED,OAAO,WAAW,CAAC,MAAM,CAAA;AAC3B,CAAC;AApBD,kDAoBC","sourcesContent":["import { MiscTypes } from '@oneblink/types'\n\n/**\n * Parse a User Profile based on a JWT payload. Will return `undefined` if not a\n * valid JWT payload\n *\n * #### Example\n *\n * ```js\n * import jwtDecode from 'jwt-decode'\n *\n * const jwtPayload = jwtDecode('a valid token from a user')\n * const userProfile = userService.parseUserProfile(jwtPayload)\n * if (userProfile) {\n * // continue\n * }\n * ```\n *\n * @param data An object containing all parameters to be passed to the function\n */\nexport function parseUserProfile(\n data: unknown,\n): MiscTypes.UserProfile | undefined {\n if (typeof data !== 'object' || !data) {\n return\n }\n const jwtPayload = data as Record<string, unknown>\n if (typeof jwtPayload.sub !== 'string') {\n return\n }\n const userProfile: MiscTypes.UserProfile = {\n isSAMLUser: false,\n providerType: 'Cognito',\n providerUserId: jwtPayload.sub,\n userId: jwtPayload.sub,\n email: typeof jwtPayload.email === 'string' ? jwtPayload.email : undefined,\n emailVerified:\n typeof jwtPayload.email_verified === 'boolean'\n ? jwtPayload.email_verified\n : undefined,\n firstName:\n typeof jwtPayload.given_name === 'string'\n ? jwtPayload.given_name\n : undefined,\n lastName:\n typeof jwtPayload.family_name === 'string'\n ? jwtPayload.family_name\n : undefined,\n fullName: typeof jwtPayload.name === 'string' ? jwtPayload.name : undefined,\n picture:\n typeof jwtPayload.picture === 'string' ? jwtPayload.picture : undefined,\n role:\n typeof jwtPayload['custom:role'] === 'string'\n ? jwtPayload['custom:role']\n : undefined,\n username:\n typeof jwtPayload.email === 'string' ? jwtPayload.email : jwtPayload.sub,\n supervisor: {\n fullName:\n typeof jwtPayload['custom:supervisor_name'] === 'string'\n ? jwtPayload['custom:supervisor_name']\n : undefined,\n email:\n typeof jwtPayload['custom:supervisor_email'] === 'string'\n ? jwtPayload['custom:supervisor_email']\n : undefined,\n providerUserId:\n typeof jwtPayload['custom:supervisor_user_id'] === 'string'\n ? jwtPayload['custom:supervisor_user_id']\n : undefined,\n },\n phoneNumber:\n typeof jwtPayload['custom:phone_number'] === 'string'\n ? jwtPayload['custom:phone_number']\n : undefined,\n phoneNumberVerified:\n typeof jwtPayload['custom:phone_number_verified'] === 'boolean'\n ? jwtPayload['custom:phone_number_verified']\n : undefined,\n }\n\n if (\n !userProfile.supervisor?.fullName &&\n !userProfile.supervisor?.email &&\n !userProfile.supervisor?.providerUserId\n ) {\n userProfile.supervisor = undefined\n }\n\n if (\n jwtPayload.identities &&\n Array.isArray(jwtPayload.identities) &&\n jwtPayload.identities.length\n ) {\n const { providerType, userId } = jwtPayload.identities[0]\n userProfile.providerType = providerType\n userProfile.providerUserId = userId\n userProfile.isSAMLUser = providerType === 'SAML'\n if (userProfile.isSAMLUser) {\n userProfile.username = jwtPayload.preferred_username || userId\n }\n }\n\n return userProfile\n}\n\n/**\n * A friendly `string` that represents the a [User Profile](#user-profile). Uses\n * first name, last name, full name, email address or username.\n *\n * #### Example\n *\n * ```js\n * const userProfile = userService.parseUserProfile(jwtPayload)\n * const name = userService.getUserFriendlyName(userProfile)\n * if (name) {\n * // Display current user's name\n * }\n * ```\n *\n * @param userProfile\n * @returns\n */\nexport function getUserFriendlyName(\n userProfile: MiscTypes.UserProfile,\n): string {\n if (userProfile.fullName) {\n return userProfile.fullName\n }\n\n if (userProfile.firstName || userProfile.lastName) {\n return [userProfile.firstName, userProfile.lastName].join(' ').trim()\n }\n\n if (userProfile.email) {\n return userProfile.email\n }\n\n if (userProfile.username) {\n return userProfile.username\n }\n\n return userProfile.userId\n}\n"]}
1
+ {"version":3,"file":"userService.js","sourceRoot":"","sources":["../src/userService.ts"],"names":[],"mappings":";;;AAEA;;;;;;;;;;;;;;;;;GAiBG;AACH,SAAgB,gBAAgB,CAC9B,IAAa;IAEb,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,CAAC,IAAI,EAAE;QACrC,OAAM;KACP;IACD,MAAM,UAAU,GAAG,IAA+B,CAAA;IAClD,IAAI,OAAO,UAAU,CAAC,GAAG,KAAK,QAAQ,EAAE;QACtC,OAAM;KACP;IACD,MAAM,WAAW,GAA0B;QACzC,UAAU,EAAE,KAAK;QACjB,YAAY,EAAE,SAAS;QACvB,cAAc,EAAE,UAAU,CAAC,GAAG;QAC9B,MAAM,EAAE,UAAU,CAAC,GAAG;QACtB,KAAK,EAAE,OAAO,UAAU,CAAC,KAAK,KAAK,QAAQ,CAAC,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S,IAAI,WAAW,CAAC,QAAQ,EAAE;QACjD,OAAO,CAAC,WAAW,CAAC,SAAS,EAAE,WAAW,CAAC,QAAQ,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,CAAA;KACtE;IAED,IAAI,WAAW,CAAC,KAAK,EAAE;QACrB,OAAO,WAAW,CAAC,KAAK,CAAA;KACzB;IAED,IAAI,WAAW,CAAC,QAAQ,EAAE;QACxB,OAAO,WAAW,CAAC,QAAQ,CAAA;KAC5B;IAED,OAAO,WAAW,CAAC,MAAM,CAAA;AAC3B,CAAC;AApBD,kDAoBC","sourcesContent":["import { MiscTypes } from '@oneblink/types'\n\n/**\n * Parse a User Profile based on a JWT payload. Will return `undefined` if not a\n * valid JWT payload\n *\n * #### Example\n *\n * ```js\n * import jwtDecode from 'jwt-decode'\n *\n * const jwtPayload = jwtDecode('a valid token from a user')\n * const userProfile = userService.parseUserProfile(jwtPayload)\n * if (userProfile) {\n * // continue\n * }\n * ```\n *\n * @param data An object containing all parameters to be passed to the function\n */\nexport function parseUserProfile(\n data: unknown,\n): MiscTypes.UserProfile | undefined {\n if (typeof data !== 'object' || !data) {\n return\n }\n const jwtPayload = data as Record<string, unknown>\n if (typeof jwtPayload.sub !== 'string') {\n return\n }\n const userProfile: MiscTypes.UserProfile = {\n isSAMLUser: false,\n providerType: 'Cognito',\n providerUserId: jwtPayload.sub,\n userId: jwtPayload.sub,\n email: typeof jwtPayload.email === 'string' ? jwtPayload.email : undefined,\n emailVerified:\n typeof jwtPayload.email_verified === 'boolean'\n ? jwtPayload.email_verified\n : undefined,\n firstName:\n typeof jwtPayload.given_name === 'string'\n ? jwtPayload.given_name\n : undefined,\n lastName:\n typeof jwtPayload.family_name === 'string'\n ? jwtPayload.family_name\n : undefined,\n fullName: typeof jwtPayload.name === 'string' ? jwtPayload.name : undefined,\n picture:\n typeof jwtPayload.picture === 'string' ? jwtPayload.picture : undefined,\n role:\n typeof jwtPayload['custom:role'] === 'string'\n ? jwtPayload['custom:role']\n : undefined,\n username:\n typeof jwtPayload.email === 'string' ? jwtPayload.email : jwtPayload.sub,\n supervisor: {\n fullName:\n typeof jwtPayload['custom:supervisor_name'] === 'string'\n ? jwtPayload['custom:supervisor_name']\n : undefined,\n email:\n typeof jwtPayload['custom:supervisor_email'] === 'string'\n ? jwtPayload['custom:supervisor_email']\n : undefined,\n providerUserId:\n typeof jwtPayload['custom:supervisor_user_id'] === 'string'\n ? jwtPayload['custom:supervisor_user_id']\n : undefined,\n },\n phoneNumber:\n typeof jwtPayload['custom:phone_number'] === 'string'\n ? jwtPayload['custom:phone_number']\n : undefined,\n phoneNumberVerified:\n typeof jwtPayload['custom:phone_number_verified'] === 'boolean'\n ? jwtPayload['custom:phone_number_verified']\n : undefined,\n }\n\n if (\n !userProfile.supervisor?.fullName &&\n !userProfile.supervisor?.email &&\n !userProfile.supervisor?.providerUserId\n ) {\n userProfile.supervisor = undefined\n }\n\n if (\n jwtPayload.identities &&\n Array.isArray(jwtPayload.identities) &&\n jwtPayload.identities.length\n ) {\n const { providerType, userId } = jwtPayload.identities[0]\n userProfile.providerType = providerType\n userProfile.providerUserId = userId\n userProfile.isSAMLUser = providerType === 'SAML'\n if (userProfile.isSAMLUser) {\n userProfile.username = jwtPayload.preferred_username || userId\n }\n }\n\n return userProfile\n}\n\n/**\n * A friendly `string` that represents the a [User Profile](#user-profile). Uses\n * first name, last name, full name, email address or username.\n *\n * #### Example\n *\n * ```js\n * const userProfile = userService.parseUserProfile(jwtPayload)\n * const name = userService.getUserFriendlyName(userProfile)\n * if (name) {\n * // Display current user's name\n * }\n * ```\n *\n * @param userProfile\n * @returns\n */\nexport function getUserFriendlyName(\n userProfile: MiscTypes.UserProfile,\n): string {\n if (userProfile.fullName) {\n return userProfile.fullName\n }\n\n if (userProfile.firstName || userProfile.lastName) {\n return [userProfile.firstName, userProfile.lastName].join(' ').trim()\n }\n\n if (userProfile.email) {\n return userProfile.email\n }\n\n if (userProfile.username) {\n return userProfile.username\n }\n\n return userProfile.userId\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": "2.0.1-beta.2",
4
+ "version": "3.0.0-beta.1",
5
5
  "author": "OneBlink <developers@oneblink.io> (https://oneblink.io)",
6
6
  "bugs": {
7
7
  "url": "https://github.com/oneblink/sdk-core-js/issues"
@@ -30,7 +30,7 @@
30
30
  "doc": "docs"
31
31
  },
32
32
  "engines": {
33
- "node": ">=14",
33
+ "node": ">=16",
34
34
  "npm": ">=8"
35
35
  },
36
36
  "files": [