Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
27 changes: 24 additions & 3 deletions packages/cpt-ui/src/constants/ui-strings/PrescriptionMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,33 @@ export const PRESCRIPTION_MESSAGES: Record<string, string> = {
"admin-action-updated": "An action update has been made by an EPS Prescription Tracker system administrator",
"prescription-reset": "Dispenser has requested to reset the prescription",
"prescription-cancelled": "This prescription or item was cancelled",
"prescription-marked-for-cancellation": "This prescription has a pending cancellation, but the prescription or items on it have not been cancelled as it has been downloaded by a dispenser",
"subsequent-cancellation": "This prescription or item was cancelled as the prescription was returned or the items to be cancelled were recorded as 'Not Dispensed'",

"prescription-marked-for-cancellation":
"This prescription has a pending cancellation, but the prescription or items on it have not been cancelled as it has been downloaded by a dispenser",

"subsequent-cancellation":
"This prescription or item was cancelled as the prescription was returned or the items to be cancelled were recorded as 'Not Dispensed'",
"dispense-history-rebuilt": "Dispenser has corrected the prescription's dispensing history",
"urgent-batch-updated": "Updated by Urgent Admin Batch worker at NHS England (an internal EPS system event)",
"routine-batch-updated": "Updated by Routine Admin Batch worker at NHS England (an internal EPS system event)",
"non-urgent-batch-updated": "Updated by Non-Urgent Admin Batch worker at NHS England (an internal EPS system event)",
"document-batch-updated": "Updated by Document Batch worker at NHS England (an internal EPS system event)",
"item-partially-collected": "The item has been partially collected or received by the patient, in less quantity than was requested by prescriber"

"prescription-not-cancelled-dispensed":
"A prescriber requested to cancel this prescription or item, but it has already been dispensed",
"prescription-expired":
"This prescription or item has expired, so it cannot be cancelled",
"prescription-already-cancelled":
"A prescriber requested to cancel this prescription or item, but it had been cancelled already",
"prescription-cancellation-requested-other":
"This prescription or item is pending cancellation requested by a different prescriber",
"prescription-not-cancelled-not-dispensed":
"This prescription or item was marked as 'Not dispensed' by the dispenser, so it cannot be cancelled by the prescriber",
"prescription-or-item-not-found":
"Information about this prescription or item cannot be found",
"cancel-request-invalid":
"This cancellation request has not been processed because the prescriber did not give enough information",

"item-partially-collected":
"The item has been partially collected or received by the patient, in less quantity than was requested by prescriber"
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,5 @@ export async function processPrescriptionRequest(
issueNumber
})

return mergePrescriptionDetails(apigeeResponse.data, doHSData, odsCodes)
return mergePrescriptionDetails(apigeeResponse.data, doHSData, odsCodes, logger)
}
41 changes: 39 additions & 2 deletions packages/prescriptionDetailsLambda/src/utils/fhirMappers.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import {MedicationDispense, MedicationRequest, Patient} from "fhir/r4"
import {ItemDetails, PatientDetails} from "@cpt-ui-common/common-types"
import {findExtensionByKey, getBooleanFromNestedExtension, getCodeFromNestedExtension} from "./extensionUtils"
import {Logger} from "@aws-lambda-powertools/logger"

/**
* Maps message history titles names to semantic message codes
*/
export const mapMessageHistoryTitleToMessageCode = (title: string): string => {
export const mapMessageHistoryTitleToMessageCode = (title: string, logger?: Logger): string => {
const titleToCodeMap: Record<string, string> = {
"Prescription upload successful": "prescription-uploaded",
"Release Request successful": "release-requested",
Expand All @@ -17,9 +18,37 @@ export const mapMessageHistoryTitleToMessageCode = (title: string): string => {
"Administrative update successful": "admin-updated",
"Administrative Action Update Successful": "admin-action-updated",
"Prescription Reset request successful": "prescription-reset",
"Prescription/Item was cancelled": "prescription-cancelled",
"Prescription/item was cancelled": "prescription-cancelled",

"Prescription/item was not cancelled. With dispenser. Marked for cancellation":
"prescription-marked-for-cancellation",
"Prescription/item was not cancelled. With dispenser active. Marked for cancellation":
"prescription-marked-for-cancellation",

"Prescription or item was not cancelled. Prescription has been dispensed.":
"prescription-not-cancelled-dispensed",

"Prescription or item had expired.": "prescription-expired",

"Prescription or item had already been cancelled.": "prescription-already-cancelled",

"Prescription or item cancellation requested by another prescriber.":
"prescription-cancellation-requested-other",

"Prescription or item was not cancelled. Prescription has been not dispensed.":
"prescription-not-cancelled-not-dispensed",

"Prescription or item not found.": "prescription-or-item-not-found",
"Unable to process message due to invalid information within the cancel request.":
"cancel-request-invalid",
// eslint-disable-next-line max-len
"The item has been partially collected / received by the patient in a less quantity than was requested by the prescriber.":
"item-partially-collected",
// eslint-disable-next-line max-len
"The item has been partially collected or received by the patient, in less quantity than was requested by prescriber.":
"item-partially-collected",

"Subsequent cancellation": "subsequent-cancellation",
"Rebuild Dispense History successful": "dispense-history-rebuilt",
"Updated by Urgent Admin Batch worker": "urgent-batch-updated",
Expand All @@ -28,7 +57,15 @@ export const mapMessageHistoryTitleToMessageCode = (title: string): string => {
"Updated by Document Batch worker": "document-batch-updated"
}

return titleToCodeMap[title]
const mapped = titleToCodeMap[title]

if (!mapped) {
logger?.warn(`missing mapping for title: "${title}"`)
// fallback: show raw Spine message in UI
return title
}

return mapped
}

/**
Expand Down
11 changes: 7 additions & 4 deletions packages/prescriptionDetailsLambda/src/utils/responseMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
PrescriptionDetailsResponse
} from "@cpt-ui-common/common-types"
import {DoHSOrg} from "@cpt-ui-common/doHSClient"
import {Logger} from "@aws-lambda-powertools/logger"

/**
* Extracts a specific resource type from the FHIR Bundle
Expand Down Expand Up @@ -85,7 +86,8 @@ const extractMessageHistory = (
requestGroup: RequestGroup,
doHSData: DoHSData,
medicationRequests: Array<MedicationRequest>,
medicationDispenses: Array<MedicationDispense>
medicationDispenses: Array<MedicationDispense>,
logger?: Logger
): Array<MessageHistory> => {
// find the specific "Prescription status transitions" action
const historyAction = requestGroup.action?.find(action =>
Expand All @@ -102,7 +104,7 @@ const extractMessageHistory = (

const orgODS = action.participant![0].extension![0].valueReference!.identifier!.value!
const messageCodeDisplayName = action.title!
const messageCode = mapMessageHistoryTitleToMessageCode(messageCodeDisplayName)
const messageCode = mapMessageHistoryTitleToMessageCode(messageCodeDisplayName, logger)

let orgName: string | undefined = undefined

Expand Down Expand Up @@ -173,7 +175,8 @@ export const mergePrescriptionDetails = (
prescribingOrganization,
nominatedPerformer,
dispensingOrganization
}: PrescriptionOdsCodes
}: PrescriptionOdsCodes,
logger?: Logger
): PrescriptionDetailsResponse => {
// Extract resources from bundle
const requestGroup = extractResourcesFromBundle<RequestGroup>(bundle, "RequestGroup")[0]
Expand Down Expand Up @@ -214,7 +217,7 @@ export const mergePrescriptionDetails = (
// extract and format all the data
const patientDetails = extractPatientDetails(patient)
const items = extractItems(medicationRequests, medicationDispenses)
const messageHistory = extractMessageHistory(requestGroup, doHSData, medicationRequests, medicationDispenses)
const messageHistory = extractMessageHistory(requestGroup, doHSData, medicationRequests, medicationDispenses, logger)

// TODO: extract NHS App status from dispensing information extension
// const dispensingInfoExt = findExtensionByKey(dispense.extension, "DISPENSING_INFORMATION")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ describe("mapMessageHistoryTitleToMessageCode", () => {
})

it("should return undefined for unrecognized message titles", () => {
expect(mapMessageHistoryTitleToMessageCode("Unknown Message")).toBeUndefined()
expect(mapMessageHistoryTitleToMessageCode("")).toBeUndefined()
expect(mapMessageHistoryTitleToMessageCode("Unknown Message")).toBe("Unknown Message")
expect(mapMessageHistoryTitleToMessageCode("")).toBe("")
})

it("should handle undefined message titles gracefully", () => {
expect(mapMessageHistoryTitleToMessageCode("")).toBeUndefined()
expect(mapMessageHistoryTitleToMessageCode("")).toBe("")
})
})

Expand Down