Merge branch 'master' of ssh://stash.dyson.global.corp:7999/~thowitz/tremor-tracker
This commit is contained in:
commit
c6ca82067f
@ -3,50 +3,50 @@ import { prisma } from "@utils/prisma";
|
|||||||
|
|
||||||
// Generates code using only the country, and highest id in DB for numbering
|
// Generates code using only the country, and highest id in DB for numbering
|
||||||
async function generateEarthquakeCode(type: string, country: string) {
|
async function generateEarthquakeCode(type: string, country: string) {
|
||||||
const typeLetter = type.trim().charAt(0).toUpperCase();
|
const typeLetter = type.trim().charAt(0).toUpperCase();
|
||||||
// Remove non-alphanumeric for the country part
|
// Remove non-alphanumeric for the country part
|
||||||
const countrySlug = (country || "Unknown").replace(/[^\w]/gi, "");
|
const countrySlug = (country || "Unknown").replace(/[^\w]/gi, "");
|
||||||
// Use highest DB id to find the latest added earthquake's code number
|
// Use highest DB id to find the latest added earthquake's code number
|
||||||
const last = await prisma.earthquake.findFirst({
|
const last = await prisma.earthquake.findFirst({
|
||||||
orderBy: { id: "desc" },
|
orderBy: { id: "desc" },
|
||||||
select: { code: true }
|
select: { code: true },
|
||||||
});
|
});
|
||||||
let num = 10000;
|
let num = 10000;
|
||||||
if (last?.code) {
|
if (last?.code) {
|
||||||
const parts = last.code.split("-");
|
const parts = last.code.split("-");
|
||||||
const lastNum = parseInt(parts[parts.length - 1], 10);
|
const lastNum = parseInt(parts[parts.length - 1], 10);
|
||||||
if (!isNaN(lastNum)) num = lastNum + 1;
|
if (!isNaN(lastNum)) num = lastNum + 1;
|
||||||
}
|
}
|
||||||
return `E${typeLetter}-${countrySlug}-${num.toString().padStart(5, "0")}`;
|
return `E${typeLetter}-${countrySlug}-${num.toString().padStart(5, "0")}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function POST(request: NextRequest) {
|
export async function POST(request: NextRequest) {
|
||||||
try {
|
try {
|
||||||
const body = await request.json();
|
const body = await request.json();
|
||||||
const { date, magnitude, type, location, latitude, longitude, depth, country } = body;
|
const { date, magnitude, type, location, latitude, longitude, depth, country } = body;
|
||||||
const creatorId = 1;
|
const creatorId = 1;
|
||||||
if (!date || !magnitude || !type || !location || !latitude || !longitude || !depth || !country) {
|
if (!date || !magnitude || !type || !location || !latitude || !longitude || !depth || !country) {
|
||||||
return NextResponse.json({ error: "Missing fields" }, { status: 400 });
|
return NextResponse.json({ error: "Missing fields" }, { status: 400 });
|
||||||
}
|
}
|
||||||
if (+magnitude > 10) {
|
if (+magnitude > 10) {
|
||||||
return NextResponse.json({ error: "Magnitude cannot exceed 10" }, { status: 400 });
|
return NextResponse.json({ error: "Magnitude cannot exceed 10" }, { status: 400 });
|
||||||
}
|
}
|
||||||
const code = await generateEarthquakeCode(type, country);
|
const code = await generateEarthquakeCode(type, country);
|
||||||
const eq = await prisma.earthquake.create({
|
const eq = await prisma.earthquake.create({
|
||||||
data: {
|
data: {
|
||||||
date: new Date(date),
|
date: new Date(date),
|
||||||
code,
|
code,
|
||||||
magnitude: +magnitude,
|
magnitude: +magnitude,
|
||||||
type,
|
type,
|
||||||
location, // "city, country"
|
location, // "city, country"
|
||||||
latitude: +latitude,
|
latitude: +latitude,
|
||||||
longitude: +longitude,
|
longitude: +longitude,
|
||||||
depth,
|
depth,
|
||||||
creatorId,
|
creatorId,
|
||||||
}
|
},
|
||||||
});
|
});
|
||||||
return NextResponse.json({ id: eq.id, code }, { status: 201 });
|
return NextResponse.json({ id: eq.id, code }, { status: 201 });
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
return NextResponse.json({ error: e.message }, { status: 500 });
|
return NextResponse.json({ error: e.message }, { status: 500 });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
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)
|
// remove all artefacts that were bought (works for both cart and single)
|
||||||
setHiddenArtefactIds((ids) => [...ids, ...artefactsToBuy.map((a) => a.id)]);
|
setHiddenArtefactIds((ids) => [...ids, ...artefactsToBuy.map((a) => a.id)]);
|
||||||
//!! todo create receiving api route
|
//!! 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();
|
const orderNum = genOrder();
|
||||||
|
|
||||||
// todo add display of error
|
// todo add display of error
|
||||||
(async () => {
|
(async () => {
|
||||||
try {
|
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);
|
setOrderNumber(orderNum);
|
||||||
onClose();
|
onClose();
|
||||||
setShowThankYouModal(true);
|
setShowThankYouModal(true);
|
||||||
@ -469,7 +475,7 @@ export default function Shop() {
|
|||||||
</p>
|
</p>
|
||||||
<div className="w-full max-w-7xl grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-10 p-2">
|
<div className="w-full max-w-7xl grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-10 p-2">
|
||||||
{currentArtefacts
|
{currentArtefacts
|
||||||
.filter((x) => !hiddenArtefactIds.includes(x.id) && x.isRequired === false)
|
.filter((x) => !hiddenArtefactIds.includes(x.id) && x.isRequired === false && x.isSold === false)
|
||||||
.map((artefact) => (
|
.map((artefact) => (
|
||||||
<ArtefactCard key={artefact.id} artefact={artefact} />
|
<ArtefactCard key={artefact.id} artefact={artefact} />
|
||||||
))}
|
))}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user