|
| 1 | +import { |
| 2 | + DynamoDBDocumentClient, |
| 3 | + GetCommand, |
| 4 | + PutCommand, |
| 5 | + UpdateCommand, |
| 6 | +} from "@aws-sdk/lib-dynamodb"; |
| 7 | +import { |
| 8 | + $DailyAllocation, |
| 9 | + $OverallAllocation, |
| 10 | + DailyAllocation, |
| 11 | + OverallAllocation, |
| 12 | +} from "./types"; |
| 13 | + |
| 14 | +export type SupplierQuotasRepositoryConfig = { |
| 15 | + supplierQuotasTableName: string; |
| 16 | +}; |
| 17 | + |
| 18 | +function ItemForRecord( |
| 19 | + entity: string, |
| 20 | + id: string, |
| 21 | + record: Record<string, any>, |
| 22 | +): Record<string, any> { |
| 23 | + return { |
| 24 | + pk: `ENTITY#${entity}`, |
| 25 | + sk: `ID#${id}`, |
| 26 | + ...record, |
| 27 | + }; |
| 28 | +} |
| 29 | + |
| 30 | +export class SupplierQuotasRepository { |
| 31 | + constructor( |
| 32 | + readonly ddbClient: DynamoDBDocumentClient, |
| 33 | + readonly config: SupplierQuotasRepositoryConfig, |
| 34 | + ) {} |
| 35 | + |
| 36 | + async getOverallAllocation(groupId: string): Promise<OverallAllocation> { |
| 37 | + const result = await this.ddbClient.send( |
| 38 | + new GetCommand({ |
| 39 | + TableName: this.config.supplierQuotasTableName, |
| 40 | + Key: { pk: "ENTITY#overall-allocation", sk: `ID#${groupId}` }, |
| 41 | + }), |
| 42 | + ); |
| 43 | + if (!result.Item) { |
| 44 | + throw new Error( |
| 45 | + `No overall allocation found for volume group id ${groupId}`, |
| 46 | + ); |
| 47 | + } |
| 48 | + return $OverallAllocation.parse(result.Item); |
| 49 | + } |
| 50 | + |
| 51 | + async putOverallAllocation(allocation: OverallAllocation): Promise<void> { |
| 52 | + await this.ddbClient.send( |
| 53 | + new PutCommand({ |
| 54 | + TableName: this.config.supplierQuotasTableName, |
| 55 | + Item: ItemForRecord( |
| 56 | + "overall-allocation", |
| 57 | + allocation.id, |
| 58 | + $OverallAllocation.parse(allocation), |
| 59 | + ), |
| 60 | + }), |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + // Update the overallAllocation table updating the allocations array for a given volume group |
| 65 | + // or adding the value if the supplier is not present // |
| 66 | + async updateOverallAllocation( |
| 67 | + groupId: string, |
| 68 | + supplierId: string, |
| 69 | + newAllocation: number, |
| 70 | + ): Promise<void> { |
| 71 | + const overallAllocation = await this.getOverallAllocation(groupId); |
| 72 | + const currentAllocation = overallAllocation.allocations[supplierId] ?? 0; |
| 73 | + const updatedAllocation = currentAllocation + newAllocation; |
| 74 | + |
| 75 | + await this.ddbClient.send( |
| 76 | + new UpdateCommand({ |
| 77 | + TableName: this.config.supplierQuotasTableName, |
| 78 | + Key: { pk: "ENTITY#overall-allocation", sk: `ID#${groupId}` }, |
| 79 | + UpdateExpression: |
| 80 | + "SET allocations.#supplierId = :updatedAllocation, updatedAt = :updatedAt", |
| 81 | + ExpressionAttributeNames: { |
| 82 | + "#supplierId": supplierId, |
| 83 | + }, |
| 84 | + ExpressionAttributeValues: { |
| 85 | + ":updatedAllocation": updatedAllocation, |
| 86 | + ":updatedAt": new Date().toISOString(), |
| 87 | + }, |
| 88 | + }), |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + async getDailyAllocation( |
| 93 | + groupId: string, |
| 94 | + date: string, |
| 95 | + ): Promise<DailyAllocation> { |
| 96 | + const result = await this.ddbClient.send( |
| 97 | + new GetCommand({ |
| 98 | + TableName: this.config.supplierQuotasTableName, |
| 99 | + Key: { |
| 100 | + pk: "ENTITY#daily-allocation", |
| 101 | + sk: `ID#${groupId}#DATE#${date}`, |
| 102 | + }, |
| 103 | + }), |
| 104 | + ); |
| 105 | + if (!result.Item) { |
| 106 | + throw new Error( |
| 107 | + `No daily allocation found for volume group id ${groupId} and date ${date}`, |
| 108 | + ); |
| 109 | + } |
| 110 | + return $DailyAllocation.parse(result.Item); |
| 111 | + } |
| 112 | + |
| 113 | + async putDailyAllocation(allocation: DailyAllocation): Promise<void> { |
| 114 | + await this.ddbClient.send( |
| 115 | + new PutCommand({ |
| 116 | + TableName: this.config.supplierQuotasTableName, |
| 117 | + Item: ItemForRecord( |
| 118 | + "daily-allocation", |
| 119 | + `${allocation.volumeGroup}#DATE#${allocation.date}`, |
| 120 | + $DailyAllocation.parse(allocation), |
| 121 | + ), |
| 122 | + }), |
| 123 | + ); |
| 124 | + } |
| 125 | + |
| 126 | + async updateDailyAllocation( |
| 127 | + groupId: string, |
| 128 | + date: string, |
| 129 | + supplierId: string, |
| 130 | + newAllocation: number, |
| 131 | + ): Promise<void> { |
| 132 | + const dailyAllocation = await this.getDailyAllocation(groupId, date); |
| 133 | + const currentAllocation = dailyAllocation.allocations[supplierId] ?? 0; |
| 134 | + const updatedAllocation = currentAllocation + newAllocation; |
| 135 | + |
| 136 | + await this.ddbClient.send( |
| 137 | + new UpdateCommand({ |
| 138 | + TableName: this.config.supplierQuotasTableName, |
| 139 | + Key: { |
| 140 | + pk: "ENTITY#daily-allocation", |
| 141 | + sk: `ID#${groupId}#DATE#${date}`, |
| 142 | + }, |
| 143 | + UpdateExpression: |
| 144 | + "SET allocations.#supplierId = :updatedAllocation, updatedAt = :updatedAt", |
| 145 | + ExpressionAttributeNames: { |
| 146 | + "#supplierId": supplierId, |
| 147 | + }, |
| 148 | + ExpressionAttributeValues: { |
| 149 | + ":updatedAllocation": updatedAllocation, |
| 150 | + ":updatedAt": new Date().toISOString(), |
| 151 | + }, |
| 152 | + }), |
| 153 | + ); |
| 154 | + } |
| 155 | +} |
0 commit comments