From 016f0c8ce4823bc2fa1bc5b1778eff519a70eb59 Mon Sep 17 00:00:00 2001 From: Patrick LaRocque Date: Mon, 9 Sep 2024 19:04:08 -0400 Subject: [PATCH 1/2] Create a new /ncpdp/script endpoint for all NCPDP SCRIPT messages --- .../buildScript.v2017071.js | 46 ++++++++++++++----- backend/src/routes/doctorOrders.js | 36 +++++++++------ backend/src/routes/ncpdp.js | 46 +++++++++++++++++++ backend/src/server.ts | 2 + 4 files changed, 106 insertions(+), 24 deletions(-) create mode 100644 backend/src/routes/ncpdp.js diff --git a/backend/src/ncpdpScriptBuilder/buildScript.v2017071.js b/backend/src/ncpdpScriptBuilder/buildScript.v2017071.js index 07d566f..4657c6d 100644 --- a/backend/src/ncpdpScriptBuilder/buildScript.v2017071.js +++ b/backend/src/ncpdpScriptBuilder/buildScript.v2017071.js @@ -12,11 +12,11 @@ const XML_BUILDER_OPTIONS = { oneListGroup: 'true' }; -export function buildRxStatus(newRxMessageConvertedToJSON) { - const { Message } = newRxMessageConvertedToJSON; +function buildMessage(inputMessage, body) { + const { Message } = inputMessage; const { Header, Body } = Message; const time = new Date(); - const rxStatus = { + const message = { Message: [ { Header: [ @@ -42,19 +42,43 @@ export function buildRxStatus(newRxMessageConvertedToJSON) { ] }, { - Body: [ + Body: body + } + ] + }; + return message; +} + +export function buildRxStatus(newRxMessageConvertedToJSON) { + const body = + [ + { + Status: [ { - Status: [ - { - Code: '000' // Placeholder: This is dependent on individual pharmacy - } - ] + Code: '000' // Placeholder: This is dependent on individual pharmacy } ] } - ] - }; + ]; + const rxStatus = buildMessage(newRxMessageConvertedToJSON, body); + const builder = new XMLBuilder(XML_BUILDER_OPTIONS); + return builder.build(rxStatus); +} +export function buildRxError(newRxMessageConvertedToJSON, errorMessage) { + const body = + [ + { + Error: [ + { + Code: 900, // Transaction was rejected + DescriptionCode: 1000, // Unable to identify based on information submitted + Description: errorMessage + } + ] + } + ]; + const rxStatus = buildMessage(newRxMessageConvertedToJSON, body); const builder = new XMLBuilder(XML_BUILDER_OPTIONS); return builder.build(rxStatus); } diff --git a/backend/src/routes/doctorOrders.js b/backend/src/routes/doctorOrders.js index c4a3851..227501e 100644 --- a/backend/src/routes/doctorOrders.js +++ b/backend/src/routes/doctorOrders.js @@ -6,7 +6,7 @@ import axios from 'axios'; import bodyParser from 'body-parser'; import bpx from 'body-parser-xml'; import env from 'var'; -import { buildRxStatus, buildRxFill } from '../ncpdpScriptBuilder/buildScript.v2017071.js'; +import { buildRxStatus, buildRxFill, buildRxError } from '../ncpdpScriptBuilder/buildScript.v2017071.js'; import { NewRx } from '../database/schemas/newRx.js'; import { medicationRequestToRemsAdmins } from '../database/data.js'; @@ -52,12 +52,10 @@ router.get('/api/getRx/pickedUp', async (_req, res) => { }); /** - * Route: 'doctorOrders/api/addRx' - * Description : 'Saves a new Doctor Order to db' + * Description: Process addRx / NewRx NCPDP message. */ -router.post('/api/addRx', async (req, res) => { - // Parsing incoming NCPDP SCRIPT XML to doctorOrder JSON - const newRxMessageConvertedToJSON = req.body; +export async function processNewRx(newRxMessageConvertedToJSON) { + console.log('processNewRx NCPDP SCRIPT message'); const newOrder = await parseNCPDPScript(newRxMessageConvertedToJSON); try { @@ -68,21 +66,33 @@ router.post('/api/addRx', async (req, res) => { await newRx.save(); console.log('Saved NewRx'); } catch (error) { - console.log('Could not store the NewRx', error); - return error; + let errorStr = 'Could not store the NewRx'; + console.log(errorStr, error); + return buildRxError(newRxMessageConvertedToJSON, errorStr); } try { await newOrder.save(); console.log('DoctorOrder was saved'); } catch (error) { - console.log('ERROR! duplicate found, prescription already exists', error); - return error; + let errorStr = 'ERROR! duplicate found, prescription already exists'; + console.log(errorStr, error); + return buildRxError(errorStr); } - const RxStatus = buildRxStatus(newRxMessageConvertedToJSON); - res.send(RxStatus); - console.log('Sent RxStatus'); + return buildRxStatus(newRxMessageConvertedToJSON); +} + +/** + * Route: 'doctorOrders/api/addRx' + * Description : 'Saves a new Doctor Order to db' + */ +router.post('/api/addRx', async (req, res) => { + // Parsing incoming NCPDP SCRIPT XML to doctorOrder JSON + const newRxMessageConvertedToJSON = req.body; + const status = await processNewRx(newRxMessageConvertedToJSON); + res.send(status); + console.log('Sent Status/Error'); }); /** diff --git a/backend/src/routes/ncpdp.js b/backend/src/routes/ncpdp.js new file mode 100644 index 0000000..1534c78 --- /dev/null +++ b/backend/src/routes/ncpdp.js @@ -0,0 +1,46 @@ +import express from 'express'; +const router = express.Router(); +// XML Parsing Middleware used for NCPDP SCRIPT +import bodyParser from 'body-parser'; +import bpx from 'body-parser-xml'; + +import { processNewRx } from './doctorOrders.js'; +import { buildRxError } from '../ncpdpScriptBuilder/buildScript.v2017071.js'; + +bpx(bodyParser); +router.use( + bodyParser.xml({ + xmlParseOptions: { + normalize: true, // Trim whitespace inside text nodes + explicitArray: false // Only put nodes in array if >1 + } + }) +); +router.use(bodyParser.urlencoded({ extended: false })); + +/** + * Route: 'ncpdp/script' + * Description : 'Supports NCPDP SCRIPT messages, currntly only NewRx' + */ +router.post('/script', async (req, res) => { + // Parsing incoming NCPDP SCRIPT XML to JSON + console.log('received /ncpdp/script message'); + const newRxMessageConvertedToJSON = req.body; + let message = newRxMessageConvertedToJSON?.Message; + let body = message?.Body; + let status = null; + if (body?.NewRx) { + // process NewRx message + status = await processNewRx(newRxMessageConvertedToJSON); + + } else { + let errorStr = 'unknown message type'; + console.log('/ncpdp/script ' + errorStr); + status = buildRxError(newRxMessageConvertedToJSON, errorStr); + } + + res.send(status); + console.log('Sent Status/Error'); +}); + +export default router; \ No newline at end of file diff --git a/backend/src/server.ts b/backend/src/server.ts index c9289f4..802c10d 100644 --- a/backend/src/server.ts +++ b/backend/src/server.ts @@ -1,5 +1,6 @@ import express from 'express'; import doctorOrders from './routes/doctorOrders.js'; +import ncpdp from './routes/ncpdp.js'; const app = express(); import cors from 'cors'; @@ -23,6 +24,7 @@ async function main() { app.use(bodyParser.urlencoded({ extended: false })); app.use(cors(options)); app.use('/doctorOrders', doctorOrders); + app.use('/ncpdp', ncpdp); let server: any = app; From f2ffa3d0a4f3f872045244548825b67b17d90e7c Mon Sep 17 00:00:00 2001 From: Patrick LaRocque Date: Mon, 9 Sep 2024 19:30:52 -0400 Subject: [PATCH 2/2] run prettier --- .../buildScript.v2017071.js | 42 +++++++++---------- backend/src/routes/doctorOrders.js | 6 ++- backend/src/routes/ncpdp.js | 35 ++++++++-------- 3 files changed, 42 insertions(+), 41 deletions(-) diff --git a/backend/src/ncpdpScriptBuilder/buildScript.v2017071.js b/backend/src/ncpdpScriptBuilder/buildScript.v2017071.js index 4657c6d..a642710 100644 --- a/backend/src/ncpdpScriptBuilder/buildScript.v2017071.js +++ b/backend/src/ncpdpScriptBuilder/buildScript.v2017071.js @@ -50,34 +50,32 @@ function buildMessage(inputMessage, body) { } export function buildRxStatus(newRxMessageConvertedToJSON) { - const body = - [ - { - Status: [ - { - Code: '000' // Placeholder: This is dependent on individual pharmacy - } - ] - } - ]; + const body = [ + { + Status: [ + { + Code: '000' // Placeholder: This is dependent on individual pharmacy + } + ] + } + ]; const rxStatus = buildMessage(newRxMessageConvertedToJSON, body); const builder = new XMLBuilder(XML_BUILDER_OPTIONS); return builder.build(rxStatus); } export function buildRxError(newRxMessageConvertedToJSON, errorMessage) { - const body = - [ - { - Error: [ - { - Code: 900, // Transaction was rejected - DescriptionCode: 1000, // Unable to identify based on information submitted - Description: errorMessage - } - ] - } - ]; + const body = [ + { + Error: [ + { + Code: 900, // Transaction was rejected + DescriptionCode: 1000, // Unable to identify based on information submitted + Description: errorMessage + } + ] + } + ]; const rxStatus = buildMessage(newRxMessageConvertedToJSON, body); const builder = new XMLBuilder(XML_BUILDER_OPTIONS); return builder.build(rxStatus); diff --git a/backend/src/routes/doctorOrders.js b/backend/src/routes/doctorOrders.js index 227501e..8f5ba55 100644 --- a/backend/src/routes/doctorOrders.js +++ b/backend/src/routes/doctorOrders.js @@ -6,7 +6,11 @@ import axios from 'axios'; import bodyParser from 'body-parser'; import bpx from 'body-parser-xml'; import env from 'var'; -import { buildRxStatus, buildRxFill, buildRxError } from '../ncpdpScriptBuilder/buildScript.v2017071.js'; +import { + buildRxStatus, + buildRxFill, + buildRxError +} from '../ncpdpScriptBuilder/buildScript.v2017071.js'; import { NewRx } from '../database/schemas/newRx.js'; import { medicationRequestToRemsAdmins } from '../database/data.js'; diff --git a/backend/src/routes/ncpdp.js b/backend/src/routes/ncpdp.js index 1534c78..6bed68d 100644 --- a/backend/src/routes/ncpdp.js +++ b/backend/src/routes/ncpdp.js @@ -23,24 +23,23 @@ router.use(bodyParser.urlencoded({ extended: false })); * Description : 'Supports NCPDP SCRIPT messages, currntly only NewRx' */ router.post('/script', async (req, res) => { - // Parsing incoming NCPDP SCRIPT XML to JSON - console.log('received /ncpdp/script message'); - const newRxMessageConvertedToJSON = req.body; - let message = newRxMessageConvertedToJSON?.Message; - let body = message?.Body; - let status = null; - if (body?.NewRx) { - // process NewRx message - status = await processNewRx(newRxMessageConvertedToJSON); + // Parsing incoming NCPDP SCRIPT XML to JSON + console.log('received /ncpdp/script message'); + const newRxMessageConvertedToJSON = req.body; + let message = newRxMessageConvertedToJSON?.Message; + let body = message?.Body; + let status = null; + if (body?.NewRx) { + // process NewRx message + status = await processNewRx(newRxMessageConvertedToJSON); + } else { + let errorStr = 'unknown message type'; + console.log('/ncpdp/script ' + errorStr); + status = buildRxError(newRxMessageConvertedToJSON, errorStr); + } - } else { - let errorStr = 'unknown message type'; - console.log('/ncpdp/script ' + errorStr); - status = buildRxError(newRxMessageConvertedToJSON, errorStr); - } - - res.send(status); - console.log('Sent Status/Error'); + res.send(status); + console.log('Sent Status/Error'); }); -export default router; \ No newline at end of file +export default router;