From 2ca4ccb736d0dde0ed47fc5534be7506341d0dbc Mon Sep 17 00:00:00 2001 From: tahmidrahman-dsi Date: Wed, 25 Oct 2023 13:18:34 +0600 Subject: [PATCH 01/10] Call country config tracking id endpoint instead of creating in core itself --- docker-compose.yml | 1 + packages/workflow/package.json | 1 - packages/workflow/src/constants.ts | 2 ++ .../registration/fhir/fhir-bundle-modifier.ts | 9 ++++--- .../src/features/registration/utils.ts | 27 ++++++++++++------- packages/workflow/typings/short-uid.d.ts | 11 -------- 6 files changed, 26 insertions(+), 25 deletions(-) delete mode 100644 packages/workflow/typings/short-uid.d.ts diff --git a/docker-compose.yml b/docker-compose.yml index 681e1610276..2218db38884 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -76,6 +76,7 @@ services: - OPENHIM_URL=http://openhim-core:5001 - APPLICATION_CONFIG_URL=http://config:2021/ - COUNTRY=${COUNTRY:-bgd} # PEN_TEST change to gbr + - COUNTRY_CONFIG_URL=http://countryconfig:3040 search: image: opencrvs/ocrvs-search:${VERSION:-latest} build: diff --git a/packages/workflow/package.json b/packages/workflow/package.json index caf141ba450..01ae7382235 100644 --- a/packages/workflow/package.json +++ b/packages/workflow/package.json @@ -31,7 +31,6 @@ "node-fetch": "^2.6.7", "node-verhoeff": "^0.0.11", "pino": "^7.0.0", - "short-uid": "^0.1.0", "tsconfig-paths": "^3.13.0", "uuid": "^3.3.2" }, diff --git a/packages/workflow/src/constants.ts b/packages/workflow/src/constants.ts index 05eef704391..a9a8d95238d 100644 --- a/packages/workflow/src/constants.ts +++ b/packages/workflow/src/constants.ts @@ -28,6 +28,8 @@ export const CERT_PUBLIC_KEY_PATH = export const USER_MANAGEMENT_URL = process.env.USER_MANAGEMENT_URL || 'http://localhost:3030/' +export const COUNTRY_CONFIG_URL = + process.env.COUNTRY_CONFIG_URL || 'http://localhost:3040' export const SENTRY_DSN = process.env.SENTRY_DSN function getAvailableLanguages() { diff --git a/packages/workflow/src/features/registration/fhir/fhir-bundle-modifier.ts b/packages/workflow/src/features/registration/fhir/fhir-bundle-modifier.ts index 8b57b2f58d8..4cea9bd88d2 100644 --- a/packages/workflow/src/features/registration/fhir/fhir-bundle-modifier.ts +++ b/packages/workflow/src/features/registration/fhir/fhir-bundle-modifier.ts @@ -76,7 +76,7 @@ export async function modifyRegistrationBundle( throw new Error('Invalid FHIR bundle found for declaration') } /* setting unique trackingid here */ - fhirBundle = setTrackingId(fhirBundle) + fhirBundle = await setTrackingId(fhirBundle, token) const taskResource = selectOrCreateTaskRefResource(fhirBundle) as fhir.Task const eventType = getEventType(fhirBundle) @@ -388,9 +388,12 @@ export async function touchBundle( return bundle } -export function setTrackingId(fhirBundle: fhir.Bundle): fhir.Bundle { +export async function setTrackingId( + fhirBundle: fhir.Bundle, + token: string +): Promise { const eventType = getEventType(fhirBundle) - const trackingId = generateTrackingIdForEvents(eventType) + const trackingId = await generateTrackingIdForEvents(eventType, token) const trackingIdFhirName = `${eventType.toLowerCase()}-tracking-id` if ( diff --git a/packages/workflow/src/features/registration/utils.ts b/packages/workflow/src/features/registration/utils.ts index 5369cbf981a..9f6b4c565fe 100644 --- a/packages/workflow/src/features/registration/utils.ts +++ b/packages/workflow/src/features/registration/utils.ts @@ -8,11 +8,11 @@ * * Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS. */ -import * as ShortUIDGen from 'short-uid' import { NOTIFICATION_SERVICE_URL, MOSIP_TOKEN_SEEDER_URL, - HEARTH_URL + HEARTH_URL, + COUNTRY_CONFIG_URL } from '@workflow/constants' import fetch from 'node-fetch' import { logger } from '@workflow/logger' @@ -62,14 +62,21 @@ export enum FHIR_RESOURCE_TYPE { PATIENT = 'Patient' } -export function generateTrackingIdForEvents(eventType: EVENT_TYPE): string { - // using first letter of eventType for prefix - // TODO: for divorce, need to think about prefix as Death & Divorce prefix is same 'D' - return generateTrackingId(eventType.charAt(0)) -} - -function generateTrackingId(prefix: string): string { - return prefix.concat(new ShortUIDGen().randomUUID()).toUpperCase() +export async function generateTrackingIdForEvents( + eventType: EVENT_TYPE, + token: string +): Promise { + return fetch( + new URL(`/tracking-id/${eventType}`, COUNTRY_CONFIG_URL).toString(), + { + headers: { + Authorization: `Bearer ${token}` + } + } + ).then((res) => { + if (res.ok) return res.text() + else throw new Error(res.statusText) + }) } export function convertStringToASCII(str: string): string { diff --git a/packages/workflow/typings/short-uid.d.ts b/packages/workflow/typings/short-uid.d.ts deleted file mode 100644 index 1d43a21f6d4..00000000000 --- a/packages/workflow/typings/short-uid.d.ts +++ /dev/null @@ -1,11 +0,0 @@ -/* - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at https://mozilla.org/MPL/2.0/. - * - * OpenCRVS is also distributed under the terms of the Civil Registration - * & Healthcare Disclaimer located at http://opencrvs.org/license. - * - * Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS. - */ -declare module 'short-uid' From c5835e61c28207b20966bf11d17eb40f9ee73b2c Mon Sep 17 00:00:00 2001 From: tahmidrahman-dsi Date: Wed, 25 Oct 2023 15:49:10 +0600 Subject: [PATCH 02/10] Revert "Call country config tracking id endpoint instead of creating in core itself" This reverts commit 2ca4ccb736d0dde0ed47fc5534be7506341d0dbc. --- docker-compose.yml | 1 - packages/workflow/package.json | 1 + packages/workflow/src/constants.ts | 2 -- .../registration/fhir/fhir-bundle-modifier.ts | 9 +++---- .../src/features/registration/utils.ts | 27 +++++++------------ packages/workflow/typings/short-uid.d.ts | 11 ++++++++ 6 files changed, 25 insertions(+), 26 deletions(-) create mode 100644 packages/workflow/typings/short-uid.d.ts diff --git a/docker-compose.yml b/docker-compose.yml index 2218db38884..681e1610276 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -76,7 +76,6 @@ services: - OPENHIM_URL=http://openhim-core:5001 - APPLICATION_CONFIG_URL=http://config:2021/ - COUNTRY=${COUNTRY:-bgd} # PEN_TEST change to gbr - - COUNTRY_CONFIG_URL=http://countryconfig:3040 search: image: opencrvs/ocrvs-search:${VERSION:-latest} build: diff --git a/packages/workflow/package.json b/packages/workflow/package.json index 01ae7382235..caf141ba450 100644 --- a/packages/workflow/package.json +++ b/packages/workflow/package.json @@ -31,6 +31,7 @@ "node-fetch": "^2.6.7", "node-verhoeff": "^0.0.11", "pino": "^7.0.0", + "short-uid": "^0.1.0", "tsconfig-paths": "^3.13.0", "uuid": "^3.3.2" }, diff --git a/packages/workflow/src/constants.ts b/packages/workflow/src/constants.ts index a9a8d95238d..05eef704391 100644 --- a/packages/workflow/src/constants.ts +++ b/packages/workflow/src/constants.ts @@ -28,8 +28,6 @@ export const CERT_PUBLIC_KEY_PATH = export const USER_MANAGEMENT_URL = process.env.USER_MANAGEMENT_URL || 'http://localhost:3030/' -export const COUNTRY_CONFIG_URL = - process.env.COUNTRY_CONFIG_URL || 'http://localhost:3040' export const SENTRY_DSN = process.env.SENTRY_DSN function getAvailableLanguages() { diff --git a/packages/workflow/src/features/registration/fhir/fhir-bundle-modifier.ts b/packages/workflow/src/features/registration/fhir/fhir-bundle-modifier.ts index 4cea9bd88d2..8b57b2f58d8 100644 --- a/packages/workflow/src/features/registration/fhir/fhir-bundle-modifier.ts +++ b/packages/workflow/src/features/registration/fhir/fhir-bundle-modifier.ts @@ -76,7 +76,7 @@ export async function modifyRegistrationBundle( throw new Error('Invalid FHIR bundle found for declaration') } /* setting unique trackingid here */ - fhirBundle = await setTrackingId(fhirBundle, token) + fhirBundle = setTrackingId(fhirBundle) const taskResource = selectOrCreateTaskRefResource(fhirBundle) as fhir.Task const eventType = getEventType(fhirBundle) @@ -388,12 +388,9 @@ export async function touchBundle( return bundle } -export async function setTrackingId( - fhirBundle: fhir.Bundle, - token: string -): Promise { +export function setTrackingId(fhirBundle: fhir.Bundle): fhir.Bundle { const eventType = getEventType(fhirBundle) - const trackingId = await generateTrackingIdForEvents(eventType, token) + const trackingId = generateTrackingIdForEvents(eventType) const trackingIdFhirName = `${eventType.toLowerCase()}-tracking-id` if ( diff --git a/packages/workflow/src/features/registration/utils.ts b/packages/workflow/src/features/registration/utils.ts index 9f6b4c565fe..5369cbf981a 100644 --- a/packages/workflow/src/features/registration/utils.ts +++ b/packages/workflow/src/features/registration/utils.ts @@ -8,11 +8,11 @@ * * Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS. */ +import * as ShortUIDGen from 'short-uid' import { NOTIFICATION_SERVICE_URL, MOSIP_TOKEN_SEEDER_URL, - HEARTH_URL, - COUNTRY_CONFIG_URL + HEARTH_URL } from '@workflow/constants' import fetch from 'node-fetch' import { logger } from '@workflow/logger' @@ -62,21 +62,14 @@ export enum FHIR_RESOURCE_TYPE { PATIENT = 'Patient' } -export async function generateTrackingIdForEvents( - eventType: EVENT_TYPE, - token: string -): Promise { - return fetch( - new URL(`/tracking-id/${eventType}`, COUNTRY_CONFIG_URL).toString(), - { - headers: { - Authorization: `Bearer ${token}` - } - } - ).then((res) => { - if (res.ok) return res.text() - else throw new Error(res.statusText) - }) +export function generateTrackingIdForEvents(eventType: EVENT_TYPE): string { + // using first letter of eventType for prefix + // TODO: for divorce, need to think about prefix as Death & Divorce prefix is same 'D' + return generateTrackingId(eventType.charAt(0)) +} + +function generateTrackingId(prefix: string): string { + return prefix.concat(new ShortUIDGen().randomUUID()).toUpperCase() } export function convertStringToASCII(str: string): string { diff --git a/packages/workflow/typings/short-uid.d.ts b/packages/workflow/typings/short-uid.d.ts new file mode 100644 index 00000000000..1d43a21f6d4 --- /dev/null +++ b/packages/workflow/typings/short-uid.d.ts @@ -0,0 +1,11 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + * + * OpenCRVS is also distributed under the terms of the Civil Registration + * & Healthcare Disclaimer located at http://opencrvs.org/license. + * + * Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS. + */ +declare module 'short-uid' From 8b58f3cc6ced294d3306e3cc9e5a559682cb5061 Mon Sep 17 00:00:00 2001 From: tahmidrahman-dsi Date: Wed, 25 Oct 2023 16:19:58 +0600 Subject: [PATCH 03/10] Rename variable RESOURCE_SERVICE_URL to COUNTRY_CONFIG_URL --- docker-compose.yml | 2 +- packages/workflow/src/constants.ts | 4 ++-- .../src/features/registration/fhir/fhir-bundle-modifier.ts | 7 ++----- 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 681e1610276..95433f09401 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -71,7 +71,7 @@ services: - NODE_ENV=development - NOTIFICATION_SERVICE_URL=http://notification:2020/ - USER_MANAGEMENT_URL=http://user-mgnt:3030/ - - RESOURCE_SERVICE_URL=http://countryconfig:3040/ + - COUNTRY_CONFIG_URL=http://countryconfig:3040 - HEARTH_URL=http://hearth:3447/fhir - OPENHIM_URL=http://openhim-core:5001 - APPLICATION_CONFIG_URL=http://config:2021/ diff --git a/packages/workflow/src/constants.ts b/packages/workflow/src/constants.ts index 05eef704391..269625b64f4 100644 --- a/packages/workflow/src/constants.ts +++ b/packages/workflow/src/constants.ts @@ -20,8 +20,8 @@ export const NOTIFICATION_SERVICE_URL = export const MOSIP_TOKEN_SEEDER_URL = process.env.MOSIP_TOKEN_SEEDER_URL || 'http://localhost:8085' -export const RESOURCE_SERVICE_URL = - process.env.RESOURCE_SERVICE_URL || `http://localhost:3040/` +export const COUNTRY_CONFIG_URL = + process.env.COUNTRY_CONFIG_URL || `http://localhost:3040/` export const CERT_PUBLIC_KEY_PATH = (process.env.CERT_PUBLIC_KEY_PATH as string) || '../../.secrets/public-key.pem' diff --git a/packages/workflow/src/features/registration/fhir/fhir-bundle-modifier.ts b/packages/workflow/src/features/registration/fhir/fhir-bundle-modifier.ts index 8b57b2f58d8..69e56b7dbb8 100644 --- a/packages/workflow/src/features/registration/fhir/fhir-bundle-modifier.ts +++ b/packages/workflow/src/features/registration/fhir/fhir-bundle-modifier.ts @@ -45,10 +45,7 @@ import { } from '@workflow/features/user/utils' import { logger } from '@workflow/logger' import * as Hapi from '@hapi/hapi' -import { - APPLICATION_CONFIG_URL, - RESOURCE_SERVICE_URL -} from '@workflow/constants' +import { APPLICATION_CONFIG_URL, COUNTRY_CONFIG_URL } from '@workflow/constants' import { getToken, getTokenPayload, @@ -165,7 +162,7 @@ export async function invokeRegistrationValidation( token: string ): Promise<{ bundle: fhir.Bundle; regValidationError?: boolean }> { try { - const res = await fetch(`${RESOURCE_SERVICE_URL}event-registration`, { + const res = await fetch(`${COUNTRY_CONFIG_URL}event-registration`, { method: 'POST', body: JSON.stringify(bundle), headers: { From 77e5e8ffdb4f5ea454a035c34af1155583e37869 Mon Sep 17 00:00:00 2001 From: tahmidrahman-dsi Date: Wed, 25 Oct 2023 16:21:37 +0600 Subject: [PATCH 04/10] Get tracking id from country config or else generate by itself --- .../registration/fhir/fhir-bundle-modifier.ts | 13 ++++-- .../src/features/registration/utils.ts | 45 ++++++++++++++++--- 2 files changed, 50 insertions(+), 8 deletions(-) diff --git a/packages/workflow/src/features/registration/fhir/fhir-bundle-modifier.ts b/packages/workflow/src/features/registration/fhir/fhir-bundle-modifier.ts index 69e56b7dbb8..ce6a31cadef 100644 --- a/packages/workflow/src/features/registration/fhir/fhir-bundle-modifier.ts +++ b/packages/workflow/src/features/registration/fhir/fhir-bundle-modifier.ts @@ -73,7 +73,7 @@ export async function modifyRegistrationBundle( throw new Error('Invalid FHIR bundle found for declaration') } /* setting unique trackingid here */ - fhirBundle = setTrackingId(fhirBundle) + fhirBundle = await setTrackingId(fhirBundle, token) const taskResource = selectOrCreateTaskRefResource(fhirBundle) as fhir.Task const eventType = getEventType(fhirBundle) @@ -385,9 +385,16 @@ export async function touchBundle( return bundle } -export function setTrackingId(fhirBundle: fhir.Bundle): fhir.Bundle { +export async function setTrackingId( + fhirBundle: fhir.Bundle, + token: string +): Promise { const eventType = getEventType(fhirBundle) - const trackingId = generateTrackingIdForEvents(eventType) + const trackingId = await generateTrackingIdForEvents( + eventType, + fhirBundle, + token + ) const trackingIdFhirName = `${eventType.toLowerCase()}-tracking-id` if ( diff --git a/packages/workflow/src/features/registration/utils.ts b/packages/workflow/src/features/registration/utils.ts index 5369cbf981a..e26afec42b7 100644 --- a/packages/workflow/src/features/registration/utils.ts +++ b/packages/workflow/src/features/registration/utils.ts @@ -12,7 +12,8 @@ import * as ShortUIDGen from 'short-uid' import { NOTIFICATION_SERVICE_URL, MOSIP_TOKEN_SEEDER_URL, - HEARTH_URL + HEARTH_URL, + COUNTRY_CONFIG_URL } from '@workflow/constants' import fetch from 'node-fetch' import { logger } from '@workflow/logger' @@ -62,10 +63,44 @@ export enum FHIR_RESOURCE_TYPE { PATIENT = 'Patient' } -export function generateTrackingIdForEvents(eventType: EVENT_TYPE): string { - // using first letter of eventType for prefix - // TODO: for divorce, need to think about prefix as Death & Divorce prefix is same 'D' - return generateTrackingId(eventType.charAt(0)) +export async function generateTrackingIdForEvents( + eventType: EVENT_TYPE, + bundle: fhir.Bundle, + token: string +): Promise { + const trackingIdFromCountryConfig = await getTrackingIdFromCountryConfig( + eventType, + bundle, + token + ) + if (trackingIdFromCountryConfig) { + return trackingIdFromCountryConfig + } else { + // using first letter of eventType for prefix + // TODO: for divorce, need to think about prefix as Death & Divorce prefix is same 'D' + return generateTrackingId(eventType.charAt(0)) + } +} + +export async function getTrackingIdFromCountryConfig( + eventType: EVENT_TYPE, + bundle: fhir.Bundle, + token: string +): Promise { + return fetch( + new URL(`/tracking-id/${eventType}`, COUNTRY_CONFIG_URL).toString(), + { + method: 'POST', + headers: { + Authorization: `Bearer ${token}` + }, + body: JSON.stringify(bundle) + } + ).then((res) => { + if (res.ok) return res.text() + else if (res.status === 404) return null + else throw new Error(res.statusText) + }) } function generateTrackingId(prefix: string): string { From 05f5f00f416dadc0f8d4f829cabe5c7e209f7c88 Mon Sep 17 00:00:00 2001 From: tahmidrahman-dsi Date: Wed, 25 Oct 2023 16:29:15 +0600 Subject: [PATCH 05/10] Remove eventType from request params --- .../workflow/src/features/registration/utils.ts | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/packages/workflow/src/features/registration/utils.ts b/packages/workflow/src/features/registration/utils.ts index e26afec42b7..4119dd65415 100644 --- a/packages/workflow/src/features/registration/utils.ts +++ b/packages/workflow/src/features/registration/utils.ts @@ -87,16 +87,13 @@ export async function getTrackingIdFromCountryConfig( bundle: fhir.Bundle, token: string ): Promise { - return fetch( - new URL(`/tracking-id/${eventType}`, COUNTRY_CONFIG_URL).toString(), - { - method: 'POST', - headers: { - Authorization: `Bearer ${token}` - }, - body: JSON.stringify(bundle) - } - ).then((res) => { + return fetch(new URL(`/tracking-id`, COUNTRY_CONFIG_URL).toString(), { + method: 'POST', + headers: { + Authorization: `Bearer ${token}` + }, + body: JSON.stringify(bundle) + }).then((res) => { if (res.ok) return res.text() else if (res.status === 404) return null else throw new Error(res.statusText) From b1bf78b2095956020e9b26adaa1280bc3e5e97f2 Mon Sep 17 00:00:00 2001 From: tahmidrahman-dsi Date: Wed, 25 Oct 2023 16:44:13 +0600 Subject: [PATCH 06/10] Remove unused variable --- packages/workflow/src/features/registration/utils.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/workflow/src/features/registration/utils.ts b/packages/workflow/src/features/registration/utils.ts index 4119dd65415..1a8b2eabc93 100644 --- a/packages/workflow/src/features/registration/utils.ts +++ b/packages/workflow/src/features/registration/utils.ts @@ -69,7 +69,6 @@ export async function generateTrackingIdForEvents( token: string ): Promise { const trackingIdFromCountryConfig = await getTrackingIdFromCountryConfig( - eventType, bundle, token ) @@ -83,11 +82,10 @@ export async function generateTrackingIdForEvents( } export async function getTrackingIdFromCountryConfig( - eventType: EVENT_TYPE, bundle: fhir.Bundle, token: string ): Promise { - return fetch(new URL(`/tracking-id`, COUNTRY_CONFIG_URL).toString(), { + return fetch(new URL('/tracking-id', COUNTRY_CONFIG_URL).toString(), { method: 'POST', headers: { Authorization: `Bearer ${token}` From cd77e50359b384a36ec3828eaf848c00a5a3ecac Mon Sep 17 00:00:00 2001 From: tahmidrahman-dsi Date: Thu, 26 Oct 2023 16:29:44 +0600 Subject: [PATCH 07/10] Add missing content-type header --- packages/workflow/src/features/registration/utils.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/workflow/src/features/registration/utils.ts b/packages/workflow/src/features/registration/utils.ts index 95cc1b509e4..0e337ad1ca3 100644 --- a/packages/workflow/src/features/registration/utils.ts +++ b/packages/workflow/src/features/registration/utils.ts @@ -90,7 +90,8 @@ export async function getTrackingIdFromCountryConfig( return fetch(new URL('/tracking-id', COUNTRY_CONFIG_URL).toString(), { method: 'POST', headers: { - Authorization: `Bearer ${token}` + Authorization: `Bearer ${token}`, + 'Content-type': 'application/json' }, body: JSON.stringify(bundle) }).then((res) => { From 6e7d21ffc0c9d0844c896ed16fd370b5a18234f4 Mon Sep 17 00:00:00 2001 From: tahmidrahman-dsi Date: Fri, 27 Oct 2023 14:36:41 +0600 Subject: [PATCH 08/10] Remove set tracking id call in loop --- .../workflow/src/features/registration/handler.ts | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/packages/workflow/src/features/registration/handler.ts b/packages/workflow/src/features/registration/handler.ts index 87074b40c35..6f3b87c2916 100644 --- a/packages/workflow/src/features/registration/handler.ts +++ b/packages/workflow/src/features/registration/handler.ts @@ -14,7 +14,6 @@ import { markBundleAsValidated, markEventAsRegistered, modifyRegistrationBundle, - setTrackingId, markBundleAsWaitingValidation, updatePatientIdentifierWithRN, touchBundle, @@ -74,10 +73,7 @@ interface IEventRegistrationCallbackPayload { }[] } -async function sendBundleToHearth( - payload: fhir.Bundle, - count = 1 -): Promise { +async function sendBundleToHearth(payload: fhir.Bundle): Promise { const res = await fetch(HEARTH_URL, { method: 'POST', body: JSON.stringify(payload), @@ -86,11 +82,6 @@ async function sendBundleToHearth( } }) if (!res.ok) { - if (res.status === 409 && count < 5) { - setTrackingId(payload) - return await sendBundleToHearth(payload, count + 1) - } - throw new Error( `FHIR post to /fhir failed with [${res.status}] body: ${await res.text()}` ) From d569a929f0e98bce5f02df56c577ebbc8fb7055b Mon Sep 17 00:00:00 2001 From: tahmidrahman-dsi Date: Mon, 30 Oct 2023 13:55:43 +0600 Subject: [PATCH 09/10] Update tests with new function signature of `setTrackingId` --- .../fhir/fhir-bundle-modifier.test.ts | 55 +++++++++++-------- .../registration/fhir/fhir-utils.test.ts | 19 +++++-- .../src/features/registration/handler.test.ts | 38 +------------ .../src/features/registration/utils.test.ts | 51 +++++++++++++---- 4 files changed, 84 insertions(+), 79 deletions(-) diff --git a/packages/workflow/src/features/registration/fhir/fhir-bundle-modifier.test.ts b/packages/workflow/src/features/registration/fhir/fhir-bundle-modifier.test.ts index d3e69e9bf62..8bd951cf582 100644 --- a/packages/workflow/src/features/registration/fhir/fhir-bundle-modifier.test.ts +++ b/packages/workflow/src/features/registration/fhir/fhir-bundle-modifier.test.ts @@ -46,8 +46,9 @@ const fetch = fetchAny as any describe('Verify fhir bundle modifier functions', () => { describe('setTrackingId', () => { - it('Successfully modified the provided fhirBundle with birth trackingid', () => { - const fhirBundle = setTrackingId(testFhirBundle) + it('Successfully modified the provided fhirBundle with birth trackingid', async () => { + fetch.mockResponses(['B123456']) + const fhirBundle = await setTrackingId(testFhirBundle, '1234') if ( fhirBundle && fhirBundle.entry && @@ -75,8 +76,9 @@ describe('Verify fhir bundle modifier functions', () => { } }) - it('Successfully modified the provided fhirBundle with death trackingid', () => { - const fhirBundle = setTrackingId(testDeathFhirBundle) + it('Successfully modified the provided fhirBundle with death trackingid', async () => { + fetch.mockResponses(['D123456']) + const fhirBundle = await setTrackingId(testDeathFhirBundle, '1234') if ( fhirBundle && fhirBundle.entry && @@ -103,8 +105,9 @@ describe('Verify fhir bundle modifier functions', () => { } }) - it('Successfully modified the provided fhirBundle with marriage trackingid', () => { - const fhirBundle = setTrackingId(testMarriageFhirBundle) + it('Successfully modified the provided fhirBundle with marriage trackingid', async () => { + fetch.mockResponses(['M123456']) + const fhirBundle = await setTrackingId(testMarriageFhirBundle, '1234') if ( fhirBundle && fhirBundle.entry && @@ -131,31 +134,35 @@ describe('Verify fhir bundle modifier functions', () => { } }) - it('Throws error if invalid fhir bundle is provided', () => { + it('Throws error if invalid fhir bundle is provided', async () => { const invalidData = { ...testFhirBundle, entry: [] } - expect(() => setTrackingId(invalidData)).toThrowError( + await expect(setTrackingId(invalidData, '1234')).rejects.toThrowError( 'Invalid FHIR bundle found' ) }) - it('Will push the composite resource identifier if it is missing on fhirDoc', () => { - const fhirBundle = setTrackingId({ - ...testFhirBundle, - entry: [ - { - resource: { - code: { - coding: [ - { - system: 'http://opencrvs.org/specs/types', - code: 'BIRTH' - } - ] + it('Will push the composite resource identifier if it is missing on fhirDoc', async () => { + fetch.mockResponses(['B123456']) + const fhirBundle = await setTrackingId( + { + ...testFhirBundle, + entry: [ + { + resource: { + code: { + coding: [ + { + system: 'http://opencrvs.org/specs/types', + code: 'BIRTH' + } + ] + } } } - } - ] - }) + ] + }, + '1234' + ) if ( fhirBundle && diff --git a/packages/workflow/src/features/registration/fhir/fhir-utils.test.ts b/packages/workflow/src/features/registration/fhir/fhir-utils.test.ts index c3385819681..2496d7da4fc 100644 --- a/packages/workflow/src/features/registration/fhir/fhir-utils.test.ts +++ b/packages/workflow/src/features/registration/fhir/fhir-utils.test.ts @@ -128,8 +128,9 @@ describe('Verify getCRVSOfficeName', () => { }) describe('Verify getTrackingId', () => { - it('Returned tracking id properly for birth', () => { - const trackingid = getTrackingId(setTrackingId(testFhirBundle)) + it('Returned tracking id properly for birth', async () => { + fetch.mockResponseOnce(null, { status: 404 }) + const trackingid = getTrackingId(await setTrackingId(testFhirBundle, '123')) if (trackingid) { expect(trackingid).toMatch(/^B/) expect(trackingid.length).toBe(7) @@ -138,8 +139,11 @@ describe('Verify getTrackingId', () => { } }) - it('Returned tracking id properly for death', () => { - const trackingid = getTrackingId(setTrackingId(testDeathFhirBundle)) + it('Returned tracking id properly for death', async () => { + fetch.mockResponseOnce(null, { status: 404 }) + const trackingid = getTrackingId( + await setTrackingId(testDeathFhirBundle, '123') + ) if (trackingid) { expect(trackingid).toMatch(/^D/) expect(trackingid.length).toBe(7) @@ -148,8 +152,11 @@ describe('Verify getTrackingId', () => { } }) - it('Returned tracking id properly for marriage', () => { - const trackingid = getTrackingId(setTrackingId(testMarriageFhirBundle)) + it('Returned tracking id properly for marriage', async () => { + fetch.mockResponseOnce(null, { status: 404 }) + const trackingid = getTrackingId( + await setTrackingId(testMarriageFhirBundle, '123') + ) if (trackingid) { expect(trackingid).toMatch(/^M/) expect(trackingid.length).toBe(7) diff --git a/packages/workflow/src/features/registration/handler.test.ts b/packages/workflow/src/features/registration/handler.test.ts index e08c4b8eba3..ba6d281811d 100644 --- a/packages/workflow/src/features/registration/handler.test.ts +++ b/packages/workflow/src/features/registration/handler.test.ts @@ -145,6 +145,7 @@ describe('Verify handler', () => { describe('createRegistrationHandler', () => { beforeEach(() => { fetch.mockResponses( + [null, { status: 404 }], [userMock, { status: 200 }], [fieldAgentPractitionerMock, { status: 200 }], [fieldAgentPractitionerRoleMock, { status: 200 }], @@ -547,43 +548,6 @@ describe('Verify handler', () => { expect(res.statusCode).toBe(500) }) - it('generates a new tracking id and repeats the request if a 409 is received from hearth', async () => { - fetch.mockResponses( - ['', { status: 409 }], - ['', { status: 409 }], - [ - JSON.stringify({ - resourceType: 'Bundle', - entry: [ - { - response: { location: 'Patient/12423/_history/1' } - } - ] - }) - ] - ) - - const token = jwt.sign( - { scope: ['declare'] }, - readFileSync('../auth/test/cert.key'), - { - algorithm: 'RS256', - issuer: 'opencrvs:auth-service', - audience: 'opencrvs:workflow-user' - } - ) - - const res = await server.server.inject({ - method: 'POST', - url: '/fhir', - payload: testFhirBundle, - headers: { - Authorization: `Bearer ${token}` - } - }) - expect(res.statusCode).toBe(200) - }) - it('fails after trying to generate a new trackingID and sending to Hearth 5 times', async () => { fetch.mockResponses( ['', { status: 409 }], diff --git a/packages/workflow/src/features/registration/utils.test.ts b/packages/workflow/src/features/registration/utils.test.ts index db1c01403bf..512e932d5b7 100644 --- a/packages/workflow/src/features/registration/utils.test.ts +++ b/packages/workflow/src/features/registration/utils.test.ts @@ -37,14 +37,24 @@ describe('Verify utility functions', () => { }) it('Generates proper birth tracking id successfully', async () => { - const trackingId = generateTrackingIdForEvents(EVENT_TYPE.BIRTH) + fetch.mockResponseOnce(null, { status: 404 }) + const trackingId = await generateTrackingIdForEvents( + EVENT_TYPE.BIRTH, + {} as fhir.Bundle, + '123' + ) expect(trackingId).toBeDefined() expect(trackingId.length).toBe(7) expect(trackingId).toMatch(/^B/) }) it('Generates proper death tracking id successfully', async () => { - const trackingId = generateTrackingIdForEvents(EVENT_TYPE.DEATH) + fetch.mockResponseOnce(null, { status: 404 }) + const trackingId = await generateTrackingIdForEvents( + EVENT_TYPE.DEATH, + {}, + '123' + ) expect(trackingId).toBeDefined() expect(trackingId.length).toBe(7) @@ -52,7 +62,12 @@ describe('Verify utility functions', () => { }) it('Generates proper marriage tracking id successfully', async () => { - const trackingId = generateTrackingIdForEvents(EVENT_TYPE.MARRIAGE) + fetch.mockResponseOnce(null, { status: 404 }) + const trackingId = await generateTrackingIdForEvents( + EVENT_TYPE.MARRIAGE, + {}, + '123' + ) expect(trackingId).toBeDefined() expect(trackingId.length).toBe(7) @@ -67,7 +82,8 @@ describe('Verify utility functions', () => { }) it('send in-progress birth declaration notification successfully', async () => { - const fhirBundle = setTrackingId(testFhirBundle) + fetch.mockResponseOnce(null, { status: 404 }) + const fhirBundle = await setTrackingId(testFhirBundle, '123') fetch.mockResponse(officeMock) expect( sendEventNotification( @@ -81,7 +97,8 @@ describe('Verify utility functions', () => { ).resolves.not.toThrow() }) it('send Birth declaration notification successfully', async () => { - const fhirBundle = setTrackingId(testFhirBundle) + fetch.mockResponseOnce(null, { status: 404 }) + const fhirBundle = await setTrackingId(testFhirBundle, '123') fetch.mockResponse(officeMock) expect( sendEventNotification( @@ -115,7 +132,8 @@ describe('Verify utility functions', () => { ) }) it('send mark birth registration notification successfully', async () => { - const fhirBundle = setTrackingId(testFhirBundle) + fetch.mockResponseOnce(null, { status: 404 }) + const fhirBundle = await setTrackingId(testFhirBundle, '123') fetch.mockResponse(officeMock) //@ts-ignore fhirBundle.entry[1].resource.identifier.push({ @@ -152,7 +170,8 @@ describe('Verify utility functions', () => { ) }) it('send Birth rejection notification successfully', async () => { - const fhirBundle = setTrackingId(testFhirBundle) + fetch.mockResponseOnce(null, { status: 404 }) + const fhirBundle = await setTrackingId(testFhirBundle, '123') fetch.mockResponse(officeMock) expect( sendEventNotification( @@ -166,7 +185,8 @@ describe('Verify utility functions', () => { ).toBeDefined() }) it('send in-progress death declaration notification successfully', async () => { - const fhirBundle = setTrackingId(testFhirBundleWithIdsForDeath) + fetch.mockResponseOnce(null, { status: 404 }) + const fhirBundle = await setTrackingId(testFhirBundleWithIdsForDeath, '123') fetch.mockResponse(officeMock) expect( sendEventNotification( @@ -180,7 +200,8 @@ describe('Verify utility functions', () => { ).toBeDefined() }) it('send Death declaration notification successfully', async () => { - const fhirBundle = setTrackingId(testFhirBundleWithIdsForDeath) + fetch.mockResponseOnce(null, { status: 404 }) + const fhirBundle = await setTrackingId(testFhirBundleWithIdsForDeath, '123') fetch.mockResponse(officeMock) expect( sendEventNotification( @@ -212,7 +233,8 @@ describe('Verify utility functions', () => { ) }) it('send mark death registration notification successfully', async () => { - const fhirBundle = setTrackingId(testFhirBundleWithIdsForDeath) + fetch.mockResponseOnce(null, { status: 404 }) + const fhirBundle = await setTrackingId(testFhirBundleWithIdsForDeath, '123') //@ts-ignore fhirBundle.entry[1].resource.identifier.push({ system: 'http://opencrvs.org/specs/id/death-registration-number', @@ -250,7 +272,8 @@ describe('Verify utility functions', () => { ) }) it('send Death rejection notification successfully', async () => { - const fhirBundle = setTrackingId(testFhirBundleWithIdsForDeath) + fetch.mockResponseOnce(null, { status: 404 }) + const fhirBundle = await setTrackingId(testFhirBundleWithIdsForDeath, '123') fetch.mockResponses([officeMock, { status: 200 }]) fetch.mockResponses([deathTaskMock, { status: 200 }]) expect( @@ -265,7 +288,8 @@ describe('Verify utility functions', () => { ).toBeDefined() }) it('send Death declaration notification successfully', async () => { - const fhirBundle = setTrackingId(testFhirBundleWithIdsForDeath) + fetch.mockResponseOnce(null, { status: 404 }) + const fhirBundle = await setTrackingId(testFhirBundleWithIdsForDeath, '123') fetch.mockResponses([officeMock, { status: 200 }]) fetch.mockResponses([deathTaskMock, { status: 200 }]) fetch.mockResponses([deathTaskMock, { status: 200 }]) @@ -283,6 +307,9 @@ describe('Verify utility functions', () => { }) describe('getMosipUINToken functions', () => { + beforeAll(() => { + fetch.mockClear() + }) it('Calls mosip token seeder function and returns success', async () => { fetch.mockResponse(mosipSuccessMock) const mosipResponse = await getMosipUINToken(mosipDeceasedPatientMock) From bbd03fa1fe1d0192cc61032af34de6b0fd9fd45f Mon Sep 17 00:00:00 2001 From: tahmidrahman-dsi Date: Mon, 30 Oct 2023 15:27:39 +0600 Subject: [PATCH 10/10] Add type assertions --- packages/workflow/src/features/registration/utils.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/workflow/src/features/registration/utils.test.ts b/packages/workflow/src/features/registration/utils.test.ts index 512e932d5b7..3a92a71f717 100644 --- a/packages/workflow/src/features/registration/utils.test.ts +++ b/packages/workflow/src/features/registration/utils.test.ts @@ -52,7 +52,7 @@ describe('Verify utility functions', () => { fetch.mockResponseOnce(null, { status: 404 }) const trackingId = await generateTrackingIdForEvents( EVENT_TYPE.DEATH, - {}, + {} as fhir.Bundle, '123' ) @@ -65,7 +65,7 @@ describe('Verify utility functions', () => { fetch.mockResponseOnce(null, { status: 404 }) const trackingId = await generateTrackingIdForEvents( EVENT_TYPE.MARRIAGE, - {}, + {} as fhir.Bundle, '123' )