From 8bd3b10368980e813f9e76a9bb17eae4a757c30f Mon Sep 17 00:00:00 2001 From: Tim Howitz Date: Mon, 19 May 2025 15:38:24 +0100 Subject: [PATCH] Fixed some errors --- prisma/schema.prisma | 10 +++---- src/app/api/login/route.ts | 47 +++++++++++++----------------- src/app/api/observatories/route.ts | 2 +- src/app/api/warehouse/route.ts | 2 +- src/app/warehouse/page.tsx | 46 +++++++++++++++-------------- 5 files changed, 51 insertions(+), 56 deletions(-) diff --git a/prisma/schema.prisma b/prisma/schema.prisma index d02cee6..23d3a4c 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -76,7 +76,7 @@ model Observatory { longitude String latitude String dateEstablished Int? - functional Boolean + isFunctional Boolean seismicSensorOnline Boolean @default(true) creatorId Int? creator Scientist? @relation("ScientistObservatoryCreator", fields: [creatorId], references: [id], onDelete: NoAction, onUpdate: NoAction) @@ -85,9 +85,9 @@ model Observatory { model Artefact { id Int @id @default(autoincrement()) - name String createdAt DateTime @default(now()) updatedAt DateTime @updatedAt + name String type String @db.VarChar(50) // Lava, Tephra, Ash, Soil warehouseArea String description String @@ -95,13 +95,13 @@ model Artefact { earthquake Earthquake @relation(fields: [earthquakeId], references: [id]) creatorId Int? creator Scientist? @relation("ScientistArtefactCreator", fields: [creatorId], references: [id], onDelete: NoAction, onUpdate: NoAction) - required Boolean @default(true) + isRequired Boolean @default(true) dateAddedToShop DateTime? shopPrice Float? - purchased Boolean @default(false) + isSold Boolean @default(false) purchasedById Int? purchasedBy User? @relation("UserPurchasedArtefacts", fields: [purchasedById], references: [id], onDelete: NoAction, onUpdate: NoAction) - pickedUp Boolean @default(false) + isCollected Boolean @default(false) } model Pallet { diff --git a/src/app/api/login/route.ts b/src/app/api/login/route.ts index 3262be3..06b8657 100644 --- a/src/app/api/login/route.ts +++ b/src/app/api/login/route.ts @@ -20,41 +20,34 @@ export async function POST(req: Request) { console.log("Email:", email); // ! remove console.log("Password:", password); // ! remove - let user; + let user = await prisma.user.findUnique({ + where: { + email, // use the email to uniquely identify the user + }, + }); - if (usingPrisma) { - user = await prisma.user.findUnique({ - where: { - email, // use the email to uniquely identify the user - }, - }); - } else { - user = findUserByEmail(userData, email); - } - - if (user && bcryptjs.compareSync(password, usingPrisma ? user.hashedPassword : user.password)) { + if (user && bcryptjs.compareSync(password, user.passwordHash)) { // todo remove password from returned user // get user and relations - if (usingPrisma) - user = await prisma.user.findUnique({ - where: { id: user.id }, - include: { - scientist: { - include: { - earthquakes: true, - observatories: true, - artefacts: true, - superior: true, - subordinates: true, - }, + user = await prisma.user.findUnique({ + where: { id: user.id }, + include: { + scientist: { + include: { + earthquakes: true, + observatories: true, + artefacts: true, + superior: true, + subordinates: true, }, - purchasedArtefacts: true, }, - }); + purchasedArtefacts: true, + }, + }); const secret = new TextEncoder().encode(env.JWT_SECRET_KEY); - const token = await new SignJWT({ userId: user.id }) + const token = await new SignJWT({ userId: user!.id }) .setProtectedHeader({ alg: "HS256" }) .setExpirationTime("2w") .sign(secret); diff --git a/src/app/api/observatories/route.ts b/src/app/api/observatories/route.ts index b4c68bf..28258fe 100644 --- a/src/app/api/observatories/route.ts +++ b/src/app/api/observatories/route.ts @@ -37,7 +37,7 @@ export async function GET(request: Request) { // todo get earthquakes associated with observatories let observatories; - if (usingPrisma) observatories = await prisma.observatories.findMany(); + if (usingPrisma) observatories = await prisma.observatory.findMany(); if (observatories) { return NextResponse.json({ message: "Got observatories successfully", observatories }, { status: 200 }); diff --git a/src/app/api/warehouse/route.ts b/src/app/api/warehouse/route.ts index 8417ca6..0eedb14 100644 --- a/src/app/api/warehouse/route.ts +++ b/src/app/api/warehouse/route.ts @@ -88,7 +88,7 @@ export async function POST(req: Request) { ]; let artefacts; - if (usingPrisma) artefacts = await prisma.artefacts.findMany(); + if (usingPrisma) artefacts = await prisma.artefact.findMany(); if (artefacts) { return NextResponse.json({ message: "Got artefacts successfully", artefacts }, { status: 200 }); diff --git a/src/app/warehouse/page.tsx b/src/app/warehouse/page.tsx index a7825af..bd5a3b2 100644 --- a/src/app/warehouse/page.tsx +++ b/src/app/warehouse/page.tsx @@ -8,62 +8,61 @@ import { IoFilter, IoFilterCircleOutline, IoFilterOutline, IoToday } from "react import type { Artefact } from "@prismaclient"; +interface WarehouseArtefact extends Artefact { + location: string; +} + // Warehouse Artefacts Data -const warehouseArtefacts: Artefact[] = [ +const warehouseArtefacts: WarehouseArtefact[] = [ { id: 1, name: "Solidified Lava Chunk", description: "A chunk of solidified lava from the 2023 Iceland eruption.", location: "Reykjanes, Iceland", - code: "EQ2023ICL", isRequired: true, isSold: false, isCollected: false, - dateAdded: "2025-05-04", + createdAt: "2025-05-04", }, { id: 2, name: "Tephra Sample", description: "Foreign debris from the 2022 Tonga volcanic eruption.", location: "Tonga", - code: "EQ2022TGA", isRequired: false, isSold: true, isCollected: true, - dateAdded: "2025-05-03", + createdAt: "2025-05-03", }, { id: 3, name: "Ash Sample", description: "Volcanic ash from the 2021 La Palma eruption.", location: "La Palma, Spain", - code: "EQ2021LPA", isRequired: false, isSold: false, isCollected: false, - dateAdded: "2025-05-04", + createdAt: "2025-05-04", }, { id: 4, name: "Ground Soil", description: "Soil sample from the 2020 Croatia earthquake site.", location: "Zagreb, Croatia", - code: "EQ2020CRO", isRequired: true, isSold: false, isCollected: false, - dateAdded: "2025-05-02", + createdAt: "2025-05-02", }, { id: 5, name: "Basalt Fragment", description: "Basalt rock from the 2019 New Zealand eruption.", location: "White Island, New Zealand", - code: "EQ2019NZL", isRequired: false, isSold: true, isCollected: false, - dateAdded: "2025-05-04", + createdAt: "2025-05-04", }, ]; @@ -185,7 +184,7 @@ function ArtefactTable({ { label: "Required", key: "isRequired", width: "6%" }, { label: "Sold", key: "isSold", width: "5%" }, { label: "Collected", key: "isCollected", width: "7%" }, - { label: "Date Added", key: "dateAdded", width: "8%" }, + { label: "Date Added", key: "createdAt", width: "8%" }, ]; return ( @@ -205,7 +204,7 @@ function ArtefactTable({ setFilters({ ...filters, [key]: value } as Record); if (value === "") clearSortConfig(); }} - type={key === "dateAdded" ? "date" : "text"} + type={key === "createdAt" ? "date" : "text"} options={["isRequired", "isSold", "isCollected"].includes(key) ? ["", "true", "false"] : undefined} /> {sortConfig?.key === key && ( @@ -501,7 +500,7 @@ function EditModal({ artefact, onClose }: { artefact: Artefact; onClose: () => v const [isRequired, setIsRequired] = useState(artefact.isRequired); const [isSold, setIsSold] = useState(artefact.isSold); const [isCollected, setIsCollected] = useState(artefact.isCollected); - const [dateAdded, setDateAdded] = useState(artefact.dateAdded); + const [createdAt, setDateAdded] = useState(artefact.createdAt.toDateString()); const [error, setError] = useState(""); const [isSubmitting, setIsSubmitting] = useState(false); @@ -512,7 +511,7 @@ function EditModal({ artefact, onClose }: { artefact: Artefact; onClose: () => v }; const handleSave = async () => { - if (!name || !description || !location || !earthquakeId || !dateAdded) { + if (!name || !description || !location || !earthquakeId || !createdAt) { setError("All fields are required."); return; } @@ -603,7 +602,7 @@ function EditModal({ artefact, onClose }: { artefact: Artefact; onClose: () => v setDateAdded(e.target.value)} className="w-full p-2 border border-neutral-300 rounded-md focus:ring-2 focus:ring-blue-500" aria-label="Date Added" @@ -658,13 +657,14 @@ const applyFilters = (artefacts: Artefact[], filters: Record): A return ( (filters.id === "" || artefact.id.toString().includes(filters.id)) && (filters.name === "" || artefact.name.toLowerCase().includes(filters.name.toLowerCase())) && + // todo fix (filters.earthquakeId === "" || artefact.earthquakeId.toLowerCase().includes(filters.earthquakeId.toLowerCase())) && (filters.location === "" || artefact.location.toLowerCase().includes(filters.location.toLowerCase())) && (filters.description === "" || artefact.description.toLowerCase().includes(filters.description.toLowerCase())) && (filters.isRequired === "" || (filters.isRequired === "true" ? artefact.isRequired : !artefact.isRequired)) && (filters.isSold === "" || (filters.isSold === "true" ? artefact.isSold : !artefact.isSold)) && (filters.isCollected === "" || (filters.isCollected === "true" ? artefact.isCollected : !artefact.isCollected)) && - (filters.dateAdded === "" || artefact.dateAdded === filters.dateAdded) + (filters.createdAt === "" || artefact.createdAt.toDateString() === filters.createdAt) ); }); }; @@ -684,7 +684,7 @@ export default function Warehouse() { isRequired: "", isSold: "", isCollected: "", - dateAdded: "", + createdAt: "", }); const [isFiltering, setIsFiltering] = useState(false); const [sortConfig, setSortConfig] = useState<{ @@ -708,9 +708,11 @@ export default function Warehouse() { // Overview stats const totalArtefacts = warehouseArtefacts.length; - const today = "2025-05-04"; - const artefactsAddedToday = warehouseArtefacts.filter((a) => a.dateAdded === today).length; - const artefactsSoldToday = warehouseArtefacts.filter((a) => a.isSold && a.dateAdded === today).length; + const today = new Date(); + const artefactsAddedToday = warehouseArtefacts.filter((a) => a.createdAt.toDateString() === today.toDateString()).length; + const artefactsSoldToday = warehouseArtefacts.filter( + (a) => a.isSold && a.createdAt.toDateString() === today.toDateString() + ).length; const clearFilters = () => { setFilters({ @@ -722,7 +724,7 @@ export default function Warehouse() { isRequired: "", isSold: "", isCollected: "", - dateAdded: "", + createdAt: "", }); setSortConfig(null); // Clear sorting };