Skip to content

Commit 49afa05

Browse files
handle non existent overall allocations
1 parent 9c39cda commit 49afa05

2 files changed

Lines changed: 34 additions & 14 deletions

File tree

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,17 @@ export class SupplierQuotasRepository {
3333
readonly config: SupplierQuotasRepositoryConfig,
3434
) {}
3535

36-
async getOverallAllocation(groupId: string): Promise<OverallAllocation> {
36+
async getOverallAllocation(
37+
groupId: string,
38+
): Promise<OverallAllocation | undefined> {
3739
const result = await this.ddbClient.send(
3840
new GetCommand({
3941
TableName: this.config.supplierQuotasTableName,
4042
Key: { pk: "ENTITY#overall-allocation", sk: `ID#${groupId}` },
4143
}),
4244
);
4345
if (!result.Item) {
44-
throw new Error(
45-
`No overall allocation found for volume group id ${groupId}`,
46-
);
46+
return undefined;
4747
}
4848
return $OverallAllocation.parse(result.Item);
4949
}
@@ -69,7 +69,10 @@ export class SupplierQuotasRepository {
6969
newAllocation: number,
7070
): Promise<void> {
7171
const overallAllocation = await this.getOverallAllocation(groupId);
72-
const currentAllocation = overallAllocation.allocations[supplierId] ?? 0;
72+
const allocations = overallAllocation?.allocations ?? {};
73+
const currentAllocation = Object.hasOwn(allocations, supplierId)
74+
? allocations[supplierId]
75+
: 0;
7376
const updatedAllocation = currentAllocation + newAllocation;
7477

7578
await this.ddbClient.send(

lambdas/supplier-allocator/src/services/supplier-quotas.ts

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,16 @@ export async function calculateSupplierAllocatedFactor(
77
deps: Deps,
88
): Promise<{ supplierId: string; factor: number }[]> {
99
const volumeGroupId = supplierAllocations[0].volumeGroup; // Assuming all allocations are for the same volume group
10-
const overallAllocation: OverallAllocation =
10+
const overallAllocation =
1111
await deps.supplierQuotasRepo.getOverallAllocation(volumeGroupId);
1212

13+
if (!overallAllocation) {
14+
return supplierAllocations.map((allocation) => ({
15+
supplierId: allocation.supplier,
16+
factor: 0,
17+
}));
18+
}
19+
1320
const { allocations } = overallAllocation;
1421

1522
const totalAllocation = Object.values(allocations).reduce(
@@ -35,15 +42,25 @@ export async function updateSupplierQuota(
3542
const overallAllocation =
3643
await deps.supplierQuotasRepo.getOverallAllocation(groupId);
3744

38-
const updatedAllocations = {
39-
...overallAllocation.allocations,
40-
[supplierId]: newAllocation,
41-
};
45+
const updatedAllocations = overallAllocation
46+
? {
47+
...overallAllocation.allocations,
48+
[supplierId]: newAllocation,
49+
}
50+
: {
51+
[supplierId]: newAllocation,
52+
};
4253

43-
const updatedOverallAllocation: OverallAllocation = {
44-
...overallAllocation,
45-
allocations: updatedAllocations,
46-
};
54+
const updatedOverallAllocation: OverallAllocation = overallAllocation
55+
? {
56+
...overallAllocation,
57+
allocations: updatedAllocations,
58+
}
59+
: {
60+
id: groupId,
61+
volumeGroup: groupId,
62+
allocations: updatedAllocations,
63+
};
4764

4865
await deps.supplierQuotasRepo.putOverallAllocation(updatedOverallAllocation);
4966
}

0 commit comments

Comments
 (0)