Functional cart and balance display
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { auth } from "~/server/auth";
|
||||
import { db } from "~/server/db";
|
||||
|
||||
export async function PATCH(request: Request) {
|
||||
const session = await auth();
|
||||
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
type CartItem = {
|
||||
id: string;
|
||||
quantity: number;
|
||||
};
|
||||
|
||||
const userId = session.user.id;
|
||||
const body = (await request.json()) as CartItem;
|
||||
const { id: itemId, quantity } = body;
|
||||
|
||||
if (!itemId || !quantity || quantity == 0) {
|
||||
return NextResponse.json(
|
||||
{ error: "Invalid request data" },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
// Find or create the user's cart
|
||||
let cart = await db.cart.findFirst({
|
||||
where: { userId },
|
||||
include: { cartItems: true },
|
||||
});
|
||||
|
||||
// Use nullish coalescing assignment
|
||||
cart ??= await db.cart.create({
|
||||
data: { userId },
|
||||
include: { cartItems: true },
|
||||
});
|
||||
|
||||
// Check if the item already exists in the cart
|
||||
const existingCartItem = await db.cartItem.findUnique({
|
||||
where: { itemId },
|
||||
});
|
||||
|
||||
if (existingCartItem) {
|
||||
if (quantity <= 0 && quantity * -1 >= existingCartItem.quantity) {
|
||||
await db.cartItem.delete({
|
||||
where: { itemId },
|
||||
});
|
||||
} else {
|
||||
// Update quantity
|
||||
await db.cartItem.update({
|
||||
where: { itemId },
|
||||
data: { quantity: existingCartItem.quantity + quantity },
|
||||
});
|
||||
}
|
||||
} else {
|
||||
// Add new item
|
||||
await db.cartItem.create({
|
||||
data: {
|
||||
itemId,
|
||||
quantity,
|
||||
cartId: userId,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// Fetch the updated cart
|
||||
const updatedCart = await db.cart.findUnique({
|
||||
where: { userId: userId },
|
||||
include: { cartItems: { include: { sellable: true } } },
|
||||
});
|
||||
|
||||
return NextResponse.json(updatedCart);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return NextResponse.json(
|
||||
{ error: "Failed to add item to cart" },
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,9 @@ export async function GET() {
|
||||
item: { select: { stock: true } },
|
||||
},
|
||||
});
|
||||
// and cache no store header
|
||||
|
||||
return NextResponse.json(sellables);
|
||||
return NextResponse.json(sellables, {
|
||||
headers: { "Cache-Control": "no-store" },
|
||||
});
|
||||
}
|
||||
|
||||
@@ -45,6 +45,12 @@ export async function GET() {
|
||||
shops: {
|
||||
select: { id: true, label: true, sellables: true },
|
||||
},
|
||||
carts: {
|
||||
select: {
|
||||
cartItems: { select: { itemId: true, quantity: true } },
|
||||
},
|
||||
},
|
||||
balance: true,
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user