Skip to content

Commit f6c2d06

Browse files
add more logging in upsert lambda
1 parent 6a5bd82 commit f6c2d06

1 file changed

Lines changed: 13 additions & 12 deletions

File tree

lambdas/upsert-letter/src/handler/upsert-handler.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
SQSHandler,
66
SQSRecord,
77
} from "aws-lambda";
8-
import { InsertLetter, UpdateLetter } from "@internal/datastore";
8+
import { InsertLetter, Letter, UpdateLetter } from "@internal/datastore";
99
import {
1010
$LetterRequestPreparedEvent,
1111
LetterRequestPreparedEvent,
@@ -26,7 +26,7 @@ type PreparedEvents = LetterRequestPreparedEventV2 | LetterRequestPreparedEvent;
2626
type UpsertOperation = {
2727
name: "Insert" | "Update";
2828
schemas: z.ZodSchema[];
29-
handler: (request: unknown, deps: Deps) => Promise<void>;
29+
handler: (request: unknown, deps: Deps) => Promise<Letter>;
3030
};
3131

3232
// small envelope that must exist in all inputs
@@ -37,7 +37,7 @@ function getOperationFromType(type: string): UpsertOperation {
3737
return {
3838
name: "Insert",
3939
schemas: [$LetterRequestPreparedEventV2, $LetterRequestPreparedEvent],
40-
handler: async (request, deps) => {
40+
handler: async (request, deps): Promise<Letter> => {
4141
const preparedRequest = request as PreparedEvents;
4242
const supplierSpec: SupplierSpec = resolveSupplierForVariant(
4343
preparedRequest.data.letterVariantId,
@@ -47,19 +47,19 @@ function getOperationFromType(type: string): UpsertOperation {
4747
preparedRequest,
4848
supplierSpec.supplierId,
4949
supplierSpec.specId,
50-
supplierSpec.specId, //use specId for now
50+
supplierSpec.specId, // use specId for now
5151
);
52-
await deps.letterRepo.putLetter(letterToInsert);
52+
return deps.letterRepo.putLetter(letterToInsert);
5353
},
5454
};
5555
if (type.startsWith("uk.nhs.notify.supplier-api.letter"))
5656
return {
5757
name: "Update",
5858
schemas: [$LetterEvent],
59-
handler: async (request, deps) => {
59+
handler: async (request, deps): Promise<Letter> => {
6060
const supplierEvent = request as LetterEvent;
6161
const letterToUpdate: UpdateLetter = mapToUpdateLetter(supplierEvent);
62-
await deps.letterRepo.updateLetterStatus(letterToUpdate);
62+
return deps.letterRepo.updateLetterStatus(letterToUpdate);
6363
},
6464
};
6565
throw new Error(`Unknown operation from type=${type}`);
@@ -86,7 +86,7 @@ function mapToInsertLetter(
8686
subject: upsertRequest.subject,
8787
createdAt: now,
8888
updatedAt: now,
89-
billingRef: billingRef,
89+
billingRef,
9090
};
9191
}
9292

@@ -108,7 +108,7 @@ function resolveSupplierForVariant(
108108
}
109109

110110
function parseSNSNotification(record: SQSRecord) {
111-
console.log("record in parseSNSNotificatino: ", record);
111+
console.log("record in parseSNSNotificatino:", record);
112112
const notification = JSON.parse(record.body) as Partial<SNSMessage>;
113113
if (
114114
notification.Type !== "Notification" ||
@@ -137,7 +137,8 @@ async function runUpsert(
137137
for (const schema of operation.schemas) {
138138
const r = schema.safeParse(letterEvent);
139139
if (r.success) {
140-
await operation.handler(r.data, deps);
140+
const letterSchemaParseResponse = await operation.handler(r.data, deps);
141+
console.log("operation handler response:", letterSchemaParseResponse);
141142
return;
142143
}
143144
}
@@ -150,11 +151,11 @@ export default function createUpsertLetterHandler(deps: Deps): SQSHandler {
150151
const batchItemFailures: SQSBatchItemFailure[] = [];
151152

152153
const tasks = event.Records.map(async (record) => {
153-
console.log("record in createUpsertLetterHandler: ", record);
154+
console.log("record in createUpsertLetterHandler:", record);
154155
try {
155156
const message: string = parseSNSNotification(record);
156157

157-
console.log("message after parsing: ", message);
158+
console.log("message after parsing:", message);
158159
const letterEvent: unknown = JSON.parse(message);
159160

160161
const type = getType(letterEvent);

0 commit comments

Comments
 (0)