Skip to content

Commit

Permalink
Merge pull request #85 from oneblink/ON-40994
Browse files Browse the repository at this point in the history
ON-40994 # Added conditional logic for `ADDRESS_DETAILS` predicate type
  • Loading branch information
mymattcarroll authored Jun 14, 2024
2 parents a823f40 + 9b29f1d commit 368977d
Show file tree
Hide file tree
Showing 4 changed files with 215 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions src/conditionalLogicService/evaluateConditionalPredicate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
160 changes: 160 additions & 0 deletions tests/conditionalLogicService/evaluateConditionalPredicate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
})

0 comments on commit 368977d

Please sign in to comment.