From 9b29f1d519b1a2473f6691c5733775f27917889e Mon Sep 17 00:00:00 2001 From: jdawg093 <49189518+jdawg093@users.noreply.github.com> Date: Fri, 14 Jun 2024 15:17:29 +1000 Subject: [PATCH] ON-40994 # Added conditional logic for `ADDRESS_DETAILS` predicate type --- CHANGELOG.md | 4 + package-lock.json | 4 +- .../evaluateConditionalPredicate.ts | 49 ++++++ .../evaluateConditionalPredicate.test.ts | 160 ++++++++++++++++++ 4 files changed, 215 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fefa252..1d1fcd8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +### Added + +- `ADDRESS_DETAILS` predicate type added to `conditionalLogicService.evaluateConditionalPredicate` + ## [6.2.0] - 2024-06-04 ### Added diff --git a/package-lock.json b/package-lock.json index fb3d343..c64114d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1817,7 +1817,7 @@ }, "node_modules/@oneblink/types": { "version": "1.0.0", - "resolved": "git+ssh://git@github.com/oneblink/types.git#fc1a49c04c047c3553552300a9fdf543385fc52b", + "resolved": "git+ssh://git@github.com/oneblink/types.git#2f118ee28c13a41a4213e5d0da25b7a3e75b4d50", "dev": true, "license": "GPL-3.0-only", "dependencies": { @@ -10794,7 +10794,7 @@ } }, "@oneblink/types": { - "version": "git+ssh://git@github.com/oneblink/types.git#fc1a49c04c047c3553552300a9fdf543385fc52b", + "version": "git+ssh://git@github.com/oneblink/types.git#2f118ee28c13a41a4213e5d0da25b7a3e75b4d50", "dev": true, "from": "@oneblink/types@github:oneblink/types", "requires": { diff --git a/src/conditionalLogicService/evaluateConditionalPredicate.ts b/src/conditionalLogicService/evaluateConditionalPredicate.ts index 73dca26..12a7507 100644 --- a/src/conditionalLogicService/evaluateConditionalPredicate.ts +++ b/src/conditionalLogicService/evaluateConditionalPredicate.ts @@ -167,6 +167,55 @@ export default function evaluateConditionalPredicate({ } } return + case 'ADDRESS_PROPERTY': { + // Only element that should work as predicate element should be Point or Geoscape. + if ( + !( + predicateElement.type === 'pointAddress' || + predicateElement.type === 'geoscapeAddress' + ) + ) { + return + } + if (predicateValue && typeof predicateValue === 'object') { + if (predicate.definition.property === 'STATE_EQUALITY') { + // Validate that it has the properties we want, in case submission data + // is incorrect but has the right element reference. + if ( + 'addressDetails' in predicateValue && + predicateValue.addressDetails && + typeof predicateValue.addressDetails === 'object' && + 'stateTerritory' in predicateValue.addressDetails && + typeof predicateValue.addressDetails.stateTerritory === 'string' + ) { + const result = + predicateValue.addressDetails.stateTerritory === + predicate.definition.value + if (result) { + return predicateElement + } + } + return + } + // If the property isn't State Equality, we are checking Physical addresses. This only exists for Point. + if (predicateElement.type === 'pointAddress') { + // If the value is true, we only want to return the element for PO Boxes. If it's false, then we only want + // to return element for non mail address addresses. + if ( + 'dataset' in predicateValue && + typeof predicateValue.dataset === 'string' + ) { + const result = predicate.definition.value + ? predicateValue.dataset === 'mailAddress' + : predicateValue.dataset === 'GNAF' + if (result) { + return predicateElement + } + } + } + } + return + } case 'OPTIONS': default: { const optionsPredicateElement = diff --git a/tests/conditionalLogicService/evaluateConditionalPredicate.test.ts b/tests/conditionalLogicService/evaluateConditionalPredicate.test.ts index 27be5dd..89bddf6 100644 --- a/tests/conditionalLogicService/evaluateConditionalPredicate.test.ts +++ b/tests/conditionalLogicService/evaluateConditionalPredicate.test.ts @@ -242,4 +242,164 @@ describe('evaluateConditionalPredicate', () => { expect(isShown).toBe(parentFormFormElement) }) + + describe('Point and Geoscape Element conditional predicates', () => { + const conditionalPredicatePoint: ConditionTypes.ConditionalPredicate = { + elementId: 'predicatePoint', + type: 'ADDRESS_PROPERTY', + definition: { property: 'IS_PO_BOX_ADDRESS', value: true }, + } + + const conditionalPredicateGeoscape: ConditionTypes.ConditionalPredicate = { + elementId: 'predicateGeoscape', + type: 'ADDRESS_PROPERTY', + definition: { property: 'STATE_EQUALITY', value: 'NSW' }, + } + + const predicatePoint: FormTypes.PointAddressElement = { + id: 'predicatePoint', + name: 'predicatePoint', + label: 'predicatePoint', + type: 'pointAddress', + environmentId: '1', + required: false, + conditionallyShow: false, + isDataLookup: false, + isElementLookup: false, + } + + const predicateGeoscape: FormTypes.GeoscapeAddressElement = { + id: 'predicateGeoscape', + name: 'predicateGeoscape', + label: 'predicateGeoscape', + type: 'geoscapeAddress', + required: false, + conditionallyShow: false, + isDataLookup: false, + isElementLookup: false, + } + + test('should show element using NSW Point PO Box', () => { + const shownElement: FormTypes.FormElement = { + id: 'shownNumber', + name: 'shownNumber', + label: 'shownNumber', + type: 'number', + required: false, + isSlider: false, + conditionallyShow: true, + conditionallyShowPredicates: [conditionalPredicatePoint], + isDataLookup: false, + isElementLookup: false, + } + const isShown = evaluateConditionalPredicate({ + predicate: conditionalPredicatePoint, + formElementsCtrl: { + flattenedElements: [predicatePoint, shownElement], + model: { + predicatePoint: { + dataset: 'mailAddress', + }, + }, + }, + }) + expect(isShown).toBe(predicatePoint) + }) + test('should show element using NSW Point physical address', () => { + const conditionalPredicatePointNoPO: ConditionTypes.ConditionalPredicate = + { + elementId: 'predicatePoint', + type: 'ADDRESS_PROPERTY', + definition: { property: 'IS_PO_BOX_ADDRESS', value: false }, + } + + const shownElement: FormTypes.FormElement = { + id: 'shownNumber', + name: 'shownNumber', + label: 'shownNumber', + type: 'number', + required: false, + isSlider: false, + conditionallyShow: true, + conditionallyShowPredicates: [conditionalPredicatePointNoPO], + isDataLookup: false, + isElementLookup: false, + } + const isShown = evaluateConditionalPredicate({ + predicate: conditionalPredicatePointNoPO, + formElementsCtrl: { + flattenedElements: [predicatePoint, shownElement], + model: { + predicatePoint: { + dataset: 'GNAF', + }, + }, + }, + }) + expect(isShown).toBe(predicatePoint) + }) + test('should show element using NSW Point with states', () => { + const conditionalPredicatePointStates: ConditionTypes.ConditionalPredicate = + { + elementId: 'predicatePoint', + type: 'ADDRESS_PROPERTY', + definition: { property: 'STATE_EQUALITY', value: 'NSW' }, + } + + const shownElement: FormTypes.FormElement = { + id: 'shownNumber', + name: 'shownNumber', + label: 'shownNumber', + type: 'number', + required: false, + isSlider: false, + conditionallyShow: true, + conditionallyShowPredicates: [conditionalPredicatePointStates], + isDataLookup: false, + isElementLookup: false, + } + const isShown = evaluateConditionalPredicate({ + predicate: conditionalPredicatePointStates, + formElementsCtrl: { + flattenedElements: [predicatePoint, shownElement], + model: { + predicatePoint: { + addressDetails: { + stateTerritory: 'NSW', + }, + }, + }, + }, + }) + expect(isShown).toBe(predicatePoint) + }) + test('should show element using Geoscape with States', () => { + const shownElement: FormTypes.FormElement = { + id: 'shownNumber', + name: 'shownNumber', + label: 'shownNumber', + type: 'number', + required: false, + isSlider: false, + conditionallyShow: true, + conditionallyShowPredicates: [conditionalPredicateGeoscape], + isDataLookup: false, + isElementLookup: false, + } + const isShown = evaluateConditionalPredicate({ + predicate: conditionalPredicateGeoscape, + formElementsCtrl: { + flattenedElements: [predicateGeoscape, shownElement], + model: { + predicateGeoscape: { + addressDetails: { + stateTerritory: 'NSW', + }, + }, + }, + }, + }) + expect(isShown).toBe(predicateGeoscape) + }) + }) })