Skip to content

Commit 0c5047d

Browse files
committed
wishlist functionality in distributionService is redundant, logic in generalItemRequestService is sufficient
1 parent 5a10fd3 commit 0c5047d

2 files changed

Lines changed: 81 additions & 82 deletions

File tree

‎src/app/api/generalItems/[generalItemId]/requests/[requestId]/route.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { NextRequest, NextResponse } from "next/server";
22
import { z } from "zod";
33

44
import { auth } from "@/auth";
5-
import DonorOfferService from "@/services/donorOfferService";
65
import { GeneralItemRequestService } from "@/services/generalItemRequestService";
76
import UserService from "@/services/userService";
87
import {

‎src/services/distributionService.ts‎

Lines changed: 81 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -672,92 +672,92 @@ export default class DistributionService {
672672

673673
if (data.pending === false && currentDistribution.pending === true) {
674674
await this.createShipmentsForDistribution(distributionId);
675-
await this.populateUnfulfilledWishlists(distributionId);
675+
// await this.populateUnfulfilledWishlists(distributionId);
676676
}
677677

678678
return updatedDistribution;
679679
}
680680

681-
private static async populateUnfulfilledWishlists(
682-
distributionId: number
683-
): Promise<void> {
684-
try {
685-
const distribution = await db.distribution.findUnique({
686-
where: { id: distributionId },
687-
include: {
688-
allocations: {
689-
include: {
690-
lineItem: {
691-
include: {
692-
generalItem: true,
693-
},
694-
},
695-
},
696-
},
697-
},
698-
});
699-
700-
if (!distribution || !distribution.partnerId) return;
701-
702-
const partnerId = distribution.partnerId;
703-
704-
const allocatedByGeneralItemId = new Map<number, number>();
705-
for (const allocation of distribution.allocations) {
706-
const generalItem = allocation.lineItem.generalItem;
707-
if (!generalItem) continue;
708-
const current = allocatedByGeneralItemId.get(generalItem.id) ?? 0;
709-
allocatedByGeneralItemId.set(
710-
generalItem.id,
711-
current + allocation.lineItem.quantity
712-
);
713-
}
714-
715-
if (allocatedByGeneralItemId.size === 0) return;
716-
717-
// Fetch all partner requests for the general items in this distribution
718-
const generalItemIds = Array.from(allocatedByGeneralItemId.keys());
719-
const requests = await db.generalItemRequest.findMany({
720-
where: {
721-
partnerId,
722-
generalItemId: { in: generalItemIds },
723-
},
724-
});
725-
726-
if (requests.length === 0) return;
727-
728-
const unfulfilledEntries: Array<{
729-
requestId: number;
730-
finalQuantity: number;
731-
}> = [];
732-
733-
for (const request of requests) {
734-
const allocated =
735-
allocatedByGeneralItemId.get(request.generalItemId) ?? 0;
736-
if (allocated < request.quantity) {
737-
await db.generalItemRequest.update({
738-
where: { id: request.id },
739-
data: { finalQuantity: allocated },
740-
});
741-
unfulfilledEntries.push({
742-
requestId: request.id,
743-
finalQuantity: allocated,
744-
});
745-
}
746-
}
747-
748-
if (unfulfilledEntries.length > 0) {
749-
await WishlistService.createUnfulfilledWishlistEntries(
750-
unfulfilledEntries
751-
);
752-
}
753-
} catch (error) {
754-
console.error(
755-
"Failed to populate unfulfilled wishlists for distribution:",
756-
distributionId,
757-
error
758-
);
759-
}
760-
}
681+
// private static async populateUnfulfilledWishlists(
682+
// distributionId: number
683+
// ): Promise<void> {
684+
// try {
685+
// const distribution = await db.distribution.findUnique({
686+
// where: { id: distributionId },
687+
// include: {
688+
// allocations: {
689+
// include: {
690+
// lineItem: {
691+
// include: {
692+
// generalItem: true,
693+
// },
694+
// },
695+
// },
696+
// },
697+
// },
698+
// });
699+
700+
// if (!distribution || !distribution.partnerId) return;
701+
702+
// const partnerId = distribution.partnerId;
703+
704+
// const allocatedByGeneralItemId = new Map<number, number>();
705+
// for (const allocation of distribution.allocations) {
706+
// const generalItem = allocation.lineItem.generalItem;
707+
// if (!generalItem) continue;
708+
// const current = allocatedByGeneralItemId.get(generalItem.id) ?? 0;
709+
// allocatedByGeneralItemId.set(
710+
// generalItem.id,
711+
// current + allocation.lineItem.quantity
712+
// );
713+
// }
714+
715+
// if (allocatedByGeneralItemId.size === 0) return;
716+
717+
// // Fetch all partner requests for the general items in this distribution
718+
// const generalItemIds = Array.from(allocatedByGeneralItemId.keys());
719+
// const requests = await db.generalItemRequest.findMany({
720+
// where: {
721+
// partnerId,
722+
// generalItemId: { in: generalItemIds },
723+
// },
724+
// });
725+
726+
// if (requests.length === 0) return;
727+
728+
// const unfulfilledEntries: Array<{
729+
// requestId: number;
730+
// finalQuantity: number;
731+
// }> = [];
732+
733+
// for (const request of requests) {
734+
// const allocated =
735+
// allocatedByGeneralItemId.get(request.generalItemId) ?? 0;
736+
// if (allocated < request.quantity) {
737+
// await db.generalItemRequest.update({
738+
// where: { id: request.id },
739+
// data: { finalQuantity: allocated },
740+
// });
741+
// unfulfilledEntries.push({
742+
// requestId: request.id,
743+
// finalQuantity: allocated,
744+
// });
745+
// }
746+
// }
747+
748+
// if (unfulfilledEntries.length > 0) {
749+
// await WishlistService.createUnfulfilledWishlistEntries(
750+
// unfulfilledEntries
751+
// );
752+
// }
753+
// } catch (error) {
754+
// console.error(
755+
// "Failed to populate unfulfilled wishlists for distribution:",
756+
// distributionId,
757+
// error
758+
// );
759+
// }
760+
// }
761761

762762
private static async createShipmentsForDistribution(distributionId: number) {
763763
const allocations = await db.allocation.findMany({

0 commit comments

Comments
 (0)