|
| 1 | +import { |
| 2 | + LetterVariant, |
| 3 | + SupplierAllocation, |
| 4 | + VolumeGroup, |
| 5 | +} from "internal/datastore/src/SupplierConfigDomain"; |
| 6 | +import { Deps } from "../config/deps"; |
| 7 | + |
| 8 | + |
| 9 | +export async function getVariantDetails( |
| 10 | + variantId: string, |
| 11 | + deps: Deps, |
| 12 | +): Promise<LetterVariant> { |
| 13 | + deps.logger.info({ |
| 14 | + description: "Fetching letter variant details from database", |
| 15 | + variantId, |
| 16 | + }); |
| 17 | + |
| 18 | + const variantDetails: LetterVariant = |
| 19 | + await deps.supplierConfigRepo.getLetterVariant(variantId); |
| 20 | + |
| 21 | + if (variantDetails) { |
| 22 | + deps.logger.info({ |
| 23 | + description: "Fetched letter variant details", |
| 24 | + variantId, |
| 25 | + variantDetails, |
| 26 | + }); |
| 27 | + } else { |
| 28 | + deps.logger.error({ |
| 29 | + description: "No letter variant found for id", |
| 30 | + variantId, |
| 31 | + }); |
| 32 | + } |
| 33 | + return variantDetails; |
| 34 | +} |
| 35 | + |
| 36 | +export async function getVolumeGroupDetails( |
| 37 | + groupId: string, |
| 38 | + deps: Deps, |
| 39 | +): Promise<VolumeGroup> { |
| 40 | + deps.logger.info({ |
| 41 | + description: "Fetching volume group details from database", |
| 42 | + groupId, |
| 43 | + }); |
| 44 | + |
| 45 | + const groupDetails = await deps.supplierConfigRepo.getVolumeGroup(groupId); |
| 46 | + if (groupDetails) { |
| 47 | + deps.logger.info({ |
| 48 | + description: "Fetched volume group details", |
| 49 | + groupId, |
| 50 | + groupDetails, |
| 51 | + }); |
| 52 | + } else { |
| 53 | + deps.logger.error({ |
| 54 | + description: "No volume group found for id", |
| 55 | + groupId, |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + if ( |
| 60 | + groupDetails.status !== "PROD" && |
| 61 | + (new Date(groupDetails.startDate) > new Date() || |
| 62 | + (groupDetails.endDate && new Date(groupDetails.endDate) < new Date())) |
| 63 | + ) { |
| 64 | + deps.logger.error({ |
| 65 | + description: "Volume group is not active based on status and dates", |
| 66 | + groupId, |
| 67 | + status: groupDetails.status, |
| 68 | + startDate: groupDetails.startDate, |
| 69 | + endDate: groupDetails.endDate, |
| 70 | + }); |
| 71 | + throw new Error(`Volume group with id ${groupId} is not active`); |
| 72 | + } |
| 73 | + return groupDetails; |
| 74 | +} |
| 75 | + |
| 76 | +export async function getSupplierAllocationsForVolumeGroup( |
| 77 | + groupId: string, |
| 78 | + supplierId: string, |
| 79 | + deps: Deps, |
| 80 | +): Promise<SupplierAllocation[]> { |
| 81 | + deps.logger.info({ |
| 82 | + description: "Fetching supplier allocations for volume group from database", |
| 83 | + groupId, |
| 84 | + }); |
| 85 | + const allocations = |
| 86 | + await deps.supplierConfigRepo.getSupplierAllocationsForVolumeGroup(groupId); |
| 87 | + |
| 88 | + if (allocations.length > 0) { |
| 89 | + deps.logger.info({ |
| 90 | + description: |
| 91 | + "Fetched supplier allocations for volume group from database", |
| 92 | + groupId, |
| 93 | + count: allocations.length, |
| 94 | + }); |
| 95 | + } else { |
| 96 | + deps.logger.error({ |
| 97 | + description: "No supplier allocations found for volume group id", |
| 98 | + groupId, |
| 99 | + }); |
| 100 | + } |
| 101 | + |
| 102 | + if (supplierId) { |
| 103 | + const filteredAllocations = allocations.filter( |
| 104 | + (alloc) => alloc.supplier === supplierId, |
| 105 | + ); |
| 106 | + if (filteredAllocations.length === 0) { |
| 107 | + deps.logger.error({ |
| 108 | + description: |
| 109 | + "No supplier allocations found for variantsupplier id in volume group", |
| 110 | + groupId, |
| 111 | + supplierId, |
| 112 | + }); |
| 113 | + throw new Error( |
| 114 | + `No supplier allocations found for variant supplier id ${supplierId} in volume group ${groupId}`, |
| 115 | + ); |
| 116 | + } |
| 117 | + return filteredAllocations; |
| 118 | + } |
| 119 | + |
| 120 | + return allocations; |
| 121 | +} |
0 commit comments