Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions .gitleaksignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,9 @@ debc75a97cfe551a69fd1e8694be483213322a9d:pact-contracts/pacts/letter-rendering/s
4fa1923947bbff2387218d698d766cbb7c121a0f:pact-contracts/pacts/letter-rendering/supplier-api-letter-request-prepared.json:generic-api-key:10
d005112adcfd286c3bef076214836dbb2fe8d0b5:.npmrc:npm-access-token:9
d005112adcfd286c3bef076214836dbb2fe8d0b5:.npmrc:github-pat:7
ff889d4c3f29da4468ecf1f05f467fe84d35b2a1:lambdas/supplier-mock/.aws-sam/build/SupplierMockFunction/index.js.map:ipv4:4
ff889d4c3f29da4468ecf1f05f467fe84d35b2a1:lambdas/supplier-mock/.aws-sam/build/SupplierMockFunction/index.js:ipv4:63
ff889d4c3f29da4468ecf1f05f467fe84d35b2a1:lambdas/supplier-mock/.aws-sam/build/SupplierMockFunction/index.js:ipv4:62
ff889d4c3f29da4468ecf1f05f467fe84d35b2a1:lambdas/supplier-mock/.aws-sam/build/SupplierMockFunction/index.js:ipv4:60
ff889d4c3f29da4468ecf1f05f467fe84d35b2a1:lambdas/supplier-mock/.aws-sam/build/SupplierMockFunction/index.js:ipv4:59
ff889d4c3f29da4468ecf1f05f467fe84d35b2a1:lambdas/supplier-mock/.aws-sam/build/SupplierMockFunction/index.js:ipv4:24
Comment on lines +28 to +33
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this has already gone in to main?

Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function createPreparedV1Event(
requestItemId: "requestItem1",
requestItemPlanId: "requestItemPlan1",
clientId: "client1",
campaignId: "campaign1",
campaignId: overrides.campaignId ?? "campaign1",
templateId: "template1",
url: overrides.url ?? "s3://letterDataBucket/letter1.pdf",
sha256Hash:
Expand Down Expand Up @@ -230,6 +230,37 @@ describe("createSupplierAllocatorHandler", () => {
});
});

test("parses SNS notification and sends message to SQS queue for v2 event without a campaignId", async () => {
const preparedEvent = createPreparedV2Event({ campaignId: "" });
const evt: SQSEvent = createSQSEvent([
createSqsRecord("msg1", JSON.stringify(preparedEvent)),
]);

setupDefaultMocks();
process.env.UPSERT_LETTERS_QUEUE_URL = "https://sqs.test.queue";

const handler = createSupplierAllocatorHandler(mockedDeps);
const result = await handler(evt, {} as any, {} as any);

expect(result).toBeDefined();
if (!result) throw new Error("expected BatchResponse, got void");

expect(result.batchItemFailures).toHaveLength(0);

expect(mockSqsClient.send).toHaveBeenCalledTimes(1);
const sendCall = (mockSqsClient.send as jest.Mock).mock.calls[0][0];
expect(sendCall).toBeInstanceOf(SendMessageCommand);

const messageBody = JSON.parse(sendCall.input.MessageBody);
expect(messageBody.letterEvent).toEqual(preparedEvent);
expect(messageBody.supplierSpec).toEqual({
supplierId: "supplier1",
specId: "spec1",
priority: 1,
billingId: "billing1",
});
});

test("parses SNS notification and sends message to SQS queue for v1 event", async () => {
const preparedEvent = createPreparedV1Event();

Expand Down
34 changes: 31 additions & 3 deletions lambdas/supplier-allocator/src/handler/allocate-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,27 @@ function emitMetrics(
}
}

function emitSupCampaignClientMetric(
letterEvent: PreparedEvents,
supplier: string,
status: string,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming Q: Is this status as in supplier status? Looks like it's used for metric key?

deps: Deps,
) {
const namespace = "supplier-allocator";
const { campaignId, clientId } = letterEvent.data;
const dimensions: Record<string, string> = {
Supplier: supplier,
ClientId: clientId,
CampaignId: campaignId || "unknown",
};
const metric: MetricEntry = {
key: status,
value: 1,
unit: Unit.Count,
};
deps.logger.info(buildEMFObject(namespace, dimensions, metric));
}

export default function createSupplierAllocatorHandler(deps: Deps): SQSHandler {
return async (event: SQSEvent) => {
const batchItemFailures: SQSBatchItemFailure[] = [];
Expand All @@ -153,8 +174,9 @@ export default function createSupplierAllocatorHandler(deps: Deps): SQSHandler {
const tasks = event.Records.map(async (record) => {
let supplier = "unknown";
let priority = "unknown";
let letterEvent: PreparedEvents | undefined;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can letterEvent ever be undefined, with the "as PreparedEvents" assertion below?
Maybe could be simplified to

Suggested change
let letterEvent: PreparedEvents | undefined;
let letterEvent: PreparedEvents;

try {
const letterEvent: unknown = JSON.parse(record.body);
letterEvent = JSON.parse(record.body) as PreparedEvents;

deps.logger.info({
description: "Extracted letter event",
Expand All @@ -163,8 +185,8 @@ export default function createSupplierAllocatorHandler(deps: Deps): SQSHandler {

validateType(letterEvent);

const supplierSpec = getSupplier(letterEvent as PreparedEvents, deps);
await getSupplierFromConfig(letterEvent as PreparedEvents, deps);
const supplierSpec = getSupplier(letterEvent, deps);
await getSupplierFromConfig(letterEvent, deps);

supplier = supplierSpec.supplierId;
priority = String(supplierSpec.priority);
Expand Down Expand Up @@ -199,6 +221,12 @@ export default function createSupplierAllocatorHandler(deps: Deps): SQSHandler {
);

incrementMetric(perAllocationSuccess, supplier, priority);
emitSupCampaignClientMetric(
letterEvent,
supplier,
"supplier_Campaign_Client",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this supposed to be a "constant" value?

deps,
);
} catch (error) {
deps.logger.error({
description: "Error processing allocation of record",
Expand Down
Loading