Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(v1.7.0): Modify location builder so it does not edit health facilities #7671

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions packages/commons/src/fhir/location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
131 changes: 130 additions & 1 deletion packages/commons/src/fhir/transformers/fhir-builders.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -445,3 +452,125 @@ describe('taskBundleWithExtension()', () => {
)
})
})

describe('updateFhirBundle', () => {
describe('when the type of a location is changed from HEALTH_FACILITY to another type', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌

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<Encounter>,
{
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<Encounter>

const encounterLocationreference =
encounter.resource.location![0].location?.reference

const originalFacility = updatedBundle.entry.find(
(e) => e.resource.id === facilityId
)! as BundleEntry<Location>

const newLocation = updatedBundle.entry.find(
(e) =>
e.resource.resourceType === 'Location' && e.resource.id !== facilityId
) as BundleEntry<Location>

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()
})
})
})
31 changes: 22 additions & 9 deletions packages/commons/src/fhir/transformers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ import {
findEntryFromBundle
} from '..'

import { CompositionSectionTitleByCode, PartialBy } from '../../types'
import {
CompositionSectionTitleByCode,
isHealthFacility,
PartialBy
} from '../../types'
import {
DOWNLOADED_EXTENSION_URL,
EVENT_TYPE,
Expand Down Expand Up @@ -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]) {
Expand All @@ -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
}

Expand Down
Loading