Skip to content

Commit 740295c

Browse files
committed
refactor contact type checker to use boolean pattern
Signed-off-by: apoorvapendse <[email protected]>
1 parent 8e37179 commit 740295c

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -314,10 +314,8 @@ export const byPersonQualifier = (data: unknown): PersonQualifier => {
314314
throw new InvalidArgumentError(`Missing required fields in the parent hierarchy [${JSON.stringify(qualifier)}].`);
315315
}
316316

317-
if (qualifier.type === 'contact' && !hasField(qualifier, { name: 'contact_type', type: 'string' })) {
318-
throw new InvalidArgumentError(`Missing or empty required fields [${JSON.stringify(qualifier)}].`);
319-
} else if (!(qualifier.type === 'person')) {
320-
throw new InvalidArgumentError('Expected `type` to be `person`.');
317+
if (!hasValidContactType(qualifier) && !hasValidLegacyContactType(qualifier, 'person')) {
318+
throw new InvalidArgumentError('Invalid type for contacts.');
321319
}
322320

323321
if (hasBloatedLineage(qualifier)) {
@@ -341,9 +339,7 @@ export const isPersonQualifier = (data: unknown): data is PersonQualifier => {
341339
return false;
342340
}
343341

344-
if (data.type === 'contact' && !hasField(data, { name: 'contact_type', type: 'string' })) {
345-
return false;
346-
} else if (!(data.type === 'person')) {
342+
if (!hasValidContactType(data) && !hasValidLegacyContactType(data, 'person')) {
347343
return false;
348344
}
349345

@@ -364,3 +360,16 @@ const hasBloatedLineage = ( data: Record<string, unknown> ): boolean => {
364360
}
365361
return true;
366362
};
363+
364+
/** @internal */
365+
const hasValidContactType = ( data: Record<string, unknown> ): boolean => {
366+
if (data.type === 'contact' && hasField(data, { name: 'contact_type', type: 'string' })) {
367+
return true;
368+
}
369+
return false;
370+
};
371+
372+
/** @internal */
373+
const hasValidLegacyContactType = ( data: Record<string, unknown>, type:string): boolean => {
374+
return data.type === type;
375+
};

0 commit comments

Comments
 (0)