Repository navigation
Tested/Working Wishlist Population #305
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
ff3fbcd
915ea6b
f2c8d50
f07ac3c
f3396b7
5a10fd3
0c5047d
d2f6aec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,8 @@ import { Prisma } from "@prisma/client"; | |
| import { PrismaClientKnownRequestError } from "@prisma/client/runtime/library"; | ||
| import { MatchingService } from "./matchingService"; | ||
|
|
||
| const AUTO_GENERATED_COMMENT = "Automatically generated due to unfulfilled request"; | ||
|
|
||
| export class WishlistService { | ||
| static async createWishlist(data: CreateWishlistData) { | ||
| const partner = await db.user.findUnique({ | ||
|
|
@@ -42,6 +44,70 @@ export class WishlistService { | |
| return wishlist; | ||
| } | ||
|
|
||
| /** | ||
| * For each request where finalQuantity < quantity, automatically creates or | ||
| * updates a wishlist entry for the partner with the unfulfilled quantity. | ||
| * If an entry with the same name already exists for the partner, the | ||
| * unfulfilled quantity is added to it. | ||
| */ | ||
|
Comment on lines
+47
to
+52
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. JSDoc comment contradicts actual implementation The doc says "the unfulfilled quantity is added to it", but line 83 does * If an entry with the same name already exists for the partner, its
* quantity is replaced with the new unfulfilled quantity. |
||
| static async createUnfulfilledWishlistEntries(requests: { requestId: number; finalQuantity: number }[]) { | ||
| const fullRequests = await db.generalItemRequest.findMany({ | ||
| where: { id: { in: requests.map(r => r.requestId) } }, | ||
| include: { partner: true, generalItem: true } | ||
| }); | ||
|
|
||
| // PRE-FETCH: Get all existing auto-generated wishlist items for these partners in ONE query | ||
| const partnerIds = [...new Set(fullRequests.map(r => r.partnerId))]; | ||
| const existingWishlistItems = await db.wishlist.findMany({ | ||
| where: { | ||
| partnerId: { in: partnerIds }, | ||
| generalItemId: null, // Only targeting auto-generated ones | ||
| } | ||
| }); | ||
|
ND68 marked this conversation as resolved.
Outdated
|
||
|
|
||
| for (const update of requests) { | ||
| const request = fullRequests.find((r) => r.id === update.requestId); | ||
| if (!request || !request.partner.enabled || request.partner.pending) continue; | ||
|
|
||
| const unfulfilledQuantity = request.quantity - update.finalQuantity; | ||
| if (unfulfilledQuantity <= 0) continue; | ||
|
|
||
| const itemName = request.generalItem.title; | ||
| const existing = existingWishlistItems.find( | ||
| (w) => w.partnerId === request.partnerId && w.name === itemName | ||
| ); | ||
|
|
||
| if (existing) { | ||
| await db.wishlist.update({ | ||
| where: { id: existing.id }, | ||
| data: { quantity: unfulfilledQuantity }, | ||
| }); | ||
| } else { | ||
| const wishlist = await db.wishlist.create({ | ||
| data: { | ||
| name: itemName, | ||
| quantity: unfulfilledQuantity, | ||
| priority: request.priority ?? "LOW", | ||
| comments: AUTO_GENERATED_COMMENT, | ||
| partnerId: request.partnerId, | ||
| }, | ||
| }); | ||
|
|
||
| try { | ||
| await MatchingService.add({ | ||
| wishlistId: wishlist.id, | ||
| title: wishlist.name, | ||
| }); | ||
| } catch (error) { | ||
| console.warn( | ||
| "Failed to generate embedding for auto-created wishlist entry:", | ||
| error | ||
| ); | ||
| } | ||
| } | ||
|
ND68 marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Automatically updates lastUpdated | ||
| */ | ||
|
|
@@ -247,4 +313,4 @@ export class WishlistService { | |
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing newline at end of file Both The same issue exists at the end of Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||
Uh oh!
There was an error while loading. Please reload this page.