Skip to content

Commit 99a9153

Browse files
updated to store total daily allocations per supplier
1 parent 0618b68 commit 99a9153

8 files changed

Lines changed: 45 additions & 129 deletions

File tree

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

Lines changed: 13 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,13 @@ function createOverallAllocationItem(
2424

2525
function createDailyAllocationItem(
2626
allocationId: string,
27-
volumeGroupId: string,
2827
date: string,
2928
allocations: Record<string, number>,
3029
) {
3130
return {
3231
pk: "ENTITY#daily-allocation",
33-
sk: `ID#${volumeGroupId}#DATE#${date}`,
32+
sk: `ID#${date}`,
3433
id: allocationId,
35-
volumeGroup: volumeGroupId,
3634
date,
3735
allocations,
3836
updatedAt: new Date().toISOString(),
@@ -157,106 +155,76 @@ describe("SupplierQuotasRepository", () => {
157155

158156
test("getDailyAllocation returns correct allocation for existing group and date", async () => {
159157
const allocationId = "daily-allocation-123";
160-
const volumeGroupId = "group-123";
161158
const date = "2023-10-01";
162159
const allocations = { supplier1: 50, supplier2: 75 };
163160
await dbContext.docClient.send(
164161
new PutCommand({
165162
TableName: dbContext.config.supplierQuotasTableName,
166-
Item: createDailyAllocationItem(
167-
allocationId,
168-
volumeGroupId,
169-
date,
170-
allocations,
171-
),
163+
Item: createDailyAllocationItem(allocationId, date, allocations),
172164
}),
173165
);
174166

175-
const result = await repository.getDailyAllocation(volumeGroupId, date);
167+
const result = await repository.getDailyAllocation(date);
176168

177169
expect(result).toEqual({
178170
id: allocationId,
179-
volumeGroup: volumeGroupId,
180171
date,
181172
allocations,
182173
});
183174
});
184175

185-
test("getDailyAllocation returns undefined for non-existent group and date", async () => {
186-
const volumeGroupId = "non-existent-group";
187-
const date = "2023-10-01";
176+
test("getDailyAllocation returns undefined for non-existent date", async () => {
177+
const date = "2023-09-01";
188178

189-
const result = await repository.getDailyAllocation(volumeGroupId, date);
179+
const result = await repository.getDailyAllocation(date);
190180

191181
expect(result).toBeUndefined();
192182
});
193183

194184
test("putDailyAllocation stores allocation correctly", async () => {
195185
const allocation = {
196186
id: "daily-allocation-123",
197-
volumeGroup: "group-123",
198187
date: "2023-10-01",
199188
allocations: { supplier1: 50, supplier2: 75 },
200189
};
201190

202191
await repository.putDailyAllocation(allocation);
203192

204-
const result = await repository.getDailyAllocation(
205-
"group-123",
206-
"2023-10-01",
207-
);
193+
const result = await repository.getDailyAllocation("2023-10-01");
208194
expect(result).toEqual(allocation);
209195
});
210196

211197
test("updateDailyAllocation creates new allocation when none exists", async () => {
212-
const volumeGroupId = "group-123";
213198
const date = "2023-10-01";
214199
const supplierId = "supplier-123";
215200
const newAllocation = 25;
216201

217-
await repository.updateDailyAllocation(
218-
volumeGroupId,
219-
date,
220-
supplierId,
221-
newAllocation,
222-
);
202+
await repository.updateDailyAllocation(date, supplierId, newAllocation);
223203

224-
const result = await repository.getDailyAllocation(volumeGroupId, date);
204+
const result = await repository.getDailyAllocation(date);
225205
expect(result).toEqual({
226-
id: `${volumeGroupId}#DATE#${date}`,
227-
volumeGroup: volumeGroupId,
206+
id: `ID#${date}`,
228207
date,
229208
allocations: { [supplierId]: newAllocation },
230209
});
231210
});
232211

233212
test("updateDailyAllocation updates existing allocation", async () => {
234213
const allocationId = "daily-allocation-123";
235-
const volumeGroupId = "group-123";
236214
const date = "2023-10-01";
237215
const supplierId = "supplier-123";
238216
const initialAllocations = { [supplierId]: 50 };
239217
await dbContext.docClient.send(
240218
new PutCommand({
241219
TableName: dbContext.config.supplierQuotasTableName,
242-
Item: createDailyAllocationItem(
243-
allocationId,
244-
volumeGroupId,
245-
date,
246-
initialAllocations,
247-
),
220+
Item: createDailyAllocationItem(allocationId, date, initialAllocations),
248221
}),
249222
);
250223

251224
const newAllocation = 25;
252-
await repository.updateDailyAllocation(
253-
volumeGroupId,
254-
date,
255-
supplierId,
256-
newAllocation,
257-
);
225+
await repository.updateDailyAllocation(date, supplierId, newAllocation);
258226

259-
const result = await repository.getDailyAllocation(volumeGroupId, date);
227+
const result = await repository.getDailyAllocation(date);
260228
expect(result?.allocations[supplierId]).toBe(75);
261229
});
262230
});

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -106,20 +106,19 @@ export class SupplierQuotasRepository {
106106
}
107107
}
108108

109-
async getDailyAllocation(
110-
groupId: string,
111-
date: string,
112-
): Promise<DailyAllocation | undefined> {
109+
async getDailyAllocation(date: string): Promise<DailyAllocation | undefined> {
110+
console.log("Getting daily allocation for date:", date);
113111
const result = await this.ddbClient.send(
114112
new GetCommand({
115113
TableName: this.config.supplierQuotasTableName,
116114
Key: {
117115
pk: "ENTITY#daily-allocation",
118-
sk: `ID#${groupId}#DATE#${date}`,
116+
sk: `ID#${date}`,
119117
},
120118
}),
121119
);
122120
if (!result.Item) {
121+
console.log("No daily allocation found for date:", date);
123122
return undefined;
124123
}
125124
// Strip DynamoDB keys before parsing
@@ -130,25 +129,26 @@ export class SupplierQuotasRepository {
130129

131130
async putDailyAllocation(allocation: DailyAllocation): Promise<void> {
132131
const parsedAllocation = $DailyAllocation.parse(allocation);
133-
await this.ddbClient.send(
132+
console.log("Putting daily allocation:", parsedAllocation);
133+
const output = await this.ddbClient.send(
134134
new PutCommand({
135135
TableName: this.config.supplierQuotasTableName,
136136
Item: ItemForRecord(
137137
"daily-allocation",
138-
`${allocation.volumeGroup}#DATE#${allocation.date}`,
138+
allocation.date,
139139
parsedAllocation,
140140
),
141141
}),
142142
);
143+
console.log("PutDailyAllocation output:", output);
143144
}
144145

145146
async updateDailyAllocation(
146-
groupId: string,
147147
date: string,
148148
supplierId: string,
149149
newAllocation: number,
150150
): Promise<void> {
151-
const dailyAllocation = await this.getDailyAllocation(groupId, date);
151+
const dailyAllocation = await this.getDailyAllocation(date);
152152
const allocations = dailyAllocation?.allocations ?? {};
153153
const currentAllocation = allocations[supplierId] ?? 0;
154154
const updatedAllocation = currentAllocation + newAllocation;
@@ -164,7 +164,7 @@ export class SupplierQuotasRepository {
164164
TableName: this.config.supplierQuotasTableName,
165165
Key: {
166166
pk: "ENTITY#daily-allocation",
167-
sk: `ID#${groupId}#DATE#${date}`,
167+
sk: `ID#${date}`,
168168
},
169169
UpdateExpression:
170170
"SET allocations = :allocations, updatedAt = :updatedAt",
@@ -177,9 +177,8 @@ export class SupplierQuotasRepository {
177177
} else {
178178
// Create new allocation
179179
const newDailyAllocation: DailyAllocation = {
180-
id: `${groupId}#DATE#${date}`,
180+
id: `ID#${date}`,
181181
date,
182-
volumeGroup: groupId,
183182
allocations: { [supplierId]: updatedAllocation },
184183
};
185184
await this.putDailyAllocation(newDailyAllocation);

internal/datastore/src/types.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ export const $DailyAllocation = z
146146
.object({
147147
id: z.string(),
148148
date: z.string(),
149-
volumeGroup: idRef($VolumeGroup, "id"),
150149
allocations: z.record(
151150
idRef($Supplier, "id"),
152151
z.number().int().nonnegative(),
@@ -155,7 +154,7 @@ export const $DailyAllocation = z
155154
.meta({
156155
title: "DailyAllocation",
157156
description:
158-
"The daily allocation for a volume group, including all suppliers",
157+
"The daily allocation for a given date, including all suppliers",
159158
});
160159

161160
export type DailyAllocation = z.infer<typeof $DailyAllocation>;

lambdas/supplier-allocator/src/handler/__tests__/allocation-config.test.ts

Lines changed: 11 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -579,11 +579,7 @@ describe("filterSuppliersWithCapacity", () => {
579579
mockDeps.supplierQuotasRepo.getDailyAllocation as jest.Mock
580580
).mockResolvedValue(mockDailyAllocation);
581581

582-
const result = await filterSuppliersWithCapacity(
583-
mockSuppliers,
584-
"volume-group-1",
585-
mockDeps,
586-
);
582+
const result = await filterSuppliersWithCapacity(mockSuppliers, mockDeps);
587583

588584
expect(result).toEqual([mockSuppliers[0], mockSuppliers[2]]);
589585
});
@@ -593,14 +589,9 @@ describe("filterSuppliersWithCapacity", () => {
593589
mockDeps.supplierQuotasRepo.getDailyAllocation as jest.Mock
594590
).mockResolvedValue(null);
595591

596-
await filterSuppliersWithCapacity(
597-
mockSuppliers,
598-
"volume-group-1",
599-
mockDeps,
600-
);
592+
await filterSuppliersWithCapacity(mockSuppliers, mockDeps);
601593

602594
expect(mockDeps.supplierQuotasRepo.getDailyAllocation).toHaveBeenCalledWith(
603-
"volume-group-1",
604595
expect.any(String),
605596
);
606597
});
@@ -610,11 +601,7 @@ describe("filterSuppliersWithCapacity", () => {
610601
mockDeps.supplierQuotasRepo.getDailyAllocation as jest.Mock
611602
).mockResolvedValue(null);
612603

613-
const result = await filterSuppliersWithCapacity(
614-
mockSuppliers,
615-
"volume-group-1",
616-
mockDeps,
617-
);
604+
const result = await filterSuppliersWithCapacity(mockSuppliers, mockDeps);
618605

619606
expect(result).toEqual(mockSuppliers);
620607
});
@@ -632,11 +619,7 @@ describe("filterSuppliersWithCapacity", () => {
632619
mockDeps.supplierQuotasRepo.getDailyAllocation as jest.Mock
633620
).mockResolvedValue(mockDailyAllocation);
634621

635-
const result = await filterSuppliersWithCapacity(
636-
mockSuppliers,
637-
"volume-group-1",
638-
mockDeps,
639-
);
622+
const result = await filterSuppliersWithCapacity(mockSuppliers, mockDeps);
640623

641624
expect(result).toEqual([
642625
mockSuppliers[0],
@@ -661,11 +644,7 @@ describe("filterSuppliersWithCapacity", () => {
661644
"Testing filterSuppliersWithCapacity with mockDailyAllocation:",
662645
mockDailyAllocation,
663646
);
664-
const result = await filterSuppliersWithCapacity(
665-
mockSuppliers,
666-
"volume-group-1",
667-
mockDeps,
668-
);
647+
const result = await filterSuppliersWithCapacity(mockSuppliers, mockDeps);
669648

670649
expect(result).toEqual([mockSuppliers[0], mockSuppliers[1]]);
671650
});
@@ -681,11 +660,7 @@ describe("filterSuppliersWithCapacity", () => {
681660
mockDeps.supplierQuotasRepo.getDailyAllocation as jest.Mock
682661
).mockResolvedValue(mockDailyAllocation);
683662

684-
const result = await filterSuppliersWithCapacity(
685-
mockSuppliers,
686-
"volume-group-1",
687-
mockDeps,
688-
);
663+
const result = await filterSuppliersWithCapacity(mockSuppliers, mockDeps);
689664

690665
expect(result).toEqual(mockSuppliers);
691666
});
@@ -701,11 +676,7 @@ describe("filterSuppliersWithCapacity", () => {
701676
mockDeps.supplierQuotasRepo.getDailyAllocation as jest.Mock
702677
).mockResolvedValue(mockDailyAllocation);
703678

704-
const result = await filterSuppliersWithCapacity(
705-
[],
706-
"volume-group-1",
707-
mockDeps,
708-
);
679+
const result = await filterSuppliersWithCapacity([], mockDeps);
709680

710681
expect(result).toEqual([]);
711682
});
@@ -717,7 +688,7 @@ describe("filterSuppliersWithCapacity", () => {
717688
).mockRejectedValue(error);
718689

719690
await expect(
720-
filterSuppliersWithCapacity(mockSuppliers, "volume-group-1", mockDeps),
691+
filterSuppliersWithCapacity(mockSuppliers, mockDeps),
721692
).rejects.toThrow("Quotas service error");
722693
});
723694

@@ -730,16 +701,12 @@ describe("filterSuppliersWithCapacity", () => {
730701
mockDeps.supplierQuotasRepo.getDailyAllocation as jest.Mock
731702
).mockResolvedValue(mockDailyAllocation);
732703

733-
await filterSuppliersWithCapacity(
734-
mockSuppliers,
735-
"volume-group-1",
736-
mockDeps,
737-
);
704+
await filterSuppliersWithCapacity(mockSuppliers, mockDeps);
738705

739706
const callArgs = (
740707
mockDeps.supplierQuotasRepo.getDailyAllocation as jest.Mock
741708
).mock.calls[0];
742-
const dateArg = callArgs[1];
709+
const dateArg = callArgs[0];
743710

744711
expect(dateArg).toMatch(/^\d{4}-\d{2}-\d{2}$/);
745712
});
@@ -757,11 +724,7 @@ describe("filterSuppliersWithCapacity", () => {
757724
mockDeps.supplierQuotasRepo.getDailyAllocation as jest.Mock
758725
).mockResolvedValue(mockDailyAllocation);
759726

760-
const result = await filterSuppliersWithCapacity(
761-
mockSuppliers,
762-
"volume-group-1",
763-
mockDeps,
764-
);
727+
const result = await filterSuppliersWithCapacity(mockSuppliers, mockDeps);
765728

766729
expect(result).toEqual([
767730
mockSuppliers[0],

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,7 @@ async function getSupplierFromConfig(
9090
);
9191

9292
const suppliersForPackWithCapacity: Supplier[] =
93-
await filterSuppliersWithCapacity(
94-
allSuppliersForPack,
95-
volumeGroup.id,
96-
deps,
97-
);
93+
await filterSuppliersWithCapacity(allSuppliersForPack, deps);
9894

9995
// selected supplier id is determined by first calling selectSupplierByFactor for suppliers with capacity and if nothing is returned tryong again with all suppliers for pack
10096
const selectedSupplierId =

lambdas/supplier-allocator/src/handler/allocation-config.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,11 @@ export async function suppliersWithValidPack(
7777

7878
export async function filterSuppliersWithCapacity(
7979
suppliers: Supplier[],
80-
volumeGroupId: string,
8180
deps: Deps,
8281
): Promise<Supplier[]> {
8382
const dailyAllocationDate = new Date().toISOString().split("T")[0]; // Get current date in YYYY-MM-DD format
84-
const dailyAllocation = await deps.supplierQuotasRepo.getDailyAllocation(
85-
volumeGroupId,
86-
dailyAllocationDate,
87-
);
83+
const dailyAllocation =
84+
await deps.supplierQuotasRepo.getDailyAllocation(dailyAllocationDate);
8885
if (dailyAllocation) {
8986
const suppliersWithCapacity = suppliers.filter((supplier) => {
9087
const allocated = dailyAllocation.allocations[supplier.id] ?? 0;

0 commit comments

Comments
 (0)