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 1 commit
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,92 @@ 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: Array<{ | ||
| requestId: number; | ||
| finalQuantity: number; | ||
| }> | ||
| ): Promise<void> { | ||
| if (requests.length === 0) return; | ||
|
|
||
| const requestIds = requests.map((r) => r.requestId); | ||
|
|
||
| const fullRequests = await db.generalItemRequest.findMany({ | ||
| where: { id: { in: requestIds } }, | ||
| include: { | ||
| generalItem: { | ||
| select: { title: true }, | ||
| }, | ||
| partner: { | ||
| select: { id: true, enabled: true, pending: true }, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| for (const update of requests) { | ||
| const request = fullRequests.find((r) => r.id === update.requestId); | ||
| if (!request) continue; | ||
|
|
||
| // Skip inactive partners | ||
| if (!request.partner.enabled || request.partner.pending) continue; | ||
|
|
||
| const unfulfilledQuantity = request.quantity - update.finalQuantity; | ||
| if (unfulfilledQuantity <= 0) continue; | ||
|
|
||
| const itemName = request.generalItem.title; | ||
| const partnerId = request.partner.id; | ||
| const priority = request.priority ?? "LOW"; | ||
|
|
||
| // Check if a wishlist entry with the same name already exists | ||
| const existing = await db.wishlist.findFirst({ | ||
| where: { | ||
| partnerId, | ||
| name: itemName, | ||
| generalItemId: null, // only unfulfilled wishlist entries | ||
| }, | ||
| }); | ||
|
|
||
| if (existing) { | ||
| // Add quantities together | ||
| await db.wishlist.update({ | ||
| where: { id: existing.id }, | ||
| data: { | ||
| quantity: (existing.quantity ?? 0) + unfulfilledQuantity, | ||
| }, | ||
| }); | ||
|
greptile-apps[bot] marked this conversation as resolved.
Outdated
|
||
| } else { | ||
| // Create a new wishlist entry | ||
| const wishlist = await db.wishlist.create({ | ||
| data: { | ||
| name: itemName, | ||
| quantity: unfulfilledQuantity, | ||
| priority, | ||
| comments: AUTO_GENERATED_COMMENT, | ||
| 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 | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
greptile-apps[bot] marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| /** | ||
| * Automatically updates lastUpdated | ||
| */ | ||
|
|
@@ -247,4 +335,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.