From 0d4e2cdd5575212b212635d70807f4c890933ac3 Mon Sep 17 00:00:00 2001 From: Emmanuel Evance Date: Mon, 7 Oct 2024 19:47:37 +0300 Subject: [PATCH] Filter patient gender observation to latest only (#46) * update gist url * filter patient latest gender update * revert gist url changes --- workflows/wf2/6-create-events.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/workflows/wf2/6-create-events.js b/workflows/wf2/6-create-events.js index 4135bc8..6652cfe 100644 --- a/workflows/wf2/6-create-events.js +++ b/workflows/wf2/6-create-events.js @@ -117,15 +117,23 @@ each( ); fn(state => { - state.genderUpdated = state.encounters.reduce((acc, e) => { + const latestGenderUpdate = state.encounters.reduce((acc, e) => { const answer = e.obs.find( o => o.concept.uuid === 'ec42d68d-3e23-43de-b8c5-a03bb538e7c7' ); if (answer) { - acc.push(answer); + const personUuid = answer.person.uuid; + if ( + !acc[personUuid] || + new Date(answer.obsDatetime) > new Date(acc[personUuid].obsDatetime) + ) { + acc[personUuid] = answer; + } } return acc; - }, []); + }, {}); + + state.genderUpdated = Object.values(latestGenderUpdate); return state; });