42 lines
1.3 KiB
TypeScript
42 lines
1.3 KiB
TypeScript
|
|
import { NextRequest, NextResponse } from "next/server";
|
||
|
|
import { apiAuthMiddleware } from "@utils/apiAuthMiddleware";
|
||
|
|
import { prisma } from "@utils/prisma";
|
||
|
|
|
||
|
|
export async function POST(request: NextRequest) {
|
||
|
|
try {
|
||
|
|
const body = await request.json();
|
||
|
|
const { name, type, description, location, earthquakeCode, warehouseLocation } = body;
|
||
|
|
|
||
|
|
const authResult = await apiAuthMiddleware();
|
||
|
|
if ("user" in authResult === false) return authResult; // Handle error response
|
||
|
|
|
||
|
|
const { user } = authResult;
|
||
|
|
|
||
|
|
if (!name || !type || !description || !location || !earthquakeCode || !warehouseLocation) {
|
||
|
|
return NextResponse.json({ error: "Missing fields" }, { status: 400 });
|
||
|
|
}
|
||
|
|
|
||
|
|
const linkedEarthquake = await prisma.earthquake.findUnique({ where: { code: earthquakeCode } });
|
||
|
|
|
||
|
|
if (!linkedEarthquake) {
|
||
|
|
return NextResponse.json({ error: "Earthquake code not found" }, { status: 400 });
|
||
|
|
}
|
||
|
|
|
||
|
|
await prisma.artefact.create({
|
||
|
|
data: {
|
||
|
|
name,
|
||
|
|
type,
|
||
|
|
description,
|
||
|
|
earthquakeId: linkedEarthquake.id,
|
||
|
|
warehouseArea: warehouseLocation,
|
||
|
|
imageName: "NoImageFound.PNG",
|
||
|
|
creatorId: user.id,
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
return NextResponse.json({ message: "Artefact logged successfully" }, { status: 200 });
|
||
|
|
} catch (e: any) {
|
||
|
|
return NextResponse.json({ error: e.message }, { status: 500 });
|
||
|
|
}
|
||
|
|
}
|