Skip to content

Commit 3a92c5d

Browse files
committed
chore: expand pharmacy ODS code for individual log update
1 parent fbc1174 commit 3a92c5d

File tree

2 files changed

+175
-1
lines changed

2 files changed

+175
-1
lines changed

packages/nhsNotifyLambda/src/utils/notify.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ async function makeFakeNotifyRequest(
119119
})
120120
}
121121

122-
function logNotificationRequest(logger: Logger,
122+
export function logNotificationRequest(logger: Logger,
123123
messageBatchReference: string, messages: Array<MessageBatchItem>,
124124
data: Array<NotifyDataItemMessage>, messageStatus: string) {
125125
// TODO: preserve legacy logging until reports updated
@@ -142,6 +142,7 @@ function logNotificationRequest(logger: Logger,
142142
messageBatchReference,
143143
messageIndex: index,
144144
nhsNumber: message.recipient.nhsNumber,
145+
pharmacyOdsCode: correspondingData?.PSUDataItem.PharmacyODSCode,
145146
messageReference: message.messageReference,
146147
psuRequestId: correspondingData?.PSUDataItem.RequestID,
147148
notifyMessageId: messageStatus === "silent running" ? crypto.randomUUID() : correspondingData?.notifyMessageId,
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
import {jest, describe, it} from "@jest/globals"
2+
3+
import {Logger} from "@aws-lambda-powertools/logger"
4+
import {logNotificationRequest} from "../src/utils/notify"
5+
import {MessageBatchItem, NotifyDataItemMessage} from "../src/utils/types"
6+
import {LOG_MESSAGES} from "@psu-common/utilities"
7+
8+
// Mock crypto.randomUUID
9+
const mockUUID = "550e8400-e29b-41d4-a716-446655440000"
10+
global.crypto = {
11+
randomUUID: () => mockUUID
12+
} as unknown as Crypto
13+
14+
const mockInfo = jest.fn()
15+
const mockError = jest.fn()
16+
const mockWarn = jest.fn()
17+
18+
describe("logNotificationRequest", () => {
19+
let mockLogger: Logger
20+
let messages: Array<MessageBatchItem>
21+
let data: Array<NotifyDataItemMessage>
22+
const messageBatchReference = "batch-ref-123"
23+
24+
beforeEach(() => {
25+
mockLogger = {
26+
info: mockInfo,
27+
error: mockError,
28+
warn: mockWarn
29+
} as unknown as Logger
30+
31+
messages = [
32+
{
33+
messageReference: "msg-ref-1",
34+
recipient: {nhsNumber: "9453740578"},
35+
originator: {odsCode: "FA566"},
36+
personalisation: {}
37+
},
38+
{
39+
messageReference: "msg-ref-2",
40+
recipient: {nhsNumber: "9912003071"},
41+
originator: {odsCode: "A83008"},
42+
personalisation: {}
43+
}
44+
]
45+
46+
const dataItem1: NotifyDataItemMessage = {
47+
messageReference: "msg-ref-1",
48+
PSUDataItem: {
49+
PatientNHSNumber: "9453740578",
50+
PharmacyODSCode: "FA566",
51+
RequestID: "req-1",
52+
TaskID: "",
53+
Status: ""
54+
},
55+
notifyMessageId: "notify-id-1"
56+
}
57+
const dataItem2: NotifyDataItemMessage = {
58+
messageReference: "msg-ref-2",
59+
PSUDataItem: {
60+
PatientNHSNumber: "9912003071",
61+
PharmacyODSCode: "A83008",
62+
RequestID: "req-2",
63+
TaskID: "",
64+
Status: ""
65+
},
66+
notifyMessageId: "notify-id-2"
67+
}
68+
69+
data = [dataItem1, dataItem2]
70+
71+
jest.clearAllMocks()
72+
})
73+
74+
it("should log legacy notification request summary", () => {
75+
logNotificationRequest(mockLogger, messageBatchReference, messages, data, "requested")
76+
77+
expect(mockInfo).toHaveBeenCalledWith("Requested notifications OK!", {
78+
messageBatchReference,
79+
messageReferences: [
80+
{nhsNumber: "9453740578", messageReference: "msg-ref-1", psuRequestId: "req-1"},
81+
{nhsNumber: "9912003071", messageReference: "msg-ref-2", psuRequestId: "req-2"}
82+
],
83+
messageStatus: "requested"
84+
})
85+
})
86+
87+
it("should log individual messages with correct details", () => {
88+
logNotificationRequest(mockLogger, messageBatchReference, messages, data, "requested")
89+
90+
const expectedCode = Object.keys(LOG_MESSAGES)
91+
.find(key => LOG_MESSAGES[key as keyof typeof LOG_MESSAGES] === LOG_MESSAGES.PSU0002)
92+
93+
expect(mockInfo).toHaveBeenCalledWith(LOG_MESSAGES.PSU0002, {
94+
reportCode: expectedCode,
95+
messageBatchReference,
96+
messageIndex: 0,
97+
nhsNumber: "9453740578",
98+
pharmacyOdsCode: "FA566",
99+
messageReference: "msg-ref-1",
100+
psuRequestId: "req-1",
101+
notifyMessageId: "notify-id-1",
102+
messageStatus: "requested"
103+
})
104+
105+
expect(mockInfo).toHaveBeenCalledWith(LOG_MESSAGES.PSU0002, {
106+
reportCode: expectedCode,
107+
messageBatchReference,
108+
messageIndex: 1,
109+
nhsNumber: "9912003071",
110+
pharmacyOdsCode: "A83008",
111+
messageReference: "msg-ref-2",
112+
psuRequestId: "req-2",
113+
notifyMessageId: "notify-id-2",
114+
messageStatus: "requested"
115+
})
116+
})
117+
118+
it("should generate UUID for notifyMessageId when messageStatus is 'silent running'", () => {
119+
logNotificationRequest(mockLogger, messageBatchReference, messages, data, "silent running")
120+
121+
const calls = mockInfo.mock.calls.filter(call => call[0] === LOG_MESSAGES.PSU0002)
122+
123+
expect(calls[0][1]).toMatchObject({
124+
notifyMessageId: mockUUID,
125+
messageStatus: "silent running"
126+
})
127+
128+
expect(calls[1][1]).toMatchObject({
129+
notifyMessageId: mockUUID,
130+
messageStatus: "silent running"
131+
})
132+
})
133+
134+
it("should handle missing corresponding data gracefully", () => {
135+
const messagesWithExtra = [
136+
...messages,
137+
{
138+
messageReference: "msg-ref-3",
139+
recipient: {nhsNumber: "1111111111"},
140+
originator: {odsCode: "DEF456"},
141+
personalisation: {}
142+
}
143+
]
144+
145+
logNotificationRequest(mockLogger, messageBatchReference, messagesWithExtra, data, "requested")
146+
147+
const individualLogs = mockInfo.mock.calls.filter(call => call[0] === LOG_MESSAGES.PSU0002)
148+
149+
expect(individualLogs[2][1]).toMatchObject({
150+
pharmacyOdsCode: undefined,
151+
psuRequestId: undefined,
152+
notifyMessageId: undefined
153+
})
154+
})
155+
156+
it("should log correct number of times", () => {
157+
logNotificationRequest(mockLogger, messageBatchReference, messages, data, "requested")
158+
159+
// 1 legacy log + 2 individual message logs
160+
expect(mockLogger.info).toHaveBeenCalledTimes(3)
161+
})
162+
163+
it("should handle empty messages array", () => {
164+
logNotificationRequest(mockLogger, messageBatchReference, [], [], "requested")
165+
166+
expect(mockLogger.info).toHaveBeenCalledTimes(1)
167+
expect(mockLogger.info).toHaveBeenCalledWith("Requested notifications OK!", {
168+
messageBatchReference,
169+
messageReferences: [],
170+
messageStatus: "requested"
171+
})
172+
})
173+
})

0 commit comments

Comments
 (0)