Added shop receiving route
This commit is contained in:
parent
976eaf7653
commit
f3ad2c8ed8
@ -9,7 +9,7 @@ async function generateEarthquakeCode(type: string, country: string) {
|
||||
// Use highest DB id to find the latest added earthquake's code number
|
||||
const last = await prisma.earthquake.findFirst({
|
||||
orderBy: { id: "desc" },
|
||||
select: { code: true }
|
||||
select: { code: true },
|
||||
});
|
||||
let num = 10000;
|
||||
if (last?.code) {
|
||||
@ -43,7 +43,7 @@ export async function POST(request: NextRequest) {
|
||||
longitude: +longitude,
|
||||
depth,
|
||||
creatorId,
|
||||
}
|
||||
},
|
||||
});
|
||||
return NextResponse.json({ id: eq.id, code }, { status: 201 });
|
||||
} catch (e: any) {
|
||||
|
||||
50
src/app/api/shop/purchase/route.ts
Normal file
50
src/app/api/shop/purchase/route.ts
Normal file
@ -0,0 +1,50 @@
|
||||
import { prisma } from "@utils/prisma";
|
||||
import { NextResponse } from "next/server";
|
||||
import { NextRequest } from "next/server";
|
||||
import { ExtendedArtefact } from "@appTypes/ApiTypes";
|
||||
|
||||
interface SuperExtendedArtefact extends ExtendedArtefact {
|
||||
location: string;
|
||||
dateReleased: string;
|
||||
image: string;
|
||||
price: number;
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const {
|
||||
artefacts,
|
||||
email,
|
||||
orderNumber,
|
||||
userId,
|
||||
}: { artefacts: SuperExtendedArtefact[]; email: string; orderNumber: string; userId: number | undefined } =
|
||||
await request.json();
|
||||
|
||||
if (!email || !artefacts || !orderNumber) {
|
||||
return NextResponse.json({ error: "Missing fields" }, { status: 401 });
|
||||
}
|
||||
|
||||
const order = await prisma.order.create({
|
||||
data: {
|
||||
orderNumber,
|
||||
email,
|
||||
userId: userId,
|
||||
artefacts: {
|
||||
connect: artefacts.map((artefact) => ({ id: artefact.id })),
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
await prisma.artefact.updateMany({
|
||||
where: { id: { in: artefacts.map((a) => a.id) } },
|
||||
data: {
|
||||
isSold: true,
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json({ order }, { status: 201 });
|
||||
} catch (error) {
|
||||
console.error("Error creating order:", error);
|
||||
return NextResponse.json({ error: "Failed to create order" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@ -292,13 +292,19 @@ export default function Shop() {
|
||||
// remove all artefacts that were bought (works for both cart and single)
|
||||
setHiddenArtefactIds((ids) => [...ids, ...artefactsToBuy.map((a) => a.id)]);
|
||||
//!! todo create receiving api route
|
||||
const genOrder = () => "#" + Math.random().toString(36).substring(2, 10).toUpperCase();
|
||||
const genOrder = () =>
|
||||
"#" + Math.random().toString(24).substring(2, 10).toUpperCase() + new Date().toLocaleDateString("en-GB");
|
||||
const orderNum = genOrder();
|
||||
|
||||
// todo add display of error
|
||||
(async () => {
|
||||
try {
|
||||
const response = await axios.post("/api/shop/purchase", artefactsToBuy);
|
||||
const response = await axios.post("/api/shop/purchase", {
|
||||
artefacts: artefactsToBuy,
|
||||
email: paymentEmail,
|
||||
orderNumber: orderNum,
|
||||
userId: user?.id,
|
||||
});
|
||||
setOrderNumber(orderNum);
|
||||
onClose();
|
||||
setShowThankYouModal(true);
|
||||
@ -469,7 +475,7 @@ export default function Shop() {
|
||||
</p>
|
||||
<div className="w-full max-w-7xl grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-10 p-2">
|
||||
{currentArtefacts
|
||||
.filter((x) => !hiddenArtefactIds.includes(x.id) && x.isRequired === false)
|
||||
.filter((x) => !hiddenArtefactIds.includes(x.id) && x.isRequired === false && x.isSold === false)
|
||||
.map((artefact) => (
|
||||
<ArtefactCard key={artefact.id} artefact={artefact} />
|
||||
))}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user