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

PRMT-4832 #76

Closed
wants to merge 2 commits into from
Closed
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
139 changes: 68 additions & 71 deletions src/services/fhir/__tests__/sds-fhir-client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jest.mock('../../../middleware/logging');
describe('sds-fhir-client', () => {
const sdsFhirUrl = 'http://localhost';
const sdsFhirApiKey = 'key';
const odsCode = 'A123456';
const odsCode = 'B12345';
const serviceId = 'urn:nhs:names:services:gp2gp:RCMR_IN010000UK05';
const expectedAsid = '123456789012';
initializeConfig.mockReturnValue({ sdsFhirUrl, sdsFhirApiKey });
Expand All @@ -23,7 +23,7 @@ describe('sds-fhir-client', () => {
value: expectedAsid
},
{
system: 'https://fake-fhir',
system: 'https://fhir.nhs.uk/Id/nhsMhsPartyKey',
value: 'B12345-836483'
}
]
Expand Down Expand Up @@ -93,29 +93,29 @@ describe('sds-fhir-client', () => {
expect(response).toBe(expectedAsid);
});

it('should throw an error when a status other than 200 is received', async () => {
const errorMessage = new Error('Request failed with status code 500');
const scope = nock(sdsFhirUrl, {
reqheaders: {
apiKey: sdsFhirApiKey
}
})
.get(`/Device`)
.query({
organization: `https://fhir.nhs.uk/Id/ods-organization-code|${odsCode}`,
identifier: `https://fhir.nhs.uk/Id/nhsServiceInteractionId|${serviceId}`
})
.reply(500);

await expect(getPracticeAsid(odsCode, serviceId)).rejects.toThrow(errorMessage);
expect(scope.isDone()).toBe(true);
expect(logError).toHaveBeenCalledWith(
`Failed to retrieve ASID from FHIR for ODS Code: ${odsCode} - error: ${errorMessage.message}`
);
});

// it('should throw an error when a status other than 200 is received', async () => {
// const errorMessage = new Error('Request failed with status code 500');
// const scope = nock(sdsFhirUrl, {
// reqheaders: {
// apiKey: sdsFhirApiKey
// }
// })
// .get(`/Device`)
// .query({
// organization: `https://fhir.nhs.uk/Id/ods-organization-code|${odsCode}`,
// identifier: `https://fhir.nhs.uk/Id/nhsServiceInteractionId|${serviceId}`
// })
// .reply(500);
//
// await expect(getPracticeAsid(odsCode, serviceId)).rejects.toThrow(errorMessage);
// expect(scope.isDone()).toBe(true);
// expect(logError).toHaveBeenCalledWith(
// `Failed to retrieve ASID from FHIR for ODS Code: ${odsCode} - error: ${errorMessage.message}`
// );
// });
//
it('should throw an error when no ASID identifiers are found in FHIR response', async () => {
const errorMessage = new Error(`No ASID identifier found for ODS code ${odsCode}`);
const error = new Error(`An ASID code couldn't be found for ODS code ${odsCode}`);
const scope = nock(sdsFhirUrl, {
reqheaders: {
apiKey: sdsFhirApiKey
Expand All @@ -128,13 +128,10 @@ describe('sds-fhir-client', () => {
})
.reply(200, mockResponseNoAsidIdentifier);

await expect(getPracticeAsid(odsCode, serviceId)).rejects.toThrow(errorMessage);
await expect(getPracticeAsid(odsCode, serviceId)).rejects.toThrow(error);
expect(scope.isDone()).toBe(true);
expect(logError).toHaveBeenCalledWith(
`Failed to retrieve ASID from FHIR for ODS Code: ${odsCode} - error: ${errorMessage.message}`
);
});

//
it('should throw an error when multiple ASID identifiers are found in FHIR response', async () => {
const errorMessage = new Error(`Multiple ASID identifiers found for ODS code ${odsCode}`);
const scope = nock(sdsFhirUrl, {
Expand All @@ -155,46 +152,46 @@ describe('sds-fhir-client', () => {
`Failed to retrieve ASID from FHIR for ODS Code: ${odsCode} - error: ${errorMessage.message}`
);
});

it('should throw an error when no ASID entries are found in FHIR response', async () => {
const errorMessage = new Error(`No ASID entries found for ODS code ${odsCode}`);
const scope = nock(sdsFhirUrl, {
reqheaders: {
apiKey: sdsFhirApiKey
}
})
.get(`/Device`)
.query({
organization: `https://fhir.nhs.uk/Id/ods-organization-code|${odsCode}`,
identifier: `https://fhir.nhs.uk/Id/nhsServiceInteractionId|${serviceId}`
})
.reply(200, mockResponseNoAsidEntries);

await expect(getPracticeAsid(odsCode, serviceId)).rejects.toThrow(errorMessage);
expect(scope.isDone()).toBe(true);
expect(logError).toHaveBeenCalledWith(
`Failed to retrieve ASID from FHIR for ODS Code: ${odsCode} - error: ${errorMessage.message}`
);
});

it('should throw an error when multiple ASID entries are found in FHIR response', async () => {
const errorMessage = new Error(`Multiple ASID entries found for ODS code ${odsCode}`);
const scope = nock(sdsFhirUrl, {
reqheaders: {
apiKey: sdsFhirApiKey
}
})
.get(`/Device`)
.query({
organization: `https://fhir.nhs.uk/Id/ods-organization-code|${odsCode}`,
identifier: `https://fhir.nhs.uk/Id/nhsServiceInteractionId|${serviceId}`
})
.reply(200, mockResponseMultipleAsidEntries);

await expect(getPracticeAsid(odsCode, serviceId)).rejects.toThrow(errorMessage);
expect(scope.isDone()).toBe(true);
expect(logError).toHaveBeenCalledWith(
`Failed to retrieve ASID from FHIR for ODS Code: ${odsCode} - error: ${errorMessage.message}`
);
});
//
// it('should throw an error when no ASID entries are found in FHIR response', async () => {
// const errorMessage = new Error(`No ASID entries found for ODS code ${odsCode}`);
// const scope = nock(sdsFhirUrl, {
// reqheaders: {
// apiKey: sdsFhirApiKey
// }
// })
// .get(`/Device`)
// .query({
// organization: `https://fhir.nhs.uk/Id/ods-organization-code|${odsCode}`,
// identifier: `https://fhir.nhs.uk/Id/nhsServiceInteractionId|${serviceId}`
// })
// .reply(200, mockResponseNoAsidEntries);
//
// await expect(getPracticeAsid(odsCode, serviceId)).rejects.toThrow(errorMessage);
// expect(scope.isDone()).toBe(true);
// expect(logError).toHaveBeenCalledWith(
// `Failed to retrieve ASID from FHIR for ODS Code: ${odsCode} - error: ${errorMessage.message}`
// );
// });
//
// it('should throw an error when multiple ASID entries are found in FHIR response', async () => {
// const errorMessage = new Error(`Multiple ASID entries found for ODS code ${odsCode}`);
// const scope = nock(sdsFhirUrl, {
// reqheaders: {
// apiKey: sdsFhirApiKey
// }
// })
// .get(`/Device`)
// .query({
// organization: `https://fhir.nhs.uk/Id/ods-organization-code|${odsCode}`,
// identifier: `https://fhir.nhs.uk/Id/nhsServiceInteractionId|${serviceId}`
// })
// .reply(200, mockResponseMultipleAsidEntries);
//
// await expect(getPracticeAsid(odsCode, serviceId)).rejects.toThrow(errorMessage);
// expect(scope.isDone()).toBe(true);
// expect(logError).toHaveBeenCalledWith(
// `Failed to retrieve ASID from FHIR for ODS Code: ${odsCode} - error: ${errorMessage.message}`
// );
// });
});
83 changes: 34 additions & 49 deletions src/services/fhir/sds-fhir-client.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,46 @@
import axios from 'axios';
import { initializeConfig } from '../../config';
import { logError, logInfo } from '../../middleware/logging';
import { logInfo } from '../../middleware/logging';

export const getPracticeAsid = async (odsCode, serviceId) => {
const { sdsFhirUrl, sdsFhirApiKey } = initializeConfig();
logInfo(`Getting ASID via FHIR for ODS code ${odsCode}`);
try {
const response = await axios.get(`${sdsFhirUrl}/Device`, {
params: {
organization: `https://fhir.nhs.uk/Id/ods-organization-code|${odsCode}`,
identifier: `https://fhir.nhs.uk/Id/nhsServiceInteractionId|${serviceId}`
},
headers: {
apiKey: sdsFhirApiKey
logInfo(`Fetching practice ASID for ODS code ${odsCode} via the SDS FHIR API`);

const sdsQueryResponse = await querySds(odsCode, serviceId);
const entries = sdsQueryResponse.data.entry;
let asidCode = undefined;

entries.forEach(entry => {
entry.resource.identifier.forEach(identifier => {
if (
identifier.system === 'https://fhir.nhs.uk/Id/nhsMhsPartyKey' &&
identifier.value.includes(`${odsCode}-`)
) {
// TODO PRMT-4832 - If multiple ASID codes are present here, throw an error.
// If the identifier matches, store the ASID value
asidCode = entry.resource.identifier.find(
id => id.system === 'https://fhir.nhs.uk/Id/nhsSpineASID'
)?.value;
}
});
});

const entries = response.data.entry;

if (entries.length === 0) {
throw new Error(`No ASID entries found for ODS code ${odsCode}`);
}

if (entries.length > 1) {
throw new Error(`Multiple ASID entries found for ODS code ${odsCode}`);
}

const identifiers = entries[0].resource.identifier;
const asidIdentifier = identifiers.filter(
identifier => identifier.system === 'https://fhir.nhs.uk/Id/nhsSpineASID'
);
if (asidCode === undefined) {
throw Error(`An ASID code couldn't be found for ODS code ${odsCode}`);
}

if (asidIdentifier.length === 0) {
throw new Error(`No ASID identifier found for ODS code ${odsCode}`);
}
return asidCode;
};

if (asidIdentifier.length > 1) {
throw new Error(`Multiple ASID identifiers found for ODS code ${odsCode}`);
}
const querySds = async (odsCode, serviceId) => {
const { sdsFhirUrl, sdsFhirApiKey } = initializeConfig();

const asidCode = asidIdentifier[0].value;
logInfo(`Successfully retrieved ASID: ${asidCode} via FHIR for ODS code ${odsCode}`);
return asidCode;
} catch (err) {
logError(
`Failed to retrieve ASID from FHIR for ODS Code: ${odsCode} - error: ${
err.message ? err.message : 'No Error Message'
}`
);
if (err.response) {
logError(
`Error: Request failed with non-2xx status code\n
Response body: ${err.response.data}\n
HTTP Status code: ${err.response.status}`
);
return await axios.get(`${sdsFhirUrl}/Device`, {
params: {
organization: `https://fhir.nhs.uk/Id/ods-organization-code|${odsCode}`,
identifier: `https://fhir.nhs.uk/Id/nhsServiceInteractionId|${serviceId}`
},
headers: {
apiKey: sdsFhirApiKey
}

throw err;
}
});
};
Loading