imports
This commit is contained in:
parent
30a9f38888
commit
46411667d6
@ -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
|
||||
|
||||
67
src/app/api/import-requests/route.ts
Normal file
67
src/app/api/import-requests/route.ts
Normal file
@ -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();
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user