Skip to content

Commit 15858d1

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

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
@@ -331,7 +331,8 @@ export const isPersonQualifier = (data: unknown): data is PersonQualifier => {
331331
if (!checkContactQualifierFields(data)) {
332332
return false;
333333
}
334-
334+
335+
// `parent` must be present for person, so cannot use `hasInvalidContactLineageForField`
335336
if (!hasField(data, { name: 'parent', type: 'object' }) || !isNormalizedParent(data.parent)) {
336337
return false;
337338
}
@@ -430,23 +431,44 @@ const checkFieldLineageAndThrowError = (data: Record<string, unknown>, field?:st
430431
}
431432
};
432433

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

434460
/** @internal*/
435461
export const isPlaceQualifier = (data:unknown) : data is PlaceQualifier => {
436462
if (!checkContactQualifierFields(data)) {
437463
return false;
438464
}
439465

440-
if (hasField(data, { name: 'parent', type: 'object' })) {
441-
if (!isNormalizedParent(data.parent) || hasBloatedLineage(data)){
442-
return false;
443-
}
466+
if (hasInvalidContactLineageForField(data)){
467+
return false;
444468
}
445469

446-
if (hasField(data, {name: 'contact', type: 'object'})) {
447-
if (!isNormalizedParent(data.contact) || hasBloatedLineage(data.contact)){
448-
return false;
449-
}
470+
if (hasInvalidContactLineageForField(data, 'contact')){
471+
return false;
450472
}
451473

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

0 commit comments

Comments
 (0)