Skip to content

Commit 12ed4a8

Browse files
committed
use saved medication on rems case creation to get ndc code from rxnorm
1 parent b072235 commit 12ed4a8

1 file changed

Lines changed: 33 additions & 19 deletions

File tree

src/lib/etasu.ts

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -197,17 +197,24 @@ export const createNewRemsCaseFromCDSHook = async (
197197
const patientDOB = patient.birthDate || '';
198198
const case_number = uid();
199199

200+
// Fetch the full medication from database to get NDC code
201+
const fullMedication = await medicationCollection.findOne({
202+
code: drug?.code
203+
}).exec();
204+
205+
const medicationData = fullMedication || drug;
206+
200207
// Check if case already exists
201208
const existingCase = await remsCaseCollection.findOne({
202209
patientFirstName: patientFirstName,
203210
patientLastName: patientLastName,
204211
patientDOB: patientDOB,
205-
drugCode: drug?.code
212+
drugCode: medicationData?.code
206213
});
207214

208215
if (existingCase) {
209216
console.log(
210-
`Case already exists for patient ${patientFirstName} ${patientLastName} and drug ${drug?.name}`
217+
`Case already exists for patient ${patientFirstName} ${patientLastName} and drug ${medicationData?.name}`
211218
);
212219
return existingCase;
213220
}
@@ -235,9 +242,9 @@ export const createNewRemsCaseFromCDSHook = async (
235242
case_number: case_number,
236243
status: 'Pending',
237244
dispenseStatus: 'Pending',
238-
drugName: drug?.name,
239-
drugCode: drug?.code,
240-
drugNdcCode: drug?.ndcCode,
245+
drugName: medicationData?.name,
246+
drugCode: medicationData?.code,
247+
drugNdcCode: medicationData?.ndcCode,
241248
patientFirstName: patientFirstName,
242249
patientLastName: patientLastName,
243250
patientDOB: patientDOB,
@@ -261,7 +268,7 @@ export const createNewRemsCaseFromCDSHook = async (
261268
};
262269

263270
// Iterate through ALL requirements and create as unmet (or link to existing if already completed)
264-
for (const requirement of drug.requirements) {
271+
for (const requirement of medicationData.requirements) {
265272
// Only process requirements that are required to dispense
266273
if (requirement.requiredToDispense) {
267274
// Figure out which stakeholder the requirement corresponds to
@@ -278,7 +285,7 @@ export const createNewRemsCaseFromCDSHook = async (
278285
.findOne({
279286
stakeholderId: stakeholderReference,
280287
requirementName: requirement.name,
281-
drugName: drug?.name
288+
drugName: medicationData?.name
282289
})
283290
.exec();
284291

@@ -293,7 +300,7 @@ export const createNewRemsCaseFromCDSHook = async (
293300
completed: false,
294301
requirementName: requirement.name,
295302
requirementDescription: requirement.description,
296-
drugName: drug?.name,
303+
drugName: medicationData?.name,
297304
stakeholderId: stakeholderReference,
298305
case_numbers: [case_number]
299306
};
@@ -497,12 +504,19 @@ const createMetRequirementAndNewCase = async (
497504
const patientDOB = patient.birthDate || '';
498505
let message = '';
499506

507+
// Fetch the full medication from database to get NDC code
508+
const fullMedication = await medicationCollection.findOne({
509+
code: drug?.code
510+
}).exec();
511+
512+
const medicationData = fullMedication || drug;
513+
500514
// Check if case already exists
501515
const existingCase = await remsCaseCollection.findOne({
502516
patientFirstName: patientFirstName,
503517
patientLastName: patientLastName,
504518
patientDOB: patientDOB,
505-
drugCode: drug?.code
519+
drugCode: medicationData?.code
506520
});
507521

508522
if (existingCase) {
@@ -519,7 +533,7 @@ const createMetRequirementAndNewCase = async (
519533
if (prescriberChanged || pharmacyChanged) {
520534
await handleStakeholderChangesAndRecordEvent(
521535
existingCase,
522-
drug,
536+
medicationData,
523537
practitionerReference,
524538
pharmacistReference,
525539
medicationRequestReference,
@@ -532,7 +546,7 @@ const createMetRequirementAndNewCase = async (
532546
.findOne({
533547
stakeholderId: reqStakeholderReference,
534548
requirementName: requirement.name,
535-
drugName: drug?.name
549+
drugName: medicationData?.name
536550
})
537551
.exec();
538552

@@ -608,9 +622,9 @@ const createMetRequirementAndNewCase = async (
608622
case_number: case_number,
609623
status: remsRequestCompletedStatus,
610624
dispenseStatus: dispenseStatusDefault,
611-
drugName: drug?.name,
612-
drugCode: drug?.code,
613-
drugNdcCode: drug?.ndcCode,
625+
drugName: medicationData?.name,
626+
drugCode: medicationData?.code,
627+
drugNdcCode: medicationData?.ndcCode,
614628
patientFirstName: patientFirstName,
615629
patientLastName: patientLastName,
616630
patientDOB: patientDOB,
@@ -639,7 +653,7 @@ const createMetRequirementAndNewCase = async (
639653
completedQuestionnaire: questionnaireResponse,
640654
requirementName: requirement.name,
641655
requirementDescription: requirement.description,
642-
drugName: drug?.name,
656+
drugName: medicationData?.name,
643657
stakeholderId: reqStakeholderReference,
644658
case_numbers: [case_number]
645659
};
@@ -651,7 +665,7 @@ const createMetRequirementAndNewCase = async (
651665
}
652666

653667
// iterate through all other requirements again to create corresponding false metRequirements / assign to existing
654-
for (const requirement2 of drug.requirements) {
668+
for (const requirement2 of medicationData.requirements) {
655669
// skip if the req found is the same as in the outer loop and has already been processed
656670
// && If the requirement is not the patient Status Form (when requiredToDispense == false)
657671
if (!(requirement2.resourceId === requirement.resourceId) && requirement2.requiredToDispense) {
@@ -668,7 +682,7 @@ const createMetRequirementAndNewCase = async (
668682
.findOne({
669683
stakeholderId: reqStakeholder2Reference,
670684
requirementName: requirement2.name,
671-
drugName: drug?.name
685+
drugName: medicationData?.name
672686
})
673687
.exec();
674688
if (matchedMetReq2) {
@@ -685,7 +699,7 @@ const createMetRequirementAndNewCase = async (
685699
completed: false,
686700
requirementName: requirement2.name,
687701
requirementDescription: requirement2.description,
688-
drugName: drug?.name,
702+
drugName: medicationData?.name,
689703
stakeholderId: reqStakeholder2Reference,
690704
case_numbers: [case_number]
691705
};
@@ -995,4 +1009,4 @@ export const processQuestionnaireResponseSubmission = async (requestBody: Bundle
9951009

9961010
export { getResource, getQuestionnaireResponse };
9971011

998-
export default router;
1012+
export default router;

0 commit comments

Comments
 (0)