Skip to content

Commit b508f0d

Browse files
get rest of dbdata
1 parent c32d801 commit b508f0d

3 files changed

Lines changed: 136 additions & 5 deletions

File tree

infrastructure/terraform/components/api/ddb_table_supplier_configuration.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ resource "aws_dynamodb_table" "supplier-configuration" {
2424
name = "entityType"
2525
type = "S"
2626
}
27+
28+
attribute {
29+
name = "volumeGroup"
30+
type = "S"
31+
}
2732
// The type-index GSI allows us to query for all supplier configurations of a given type (e.g. all letter supplier configurations)
2833
global_secondary_index {
2934
name = "EntityTypeIndex"
@@ -32,6 +37,13 @@ resource "aws_dynamodb_table" "supplier-configuration" {
3237
projection_type = "ALL"
3338
}
3439

40+
global_secondary_index {
41+
name = "volumeGroupIndex"
42+
hash_key = "PK"
43+
range_key = "volumeGroup"
44+
projection_type = "ALL"
45+
}
46+
3547
point_in_time_recovery {
3648
enabled = true
3749
}

internal/datastore/src/supplier-config-repository.ts

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
1-
import { DynamoDBDocumentClient, GetCommand } from "@aws-sdk/lib-dynamodb";
1+
import {
2+
DynamoDBDocumentClient,
3+
GetCommand,
4+
QueryCommand,
5+
} from "@aws-sdk/lib-dynamodb";
26
import { Logger } from "pino";
3-
import { $LetterVariant, LetterVariant } from "./SupplierConfigDomain";
7+
import {
8+
$LetterVariant,
9+
LetterVariant,
10+
$VolumeGroup,
11+
VolumeGroup,
12+
$SupplierAllocation,
13+
SupplierAllocation,
14+
} from "./SupplierConfigDomain";
415

516
export type SupplierConfigRepositoryConfig = {
617
supplierConfigTableName: string;
@@ -25,4 +36,39 @@ export class SupplierConfigRepository {
2536
}
2637
return $LetterVariant.parse(result.Item);
2738
}
39+
40+
async getVolumeGroup(groupId: string): Promise<VolumeGroup> {
41+
const result = await this.ddbClient.send(
42+
new GetCommand({
43+
TableName: this.config.supplierConfigTableName,
44+
Key: { PK: "VOLUME_GROUP", SK: groupId },
45+
}),
46+
);
47+
if (!result.Item) {
48+
throw new Error(`Volume group with id ${groupId} not found`);
49+
}
50+
return $VolumeGroup.parse(result.Item);
51+
}
52+
53+
async getSupplierAllocationsForVolumeGroup(
54+
groupId: string,
55+
): Promise<SupplierAllocation[]> {
56+
const result = await this.ddbClient.send(
57+
new QueryCommand({
58+
TableName: this.config.supplierConfigTableName,
59+
IndexName: "VolumeGroupIndex",
60+
KeyConditionExpression: "PK = :pk AND volumeGroup = :groupId",
61+
ExpressionAttributeValues: {
62+
":pk": "SUPPLIER_ALLOCATIONS",
63+
":groupId": groupId,
64+
},
65+
}),
66+
);
67+
if (!result.Item) {
68+
throw new Error(
69+
`Supplier allocations for volume group with id ${groupId} not found`,
70+
);
71+
}
72+
return $SupplierAllocation.array().parse(result.Item.items);
73+
}
2874
}

lambdas/supplier-allocator/src/handler/allocate-handler.ts

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { SQSBatchItemFailure, SQSEvent, SQSHandler } from "aws-lambda";
22
import { SendMessageCommand } from "@aws-sdk/client-sqs";
33
import { LetterRequestPreparedEvent } from "@nhsdigital/nhs-notify-event-schemas-letter-rendering-v1";
4-
import { LetterVariant } from "internal/datastore/src/SupplierConfigDomain";
4+
import {
5+
LetterVariant,
6+
SupplierAllocation,
7+
VolumeGroup,
8+
} from "internal/datastore/src/SupplierConfigDomain";
59
import { LetterRequestPreparedEventV2 } from "@nhsdigital/nhs-notify-event-schemas-letter-rendering";
610
import z from "zod";
711
import { Deps } from "../config/deps";
@@ -46,23 +50,90 @@ function validateType(event: unknown) {
4650
}
4751
}
4852

49-
async function getVariantDetails(variantId: string, deps: Deps) {
53+
async function getVariantDetails(
54+
variantId: string,
55+
deps: Deps,
56+
): Promise<LetterVariant> {
5057
deps.logger.info({
5158
description: "Fetching letter variant details from database",
5259
variantId,
5360
});
5461

5562
const variantDetails: LetterVariant =
5663
await deps.supplierConfigRepo.getLetterVariant(variantId);
64+
if (!variantDetails) {
65+
throw new Error(`No letter variant found for id: ${variantId}`);
66+
}
67+
5768
deps.logger.info({
5869
description: "Fetched letter variant details",
5970
variantId,
6071
variantDetails,
6172
});
73+
return variantDetails;
74+
}
75+
76+
async function getVolumeGroupDetails(
77+
groupId: string,
78+
deps: Deps,
79+
): Promise<VolumeGroup> {
80+
deps.logger.info({
81+
description: "Fetching volume group details from database",
82+
groupId,
83+
});
84+
85+
const groupDetails = await deps.supplierConfigRepo.getVolumeGroup(groupId);
86+
if (!groupDetails) {
87+
throw new Error(`No volume group found for id: ${groupId}`);
88+
}
89+
90+
deps.logger.info({
91+
description: "Fetched volume group details",
92+
groupId,
93+
groupDetails,
94+
});
95+
return groupDetails;
96+
}
97+
98+
async function getSupplierFromConfig(letterEvent: PreparedEvents, deps: Deps) {
99+
if (!letterEvent.data.letterVariantId) {
100+
throw new Error("letterVariantId is required in event data");
101+
}
102+
const variantDetails: LetterVariant = await getVariantDetails(
103+
letterEvent.data.letterVariantId,
104+
deps,
105+
);
106+
deps.logger.info({
107+
description: "Fetched variant details for letter variant",
108+
variantDetails,
109+
});
110+
111+
if (!variantDetails.volumeGroupId) {
112+
throw new Error(
113+
`No volume group id for variantId: ${letterEvent.data.letterVariantId}`,
114+
);
115+
}
116+
117+
const volumeGroupDetails: VolumeGroup = await getVolumeGroupDetails(
118+
variantDetails.volumeGroupId,
119+
deps,
120+
);
121+
deps.logger.info({
122+
description: "Fetched volume group details for letter variant",
123+
volumeGroupDetails,
124+
});
125+
126+
const supplierAllocations: SupplierAllocation[] =
127+
await deps.supplierConfigRepo.getSupplierAllocationsForVolumeGroup(
128+
variantDetails.volumeGroupId,
129+
);
130+
deps.logger.info({
131+
description: "Fetched supplier allocations for volume group",
132+
supplierAllocations,
133+
});
62134
}
63135

64136
function getSupplier(letterEvent: PreparedEvents, deps: Deps): SupplierSpec {
65-
getVariantDetails(letterEvent.data.letterVariantId, deps);
66137
return resolveSupplierForVariant(letterEvent.data.letterVariantId, deps);
67138
}
68139

@@ -81,6 +152,8 @@ export default function createSupplierAllocatorHandler(deps: Deps): SQSHandler {
81152

82153
validateType(letterEvent);
83154

155+
await getSupplierFromConfig(letterEvent as PreparedEvents, deps);
156+
84157
const supplierSpec = getSupplier(letterEvent as PreparedEvents, deps);
85158

86159
deps.logger.info({

0 commit comments

Comments
 (0)