From 46411667d604f9428cd8f98d3e1cb0bf19e73d07 Mon Sep 17 00:00:00 2001 From: IZZY Date: Mon, 19 May 2025 12:42:13 +0100 Subject: [PATCH] imports --- prisma/schema.prisma | 26 +++++++++++ src/app/api/import-requests/route.ts | 67 ++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 src/app/api/import-requests/route.ts diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 3e0e8c6..c7a2f1f 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -4,6 +4,21 @@ datasource db { url = env("DATABASE_URL") } +// Enums for Request +enum RequestType { + NEW_USER + CHANGE_LEVEL + DELETE +} + +enum RequestOutcome { + FULFILLED + REJECTED + IN_PROGRESS + CANCELLED + OTHER +} + // User model model User { id Int @id @default(autoincrement()) @@ -14,6 +29,17 @@ model User { role String @default("GUEST") @db.VarChar(10) // ADMIN, SCIENTIST, GUEST scientist Scientist? @relation purchasedArtefacts Artefact[] @relation("UserPurchasedArtefacts") + requests Request[] @relation("UserRequests") +} + +// Request model +model Request { + id Int @id @default(autoincrement()) + createdAt DateTime @default(now()) + requestType RequestType + requestingUser User @relation("UserRequests", fields: [requestingUserId], references: [id]) + requestingUserId Int + outcome RequestOutcome @default(IN_PROGRESS) } // Scientist model diff --git a/src/app/api/import-requests/route.ts b/src/app/api/import-requests/route.ts new file mode 100644 index 0000000..e792b66 --- /dev/null +++ b/src/app/api/import-requests/route.ts @@ -0,0 +1,67 @@ +import { NextResponse } from "next/server"; +import { PrismaClient, RequestType, RequestOutcome } from "@prisma/client"; +import fs from "fs/promises"; +import path from "path"; +import { parse } from "csv-parse/sync"; + +// Path to CSV file +const csvFilePath = path.resolve(process.cwd(), "public/requests.csv"); +const prisma = new PrismaClient(); + +type CsvRow = { + RequestType: string; + RequestingUserId: string; // User id as string in CSV + Outcome?: string; // optional; default is IN_PROGRESS if not supplied +}; + +// Valid enums for checking +const validRequestTypes = Object.values(RequestType); +const validOutcomes = Object.values(RequestOutcome); + +function normalizeRequestType(type: string | undefined): RequestType { + if (!type) return RequestType.NEW_USER; + const norm = type.trim().toUpperCase().replace(" ", "_"); + return (validRequestTypes.includes(norm as any) ? norm : RequestType.NEW_USER) as RequestType; +} + +function normalizeOutcome(outcome: string | undefined): RequestOutcome { + if (!outcome) return RequestOutcome.IN_PROGRESS; + const norm = outcome.trim().toUpperCase().replace(" ", "_"); + return (validOutcomes.includes(norm as any) ? norm : RequestOutcome.IN_PROGRESS) as RequestOutcome; +} + +export async function POST() { + try { + // 1. Read the CSV file + const fileContent = await fs.readFile(csvFilePath, "utf8"); + + // 2. Parse the CSV + const records: CsvRow[] = parse(fileContent, { + columns: true, + skip_empty_lines: true, + }); + + // 3. Transform each record for Prisma + const requests = records.map(row => ({ + requestType: normalizeRequestType(row.RequestType), + requestingUserId: parseInt(row.RequestingUserId, 10), + outcome: normalizeOutcome(row.Outcome), + })); + + // (optional) Filter out rows missing requestingUserId + const filteredRequests = requests.filter(r => !isNaN(r.requestingUserId)); + + // 4. Bulk create requests in database + await prisma.request.createMany({ + data: filteredRequests, + skipDuplicates: true, // in case request id/user-id already exists + }); + + return NextResponse.json({ success: true, count: filteredRequests.length }); + } catch (error: any) { + console.error(error); + return NextResponse.json({ success: false, error: error.message }, { status: 500 }); + } finally { + await prisma.$disconnect(); + } +} \ No newline at end of file