|
1 | | -import { MedicationRequest, Coding, FhirResource, Task, Patient } from 'fhir/r4'; |
| 1 | +import { MedicationRequest, Coding, FhirResource, Task, Patient, Bundle } from 'fhir/r4'; |
2 | 2 | import Card, { Link, Suggestion, Action } from '../cards/Card'; |
3 | 3 | import { HookPrefetch, TypedRequestBody } from '../rems-cds-hooks/resources/HookTypes'; |
4 | 4 | import config from '../config'; |
@@ -503,6 +503,154 @@ export function handleHook( |
503 | 503 | } |
504 | 504 | } |
505 | 505 |
|
| 506 | +// process the MedicationRequests to add the Medication into contained resources |
| 507 | +function processMedicationRequests(medicationRequestsBundle: Bundle) { |
| 508 | + medicationRequestsBundle?.entry?.forEach(entry => { |
| 509 | + if (entry?.resource?.resourceType === 'MedicationRequest') { |
| 510 | + if (entry?.resource?.medicationReference) { |
| 511 | + const medicationReference = entry?.resource?.medicationReference; |
| 512 | + medicationRequestsBundle?.entry?.forEach(e => { |
| 513 | + if (e?.resource?.resourceType === 'Medication') { |
| 514 | + if ( |
| 515 | + e?.resource?.resourceType + '/' + e?.resource?.id === |
| 516 | + medicationReference?.reference |
| 517 | + ) { |
| 518 | + if (entry) { |
| 519 | + if (entry.resource) { |
| 520 | + const reference = e?.resource; |
| 521 | + const request = entry.resource as MedicationRequest; |
| 522 | + |
| 523 | + // add the reference as a contained resource to the request |
| 524 | + if (!request?.contained) { |
| 525 | + request.contained = []; |
| 526 | + request.contained.push(reference); |
| 527 | + } else { |
| 528 | + // only add to contained if not already in there |
| 529 | + let found = false; |
| 530 | + request.contained.forEach(c => { |
| 531 | + if (c.id === reference.id) { |
| 532 | + found = true; |
| 533 | + } |
| 534 | + }); |
| 535 | + if (!found) { |
| 536 | + request.contained.push(reference); |
| 537 | + } |
| 538 | + } |
| 539 | + } |
| 540 | + } |
| 541 | + } |
| 542 | + } |
| 543 | + }); |
| 544 | + } |
| 545 | + } |
| 546 | + }); |
| 547 | +} |
| 548 | + |
| 549 | +// handles order-sign and order-select currently |
| 550 | +export async function handleCardEncounter( |
| 551 | + res: any, |
| 552 | + hookPrefetch: HookPrefetch | undefined, |
| 553 | + contextRequest: FhirResource | undefined, |
| 554 | + patient: FhirResource | undefined |
| 555 | +) { |
| 556 | + //TODO: should we add the other pdf information links to the card, or just have the smart links? |
| 557 | + |
| 558 | + const medResource = hookPrefetch?.medicationRequests; |
| 559 | + const medicationRequestsBundle = medResource?.resourceType === 'Bundle' ? medResource : undefined; |
| 560 | + |
| 561 | + // create empty card array |
| 562 | + const cardArray: Card[] = []; |
| 563 | + |
| 564 | + // find all matching rems cases for the patient |
| 565 | + const patientName = patient?.resourceType === 'Patient' ? patient?.name?.[0] : undefined; |
| 566 | + const patientBirth = patient?.resourceType === 'Patient' ? patient?.birthDate : undefined; |
| 567 | + const remsCaseList = await remsCaseCollection.find({ |
| 568 | + patientFirstName: patientName?.given?.[0], |
| 569 | + patientLastName: patientName?.family, |
| 570 | + patientDOB: patientBirth |
| 571 | + }); |
| 572 | + |
| 573 | + // loop through all the rems cases in the list |
| 574 | + for (const remsCase of remsCaseList) { |
| 575 | + // find the drug in the medicationCollection that matches the REMS case to get the smart links |
| 576 | + const drug = await medicationCollection |
| 577 | + .findOne({ |
| 578 | + code: remsCase.drugCode, |
| 579 | + name: remsCase.drugName |
| 580 | + }) |
| 581 | + .exec(); |
| 582 | + |
| 583 | + // get the rule summary from the codemap |
| 584 | + const codeRule = codeMap[remsCase.drugCode]; |
| 585 | + let summary = ''; |
| 586 | + for (const rule of codeRule) { |
| 587 | + if (rule.stakeholderType === 'patient') { |
| 588 | + summary = rule.summary || remsCase.drugName || 'Rems'; |
| 589 | + } |
| 590 | + } |
| 591 | + |
| 592 | + // create the card |
| 593 | + let smartLinkCount = 0; |
| 594 | + const card = new Card(summary, CARD_DETAILS, source, 'info'); |
| 595 | + |
| 596 | + // process the MedicationRequests to add the Medication into contained resources |
| 597 | + if (medicationRequestsBundle) { |
| 598 | + processMedicationRequests(medicationRequestsBundle); |
| 599 | + } |
| 600 | + |
| 601 | + // find the matching MedicationRequest for the context |
| 602 | + const request = medicationRequestsBundle?.entry?.find(entry => { |
| 603 | + if (entry.resource) { |
| 604 | + if (entry.resource.resourceType === 'MedicationRequest') { |
| 605 | + const medReq: MedicationRequest = entry.resource; |
| 606 | + const medicationCode = getDrugCodeFromMedicationRequest(medReq); |
| 607 | + return remsCase.drugCode === medicationCode?.code; |
| 608 | + } |
| 609 | + } |
| 610 | + })?.resource; |
| 611 | + |
| 612 | + // if no valid request or not a MedicationRequest found skip this REMS case |
| 613 | + if (!request || (request && request.resourceType !== 'MedicationRequest')) { |
| 614 | + continue; |
| 615 | + } |
| 616 | + |
| 617 | + // loop through all of the ETASU requirements for this drug |
| 618 | + const requirements = drug?.requirements || []; |
| 619 | + for (const requirement of requirements) { |
| 620 | + // find all of the matching patient forms |
| 621 | + if (requirement?.stakeholderType === 'patient') { |
| 622 | + let found = false; |
| 623 | + // match the requirement to the metRequirement of the REMS case |
| 624 | + for (const metRequirement of remsCase.metRequirements) { |
| 625 | + // only add the link if the form is still needed to be completed |
| 626 | + if (metRequirement.requirementName === requirement.name) { |
| 627 | + found = true; |
| 628 | + if (!metRequirement.completed) { |
| 629 | + card.addLink(createSmartLink(requirement.name, requirement.appContext, request)); |
| 630 | + smartLinkCount++; |
| 631 | + } |
| 632 | + } |
| 633 | + } |
| 634 | + |
| 635 | + // if not in the list of metRequirements, add it as well |
| 636 | + if (!found) { |
| 637 | + card.addLink(createSmartLink(requirement.name, requirement.appContext, request)); |
| 638 | + smartLinkCount++; |
| 639 | + } |
| 640 | + } |
| 641 | + } |
| 642 | + |
| 643 | + // only add the card to the list if there is a link |
| 644 | + if (smartLinkCount > 0) { |
| 645 | + cardArray.push(card); |
| 646 | + } |
| 647 | + } |
| 648 | + |
| 649 | + res.json({ |
| 650 | + cards: cardArray |
| 651 | + }); |
| 652 | +} |
| 653 | + |
506 | 654 | export function createQuestionnaireSuggestion( |
507 | 655 | card: Card, |
508 | 656 | requirement: Requirement, |
|
0 commit comments