Repository navigation
Wishlist Business Logic #154
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
Merged
Merged
Changes from 17 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
d694fd1
Add Wishlist to Prisma Schema
renatodellosso f212a9b
Add db migration
renatodellosso 2d26839
Rename comment field to comments
renatodellosso f0bd900
Add wishlists to seeding
renatodellosso afb34ad
Add wishlist types and service
renatodellosso f00fd6d
Create WishlistStats type
renatodellosso 5e67b53
Add POST api/wishlists
renatodellosso 8198323
Fix return in POST api/wishlists
renatodellosso 2bc32e9
Add GET api/wishlists
renatodellosso a3900a9
Add PATCH api/wishlists/[wishlistId]. Add authRequirePartner. Add sch…
renatodellosso 6f16318
Add DELETE api/wishlists/[wishlistId[
renatodellosso c846d8e
Better typing for updateWishlistSchema
renatodellosso 09f4fb7
Make quantity a decimal not an int
renatodellosso 0d9267b
Revert prior change
renatodellosso cadc4f6
Add note about errors to authRequirePartner
renatodellosso e0b6ffc
Clean up updateWishlist
renatodellosso fb48e30
Make UpdateWishlistData build off of CreateWishListData
renatodellosso 38ef77f
Parse PATCH api/wishlists/[id] params with zod
renatodellosso 28a4851
Parse DELETE api/wishlists/[id] params with zod
renatodellosso aa2f04c
Parse GET api/wishlists params with zod
renatodellosso f00bbaa
Remove unneeded import
renatodellosso 35be09d
Convert authRequirePartner to isPartner
renatodellosso 188af22
Fix GET /wishlists without ID
renatodellosso 6f5917d
Use isPartner in GET
renatodellosso 0f94927
Remove unneeded import
renatodellosso 5d3f1c7
Misc fixes
renatodellosso 7ce38eb
Fix per wishlist endpoints
renatodellosso 842e3b9
Use queryRaw for counting wishlists
renatodellosso e93302e
Update src/services/wishlistService.ts
renatodellosso fc07b3f
Fix typo
renatodellosso b076184
Update src/app/api/wishlists/[wishlistId]/route.ts
renatodellosso 4a932bc
Update src/app/api/wishlists/route.ts
renatodellosso File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
prisma/migrations/20250903023312_create_wishlists/migration.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| -- CreateTable | ||
| CREATE TABLE "Wishlist" ( | ||
| "id" SERIAL NOT NULL, | ||
| "name" TEXT NOT NULL, | ||
| "unitSize" TEXT NOT NULL, | ||
| "quantity" INTEGER NOT NULL, | ||
| "priority" "RequestPriority" NOT NULL, | ||
| "lastUpdated" TIMESTAMP(3) NOT NULL, | ||
| "comment" TEXT NOT NULL, | ||
| "partnerId" INTEGER NOT NULL, | ||
|
|
||
| CONSTRAINT "Wishlist_pkey" PRIMARY KEY ("id") | ||
| ); | ||
|
|
||
| -- AddForeignKey | ||
| ALTER TABLE "Wishlist" ADD CONSTRAINT "Wishlist_partnerId_fkey" FOREIGN KEY ("partnerId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; |
10 changes: 10 additions & 0 deletions
10
prisma/migrations/20250903023635_rename_comment_to_comments/migration.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| /* | ||
| Warnings: | ||
|
|
||
| - You are about to drop the column `comment` on the `Wishlist` table. All the data in the column will be lost. | ||
| - Added the required column `comments` to the `Wishlist` table without a default value. This is not possible if the table is not empty. | ||
|
|
||
| */ | ||
| -- AlterTable | ||
| ALTER TABLE "Wishlist" DROP COLUMN "comment", | ||
| ADD COLUMN "comments" TEXT NOT NULL; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import { updateWishlistSchema } from "@/schema/wishlist"; | ||
| import UserService from "@/services/userService"; | ||
| import { WishlistService } from "@/services/wishlistService"; | ||
| import { ArgumentError, errorResponse, ok } from "@/util/errors"; | ||
| import { NextRequest } from "next/server"; | ||
|
|
||
| /** | ||
| * Updates a wishlist item. Allows changes to name, unit size, quantity, priority, and comments. | ||
| * @param req | ||
| * @returns | ||
| */ | ||
| export async function PATCH( | ||
| req: NextRequest, | ||
| { params }: { params: Promise<{ wishlistId: string }> } | ||
| ) { | ||
| try { | ||
| const session = await UserService.authRequirePartner(); | ||
|
|
||
| const { wishlistId } = await params; | ||
| const body = await req.json(); | ||
|
|
||
| const parsed = updateWishlistSchema.safeParse(body); | ||
| if (!parsed.success) { | ||
| throw new ArgumentError(parsed.error.message); | ||
| } | ||
|
|
||
| const wishlistItem = await WishlistService.getWishlistItem( | ||
| Number(wishlistId) | ||
| ); | ||
|
|
||
| // Check ID first to avoid revealing the existence/non-existence of the wishlist item to an unauthorized user | ||
| if (wishlistItem?.partnerId !== Number(session.user.id)) { | ||
| throw new ArgumentError("Cannot update another partner's wishlist item"); | ||
| } | ||
|
|
||
| if (!wishlistItem) { | ||
| throw new ArgumentError("Wishlist item not found"); | ||
| } | ||
|
renatodellosso marked this conversation as resolved.
|
||
|
|
||
| await WishlistService.updateWishlist({ | ||
| id: Number(wishlistId), | ||
| ...parsed.data, | ||
| }); | ||
|
|
||
| return ok(); | ||
| } catch (error) { | ||
| return errorResponse(error); | ||
| } | ||
| } | ||
|
|
||
| export async function DELETE( | ||
| _: NextRequest, | ||
| { params }: { params: Promise<{ wishlistId: string }> } | ||
| ) { | ||
| try { | ||
| const session = await UserService.authRequirePartner(); | ||
|
|
||
| const { wishlistId } = await params; | ||
|
renatodellosso marked this conversation as resolved.
Outdated
|
||
|
|
||
| const wishlistItem = await WishlistService.getWishlistItem( | ||
| Number(wishlistId) | ||
| ); | ||
|
|
||
| // Check ID first to avoid revealing the existence/non-existence of the wishlist item to an unauthorized user | ||
| if (wishlistItem?.partnerId !== Number(session.user.id)) { | ||
| throw new ArgumentError("Cannot delete another partner's wishlist item"); | ||
| } | ||
|
|
||
| if (!wishlistItem) { | ||
| throw new ArgumentError("Wishlist item not found"); | ||
| } | ||
|
renatodellosso marked this conversation as resolved.
|
||
|
|
||
| await WishlistService.deleteWishlist(Number(wishlistId)); | ||
|
|
||
| return ok(); | ||
| } catch (error) { | ||
| return errorResponse(error); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.