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
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ resource "aws_dynamodb_table" "supplier-configuration" {
type = "S"
}

attribute {
name = "packSpecificationId"
type = "S"
}

// The type-index GSI allows us to query for all supplier configurations of a given type (e.g. all letter supplier configurations)
global_secondary_index {
name = "EntityTypeIndex"
Expand All @@ -45,6 +50,13 @@ resource "aws_dynamodb_table" "supplier-configuration" {
projection_type = "ALL"
}

global_secondary_index {
name = "packSpecificationId-index"
hash_key = "PK"
range_key = "packSpecificationId"
projection_type = "ALL"
}

point_in_time_recovery {
enabled = true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ data "aws_iam_policy_document" "supplier_allocator_lambda" {

resources = [
aws_dynamodb_table.supplier-configuration.arn,
"${aws_dynamodb_table.supplier-configuration.arn}/index/volumeGroup-index"

"${aws_dynamodb_table.supplier-configuration.arn}/index/*"
]
}
}
11 changes: 11 additions & 0 deletions internal/datastore/src/__test__/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,22 @@ const createSupplierConfigTableCommand = new CreateTableCommand({
ProjectionType: "ALL",
},
},
{
IndexName: "packSpecificationId-index",
KeySchema: [
{ AttributeName: "PK", KeyType: "HASH" }, // Partition key for GSI
{ AttributeName: "packSpecificationId", KeyType: "RANGE" }, // Sort key for GSI
],
Projection: {
ProjectionType: "ALL",
},
},
],
AttributeDefinitions: [
{ AttributeName: "PK", AttributeType: "S" },
{ AttributeName: "SK", AttributeType: "S" },
{ AttributeName: "volumeGroup", AttributeType: "S" },
{ AttributeName: "packSpecificationId", AttributeType: "S" },
],
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,4 +263,86 @@ describe("SupplierConfigRepository", () => {
`Supplier with id ${supplierId} not found`,
);
});

test("getSupplierPacksForPackSpecification returns correct supplier packs", async () => {
const packSpecId = "pack-spec-123";
const supplierId = "supplier-123";
const supplierPackId = "supplier-pack-123";

await dbContext.docClient.send(
new PutCommand({
TableName: dbContext.config.supplierConfigTableName,
Item: {
PK: "SUPPLIER_PACK",
SK: supplierPackId,
id: supplierPackId,
packSpecificationId: packSpecId,
supplierId,
status: "PROD",
approval: "APPROVED",
},
}),
);

const result =
await repository.getSupplierPacksForPackSpecification(packSpecId);
expect(result).toEqual([
{
approval: "APPROVED",
id: supplierPackId,
packSpecificationId: packSpecId,
supplierId,
status: "PROD",
},
]);
});

test("getSupplierPacksForPackSpecification returns empty array for non-existent pack specification", async () => {
const packSpecId = "non-existent-pack-spec";
const result =
await repository.getSupplierPacksForPackSpecification(packSpecId);
expect(result).toEqual([]);
});

test("getPackSpecification returns correct pack specification details", async () => {
const packSpecId = "pack-spec-123";

await dbContext.docClient.send(
new PutCommand({
TableName: dbContext.config.supplierConfigTableName,
Item: {
PK: "PACK_SPECIFICATION",
SK: packSpecId,
id: packSpecId,
name: `Pack Specification ${packSpecId}`,
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
version: 1,
billingId: `billing-${packSpecId}`,
postage: { id: "postageId", size: "STANDARD" },
status: "PROD",
},
}),
);

const result = await repository.getPackSpecification(packSpecId);
expect(result).toEqual({
billingId: `billing-${packSpecId}`,
createdAt: expect.any(String),
id: packSpecId,
name: `Pack Specification ${packSpecId}`,
postage: { id: "postageId", size: "STANDARD" },
updatedAt: expect.any(String),
version: 1,
status: "PROD",
});
});

test("getPackSpecification throws error for non-existent pack specification", async () => {
const packSpecId = "non-existent-pack-spec";

await expect(repository.getPackSpecification(packSpecId)).rejects.toThrow(
`No pack specification found for id ${packSpecId}`,
);
});
});
44 changes: 44 additions & 0 deletions internal/datastore/src/supplier-config-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ import {
} from "@aws-sdk/lib-dynamodb";
import {
$LetterVariant,
$PackSpecification,
$Supplier,
$SupplierAllocation,
$SupplierPack,
$VolumeGroup,
LetterVariant,
PackSpecification,
Supplier,
SupplierAllocation,
SupplierPack,
VolumeGroup,
} from "@nhsdigital/nhs-notify-event-schemas-supplier-config";

Expand Down Expand Up @@ -97,4 +101,44 @@ export class SupplierConfigRepository {
}
return suppliers;
}

async getSupplierPacksForPackSpecification(
packSpecId: string,
): Promise<SupplierPack[]> {
const result = await this.ddbClient.send(
new QueryCommand({
TableName: this.config.supplierConfigTableName,
IndexName: "packSpecificationId-index",
KeyConditionExpression: "#pk = :pk AND #packSpecId = :packSpecId",
FilterExpression: "#status = :status AND #approval = :approval",
ExpressionAttributeNames: {
"#pk": "PK",
"#packSpecId": "packSpecificationId",
"#status": "status",
"#approval": "approval",
},
ExpressionAttributeValues: {
":pk": "SUPPLIER_PACK",
":packSpecId": packSpecId,
":status": "PROD",
":approval": "APPROVED",
},
}),
);

return $SupplierPack.array().parse(result.Items);
}

async getPackSpecification(packSpecId: string): Promise<PackSpecification> {
const result = await this.ddbClient.send(
new GetCommand({
TableName: this.config.supplierConfigTableName,
Key: { PK: "PACK_SPECIFICATION", SK: packSpecId },
}),
);
if (!result.Item) {
throw new Error(`No pack specification found for id ${packSpecId}`);
}
return $PackSpecification.parse(result.Item);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SQSClient, SendMessageCommand } from "@aws-sdk/client-sqs";
import { SQSEvent, SQSRecord } from "aws-lambda";
import pino from "pino";
import { SQSClient, SendMessageCommand } from "@aws-sdk/client-sqs";
import { LetterRequestPreparedEventV2 } from "@nhsdigital/nhs-notify-event-schemas-letter-rendering";
import { LetterRequestPreparedEvent } from "@nhsdigital/nhs-notify-event-schemas-letter-rendering-v1";
import {
Expand Down Expand Up @@ -158,6 +158,17 @@ function setupDefaultMocks() {
priority: 1,
billingId: "billing-1",
});
(supplierConfig.getPreferredSupplierPacks as jest.Mock).mockResolvedValue([
{
packSpecificationId: "pack-spec-1",
},
]);
(supplierConfig.getPackSpecification as jest.Mock).mockResolvedValue({
id: "pack-spec-1",
type: "A4",
colour: false,
duplex: false,
});
}

describe("createSupplierAllocatorHandler", () => {
Expand Down
38 changes: 34 additions & 4 deletions lambdas/supplier-allocator/src/handler/allocate-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@ import { SendMessageCommand } from "@aws-sdk/client-sqs";
import { LetterRequestPreparedEvent } from "@nhsdigital/nhs-notify-event-schemas-letter-rendering-v1";
import {
LetterVariant,
PackSpecification,
Supplier,
SupplierAllocation,
SupplierPack,
VolumeGroup,
} from "@nhsdigital/nhs-notify-event-schemas-supplier-config";
import { LetterRequestPreparedEventV2 } from "@nhsdigital/nhs-notify-event-schemas-letter-rendering";
import z from "zod";
import { Unit } from "aws-embedded-metrics";
import { MetricEntry, MetricStatus, buildEMFObject } from "@internal/helpers";
import {
getPackSpecification,
getPreferredSupplierPacks,
getSupplierAllocationsForVolumeGroup,
getSupplierDetails,
getSuppliersWithValidPack,
getVariantDetails,
getVolumeGroupDetails,
} from "../services/supplier-config";
Expand Down Expand Up @@ -83,19 +88,44 @@ async function getSupplierFromConfig(letterEvent: PreparedEvents, deps: Deps) {
variantDetails.supplierId,
);

const supplierDetails: Supplier[] = await getSupplierDetails(
supplierAllocations,
const supplierIds = supplierAllocations.map((alloc) => alloc.supplier);

const allocatedSuppliers: Supplier[] = await getSupplierDetails(
supplierIds,
deps,
);

const preferredSupplierPacks: SupplierPack[] =
await getPreferredSupplierPacks(
variantDetails.packSpecificationIds,
allocatedSuppliers,
deps,
);

const preferredPack: PackSpecification = await getPackSpecification(
preferredSupplierPacks[0].packSpecificationId,
deps,
);

const suppliersForPack: Supplier[] = await getSuppliersWithValidPack(
allocatedSuppliers,
preferredPack.id,
deps,
);

deps.logger.info({
description: "Fetched supplier details for supplier allocations",
variantId: letterEvent.data.letterVariantId,
volumeGroupId: volumeGroupDetails.id,
supplierAllocationIds: supplierAllocations.map((a) => a.id),
supplierDetails,
allocatedSuppliers,
eligiblePacks: variantDetails.packSpecificationIds,
preferredSupplierPacks,
preferredPack,
suppliersForPack,
});

return supplierDetails;
return allocatedSuppliers;
} catch (error) {
deps.logger.error({
description: "Error fetching supplier from config",
Expand Down
Loading
Loading