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

Ocrvs 6093 #6112

Merged
merged 15 commits into from
Nov 23, 2023
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 docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
4 changes: 2 additions & 2 deletions packages/workflow/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand Down Expand Up @@ -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 &&
Expand All @@ -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 &&
Expand All @@ -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 &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -76,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)
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -388,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<fhir.Bundle> {
const eventType = getEventType(fhirBundle)
const trackingId = generateTrackingIdForEvents(eventType)
const trackingId = await generateTrackingIdForEvents(
eventType,
fhirBundle,
token
)
const trackingIdFhirName = `${eventType.toLowerCase()}-tracking-id`

if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
38 changes: 1 addition & 37 deletions packages/workflow/src/features/registration/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ describe('Verify handler', () => {
describe('createRegistrationHandler', () => {
beforeEach(() => {
fetch.mockResponses(
[null, { status: 404 }],
[userMock, { status: 200 }],
[fieldAgentPractitionerMock, { status: 200 }],
[fieldAgentPractitionerRoleMock, { status: 200 }],
Expand Down Expand Up @@ -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 }],
Expand Down
11 changes: 1 addition & 10 deletions packages/workflow/src/features/registration/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
markBundleAsValidated,
markEventAsRegistered,
modifyRegistrationBundle,
setTrackingId,
markBundleAsWaitingValidation,
updatePatientIdentifierWithRN,
touchBundle,
Expand Down Expand Up @@ -74,10 +73,7 @@ interface IEventRegistrationCallbackPayload {
}[]
}

async function sendBundleToHearth(
payload: fhir.Bundle,
count = 1
): Promise<fhir.Bundle> {
async function sendBundleToHearth(payload: fhir.Bundle): Promise<fhir.Bundle> {
const res = await fetch(HEARTH_URL, {
method: 'POST',
body: JSON.stringify(payload),
Expand All @@ -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()}`
)
Expand Down
Loading
Loading