|
| 1 | +import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; |
| 2 | +import { DynamoDBDocumentClient, GetCommand } from "@aws-sdk/lib-dynamodb"; |
| 3 | +import z from "zod"; |
| 4 | +import { SUPPLIER_QUOTAS_TABLENAME } from "../constants/api-constants"; |
| 5 | +import { logger } from "./pino-logger"; |
| 6 | + |
| 7 | +const ddb = new DynamoDBClient({}); |
| 8 | +const docClient = DynamoDBDocumentClient.from(ddb); |
| 9 | + |
| 10 | +export const overallAllocationSchema = z.object({ |
| 11 | + id: z.string(), |
| 12 | + volumeGroup: z.string(), |
| 13 | + allocations: z.record(z.string(), z.number()), |
| 14 | +}); |
| 15 | + |
| 16 | +export const dailyAllocationSchema = z.object({ |
| 17 | + id: z.string(), |
| 18 | + date: z.string(), |
| 19 | + allocations: z.record(z.string(), z.number()), |
| 20 | +}); |
| 21 | + |
| 22 | +export async function getTotalAllocationForVolumeGroup( |
| 23 | + volumeGroupId: string, |
| 24 | +): Promise<number> { |
| 25 | + try { |
| 26 | + const result = await docClient.send( |
| 27 | + new GetCommand({ |
| 28 | + TableName: SUPPLIER_QUOTAS_TABLENAME, |
| 29 | + Key: { pk: "ENTITY#overall-allocation", sk: `ID#${volumeGroupId}` }, |
| 30 | + }), |
| 31 | + ); |
| 32 | + logger.info(`Selecting from table name: ${SUPPLIER_QUOTAS_TABLENAME}`); |
| 33 | + |
| 34 | + if (!result.Item) { |
| 35 | + logger.warn( |
| 36 | + `No overall allocation found for volume group ${volumeGroupId}`, |
| 37 | + ); |
| 38 | + return 0; // Default to 0 if no allocation record exists |
| 39 | + } |
| 40 | + // Strip DynamoDB keys before parsing |
| 41 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 42 | + const { pk, sk, ...item } = result.Item; |
| 43 | + const overallAllocation = overallAllocationSchema.parse(item); |
| 44 | + |
| 45 | + logger.info( |
| 46 | + `Fetched overall allocation for volume group ${volumeGroupId}: ${JSON.stringify(overallAllocation)}`, |
| 47 | + ); |
| 48 | + const { allocations } = overallAllocation; |
| 49 | + |
| 50 | + const totalAllocation = Object.values(allocations).reduce( |
| 51 | + (sum, allocation) => sum + allocation, |
| 52 | + 0, |
| 53 | + ); |
| 54 | + logger.info( |
| 55 | + `Fetched overall allocation for volume group ${volumeGroupId}: ${totalAllocation}`, |
| 56 | + ); |
| 57 | + return totalAllocation; |
| 58 | + } catch (error) { |
| 59 | + logger.error( |
| 60 | + `Error fetching overall allocation for volume group ${volumeGroupId}: ${error}`, |
| 61 | + ); |
| 62 | + throw error; |
| 63 | + } |
| 64 | +} |
| 65 | +export async function getTotalDailyAllocation( |
| 66 | + allocationDate: string, |
| 67 | +): Promise<number> { |
| 68 | + try { |
| 69 | + const result = await docClient.send( |
| 70 | + new GetCommand({ |
| 71 | + TableName: SUPPLIER_QUOTAS_TABLENAME, |
| 72 | + Key: { pk: "ENTITY#daily-allocation", sk: `ID#${allocationDate}` }, |
| 73 | + }), |
| 74 | + ); |
| 75 | + logger.info(`Selecting from table name: ${SUPPLIER_QUOTAS_TABLENAME}`); |
| 76 | + |
| 77 | + if (!result.Item) { |
| 78 | + logger.warn(`No daily allocation found for date ${allocationDate}`); |
| 79 | + return 0; // Default to 0 if no allocation record exists |
| 80 | + } |
| 81 | + // Strip DynamoDB keys before parsing |
| 82 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 83 | + const { pk, sk, ...item } = result.Item; |
| 84 | + const dailyAllocation = dailyAllocationSchema.parse(item); |
| 85 | + |
| 86 | + logger.info( |
| 87 | + `Fetched daily allocation for date ${allocationDate}: ${JSON.stringify(dailyAllocation)}`, |
| 88 | + ); |
| 89 | + const { allocations } = dailyAllocation; |
| 90 | + |
| 91 | + const totalAllocation = Object.values(allocations).reduce( |
| 92 | + (sum, allocation) => sum + allocation, |
| 93 | + 0, |
| 94 | + ); |
| 95 | + logger.info( |
| 96 | + `Fetched daily allocation for date ${allocationDate}: ${totalAllocation}`, |
| 97 | + ); |
| 98 | + return totalAllocation; |
| 99 | + } catch (error) { |
| 100 | + logger.error( |
| 101 | + `Error fetching daily allocation for date ${allocationDate}: ${error}`, |
| 102 | + ); |
| 103 | + throw error; |
| 104 | + } |
| 105 | +} |
0 commit comments