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: add the missing location details to a newly created declaration which was causing advanced search to break #7551

Merged
merged 2 commits into from
Sep 3, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
## Bug fixes

- On slow connections or in rare corner cases, it was possible that the same record got saved to the database twice. This was caused by a bug in how the unique technical identifier we generate were stored as FHIR. The backend now ensures every record is submitted only once. [#7477](https://github.com/opencrvs/opencrvs-core/issues/7477)
- When a declaration(birth/death) is created the event location information was not being parsed to ElasticSearch which caused the Advanced search feature to not work when searching for records by event location.[7494](https://github.com/opencrvs/opencrvs-core/issues/7494)

## 1.5.0 (TBD)

Expand Down
24 changes: 23 additions & 1 deletion packages/workflow/src/records/fhir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ import {
RegistrationStatus,
getResourceFromBundleById,
TransactionResponse,
TaskIdentifierSystem
TaskIdentifierSystem,
Location
} from '@opencrvs/commons/types'
import { FHIR_URL } from '@workflow/constants'
import fetch from 'node-fetch'
Expand Down Expand Up @@ -1453,3 +1454,24 @@ export async function getTaskHistory(taskId: string): Promise<Bundle<Task>> {

return res.json()
}

export async function getLocationsById(
locationIds: Array<string>
): Promise<Bundle<Location>> {
const res = await fetch(
new URL(`/fhir/Location?_id=${locationIds.join(',')}`, FHIR_URL).href,
{
method: 'GET',
headers: {
'Content-Type': 'application/fhir+json'
}
}
)
if (!res.ok) {
throw new Error(
`Fetching locations from Hearth failed with [${res.status}] body: ${res.statusText}`
)
}

return res.json()
}
51 changes: 49 additions & 2 deletions packages/workflow/src/records/handler/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ import {
isValidated,
isWaitingExternalValidation,
isComposition,
getTaskFromSavedBundle
getTaskFromSavedBundle,
Encounter,
Location,
findEncounterFromRecord,
Saved
} from '@opencrvs/commons/types'
import {
getToken,
Expand All @@ -39,6 +43,7 @@ import {
} from '@workflow/utils/auth-utils'
import {
findTaskFromIdentifier,
getLocationsById,
mergeBundles,
sendBundleToHearth,
toSavedBundle,
Expand Down Expand Up @@ -137,6 +142,31 @@ function createInProgressOrReadyForReviewTask(
}
}

async function resolveLocationsForEncounter(
encounter: Encounter
): Promise<Bundle<Location> | null> {
if (encounter.location == null) {
return null
}
const locationIds: Array<string> = []
for (const { location } of encounter.location) {
locationIds.push(location.reference.split('/')[1])
}
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 @@ -192,7 +222,24 @@ async function createRecord(
? changeState(savedBundle, 'IN_PROGRESS')
: changeState(savedBundle, 'READY_FOR_REVIEW')

return mergeBundles(record, practitionerResourcesBundle)
const mergedBundle = mergeBundles(record, practitionerResourcesBundle)

const encounter = findEncounterFromRecord(record)
if (encounter == null) {
return mergedBundle
}

if (bundleIncludesLocationResources(record)) {
return mergedBundle
}

const locationResourcesBundle = await resolveLocationsForEncounter(encounter)

if (locationResourcesBundle == null) {
return mergedBundle
}

return mergeBundles(mergedBundle, locationResourcesBundle)
}

type CreatedRecord =
Expand Down
Loading