Skip to content

Commit

Permalink
Only add location is it does not exist in bundle
Browse files Browse the repository at this point in the history
We were fetching the location information with checking if it already
exists yet, this happens when an event location is a residential
address.

#7494
  • Loading branch information
Siyasanga committed Sep 2, 2024
1 parent b330aa4 commit b1f28b5
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion packages/workflow/src/records/handler/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ import {
getTaskFromSavedBundle,
Encounter,
Location,
findEncounterFromRecord
findEncounterFromRecord,
Saved
} from '@opencrvs/commons/types'
import {
getToken,
Expand Down Expand Up @@ -154,6 +155,18 @@ async function resolveLocationsForEncounter(
return getLocationsById(locationIds)
}

function bundleIncludesLocationResources(record: Saved<Bundle>) {
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<typeof requestSchema>['record'],
event: z.TypeOf<typeof requestSchema>['event'],
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit b1f28b5

Please sign in to comment.