From 16759a9c43dff8ba3d002ffb7b71e6ad7282afdb Mon Sep 17 00:00:00 2001 From: Barry Dwyer Date: Thu, 26 Sep 2024 12:13:45 +0200 Subject: [PATCH 1/2] Modify location builder so it does not edit health facilities Health facilities were being edited as private homes on corrections which should not be allowed as there are static --- packages/commons/src/fhir/location.ts | 4 + .../fhir/transformers/fhir-builders.test.ts | 131 +++++++++++++++++- .../commons/src/fhir/transformers/utils.ts | 31 +++-- 3 files changed, 156 insertions(+), 10 deletions(-) diff --git a/packages/commons/src/fhir/location.ts b/packages/commons/src/fhir/location.ts index df5129dc5f..d2e1cd52bf 100644 --- a/packages/commons/src/fhir/location.ts +++ b/packages/commons/src/fhir/location.ts @@ -120,3 +120,7 @@ export function isHealthFacility( ): location is HealthFacility { return location.type?.coding?.[0].code === 'HEALTH_FACILITY' } + +export function getLocationType(location: Location): string | undefined { + return location.type?.coding?.[0].code +} diff --git a/packages/commons/src/fhir/transformers/fhir-builders.test.ts b/packages/commons/src/fhir/transformers/fhir-builders.test.ts index 27fedfeb8a..ed4fe1ffe1 100644 --- a/packages/commons/src/fhir/transformers/fhir-builders.test.ts +++ b/packages/commons/src/fhir/transformers/fhir-builders.test.ts @@ -21,12 +21,19 @@ import { findExtension, isTask, taskBundleWithExtension, - updateFHIRTaskBundle + updateFHIRTaskBundle, + updateFHIRBundle, + Bundle, + Location, + Encounter, + BundleEntry, + getLocationType } from '..' import * as fetchMock from 'jest-fetch-mock' import { DeathRegistration } from './input' +import { UUID } from 'src/uuid' const fetch = fetchMock as fetchMock.FetchMock @@ -445,3 +452,125 @@ describe('taskBundleWithExtension()', () => { ) }) }) + +describe('updateFhirBundle', () => { + describe('when the type of a location is changed from HEALTH_FACILITY to another type', () => { + const facilityId = 'TestFacility1' + const encounterId = 'Encounter1' + + const bundle = { + resourceType: 'Bundle', + type: 'document', + entry: [ + { + resource: { + resourceType: 'Composition', + title: 'Birth Declaration', + section: [ + { + title: 'Birth encounter', + code: { + coding: [ + { + system: 'http://opencrvs.org/specs/sections', + code: 'birth-encounter' + } + ], + text: 'Birth encounter' + }, + entry: [ + { + reference: `Encounter/${encounterId}` + } + ] + } + ], + id: 'Composition1' + } + }, + { + resource: { + resourceType: 'Encounter', + status: 'finished', + id: encounterId as UUID, + location: [ + { + location: { + reference: `Location/${facilityId}` as ResourceIdentifier + } + } + ] + } + } as BundleEntry, + { + resource: { + resourceType: 'Location', + name: 'My Test Facility', + type: { + coding: [ + { + system: 'http://opencrvs.org/specs/location-type', + code: 'HEALTH_FACILITY' + } + ] + }, + id: facilityId + } + } + ] + } as Bundle + + const updatedBundle = updateFHIRBundle( + bundle, + { + eventLocation: { + address: { + country: 'FAR', + state: 'State1', + district: 'District1', + city: 'City1', + postalCode: 'Code1', + line: ['line1', 'line2', 'line3', 'line4'], + partOf: 'Dictrict1' + }, + type: 'PRIVATE_HOME' + } + }, + 'BIRTH' as EVENT_TYPE + ) + + const encounter = updatedBundle.entry.find( + (e) => e.resource.resourceType === 'Encounter' + ) as BundleEntry + + const encounterLocationreference = + encounter.resource.location![0].location?.reference + + const originalFacility = updatedBundle.entry.find( + (e) => e.resource.id === facilityId + )! as BundleEntry + + const newLocation = updatedBundle.entry.find( + (e) => + e.resource.resourceType === 'Location' && e.resource.id !== facilityId + ) as BundleEntry + + const locations = updatedBundle.entry.filter( + (entry) => entry.resource.resourceType === 'Location' + ) + + it('should create a new location', () => { + expect(locations.length).toEqual(2) + expect(encounterLocationreference).toEqual(newLocation.fullUrl) + expect(getLocationType(newLocation.resource)).toEqual('PRIVATE_HOME') + expect(newLocation.resource.address).not.toBeUndefined() + }) + + it('should not edit the original facility', () => { + expect(getLocationType(originalFacility.resource)).toEqual( + 'HEALTH_FACILITY' + ) + expect(originalFacility.resource.address).toBeUndefined() + }) + }) +}) diff --git a/packages/commons/src/fhir/transformers/utils.ts b/packages/commons/src/fhir/transformers/utils.ts index bef28b922a..dcc3b4379e 100644 --- a/packages/commons/src/fhir/transformers/utils.ts +++ b/packages/commons/src/fhir/transformers/utils.ts @@ -65,7 +65,11 @@ import { findEntryFromBundle } from '..' -import { CompositionSectionTitleByCode, PartialBy } from '../../types' +import { + CompositionSectionTitleByCode, + isHealthFacility, + PartialBy +} from '../../types' import { DOWNLOADED_EXTENSION_URL, EVENT_TYPE, @@ -333,14 +337,7 @@ export function selectOrCreateLocationRefResource( if (!encounter.location) { // create location - const locationRef = getUUID() - const locationEntry = createLocationResource(locationRef) - fhirBundle.entry.push(locationEntry) - encounter.location = [] - encounter.location.push({ - location: { reference: `urn:uuid:${locationRef}` as URNReference } - }) - return locationEntry.resource + return createNewLocation(fhirBundle, encounter) } if (!encounter.location || !encounter.location[0]) { @@ -358,6 +355,22 @@ export function selectOrCreateLocationRefResource( 'Location referenced from encounter section not found in FHIR bundle' ) } + + if (isHealthFacility(locationEntry.resource)) { + return createNewLocation(fhirBundle, encounter) + } + + return locationEntry.resource +} + +function createNewLocation(fhirBundle: Bundle, encounter: Encounter): Location { + const locationRef = getUUID() + const locationEntry = createLocationResource(locationRef) + fhirBundle.entry.push(locationEntry) + encounter.location = [] + encounter.location.push({ + location: { reference: `urn:uuid:${locationRef}` as URNReference } + }) return locationEntry.resource } From a1f11e2f86e9797b60985f8aa33d9ce543e2499c Mon Sep 17 00:00:00 2001 From: Barry Dwyer Date: Thu, 26 Sep 2024 12:21:34 +0200 Subject: [PATCH 2/2] Update changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 92896977b6..13da88c82e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,7 +27,7 @@ ## Bug fixes -- TBC +- Fix health facilities missing from dropdown after correcting a record address [#7528](https://github.com/opencrvs/opencrvs-core/issues/7528) ## 1.6.0 Release candidate