Skip to content

Commit

Permalink
Merge pull request #79 from nhsconnect/PRMP-534
Browse files Browse the repository at this point in the history
Prmp 534 NACK POC
  • Loading branch information
AndyFlintNHS authored Jul 9, 2024
2 parents 05479b1 + b3f386a commit 9b4e62b
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,25 @@ export const sendEhrAcknowledgement = async (req, res) => {
try {
const interactionId = 'MCCI_IN010000UK13';
const serviceId = `urn:nhs:names:services:gp2gp:${interactionId}`;
const { messageId: ehrCoreMessageId, conversationId, odsCode, repositoryAsid } = req.body;
const {
messageId: ehrCoreMessageId,
conversationId,
odsCode,
repositoryAsid,
errorCode,
errorDisplayName
} = req.body;
const practiceAsid = await getPracticeAsid(odsCode, serviceId);

setCurrentSpanAttributes({ conversationId, messageId: ehrCoreMessageId });

const message = await buildEhrAcknowledgementPayload({
const message = buildEhrAcknowledgementPayload({
acknowledgementMessageId: conversationId,
receivingAsid: practiceAsid,
sendingAsid: repositoryAsid,
acknowledgedMessageId: ehrCoreMessageId
acknowledgedMessageId: ehrCoreMessageId,
errorCode,
errorDisplayName
});
await sendMessage({ interactionId, conversationId, odsCode, message });
logInfo('Acknowledgement sent to MHS');
Expand Down
60 changes: 56 additions & 4 deletions src/templates/generate-ehr-acknowledgement.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,77 @@
import dateFormat from 'dateformat';
import { logInfo } from '../middleware/logging';

export const buildEhrAcknowledgementPayload = ({
acknowledgementMessageId,
receivingAsid,
sendingAsid,
acknowledgedMessageId
acknowledgedMessageId,
errorCode,
errorDisplayName
}) => {
const timestamp = dateFormat(Date.now(), 'yyyymmddHHMMss');
let acknowledgementTypeCode;
let acknowledgementDetail;
let controlActEventReason;

switch (errorCode) {
case null:
logInfo('Building positive acknowledgement message with typeCode: AA');
acknowledgementTypeCode = 'AA'; // positive ACK
acknowledgementDetail = '';
controlActEventReason = '';
break;
case '30':
case '99':
logInfo(`Building negative acknowledgement message with typeCode: AE, errorCode: ${errorCode} and errorDisplayName: ${errorDisplayName}`);
acknowledgementTypeCode = 'AE'; // negative ACK, unknown failure reason
acknowledgementDetail = acknowledgementDetailTemplate(acknowledgementTypeCode, errorCode, errorDisplayName);
controlActEventReason = controlActEventReasonTemplate(errorCode, errorDisplayName);
break;
default: // negative ACK, known failure reason
logInfo(`Building negative acknowledgement message with typeCode: AR and errorCode: ${errorCode} and errorDisplayName: ${errorDisplayName}`);
acknowledgementTypeCode = 'AR';
acknowledgementDetail = acknowledgementDetailTemplate(acknowledgementTypeCode, errorCode, errorDisplayName);
controlActEventReason = controlActEventReasonTemplate(errorCode, errorDisplayName);
}

return ackMessageTemplate({
acknowledgementMessageId: acknowledgementMessageId.toUpperCase(),
timestamp,
acknowledgementTypeCode,
acknowledgementDetail,
acknowledgedMessageId: acknowledgedMessageId.toUpperCase(),
receivingAsid,
sendingAsid,
acknowledgedMessageId: acknowledgedMessageId.toUpperCase()
controlActEventReason
});
};

const acknowledgementDetailTemplate = (acknowledgementTypeCode, errorCode, errorDisplayName) =>
`<acknowledgementDetail typeCode="${acknowledgementTypeCode}">
<code code="${errorCode}" codeSystem="2.16.840.1.113883.2.1.3.2.4.17.101" displayName="${errorDisplayName}"/>
</acknowledgementDetail>`;

const controlActEventReasonTemplate = (errorCode, errorDisplayName) =>
`<reason typeCode="RSON">
<justifyingDetectedIssueEvent classCode="ALRT" moodCode="EVN">
<code code="${errorCode}" codeSystem="2.16.840.1.113883.2.1.3.2.4.17.101" displayName="${errorDisplayName}">
<qualifier>
<value code="ER" codeSystem="2.16.840.1.113883.2.1.3.2.4.17.104" />
</qualifier>
</code>
</justifyingDetectedIssueEvent>
</reason>`;

const ackMessageTemplate = ({
acknowledgementMessageId,
timestamp,
acknowledgementTypeCode,
acknowledgementDetail, // will be null in the case of positive ack
acknowledgedMessageId,
receivingAsid,
sendingAsid,
acknowledgedMessageId
controlActEventReason // will be null in the case of positive ack
}) =>
`<MCCI_IN010000UK13 xmlns="urn:hl7-org:v3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:hl7-org:v3 ..SchemasMCCI_IN010000UK13.xsd">
<id root="${acknowledgementMessageId}" />
Expand All @@ -31,7 +81,8 @@ const ackMessageTemplate = ({
<processingCode code="P" />
<processingModeCode code="T" />
<acceptAckCode code="NE" />
<acknowledgement typeCode="AA">
<acknowledgement typeCode="${acknowledgementTypeCode}">
${acknowledgementDetail}
<messageRef>
<id root="${acknowledgedMessageId}" />
</messageRef>
Expand All @@ -47,6 +98,7 @@ const ackMessageTemplate = ({
</device>
</communicationFunctionSnd>
<ControlActEvent classCode="CACT" moodCode="EVN">
${controlActEventReason}
<author1 typeCode="AUT">
<AgentSystemSDS classCode="AGNT">
<agentSystemSDS classCode="DEV" determinerCode="INSTANCE">
Expand Down

0 comments on commit 9b4e62b

Please sign in to comment.