Skip to content

Commit b41451d

Browse files
committed
refactor lineage check using hasInvalidContactLineageForField
Signed-off-by: Apoorva Pendse <[email protected]>
1 parent 459e415 commit b41451d

File tree

1 file changed

+31
-9
lines changed

1 file changed

+31
-9
lines changed

shared-libs/cht-datasource/src/qualifier.ts

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,8 @@ export const isPersonQualifier = (data: unknown): data is PersonQualifier => {
338338
if (!checkContactQualifierFields(data)) {
339339
return false;
340340
}
341-
341+
342+
// `parent` must be present for person, so cannot use `hasInvalidContactLineageForField`
342343
if (!hasField(data, { name: 'parent', type: 'object' }) || !isNormalizedParent(data.parent)) {
343344
return false;
344345
}
@@ -437,23 +438,44 @@ const checkFieldLineageAndThrowError = (data: Record<string, unknown>, field?:st
437438
}
438439
};
439440

441+
/** @internal*/
442+
const hasInvalidContactLineageForField = (data: Record<string, unknown>, field?:string) => {
443+
let hierarchyRoot = data;
444+
let normalized_parent = data.parent;
445+
446+
if (field && hasField(data, {name: field, type: 'object'})) {
447+
hierarchyRoot = data[field] as unknown as Record<string, unknown>;
448+
normalized_parent = data[field];
449+
}
450+
451+
// `parent` is optional, so this lineage is valid
452+
if (!hasField(hierarchyRoot, {type: 'object', name: 'parent'})){
453+
return false;
454+
}
455+
456+
if (!isNormalizedParent(normalized_parent)) {
457+
// eslint-disable-next-line max-len
458+
return true;
459+
}
460+
if (hasBloatedLineage(hierarchyRoot)) {
461+
// eslint-disable-next-line max-len
462+
return true;
463+
}
464+
};
465+
440466

441467
/** @internal*/
442468
export const isPlaceQualifier = (data:unknown) : data is PlaceQualifier => {
443469
if (!checkContactQualifierFields(data)) {
444470
return false;
445471
}
446472

447-
if (hasField(data, { name: 'parent', type: 'object' })) {
448-
if (!isNormalizedParent(data.parent) || hasBloatedLineage(data)){
449-
return false;
450-
}
473+
if (hasInvalidContactLineageForField(data)){
474+
return false;
451475
}
452476

453-
if (hasField(data, {name: 'contact', type: 'object'})) {
454-
if (!isNormalizedParent(data.contact) || hasBloatedLineage(data.contact)){
455-
return false;
456-
}
477+
if (hasInvalidContactLineageForField(data, 'contact')){
478+
return false;
457479
}
458480

459481
return hasValidContactType(data) || hasValidLegacyContactType(data, 'place');

0 commit comments

Comments
 (0)