Fixed users importing script
This commit is contained in:
parent
b40d0aedb4
commit
68d47a4fe3
@ -1,4 +1,4 @@
|
||||
- [ ] Import users
|
||||
- [x] Import users
|
||||
- [x] Import artefacts
|
||||
- [ ] Import earthquakes
|
||||
- [ ] Import observatoies
|
||||
|
||||
@ -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<typeof artefact> => artefact !== undefined);
|
||||
const validArtefacts = artefacts.filter((artefact): artefact is NonNullable<typeof artefact> => artefact !== null);
|
||||
|
||||
await prisma.artefact.createMany({
|
||||
data: validArtefacts,
|
||||
|
||||
@ -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<typeof user> => 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 });
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user