Skip to content
This repository has been archived by the owner on Jan 4, 2023. It is now read-only.

Commit

Permalink
feat: generic getResourceTagValues function
Browse files Browse the repository at this point in the history
  • Loading branch information
BPierrick committed Jan 25, 2022
1 parent ee30577 commit c6d7cfb
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion src/features/resources/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
import type { IResourceList } from "@ahryman40k/ts-fhir-types/lib/R4";
import type {
IResourceList,
ICodeableConcept,
} from "@ahryman40k/ts-fhir-types/lib/R4";
import { DateTime } from "luxon";

const getValueFromCodeableConcept = (
codeableConcept?: ICodeableConcept
): string | undefined => {
const firstCodingItem = codeableConcept?.coding?.[0];
return codeableConcept?.text ??
firstCodingItem?.display ??
(firstCodingItem?.system && firstCodingItem?.code)
? `${firstCodingItem?.system}-${firstCodingItem?.code}`
: firstCodingItem?.code;
};

export const getResourceDateOrPeriod = (
resource: IResourceList
): string | { start: string; end: string } | undefined => {
Expand Down Expand Up @@ -127,3 +141,28 @@ export const sortResourcesByDate = (
}
return 0;
};

export const getResourceTagValues = (resource: IResourceList): string[] => {
const tagValues: (string | undefined)[] = [];

switch (resource.resourceType) {
case "Condition":
case "Observation":
tagValues.push(getValueFromCodeableConcept(resource.code));
break;

case "Organization":
tagValues.push(resource.name);
break;
case "Encounter":
resource.type &&
tagValues.push(...resource.type.map(getValueFromCodeableConcept));
tagValues.push(resource.location?.[0]?.location.display);
break;

default:
break;
}

return tagValues.filter((value): value is string => value !== undefined);
};

0 comments on commit c6d7cfb

Please sign in to comment.