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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -28,15 +28,16 @@ const createLegalName = ({ familyName, givenName, otherGivenName, }) => {
28
28
  * @returns
29
29
  */
30
30
  const getBusinessNameFromABNRecord = (abnRecord) => {
31
+ var _a, _b;
31
32
  if (abnRecord.entityType.entityTypeCode === 'IND' && abnRecord.legalName) {
32
33
  const legalName = createLegalName(abnRecord.legalName);
33
34
  if (legalName)
34
35
  return legalName.trim();
35
36
  }
36
- if (abnRecord.mainName?.organisationName) {
37
+ if ((_a = abnRecord.mainName) === null || _a === void 0 ? void 0 : _a.organisationName) {
37
38
  return abnRecord.mainName.organisationName;
38
39
  }
39
- if (abnRecord.mainTradingName?.organisationName) {
40
+ if ((_b = abnRecord.mainTradingName) === null || _b === void 0 ? void 0 : _b.organisationName) {
40
41
  return abnRecord.mainTradingName.organisationName;
41
42
  }
42
43
  if (abnRecord.legalName) {
@@ -83,7 +84,7 @@ const getABNNumberFromABNRecord = (abnRecord) => {
83
84
  return;
84
85
  if (Array.isArray(abnRecord.ABN)) {
85
86
  const relevantABN = abnRecord.ABN.find((abn) => abn.isCurrentIndicator === 'Y');
86
- return relevantABN?.identifierValue;
87
+ return relevantABN === null || relevantABN === void 0 ? void 0 : relevantABN.identifierValue;
87
88
  }
88
89
  return abnRecord.ABN.identifierValue;
89
90
  };
@@ -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,SA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I,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"]}
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;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M,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,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"]}
@@ -67,7 +67,8 @@ const conditionallyShowOptionByPredicate = (formElementsCtrl, predicate, element
67
67
  }
68
68
  }
69
69
  const everyOptionIsShowing = predicate.optionIds.every((id) => {
70
- const predicateOption = optionsPredicateElement.options?.find((o) => o.id === id);
70
+ var _a;
71
+ const predicateOption = (_a = optionsPredicateElement.options) === null || _a === void 0 ? void 0 : _a.find((o) => o.id === id);
71
72
  if (!predicateOption)
72
73
  return 'HIDE';
73
74
  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;QACp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;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;QACA,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I,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,7 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  function evaluateConditionalOptionsPredicate({ predicate, predicateElement, predicateValue: value, }) {
4
4
  return predicate.optionIds.some((optionId) => {
5
- const option = predicateElement.options?.find((o) => o.id === optionId);
5
+ var _a;
6
+ const option = (_a = predicateElement.options) === null || _a === void 0 ? void 0 : _a.find((o) => o.id === optionId);
6
7
  if (option) {
7
8
  if (Array.isArray(value)) {
8
9
  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,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"]}
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"]}
@@ -203,7 +203,7 @@ exports.ElementWYSIWYGRegex = ElementWYSIWYGRegex;
203
203
  const matchElementsTagRegex = (data, matchHandler) => {
204
204
  let matches;
205
205
  while ((matches = ElementWYSIWYGRegex.exec(data)) !== null) {
206
- if (matches?.length < 3)
206
+ if ((matches === null || matches === void 0 ? void 0 : matches.length) < 3)
207
207
  continue;
208
208
  const elementName = matches[2];
209
209
  matchHandler({ elementName, elementMatch: matches[0] });
@@ -1 +1 @@
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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,CAAC;gBACX,iBAAiB,CAAC,IAAI,CAAC,GAAG,mBAAmB,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAA;aACjE;SACF;QACD,OAAO,iBAAiB,CAAA;IAC1B,CAAC,EACD,EAAE,CACH,CAAA;AACH,CAAC;AAkDC,kDAAmB;AAhDrB;;;GAGG;AACH,MAAM,mBAAmB,GAAG,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"]}
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,SAGY,EACZ,iBAA0C,EAAE;IAE5C,KAAK,MAAM,OAAO,IAAI,QAAQ,EAAE;QAC9B,IAAI,SAAS,CAAC,OAAO,EAAE,cAAc,CAAC,EAAE;YACtC,OAAO,OAAO,CAAA;SACf;QAED,IACE,CAAC,OAAO,CAAC,IAAI,KAAK,eAAe;YAC/B,OAAO,CAAC,IAAI,KAAK,MAAM;YACvB,OAAO,CAAC,I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AI,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,MAAM,IAAG,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"]}
@@ -83,6 +83,7 @@ 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;
86
87
  const formElement = (0, formElementsService_1.findFormElement)(formElements, (element) => element.type !== 'page' &&
87
88
  element.type !== 'section' &&
88
89
  element.name === propertyName);
@@ -90,7 +91,7 @@ function getElementSubmissionValue({ propertyName, submission, formElements, for
90
91
  if (unknown === undefined || unknown === null) {
91
92
  return undefined;
92
93
  }
93
- switch (formElement?.type) {
94
+ switch (formElement === null || formElement === void 0 ? void 0 : formElement.type) {
94
95
  case 'datetime': {
95
96
  const value = unknown;
96
97
  return formatDateTime(value);
@@ -106,13 +107,14 @@ function getElementSubmissionValue({ propertyName, submission, formElements, for
106
107
  case 'radio':
107
108
  case 'autocomplete': {
108
109
  const value = unknown;
109
- const option = formElement.options?.find((opt) => opt.value === value);
110
- return option?.label || value;
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;
111
112
  }
112
113
  case 'checkboxes': {
113
114
  const value = unknown;
114
115
  const selectedOptionLabels = value.reduce((labels, selectedOption) => {
115
- const foundOption = formElement.options?.find((o) => o.value === selectedOption);
116
+ var _a;
117
+ const foundOption = (_a = formElement.options) === null || _a === void 0 ? void 0 : _a.find((o) => o.value === selectedOption);
116
118
  if (foundOption)
117
119
  labels.push(foundOption.label);
118
120
  return labels;
@@ -124,14 +126,15 @@ function getElementSubmissionValue({ propertyName, submission, formElements, for
124
126
  const option = (formElement.options || []).find((option) => option.value === value.value);
125
127
  return {
126
128
  ...value,
127
- value: option?.label || value.value,
129
+ value: (option === null || option === void 0 ? void 0 : option.label) || value.value,
128
130
  };
129
131
  }
130
132
  case 'select': {
131
133
  if (formElement.multi) {
132
134
  const value = unknown;
133
135
  const selectedOptionLabels = value.reduce((labels, selectedOption) => {
134
- const foundOption = formElement.options?.find((o) => o.value === selectedOption);
136
+ var _a;
137
+ const foundOption = (_a = formElement.options) === null || _a === void 0 ? void 0 : _a.find((o) => o.value === selectedOption);
135
138
  if (foundOption)
136
139
  labels.push(foundOption.label);
137
140
  return labels;
@@ -140,8 +143,8 @@ function getElementSubmissionValue({ propertyName, submission, formElements, for
140
143
  }
141
144
  else {
142
145
  const value = unknown;
143
- const option = formElement.options?.find((opt) => opt.value === value);
144
- return option?.label || '';
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) || '';
145
148
  }
146
149
  }
147
150
  case 'boolean': {
@@ -165,17 +168,17 @@ function getElementSubmissionValue({ propertyName, submission, formElements, for
165
168
  case 'pointAddress':
166
169
  case 'geoscapeAddress': {
167
170
  const value = unknown;
168
- return value?.addressDetails?.formattedAddress || value?.addressId;
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);
169
172
  }
170
173
  case 'civicaStreetName': {
171
174
  const value = unknown;
172
- return value?.formattedStreet;
175
+ return value === null || value === void 0 ? void 0 : value.formattedStreet;
173
176
  }
174
177
  case 'civicaNameRecord': {
175
178
  const value = unknown;
176
- return ([value?.title, value?.givenName1, value?.familyName]
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]
177
180
  .filter((t) => t)
178
- .join(' ') || value?.emailAddress);
181
+ .join(' ') || (value === null || value === void 0 ? void 0 : value.emailAddress));
179
182
  }
180
183
  case 'abn': {
181
184
  const value = unknown;
@@ -239,7 +242,8 @@ function replaceInjectablesWithElementValues(text, options) {
239
242
  // User based values should be replaced with an empty string if
240
243
  // there is no user profile or if the property does not have a value
241
244
  keys.forEach((key) => {
242
- text = text.replaceAll(`{USER:${key}}`, options.userProfile?.[key]?.toString() || '');
245
+ var _a, _b;
246
+ text = text.replaceAll(`{USER:${key}}`, ((_b = (_a = options.userProfile) === null || _a === void 0 ? void 0 : _a[key]) === null || _b === void 0 ? void 0 : _b.toString()) || '');
243
247
  });
244
248
  const matchesElement = text.match(formElementsService_1.ElementWYSIWYGRegex);
245
249
  if (!matchesElement) {
@@ -1 +1 @@
1
- {"version":3,"file":"replaceCustomValues.js","sourceRoot":"","sources":["../src/replaceCustomValues.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ink/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"]}
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,Q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,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,6 +20,7 @@ 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;
23
24
  if (typeof data !== 'object' || !data) {
24
25
  return;
25
26
  }
@@ -66,9 +67,9 @@ function parseUserProfile(data) {
66
67
  ? jwtPayload['custom:phone_number_verified']
67
68
  : undefined,
68
69
  };
69
- if (!userProfile.supervisor?.fullName &&
70
- !userProfile.supervisor?.email &&
71
- !userProfile.supervisor?.providerUserId) {
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)) {
72
73
  userProfile.supervisor = undefined;
73
74
  }
74
75
  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,U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O,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,IAA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AG,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"]}
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.3",
4
+ "version": "3.0.0-beta.2",
5
5
  "author": "OneBlink <developers@oneblink.io> (https://oneblink.io)",
6
6
  "bugs": {
7
7
  "url": "https://github.com/oneblink/sdk-core-js/issues"