diff --git a/importersFixed.md b/importersFixed.md index 9c49ae5..050c3d0 100644 --- a/importersFixed.md +++ b/importersFixed.md @@ -1,4 +1,4 @@ -- [ ] Import users +- [x] Import users - [x] Import artefacts - [ ] Import earthquakes - [ ] Import observatoies diff --git a/src/app/api/import-artefacts/route.ts b/src/app/api/import-artefacts/route.ts index 01d2b08..7c39988 100644 --- a/src/app/api/import-artefacts/route.ts +++ b/src/app/api/import-artefacts/route.ts @@ -6,7 +6,7 @@ import { stringToBool } from "@utils/parsingUtils"; import { prisma } from "@utils/prisma"; import { getRandomNumber } from "@utils/maths"; -const csvFilePath = path.resolve(process.cwd(), "public/artefacts.csv"); +const csvFilePath = path.resolve(process.cwd(), "public/Artefacts.csv"); type CsvRow = { Type: string; @@ -45,7 +45,7 @@ export async function POST() { if (!earthquake || !randomCreator) { failedImports.push({ row, reason: `Earthquake: ${earthquake}, RandomCreator: ${randomCreator}` }); - return undefined; + return null; } return { @@ -64,7 +64,7 @@ export async function POST() { }) ); - const validArtefacts = artefacts.filter((artefact): artefact is NonNullable => artefact !== undefined); + const validArtefacts = artefacts.filter((artefact): artefact is NonNullable => artefact !== null); await prisma.artefact.createMany({ data: validArtefacts, diff --git a/src/app/api/import-users/route.ts b/src/app/api/import-users/route.ts index 1e4f166..77ae9d9 100644 --- a/src/app/api/import-users/route.ts +++ b/src/app/api/import-users/route.ts @@ -5,18 +5,21 @@ import path from "path"; import { prisma } from "@utils/prisma"; -// Path to users.csv - adjust as needed -const csvFilePath = path.resolve(process.cwd(), "public/users.csv"); +const csvFilePath = path.resolve(process.cwd(), "public/Users.csv"); type CsvRow = { + id: string; + createdAt: string; Name: string; Email: string; PasswordHash: string; - Role?: string; + Role: string; + Scientist: string; + PurchasedArtefacts: string; + Requests: string; }; function normalizeRole(role: string | undefined): string { - // Only allow ADMIN, SCIENTIST, GUEST; default GUEST if (!role || !role.trim()) return "GUEST"; const r = role.trim().toUpperCase(); return ["ADMIN", "SCIENTIST", "GUEST"].includes(r) ? r : "GUEST"; @@ -24,29 +27,46 @@ function normalizeRole(role: string | undefined): string { 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 CSV row to User model format - const users = records.map((row) => ({ - name: row.Name, - email: row.Email, - passwordHash: row.PasswordHash, - role: normalizeRole(row.Role), - })); + const failedImports: { row: CsvRow; reason: string }[] = []; + + const users = await Promise.all( + records.map(async (row) => { + try { + return { + name: row.Name, + email: row.Email, + passwordHash: row.PasswordHash, + role: normalizeRole(row.Role), + }; + } catch (error: any) { + failedImports.push({ row, reason: error.message }); + return null; + } + }) + ); + + const validUsers = users.filter((user): user is NonNullable => user !== null); - // 4. Bulk create users in database await prisma.user.createMany({ - data: users, + data: validUsers, }); - return NextResponse.json({ success: true, count: users.length }); + if (failedImports.length > 0) { + console.warn("Failed imports:", failedImports); + await fs.writeFile(path.resolve(process.cwd(), "failed_imports_users.json"), JSON.stringify(failedImports, null, 2)); + } + + return NextResponse.json({ + success: true, + count: validUsers.length, + failedCount: failedImports.length, + }); } catch (error: any) { console.error(error); return NextResponse.json({ success: false, error: error.message }, { status: 500 });