Files
Travel-policy-/backend/prisma/seed.js

150 lines
4.1 KiB
JavaScript

import { PrismaClient } from "@prisma/client";
import bcrypt from "bcryptjs";
const prisma = new PrismaClient();
async function main() {
// Use a common password for all development users to make testing easier
const commonPassword = "securePassword123";
// Secure the password with hashing even for seed data!
const salt = await bcrypt.genSalt(10);
const hashedPassword = await bcrypt.hash(commonPassword, salt);
const institutes = [
"KJSIDS",
"SKSC",
"KJSCE",
"SIRC",
"KJSIM",
"SSA",
"KJSCEd",
"DLIS",
"MSSMPA",
];
const departments = [
"Mechanical",
"Electronics",
"CBE",
"Electronics & Telecommunication",
"Computer",
"Information Technology",
"Science & Humanities",
"Admin",
"Library",
];
console.log("Seeding started...");
// VC is usually global
// We use 'upsert' here. It means "Update if exists, Insert if not".
// This prevents errors if we run the seed script multiple times.
await prisma.user.upsert({
where: { email: "vc@somaiya.edu" },
update: {},
create: {
userName: "Vice Chancellor",
email: "vc@somaiya.edu",
password: hashedPassword,
institute: "KJSCE",
department: "Management",
designation: "VC",
},
});
for (const institute of institutes) {
// 1. Create HOI for this institute
console.log(`Seeding HOI for ${institute}...`);
const hoiEmail = `hoi@${institute.toLowerCase()}.edu`;
await prisma.user.upsert({
where: { email: hoiEmail },
update: {},
create: {
userName: `HOI_${institute}`,
email: hoiEmail,
password: hashedPassword,
institute,
department: "Administration",
designation: "HOI",
},
});
// 2. Create Accounts for this institute
console.log(`Seeding Accounts for ${institute}...`);
const accountsEmail = `accounts.${institute.toLowerCase()}@example.com`;
await prisma.user.upsert({
where: { email: accountsEmail },
update: {},
create: {
userName: `Validator_Accounts_${institute}`,
email: accountsEmail,
password: hashedPassword,
institute,
designation: "ACCOUNTS",
},
});
for (const department of departments) {
// 3. Create HOD for each department of each institute
console.log(`Seeding HOD for ${department} in ${institute}...`);
const hodEmail = `hod.${department.toLowerCase()}.${institute.toLowerCase()}@example.com`;
await prisma.user.upsert({
where: { email: hodEmail },
update: {},
create: {
userName: `HOD_${department}_${institute}`,
email: hodEmail,
password: hashedPassword,
institute,
department,
designation: "HOD",
},
});
// 4. Create Faculty for each department of each institute
console.log(`Seeding Faculty for ${department} in ${institute}...`);
const facultyEmail = `faculty.${department.toLowerCase()}.${institute.toLowerCase()}@example.com`;
await prisma.user.upsert({
where: { email: facultyEmail },
update: {},
create: {
userName: `Faculty_${department}_${institute}`,
email: facultyEmail,
password: hashedPassword,
institute,
department,
designation: "FACULTY",
},
});
// 5. Create Student for each department of each institute
console.log(`Seeding Student for ${department} in ${institute}...`);
const studentEmail = `student.${department.toLowerCase()}.${institute.toLowerCase()}@example.com`;
await prisma.user.upsert({
where: { email: studentEmail },
update: {},
create: {
userName: `Student_${department}_${institute}`,
email: studentEmail,
password: hashedPassword,
institute,
department,
designation: "STUDENT",
},
});
}
}
console.log("Seeding completed successfully.");
}
main()
.then(async () => {
await prisma.$disconnect();
})
.catch(async (e) => {
console.error(e);
await prisma.$disconnect();
process.exit(1);
});