Skip to content

Commit c5eb5d5

Browse files
committed
Accept types ending in status and version
1 parent afa92eb commit c5eb5d5

2 files changed

Lines changed: 36 additions & 3 deletions

File tree

lambdas/supplier-config-ingress/src/__tests__/supplier-config-ingress-handler.test.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,25 @@ function createSqsRecord(
1414

1515
function createSupplierConfig(overrides: Record<string, any> = {}) {
1616
return {
17-
id: "supplier-123",
18-
name: "Supplier supplier-123",
17+
id: "supplier-1",
18+
name: "Supplier 1",
1919
channelType: "LETTER",
2020
dailyCapacity: 2000,
2121
status: "PROD",
2222
...overrides,
2323
};
2424
}
2525

26+
function createSupplierPackConfig() {
27+
return {
28+
id: "supplier1-client1-campaign",
29+
packSpecificationId: "client-1-campaign",
30+
supplierId: "supplier1",
31+
approval: "APPROVED",
32+
status: "PROD",
33+
};
34+
}
35+
2636
describe("supplierConfigHandler", () => {
2737
let mockDeps: Deps;
2838
let handler: ReturnType<typeof createSupplierConfigIngressHandler>;
@@ -110,6 +120,22 @@ describe("supplierConfigHandler", () => {
110120
).not.toHaveBeenCalled();
111121
});
112122

123+
it("accepts a type field ending in a status and version", async () => {
124+
const data = createSupplierPackConfig();
125+
const record = createSqsRecord(
126+
data,
127+
"uk.nhs.notify.supplier-config.supplier-pack.prod.v1",
128+
);
129+
const event = { Records: [record] } as unknown as SQSEvent;
130+
131+
const result = await handler(event);
132+
133+
expect(result).toEqual({ batchItemFailures: [] });
134+
expect(
135+
mockDeps.supplierConfigRepo.upsertSupplierConfig,
136+
).toHaveBeenCalledWith("supplier-pack", data);
137+
});
138+
113139
it("rejects an entity not matching the appropriate schema", async () => {
114140
const invalidData = createSupplierConfig({ dailyCapacity: undefined });
115141
const record = createSqsRecord(invalidData);

lambdas/supplier-config-ingress/src/handler/supplier-config-ingress-handler.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,20 @@ const entitySchemas: Record<SupplierConfigEntity, z.ZodType<{ id: string }>> = {
3232
"supplier-pack": $SupplierPack as unknown as z.ZodType<{ id: string }>,
3333
};
3434

35+
function extractEntityFromType(type: string) {
36+
const elements = type.split(".");
37+
return $SupplierConfigEntity.parse(
38+
elements.length > 4 ? elements[4] : undefined,
39+
);
40+
}
41+
3542
function parseSupplierConfigFromRecord(record: SQSRecord): {
3643
entity: SupplierConfigEntity;
3744
config: { id: string };
3845
} {
3946
const event = $EventEnvelope.parse(JSON.parse(record.body));
4047

41-
const entity = $SupplierConfigEntity.parse(event.type.split(".").pop());
48+
const entity = extractEntityFromType(event.type);
4249
const config = entitySchemas[entity].parse(event.data);
4350

4451
return { entity, config };

0 commit comments

Comments
 (0)