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
|
- [x] Import artefacts
|
||||||
- [ ] Import earthquakes
|
- [ ] Import earthquakes
|
||||||
- [ ] Import observatoies
|
- [ ] Import observatoies
|
||||||
|
|||||||
@ -6,7 +6,7 @@ import { stringToBool } from "@utils/parsingUtils";
|
|||||||
import { prisma } from "@utils/prisma";
|
import { prisma } from "@utils/prisma";
|
||||||
import { getRandomNumber } from "@utils/maths";
|
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 CsvRow = {
|
||||||
Type: string;
|
Type: string;
|
||||||
@ -45,7 +45,7 @@ export async function POST() {
|
|||||||
|
|
||||||
if (!earthquake || !randomCreator) {
|
if (!earthquake || !randomCreator) {
|
||||||
failedImports.push({ row, reason: `Earthquake: ${earthquake}, RandomCreator: ${randomCreator}` });
|
failedImports.push({ row, reason: `Earthquake: ${earthquake}, RandomCreator: ${randomCreator}` });
|
||||||
return undefined;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
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({
|
await prisma.artefact.createMany({
|
||||||
data: validArtefacts,
|
data: validArtefacts,
|
||||||
|
|||||||
@ -5,18 +5,21 @@ import path from "path";
|
|||||||
|
|
||||||
import { prisma } from "@utils/prisma";
|
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 = {
|
type CsvRow = {
|
||||||
|
id: string;
|
||||||
|
createdAt: string;
|
||||||
Name: string;
|
Name: string;
|
||||||
Email: string;
|
Email: string;
|
||||||
PasswordHash: string;
|
PasswordHash: string;
|
||||||
Role?: string;
|
Role: string;
|
||||||
|
Scientist: string;
|
||||||
|
PurchasedArtefacts: string;
|
||||||
|
Requests: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
function normalizeRole(role: string | undefined): string {
|
function normalizeRole(role: string | undefined): string {
|
||||||
// Only allow ADMIN, SCIENTIST, GUEST; default GUEST
|
|
||||||
if (!role || !role.trim()) return "GUEST";
|
if (!role || !role.trim()) return "GUEST";
|
||||||
const r = role.trim().toUpperCase();
|
const r = role.trim().toUpperCase();
|
||||||
return ["ADMIN", "SCIENTIST", "GUEST"].includes(r) ? r : "GUEST";
|
return ["ADMIN", "SCIENTIST", "GUEST"].includes(r) ? r : "GUEST";
|
||||||
@ -24,29 +27,46 @@ function normalizeRole(role: string | undefined): string {
|
|||||||
|
|
||||||
export async function POST() {
|
export async function POST() {
|
||||||
try {
|
try {
|
||||||
// 1. Read the CSV file
|
|
||||||
const fileContent = await fs.readFile(csvFilePath, "utf8");
|
const fileContent = await fs.readFile(csvFilePath, "utf8");
|
||||||
|
|
||||||
// 2. Parse the CSV
|
|
||||||
const records: CsvRow[] = parse(fileContent, {
|
const records: CsvRow[] = parse(fileContent, {
|
||||||
columns: true,
|
columns: true,
|
||||||
skip_empty_lines: true,
|
skip_empty_lines: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
// 3. Transform each CSV row to User model format
|
const failedImports: { row: CsvRow; reason: string }[] = [];
|
||||||
const users = records.map((row) => ({
|
|
||||||
name: row.Name,
|
const users = await Promise.all(
|
||||||
email: row.Email,
|
records.map(async (row) => {
|
||||||
passwordHash: row.PasswordHash,
|
try {
|
||||||
role: normalizeRole(row.Role),
|
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({
|
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) {
|
} catch (error: any) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
return NextResponse.json({ success: false, error: error.message }, { status: 500 });
|
return NextResponse.json({ success: false, error: error.message }, { status: 500 });
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user