From 6d7693025188021bfed0cb4575d5e7c382be775d Mon Sep 17 00:00:00 2001 From: Siyasanga Date: Mon, 2 Sep 2024 16:03:41 +0200 Subject: [PATCH] Only add location is it does not exist in bundle We were fetching the location information with checking if it already exists yet, this happens when an event location is a residential address. https://github.com/opencrvs/opencrvs-core/issues/7494 --- .../workflow/src/records/handler/create.ts | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/packages/workflow/src/records/handler/create.ts b/packages/workflow/src/records/handler/create.ts index a754ff8aeaf..b9891e85ea9 100644 --- a/packages/workflow/src/records/handler/create.ts +++ b/packages/workflow/src/records/handler/create.ts @@ -33,7 +33,8 @@ import { getTaskFromSavedBundle, Encounter, Location, - findEncounterFromRecord + findEncounterFromRecord, + Saved } from '@opencrvs/commons/types' import { getToken, @@ -154,6 +155,18 @@ async function resolveLocationsForEncounter( return getLocationsById(locationIds) } +function bundleIncludesLocationResources(record: Saved) { + const encounter = findEncounterFromRecord(record) + const locationIds = + encounter?.location?.map( + ({ location }) => location.reference.split('/')[1] + ) || [] + + return record.entry + .filter(({ resource }) => resource.resourceType == 'Location') + .every(({ resource }) => locationIds?.includes(resource.id)) +} + async function createRecord( recordDetails: z.TypeOf['record'], event: z.TypeOf['event'], @@ -215,6 +228,11 @@ async function createRecord( if (encounter == null) { return mergedBundle } + + if (bundleIncludesLocationResources(record)) { + return mergedBundle + } + const locationResourcesBundle = await resolveLocationsForEncounter(encounter) if (locationResourcesBundle == null) {