@@ -285,30 +285,56 @@ const handleRxFill = async (message: any, res: Response) => {
285285 const header = message . header ;
286286 const rxFill = message . body . rxfill ;
287287 const patient = rxFill . patient ?. humanpatient ;
288- const drugNdcCode = rxFill . medicationprescribed ?. product ?. drugcoded ?. ndc ;
288+
289+ const medicationDispensed = rxFill . medicationdispensed ;
289290
290- logger . info ( `RxFill for patient: ${ patient ?. names ?. name ?. firstname } ${ patient ?. names ?. name ?. lastname } , NDC: ${ drugNdcCode } ` ) ;
291+ if ( ! medicationDispensed ) {
292+ logger . error ( 'MedicationDispensed not found in RxFill message' ) ;
293+ logger . error ( `Available RxFill fields: ${ JSON . stringify ( Object . keys ( rxFill ) ) } ` ) ;
294+ }
291295
292- // Update case dispense status
293- const remsCase = await remsCaseCollection . findOne ( {
294- patientFirstName : patient ?. names ?. name ?. firstname ,
295- patientLastName : patient ?. names ?. name ?. lastname ,
296- patientDOB : patient ?. dateofbirth ?. date ,
297- drugNdcCode : drugNdcCode
298- } ) ;
296+ let drugNdcCode = medicationDispensed ?. drugcoded ?. productcode ?. code
297+
298+ const patientInfo = {
299+ firstName : patient ?. name ?. firstname || patient ?. names ?. name ?. firstname ,
300+ lastName : patient ?. name ?. lastname || patient ?. names ?. name ?. lastname ,
301+ dob : patient ?. dateofbirth ?. date ,
302+ ndc : drugNdcCode ,
303+ } ;
304+
305+ logger . info ( `RxFill received for: ${ JSON . stringify ( patientInfo ) } ` ) ;
306+
307+ // Try to find case - if NDC not available, try by patient + drug description
308+ let remsCase = null ;
309+
310+ if ( drugNdcCode ) {
311+ remsCase = await remsCaseCollection . findOne ( {
312+ patientFirstName : patientInfo . firstName ,
313+ patientLastName : patientInfo . lastName ,
314+ patientDOB : patientInfo . dob ,
315+ drugNdcCode : drugNdcCode
316+ } ) ;
317+ }
299318
300319 if ( remsCase ) {
301320 remsCase . dispenseStatus = 'Dispensed' ;
302321 await remsCase . save ( ) ;
303- logger . info ( `Updated case ${ remsCase . case_number } dispense status to Dispensed` ) ;
322+
323+ logger . info ( `Updated case ${ remsCase . case_number } dispense status to 'Dispensed'` ) ;
324+ logger . info ( ` Patient: ${ remsCase . patientFirstName } ${ remsCase . patientLastName } ` ) ;
325+ logger . info ( ` Drug: ${ remsCase . drugName } (NDC: ${ remsCase . drugNdcCode } )` ) ;
326+ logger . info ( ` Case Status: ${ remsCase . status } ` ) ;
304327 } else {
305- logger . warn ( 'Case not found for RxFill notification' ) ;
328+ logger . warn ( `Case not found for RxFill notification` ) ;
329+ logger . warn ( ` Searched for: ${ JSON . stringify ( patientInfo ) } ` ) ;
306330 }
307331
332+ // Return success status per NCPDP
308333 res . type ( 'application/xml' ) ;
309334 return res . status ( 200 ) . send ( buildRxFillResponse ( header , rxFill ) ) ;
310335 } catch ( error : any ) {
311336 logger . error ( `ERROR in handleRxFill: ${ error . message } ` ) ;
337+ logger . error ( `Stack trace: ${ error . stack } ` ) ;
312338 res . type ( 'application/xml' ) ;
313339 return res . status ( 500 ) . send ( buildErrorResponse ( error . message ) ) ;
314340 }
@@ -552,7 +578,7 @@ const buildRxFillResponse = (header: any, rxFill: any): string => {
552578 Body : {
553579 Status : {
554580 Code : '000' ,
555- Description : 'Dispense notification received'
581+ Description : 'Dispense notification received and processed '
556582 }
557583 }
558584 }
0 commit comments