Repository navigation
Expand file tree
/
Copy pathroute.ts
More file actions
93 lines (75 loc) · 2.66 KB
/
Copy pathroute.ts
File metadata and controls
93 lines (75 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { auth } from "@/auth";
import { idSchema, 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 auth();
if (!UserService.isPartner(session?.user)) {
throw new ArgumentError("Must be PARTNER");
}
const parsedId = idSchema.safeParse((await params).wishlistId);
if (!parsedId.success) {
throw new ArgumentError("Invalid wishlist ID");
}
const wishlistId = parsedId.data;
const body = await req.json();
const parsed = updateWishlistSchema.safeParse(body);
if (!parsed.success) {
throw new ArgumentError(parsed.error.message);
}
const wishlistItem = await WishlistService.getWishlistItem(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");
}
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 auth();
if (!UserService.isPartner(session?.user)) {
throw new ArgumentError("Must be PARTNER");
}
const parsedId = idSchema.safeParse((await params).wishlistId);
if (!parsedId.success) {
throw new ArgumentError(parsedId.error.message);
}
const wishlistId = parsedId.data;
const wishlistItem = await WishlistService.getWishlistItem(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");
}
await WishlistService.deleteWishlist(Number(wishlistId));
return ok();
} catch (error) {
return errorResponse(error);
}
}