Skip to content

Commit fcae57d

Browse files
committed
proper reason code handling
1 parent 98c48cf commit fcae57d

1 file changed

Lines changed: 86 additions & 42 deletions

File tree

src/ncpdp/script.ts

Lines changed: 86 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,13 @@ const handleRemsRequest = async (message: any, res: Response) => {
151151
));
152152
}
153153

154-
// Requirements not met - denial with reason codes
154+
// Requirements not met - denial with reason code
155155
const reasonCodes = determineReasonCodes(outstandingRequirements);
156-
const reasonText = buildReasonText(outstandingRequirements);
156+
const reasonCode = reasonCodes[0];
157+
const reasonText = buildReasonText(reasonCode);
157158

158159
const denialDetails = {
159-
reasonCodes: reasonCodes.join(','),
160+
reasonCode: reasonCode,
160161
reasonText,
161162
outstandingCount: outstandingRequirements.length
162163
};
@@ -174,7 +175,7 @@ const handleRemsRequest = async (message: any, res: Response) => {
174175

175176
logger.info('Sending DENIED response');
176177
res.type('application/xml');
177-
return res.status(200).send(buildDeniedResponse(header, remsRequest, reasonCodes.join(','), reasonText));
178+
return res.status(200).send(buildDeniedResponse(header, remsRequest, reasonCode, reasonText));
178179

179180
} catch (error: any) {
180181
logger.error(`ERROR in handleRemsRequest: ${error.message}`);
@@ -342,30 +343,48 @@ const handleRxFill = async (message: any, res: Response) => {
342343

343344

344345
const determineReasonCodes = (outstandingRequirements: any[]): string[] => {
345-
const codes = new Set<string>();
346+
let hasPatientReq = false;
347+
let hasPrescriberReq = false;
348+
let hasPharmacyReq = false;
346349

347350
for (const req of outstandingRequirements) {
348-
switch (req.stakeholder?.toLowerCase()) {
349-
case 'patient':
350-
codes.add('EM');
351-
break;
352-
case 'prescriber':
353-
codes.add('ES');
354-
break;
355-
case 'pharmacist':
356-
case 'pharmacy':
357-
codes.add('EO');
358-
break;
351+
const stakeholder = req.stakeholder?.toLowerCase();
352+
if (stakeholder === 'patient') {
353+
hasPatientReq = true;
354+
} else if (stakeholder === 'prescriber') {
355+
hasPrescriberReq = true;
356+
} else if (stakeholder === 'pharmacist' || stakeholder === 'pharmacy') {
357+
hasPharmacyReq = true;
359358
}
360359
}
361360

362-
return Array.from(codes);
361+
// Return only the highest priority requirement
362+
if (hasPatientReq) {
363+
return ['EM'];
364+
} else if (hasPrescriberReq) {
365+
return ['ES'];
366+
} else if (hasPharmacyReq) {
367+
return ['EO'];
368+
}
369+
370+
// Fallback - should not reach here
371+
return ['EC'];
363372
};
364373

365374

366-
const buildReasonText = (outstandingRequirements: any[]): string => {
367-
const reqNames = outstandingRequirements.map(r => `${r.name} (${r.stakeholder})`).join(', ');
368-
return `Outstanding REMS requirements: ${reqNames}`;
375+
const buildReasonText = (reasonCode: string): string => {
376+
const reasonCodeNotes: { [key: string]: string } = {
377+
'EM': 'Patient enrollment/certification required',
378+
'ES': 'Prescriber enrollment/certification required',
379+
'EO': 'Pharmacy enrollment/certification required',
380+
'EC': 'Case information incomplete or invalid',
381+
'ER': 'REMS program error',
382+
'EX': 'Prescriber deactivated/decertified',
383+
'EY': 'Pharmacy deactivated/decertified',
384+
'EZ': 'Patient deactivated/decertified'
385+
};
386+
387+
return reasonCodeNotes[reasonCode] || 'REMS requirement not met';
369388
};
370389

371390

@@ -379,6 +398,13 @@ const buildApprovedResponse = (
379398
): string => {
380399
const builder = new Builder({ headless: false });
381400

401+
// Handle both capitalized and lowercased keys from parsed XML
402+
const patient = request.patient;
403+
const pharmacy = request.pharmacy;
404+
const prescriber = request.prescriber;
405+
const medicationPrescribed = request.medicationprescribed;
406+
const remsReferenceID = request.remsreferenceid;
407+
382408
const response = {
383409
Message: {
384410
$: {
@@ -393,11 +419,11 @@ const buildApprovedResponse = (
393419
Header: header,
394420
Body: {
395421
REMSResponse: {
396-
REMSReferenceID: request.REMSReferenceID,
397-
Patient: request.Patient,
398-
Pharmacy: request.Pharmacy,
399-
Prescriber: request.Prescriber,
400-
MedicationPrescribed: request.MedicationPrescribed,
422+
REMSReferenceID: remsReferenceID,
423+
Patient: patient,
424+
Pharmacy: pharmacy,
425+
Prescriber: prescriber,
426+
MedicationPrescribed: medicationPrescribed,
401427
Response: {
402428
ResponseStatus: {
403429
Approved: {
@@ -427,6 +453,14 @@ const buildDeniedResponse = (
427453
): string => {
428454
const builder = new Builder({ headless: false });
429455

456+
const patient = request.patient;
457+
const pharmacy = request.pharmacy;
458+
const prescriber = request.prescriber;
459+
const medicationPrescribed = request.medicationprescribed;
460+
const remsReferenceID = request.remsreferenceid;
461+
const solicitedModel = request.request?.solicitedmodel;
462+
const caseId = solicitedModel?.remscaseid;
463+
430464
const response = {
431465
Message: {
432466
$: {
@@ -441,15 +475,15 @@ const buildDeniedResponse = (
441475
Header: header,
442476
Body: {
443477
REMSResponse: {
444-
REMSReferenceID: request.REMSReferenceID,
445-
Patient: request.Patient,
446-
Pharmacy: request.Pharmacy,
447-
Prescriber: request.Prescriber,
448-
MedicationPrescribed: request.MedicationPrescribed,
478+
REMSReferenceID: remsReferenceID,
479+
Patient: patient,
480+
Pharmacy: pharmacy,
481+
Prescriber: prescriber,
482+
MedicationPrescribed: medicationPrescribed,
449483
Response: {
450484
ResponseStatus: {
451485
Denied: {
452-
REMSCaseID: request.request?.solicitedmodel?.remscaseid,
486+
REMSCaseID: caseId,
453487
DeniedReasonCode: reasonCode,
454488
REMSNote: note
455489
}
@@ -472,6 +506,12 @@ const buildInitiationClosedResponse = (
472506
): string => {
473507
const builder = new Builder({ headless: false });
474508

509+
const patient = request.patient;
510+
const pharmacy = request.pharmacy;
511+
const prescriber = request.prescriber;
512+
const medicationPrescribed = request.medicationprescribed;
513+
const remsReferenceID = request.remsreferenceid;
514+
475515
const response = {
476516
Message: {
477517
$: {
@@ -486,11 +526,11 @@ const buildInitiationClosedResponse = (
486526
Header: header,
487527
Body: {
488528
REMSInitiationResponse: {
489-
REMSReferenceID: request.REMSReferenceID,
490-
Patient: request.Patient,
491-
Pharmacy: request.Pharmacy,
492-
Prescriber: request.Prescriber,
493-
MedicationPrescribed: request.MedicationPrescribed,
529+
REMSReferenceID: remsReferenceID,
530+
Patient: patient,
531+
Pharmacy: pharmacy,
532+
Prescriber: prescriber,
533+
MedicationPrescribed: medicationPrescribed,
494534
Response: {
495535
ResponseStatus: {
496536
Closed: {
@@ -511,8 +551,12 @@ const buildInitiationClosedResponse = (
511551
const buildInitiationSuccessResponse = (header: any, request: any, remsCase: any): string => {
512552
const builder = new Builder({ headless: false });
513553

514-
const patient = request.Patient || request.patient;
515-
const humanPatient = patient?.HumanPatient || patient?.humanpatient;
554+
const patient = request.patient;
555+
const humanPatient = patient?.humanpatient;
556+
const pharmacy = request.pharmacy;
557+
const prescriber = request.prescriber;
558+
const medicationPrescribed = request.medicationprescribed;
559+
const remsReferenceID = request.remsreferenceid;
516560

517561
const response = {
518562
Message: {
@@ -528,7 +572,7 @@ const buildInitiationSuccessResponse = (header: any, request: any, remsCase: any
528572
Header: header,
529573
Body: {
530574
REMSInitiationResponse: {
531-
REMSReferenceID: request.REMSReferenceID,
575+
REMSReferenceID: remsReferenceID,
532576
Patient: {
533577
HumanPatient: {
534578
$: {
@@ -548,9 +592,9 @@ const buildInitiationSuccessResponse = (header: any, request: any, remsCase: any
548592
}
549593
}
550594
},
551-
Pharmacy: request.Pharmacy,
552-
Prescriber: request.Prescriber,
553-
MedicationPrescribed: request.MedicationPrescribed
595+
Pharmacy: pharmacy,
596+
Prescriber: prescriber,
597+
MedicationPrescribed: medicationPrescribed
554598
}
555599
}
556600
}

0 commit comments

Comments
 (0)