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

Create a new /ncpdp/script endpoint for all NCPDP SCRIPT messages #99

Merged
merged 3 commits into from
Sep 25, 2024
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
46 changes: 34 additions & 12 deletions backend/src/ncpdpScriptBuilder/buildScript.v2017071.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand All @@ -42,19 +42,41 @@ export function buildRxStatus(newRxMessageConvertedToJSON) {
]
},
{
Body: [
{
Status: [
{
Code: '000' // Placeholder: This is dependent on individual pharmacy
}
]
}
]
Body: body
}
]
};
return message;
}

export function buildRxStatus(newRxMessageConvertedToJSON) {
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 rxStatus = buildMessage(newRxMessageConvertedToJSON, body);
const builder = new XMLBuilder(XML_BUILDER_OPTIONS);
return builder.build(rxStatus);
}
Expand Down
40 changes: 27 additions & 13 deletions backend/src/routes/doctorOrders.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 } 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';

Expand Down Expand Up @@ -52,12 +56,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 {
Expand All @@ -68,21 +70,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');
});

/**
Expand Down
45 changes: 45 additions & 0 deletions backend/src/routes/ncpdp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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;
2 changes: 2 additions & 0 deletions backend/src/server.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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;

Expand Down
Loading