Skip to content

Commit 8ffd8ee

Browse files
add filters and error checking
1 parent 118aa78 commit 8ffd8ee

2 files changed

Lines changed: 72 additions & 3 deletions

File tree

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export class SupplierConfigRepository {
4747
if (!result.Item) {
4848
throw new Error(`Volume group with id ${groupId} not found`);
4949
}
50+
5051
return $VolumeGroup.parse(result.Item);
5152
}
5253

@@ -63,13 +64,16 @@ export class SupplierConfigRepository {
6364
TableName: this.config.supplierConfigTableName,
6465
IndexName: "volumeGroup-index",
6566
KeyConditionExpression: "#pk = :pk AND #group = :groupId",
67+
FilterExpression: "#status = :status ",
6668
ExpressionAttributeNames: {
67-
"#pk": "PK", // make sure this is the GSI's PK attribute name
68-
"#group": "volumeGroup", // <-- use the **actual** GSI key name
69+
"#pk": "PK",
70+
"#group": "volumeGroup",
71+
"#status": "status",
6972
},
7073
ExpressionAttributeValues: {
7174
":pk": "SUPPLIER_ALLOCATION",
7275
":groupId": groupId,
76+
":status": "PROD",
7377
},
7478
}),
7579
);
@@ -84,6 +88,7 @@ export class SupplierConfigRepository {
8488
`No supplier allocations found for volume group id ${groupId}`,
8589
);
8690
}
91+
8792
return $SupplierAllocation.array().parse(result.Items);
8893
}
8994
}

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

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,71 @@ async function getVolumeGroupDetails(
9999
groupId,
100100
});
101101
}
102+
103+
if (
104+
groupDetails.status !== "PROD" &&
105+
(new Date(groupDetails.startDate) > new Date() ||
106+
(groupDetails.endDate && new Date(groupDetails.endDate) < new Date()))
107+
) {
108+
deps.logger.error({
109+
description: "Volume group is not active based on status and dates",
110+
groupId,
111+
status: groupDetails.status,
112+
startDate: groupDetails.startDate,
113+
endDate: groupDetails.endDate,
114+
});
115+
throw new Error(`Volume group with id ${groupId} is not active`);
116+
}
102117
return groupDetails;
103118
}
104119

120+
async function getSupplierAllocationsForVolumeGroup(
121+
groupId: string,
122+
supplierId: string,
123+
deps: Deps,
124+
): Promise<SupplierAllocation[]> {
125+
deps.logger.info({
126+
description: "Fetching supplier allocations for volume group from database",
127+
groupId,
128+
});
129+
const allocations =
130+
await deps.supplierConfigRepo.getSupplierAllocationsForVolumeGroup(groupId);
131+
132+
if (allocations.length > 0) {
133+
deps.logger.info({
134+
description:
135+
"Fetched supplier allocations for volume group from database",
136+
groupId,
137+
count: allocations.length,
138+
});
139+
} else {
140+
deps.logger.error({
141+
description: "No supplier allocations found for volume group id",
142+
groupId,
143+
});
144+
}
145+
146+
if (supplierId) {
147+
const filteredAllocations = allocations.filter(
148+
(alloc) => alloc.supplier === supplierId,
149+
);
150+
if (filteredAllocations.length === 0) {
151+
deps.logger.error({
152+
description:
153+
"No supplier allocations found for variantsupplier id in volume group",
154+
groupId,
155+
supplierId,
156+
});
157+
throw new Error(
158+
`No supplier allocations found for variant supplier id ${supplierId} in volume group ${groupId}`,
159+
);
160+
}
161+
return filteredAllocations;
162+
}
163+
164+
return allocations;
165+
}
166+
105167
async function getSupplierFromConfig(letterEvent: PreparedEvents, deps: Deps) {
106168
const variantDetails: LetterVariant = await getVariantDetails(
107169
letterEvent.data.letterVariantId,
@@ -122,8 +184,10 @@ async function getSupplierFromConfig(letterEvent: PreparedEvents, deps: Deps) {
122184
});
123185

124186
const supplierAllocations: SupplierAllocation[] =
125-
await deps.supplierConfigRepo.getSupplierAllocationsForVolumeGroup(
187+
await getSupplierAllocationsForVolumeGroup(
126188
variantDetails.volumeGroupId,
189+
variantDetails.supplierId ?? "",
190+
deps,
127191
);
128192
deps.logger.info({
129193
description: "Fetched supplier allocations for volume group",

0 commit comments

Comments
 (0)