All the route files

This commit is contained in:
IZZY 2025-05-12 14:19:52 +01:00
parent 52f17d5a00
commit db3e0f958b
7 changed files with 257 additions and 3 deletions

View File

@ -1,4 +1,3 @@
Name,Location,Latitude,Longitude,Date Established,Functional
Pacific Apex Seismic Center,"Aleutian Trench, Alaska, USA",53.0000,-168.0000,1973-06-15,Yes
Cascadia Quake Research Institute,"Oregon Coast, USA",44.5000,-124.0000,1985-03-22,Yes
Andes Fault Survey Observatory,"Nazca-South American Plate, Santiago, Chile",-33.4500,-70.6667,1992-10-10,Yes
1 Name Pacific Apex Seismic Center Location Aleutian Trench, Alaska, USA Latitude 53.0000 Longitude -168.0000 Date Established 1973-06-15 Functional Yes
Name Location Latitude Longitude Date Established Functional
1 Pacific Apex Seismic Center Pacific Apex Seismic Center Aleutian Trench, Alaska, USA Aleutian Trench, Alaska, USA 53.0000 53.0000 -168.0000 -168.0000 1973-06-15 1973-06-15 Yes
2 Cascadia Quake Research Institute Cascadia Quake Research Institute Oregon Coast, USA Oregon Coast, USA 44.5000 44.5000 -124.0000 -124.0000 1985-03-22 1985-03-22 Yes
3 Andes Fault Survey Observatory Andes Fault Survey Observatory Nazca-South American Plate, Santiago, Chile Nazca-South American Plate, Santiago, Chile -33.4500 -33.4500 -70.6667 -70.6667 1992-10-10 1992-10-10 Yes

View File

@ -1,4 +1,3 @@
Name,Level,Superior
Dr. Emily Neighbour Carter,Senior,None
Dr. Rajiv Menon,Senior,None
Dr. Izzy Patterson,Senior,None
1 Name Dr. Emily Neighbour Carter Level Senior Superior None
Name Level Superior
1 Dr. Emily Neighbour Carter Dr. Emily Neighbour Carter Senior None None
2 Dr. Rajiv Menon Dr. Rajiv Menon Senior None None
3 Dr. Izzy Patterson Dr. Izzy Patterson Senior None None

View File

@ -1,4 +1,4 @@
Date,Magnitude,Latitude,Longitude,Location,Depth
Date,Magnitude,Latitude,Longitude,Location,Depth
2024-01-01,4.7,23.031,119.8622,Banda Arc Indonesia,17 km
2024-01-01,4.8,-84.496,-111.9335,South Sandwich Plate Collision Zone South Sandwich Islands,56 km
2024-01-01,8.6,38.2523,-167.0921,Cape Fold Belt South Africa,150 km

1 Date Magnitude Latitude Longitude Location Depth
2 2024-01-01 4.7 23.031 119.8622 Banda Arc Indonesia 17 km
3 2024-01-01 4.8 -84.496 -111.9335 South Sandwich Plate Collision Zone South Sandwich Islands 56 km
4 2024-01-01 8.6 38.2523 -167.0921 Cape Fold Belt South Africa 150 km

View File

@ -0,0 +1,69 @@
import { NextResponse } from "next/server";
import { PrismaClient } from "@prisma/client";
import fs from "fs/promises";
import path from "path";
import { parse } from "csv-parse/sync";
// CSV location
const csvFilePath = path.resolve(process.cwd(), "public/artefacts.csv");
const prisma = new PrismaClient();
type CsvRow = {
Type: string;
WarehouseArea: string;
EarthquakeId: string;
Required?: string;
ShopPrice?: string;
PickedUp?: string;
};
function stringToBool(val: string | undefined, defaultValue: boolean = false): boolean {
if (!val) return defaultValue;
return /^true$/i.test(val.trim());
}
export async function POST() {
try {
// 1. Read file
const fileContent = await fs.readFile(csvFilePath, "utf8");
// 2. Parse CSV
const records: CsvRow[] = parse(fileContent, {
columns: true,
skip_empty_lines: true,
});
// 3. Map records to artefact input
const artefacts = records.map((row) => ({
type: row.Type,
warehouseArea: row.WarehouseArea,
earthquakeId: parseInt(row.EarthquakeId, 10),
required: stringToBool(row.Required, true), // default TRUE
shopPrice: row.ShopPrice && row.ShopPrice !== ""
? parseFloat(row.ShopPrice)
: null,
pickedUp: stringToBool(row.PickedUp, false), // default FALSE
creatorId: null,
purchasedById: null,
}));
// 4. Bulk insert
await prisma.artefact.createMany({
data: artefacts,
skipDuplicates: true,
});
return NextResponse.json({
success: true,
count: artefacts.length,
});
} catch (error: any) {
console.error(error);
return NextResponse.json(
{ success: false, error: error.message },
{ status: 500 }
);
} finally {
await prisma.$disconnect();
}
}

View File

@ -0,0 +1,73 @@
import { NextResponse } from "next/server";
import { PrismaClient } from "@prisma/client";
import fs from "fs/promises";
import path from "path";
import { parse } from "csv-parse/sync";
// CSV location (update filename as needed)
const csvFilePath = path.resolve(process.cwd(), "public/observatories.csv");
const prisma = new PrismaClient();
type CsvRow = {
Name: string;
Location: string;
Latitude: string;
Longitude: string;
DateEstablished?: string;
Functional: string;
SeismicSensorOnline?: string;
};
function stringToBool(val: string | undefined): boolean {
// Accepts "TRUE", "true", "True", etc.
if (!val) return false;
return /^true$/i.test(val.trim());
}
export async function POST() {
try {
// 1. Read file
const fileContent = await fs.readFile(csvFilePath, "utf8");
// 2. Parse CSV
const records: CsvRow[] = parse(fileContent, {
columns: true,
skip_empty_lines: true,
});
// 3. Map records to Prisma inputs
const observatories = records.map((row) => ({
name: row.Name,
location: row.Location,
latitude: row.Latitude,
longitude: row.Longitude,
dateEstablished: row.DateEstablished
? parseInt(row.DateEstablished, 10)
: null,
functional: stringToBool(row.Functional),
seismicSensorOnline: row.SeismicSensorOnline
? stringToBool(row.SeismicSensorOnline)
: true, // default true per schema
creatorId: null,
}));
// 4. Bulk insert
await prisma.observatory.createMany({
data: observatories,
skipDuplicates: true,
});
return NextResponse.json({
success: true,
count: observatories.length,
});
} catch (error: any) {
console.error(error);
return NextResponse.json(
{ success: false, error: error.message },
{ status: 500 }
);
} finally {
await prisma.$disconnect();
}
}

View File

@ -0,0 +1,57 @@
import { NextResponse } from "next/server";
import { PrismaClient } 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/scientists.csv");
const prisma = new PrismaClient();
type CsvRow = {
Name: string;
Level?: string;
UserId: string;
SuperiorId?: string;
};
function normalizeLevel(level: string | undefined): string {
// Only allow JUNIOR, SENIOR; default JUNIOR
if (!level || !level.trim()) return "JUNIOR";
const lv = level.trim().toUpperCase();
return ["JUNIOR", "SENIOR"].includes(lv) ? lv : "JUNIOR";
}
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 scientists = records.map(row => ({
name: row.Name,
level: normalizeLevel(row.Level),
userId: parseInt(row.UserId, 10),
superiorId: row.SuperiorId && row.SuperiorId.trim() !== "" ? parseInt(row.SuperiorId, 10) : null,
}));
// 4. Bulk create scientists in database
await prisma.scientist.createMany({
data: scientists,
skipDuplicates: true, // in case the scientist/userid combo already exists
});
return NextResponse.json({ success: true, count: scientists.length });
} catch (error: any) {
console.error(error);
return NextResponse.json({ success: false, error: error.message }, { status: 500 });
} finally {
await prisma.$disconnect();
}
}

View File

@ -0,0 +1,57 @@
import { NextResponse } from "next/server";
import { PrismaClient } from "@prisma/client";
import fs from "fs/promises";
import path from "path";
import { parse } from "csv-parse/sync";
// Path to users.csv - adjust as needed
const csvFilePath = path.resolve(process.cwd(), "public/users.csv");
const prisma = new PrismaClient();
type CsvRow = {
Name: string;
Email: string;
PasswordHash: string;
Role?: 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";
}
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),
}));
// 4. Bulk create users in database
await prisma.user.createMany({
data: users,
skipDuplicates: true, // because email is unique
});
return NextResponse.json({ success: true, count: users.length });
} catch (error: any) {
console.error(error);
return NextResponse.json({ success: false, error: error.message }, { status: 500 });
} finally {
await prisma.$disconnect();
}
}