better item primary key and cart updates, balance updating WIP
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { auth } from "~/server/auth";
|
||||
import { db } from "~/server/db";
|
||||
|
||||
type Order = {
|
||||
cart: { id: string; quantity: number }[];
|
||||
address: string;
|
||||
};
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const { address, cart } = (await request.json()) as Order;
|
||||
const session = await auth();
|
||||
|
||||
if (!session) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const userId = session.user.id;
|
||||
|
||||
try {
|
||||
if (!cart || cart.length === 0) {
|
||||
return NextResponse.json({ error: "Cart is empty" }, { status: 400 });
|
||||
}
|
||||
|
||||
// Fetch all cart items with related sellable and item to get shopId
|
||||
const cartItemsWithShop = await db.cartItem.findMany({
|
||||
where: { itemId: { in: cart.map((c) => c.id) } },
|
||||
include: {
|
||||
sellable: {
|
||||
include: {
|
||||
item: true, // include item to get shopId
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (cartItemsWithShop.length === 0) {
|
||||
return NextResponse.json(
|
||||
{ error: "No valid items in cart" },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
|
||||
// Group items by shopId
|
||||
const itemsByShop: Record<number, { id: string; quantity: number }[]> = {};
|
||||
|
||||
for (const ci of cartItemsWithShop) {
|
||||
const shopId = ci.sellable.item.shopId; // get shopId from item
|
||||
itemsByShop[shopId] ??= []; // initialize if undefined or null
|
||||
itemsByShop[shopId].push({
|
||||
id: ci.sellable.item.item_name, // API expects item_name as id
|
||||
quantity: ci.quantity,
|
||||
});
|
||||
}
|
||||
|
||||
console.log(itemsByShop);
|
||||
|
||||
// Send requests per shop
|
||||
for (const [shopId, items] of Object.entries(itemsByShop)) {
|
||||
const body = JSON.stringify({
|
||||
shopId: Number(shopId),
|
||||
address,
|
||||
items,
|
||||
});
|
||||
|
||||
console.log("Sending to shop:", shopId, body);
|
||||
|
||||
const response = await fetch(process.env.ITEM_SEND_API!, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body,
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text();
|
||||
return NextResponse.json(
|
||||
{ error: `Failed to send items to shop ${shopId}: ${errorText}` },
|
||||
{ status: response.status },
|
||||
);
|
||||
}
|
||||
|
||||
// Delete successfully sent items from cart
|
||||
const itemIds = items.map((i) => i.id);
|
||||
await db.cartItem.deleteMany({
|
||||
where: {
|
||||
cartId: userId,
|
||||
sellable: { item_name: { in: itemIds } },
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return NextResponse.json({ success: true });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return NextResponse.json({ error: "Server error" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user