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