Skip to content

Commit cecede6

Browse files
committed
I was passing the incorrect table name in to the post dated lambda
1 parent efc7629 commit cecede6

2 files changed

Lines changed: 41 additions & 38 deletions

File tree

SAMtemplates/functions/main.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ Resources:
536536
LOG_LEVEL: !Ref LogLevel
537537
NHS_NOTIFY_PRESCRIPTIONS_SQS_QUEUE_URL: !Ref NHSNotifyPrescriptionsSQSQueueUrl
538538
POST_DATED_PRESCRIPTIONS_SQS_QUEUE_URL: !Ref PostDatedNotificationsSQSQueueUrl
539-
TABLE_NAME: !Ref PrescriptionNotificationStatesTableName
539+
TABLE_NAME: !Ref PrescriptionStatusUpdatesTableName
540540
Events:
541541
ScheduleEvent:
542542
Type: ScheduleV2

packages/postDatedLambda/src/databaseClient.ts

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -106,50 +106,53 @@ export async function fetchExistingRecordsForPrescriptions(
106106
prescriptionCount: postDatedItems.length
107107
})
108108

109-
// Cache fetch promises per unique prescription/ODS pair to avoid duplicate lookups
110-
const recordsPromises = new Map<string, Promise<Array<PSUDataItem>>>()
111-
112-
const getOrCreateRecordsPromise = (
113-
prescriptionID: string,
114-
pharmacyODSCode: string
115-
): Promise<Array<PSUDataItem>> => {
116-
const lookupKey = createPrescriptionLookupKey(prescriptionID, pharmacyODSCode)
117-
118-
if (!recordsPromises.has(lookupKey)) {
119-
const fetchPromise = (async () => {
120-
try {
121-
// Each element of recordsPromises is a wrapper around the actual fetch promise for that ID/ODS pair
122-
return await getExistingRecordsByPrescriptionID(prescriptionID, pharmacyODSCode, logger)
123-
} catch (error) {
124-
logger.error("Failed to fetch existing records for prescription", {
125-
prescriptionID,
126-
pharmacyODSCode,
127-
error
128-
})
129-
return []
130-
}
131-
})()
132-
133-
recordsPromises.set(lookupKey, fetchPromise)
109+
const uniquePrescriptionLookups = new Map<string, {prescriptionID: string; pharmacyODSCode: string}>()
110+
for (const item of postDatedItems) {
111+
const lookupKey = createPrescriptionLookupKey(item.PrescriptionID, item.PharmacyODSCode)
112+
if (!uniquePrescriptionLookups.has(lookupKey)) {
113+
uniquePrescriptionLookups.set(lookupKey, {
114+
prescriptionID: item.PrescriptionID,
115+
pharmacyODSCode: item.PharmacyODSCode
116+
})
134117
}
135-
136-
return recordsPromises.get(lookupKey)!
137118
}
138119

139-
// Now, we map over the fetch promise wrappers, and await them all in parallel
140-
const results: Array<PostDatedPrescriptionWithExistingRecords> = await Promise.all(
141-
postDatedItems.map(async (postDatedData) => ({
142-
postDatedData,
143-
existingRecords: await getOrCreateRecordsPromise(
144-
postDatedData.PrescriptionID,
145-
postDatedData.PharmacyODSCode
146-
)
147-
}))
120+
// Create a map of prescription ID to existing records
121+
const existingRecordsMap = new Map<string, Array<PSUDataItem>>()
122+
123+
// Fetch existing records for each unique prescription ID
124+
await Promise.all(
125+
Array.from(uniquePrescriptionLookups.entries()).map(async ([lookupKey, {prescriptionID, pharmacyODSCode}]) => {
126+
try {
127+
const records = await getExistingRecordsByPrescriptionID(prescriptionID, pharmacyODSCode, logger)
128+
existingRecordsMap.set(lookupKey, records)
129+
} catch (error) {
130+
logger.error("Failed to fetch existing records for prescription", {
131+
prescriptionID,
132+
pharmacyODSCode,
133+
error
134+
})
135+
// Store empty array on error to allow processing to continue
136+
existingRecordsMap.set(lookupKey, [])
137+
}
138+
})
148139
)
149140

141+
// Map each post-dated item to its corresponding existing records
142+
const results: Array<PostDatedPrescriptionWithExistingRecords> = postDatedItems.map(
143+
(postDatedData) => {
144+
const lookupKey = createPrescriptionLookupKey(postDatedData.PrescriptionID, postDatedData.PharmacyODSCode)
145+
const existingRecords = existingRecordsMap.get(lookupKey) ?? []
146+
147+
return {
148+
postDatedData,
149+
existingRecords
150+
}
151+
})
152+
150153
logger.info("fetched existing prescription update records for all post-dated prescription IDs", {
151154
totalPrescriptions: postDatedItems.length,
152-
uniquePrescriptionLookups: recordsPromises.size
155+
uniquePrescriptionLookups: existingRecordsMap.size
153156
})
154157

155158
return results

0 commit comments

Comments
 (0)