Skip to content

Commit

Permalink
Created getAgeFromBirthdate and getBirthdateFromAge Calculations for …
Browse files Browse the repository at this point in the history
…ICT Contact Followup form
  • Loading branch information
melkam-mekonnen committed Aug 14, 2024
1 parent 1384a66 commit 0f9f594
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/custom-expressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
16 changes: 16 additions & 0 deletions src/forms/index-contact-followup.json
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@
"id": "contactAge",
"questionOptions": {
"rendering": "number",
"calculate": {
"calculateExpression": "getAgeFromBirthdate('dateOfBirth')"
},
"concept": "1532AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
"max": 200,
"min": 0,
Expand All @@ -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",
Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ import {
isSupplementaryFoodVisible,
isTreatmentVisible,
loadFollowupStatus,
getBirthdateFromAge,
getAgeFromBirthdate
} from "./custom-expressions";
import {
createConditionalDashboardGroup,
Expand Down Expand Up @@ -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"),
Expand Down

0 comments on commit 0f9f594

Please sign in to comment.