diff --git a/src/custom-expressions.ts b/src/custom-expressions.ts index 257a4d5..4de5ffb 100644 --- a/src/custom-expressions.ts +++ b/src/custom-expressions.ts @@ -440,3 +440,43 @@ export async function loadFollowupStatus(patient) { } return status?.valueCodeableConcept?.coding[0]?.code; } + +export async function getAgeFromBirthdate(dateOfBirth) { + if (dateOfBirth) { + const birthdate = new Date(dateOfBirth); + const today = new Date(); + let age = today.getFullYear() - birthdate.getFullYear(); + const monthDifference = today.getMonth() - birthdate.getMonth(); + if ( + monthDifference < 0 || + (monthDifference === 0 && today.getDate() < birthdate.getDate()) + ) { + age--; + } + return age; + } else { + return null; + } +} + +export async function getBirthdateFromAge(contactAge) { + if (contactAge) { + const today = new Date(); + const birthYear = today.getFullYear() - contactAge; + const birthdate = new Date(today.setFullYear(birthYear)); + const options: Intl.DateTimeFormatOptions = { + weekday: "short", + year: "numeric", + month: "short", + day: "2-digit", + timeZoneName: "short", + }; + return ( + birthdate.toLocaleDateString("en-US", options) + + " " + + birthdate.toTimeString().split(" ")[0] + ); + } else { + return null; + } +} diff --git a/src/forms/index-contact-followup.json b/src/forms/index-contact-followup.json index 3a007d9..a22d923 100644 --- a/src/forms/index-contact-followup.json +++ b/src/forms/index-contact-followup.json @@ -169,6 +169,9 @@ "id": "contactAge", "questionOptions": { "rendering": "number", + "calculate": { + "calculateExpression": "getAgeFromBirthdate('dateOfBirth')" + }, "concept": "1532AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", "max": 200, "min": 0, @@ -181,6 +184,19 @@ ] } }, + { + "label": "Date of Birth", + "type": "obs", + "required": false, + "id": "dateOfBirth", + "questionOptions": { + "rendering": "date", + "calculate": { + "calculateExpression": "getBirthdateFromAge('contactAge')" + }, + "concept": "e27ae70c-6131-4004-9da8-841619e19480" + } + }, { "label": "Sex", "type": "obs", diff --git a/src/index.ts b/src/index.ts index 5069365..29d253a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -43,6 +43,8 @@ import { isSupplementaryFoodVisible, isTreatmentVisible, loadFollowupStatus, + getBirthdateFromAge, + getAgeFromBirthdate } from "./custom-expressions"; import { createConditionalDashboardGroup, @@ -104,6 +106,8 @@ export function startupApp() { isSupplementaryFoodVisible ); registerExpressionHelper("loadFollowupStatus", loadFollowupStatus); + registerExpressionHelper("getBirthdateFromAge", getBirthdateFromAge); + registerExpressionHelper("getAgeFromBirthdate", getAgeFromBirthdate); // registerControl({ // name: "eth-date", // load: () => import("./components/controls/date/ethiohri-date.component"),