From 942ab81f38eb0bd52a234d77187589a1c3e9d940 Mon Sep 17 00:00:00 2001 From: Siyasanga Date: Thu, 12 Sep 2024 16:14:25 +0200 Subject: [PATCH 1/2] Refactor bundleIncludesLocationResources() The logic to check if a bundle contains the require location resource was incorrect, it's fixed now by checking that all the encounter locations exist a resources in the bundle https://github.com/opencrvs/opencrvs-core/issues/7494 --- .../src/records/handler/create.test.ts | 29 ++++++++++++++++++- .../workflow/src/records/handler/create.ts | 12 +++++--- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/packages/workflow/src/records/handler/create.test.ts b/packages/workflow/src/records/handler/create.test.ts index 26517fb7785..a258c55c86f 100644 --- a/packages/workflow/src/records/handler/create.test.ts +++ b/packages/workflow/src/records/handler/create.test.ts @@ -14,8 +14,14 @@ import { readFileSync } from 'fs' import { createBirthRegistrationPayload } from '@test/mocks/createBirthRecord' import { server as mswServer } from '@test/setupServer' import { rest } from 'msw' -import { SavedBundle, SavedTask, URLReference } from '@opencrvs/commons/types' import { TransactionResponse } from '@workflow/records/fhir' +import { + SavedBundle, + SavedTask, + URLReference, + SavedLocation +} from '@opencrvs/commons/types' +import { UUID } from '@opencrvs/commons' const existingTaskBundle: SavedBundle = { resourceType: 'Bundle', @@ -23,6 +29,21 @@ const existingTaskBundle: SavedBundle = { entry: [] } +const existingLocationBundle: SavedBundle = { + resourceType: 'Bundle', + type: 'document', + entry: [ + { + resource: { + resourceType: 'Location', + id: '146251e9-df90-4068-82b0-27d8f979e8e2' as UUID + }, + fullUrl: + 'http://localhost:3447/fhir/Location/146251e9-df90-4068-82b0-27d8f979e8e2/_history/e79c368d-f8e4-49b7-8573-d885e11ebb47' as URLReference + } + ] +} + describe('Create record endpoint', () => { let server: Awaited> @@ -116,6 +137,12 @@ describe('Create record endpoint', () => { }) ) + mswServer.use( + rest.get('http://localhost:3447/fhir/Location', (_, res, ctx) => { + return res(ctx.json(existingLocationBundle)) + }) + ) + // mock tracking-id generation from country confgi mswServer.use( rest.post('http://localhost:3040/tracking-id', (_, res, ctx) => { diff --git a/packages/workflow/src/records/handler/create.ts b/packages/workflow/src/records/handler/create.ts index b9891e85ea9..d0613b9d4ea 100644 --- a/packages/workflow/src/records/handler/create.ts +++ b/packages/workflow/src/records/handler/create.ts @@ -157,14 +157,18 @@ async function resolveLocationsForEncounter( function bundleIncludesLocationResources(record: Saved) { const encounter = findEncounterFromRecord(record) - const locationIds = + const encounterLocationIds = encounter?.location?.map( ({ location }) => location.reference.split('/')[1] ) || [] - return record.entry - .filter(({ resource }) => resource.resourceType == 'Location') - .every(({ resource }) => locationIds?.includes(resource.id)) + const bundleLocations = record.entry.filter( + ({ resource }) => resource.resourceType == 'Location' + ) + + return encounterLocationIds.every((locationId) => + bundleLocations.some((location) => location.id === locationId) + ) } async function createRecord( From 133fc0ed379d540038c4966ab566b53b400e63f2 Mon Sep 17 00:00:00 2001 From: Siyasanga Date: Thu, 12 Sep 2024 16:45:34 +0200 Subject: [PATCH 2/2] Record bundleIncludesLocationResources() fixes Add a CHANGELOG.md entry for the recent fixes to clearly communicate the reason for the change https://github.com/opencrvs/opencrvs-core/issues/7494 --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index dcf34a958c3..099cda274cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -65,6 +65,7 @@ - When any user's role was updated, incorrect role was shown for that user's actions in the history section of a declaration's record audit page. [#7495](https://github.com/opencrvs/opencrvs-core/issues/7495) - Registration agent was unable to download declarations that were previously corrected by registrar. [#7582](https://github.com/opencrvs/opencrvs-core/issues/7582) - When a user updates a marriage declaration editing the signature of the bride, groom, witness one or witness two, handle the changed value of the signature properly. [#7462](https://github.com/opencrvs/opencrvs-core/issues/7462) +- The internal function we used to check if all the location references listed in the encounter are included in the bundle had incorrect logic which resulted in location details missing in ElasticSearch which broke Advanced search. [7494](https://github.com/opencrvs/opencrvs-core/issues/7494) ## 1.5.0 (TBD)