Skip to content

Commit b766a5b

Browse files
add filters and error checking
1 parent 0dcf714 commit b766a5b

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
@@ -106,9 +106,71 @@ async function getVolumeGroupDetails(
106106
groupId,
107107
});
108108
}
109+
110+
if (
111+
groupDetails.status !== "PROD" &&
112+
(new Date(groupDetails.startDate) > new Date() ||
113+
(groupDetails.endDate && new Date(groupDetails.endDate) < new Date()))
114+
) {
115+
deps.logger.error({
116+
description: "Volume group is not active based on status and dates",
117+
groupId,
118+
status: groupDetails.status,
119+
startDate: groupDetails.startDate,
120+
endDate: groupDetails.endDate,
121+
});
122+
throw new Error(`Volume group with id ${groupId} is not active`);
123+
}
109124
return groupDetails;
110125
}
111126

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

131193
const supplierAllocations: SupplierAllocation[] =
132-
await deps.supplierConfigRepo.getSupplierAllocationsForVolumeGroup(
194+
await getSupplierAllocationsForVolumeGroup(
133195
variantDetails.volumeGroupId,
196+
variantDetails.supplierId ?? "",
197+
deps,
134198
);
135199
deps.logger.info({
136200
description: "Fetched supplier allocations for volume group",

0 commit comments

Comments
 (0)