(Feat): Initial Commit
This commit is contained in:
247
backend-deno/scripts/seed.ts
Normal file
247
backend-deno/scripts/seed.ts
Normal file
@@ -0,0 +1,247 @@
|
||||
import { hash } from "bcrypt";
|
||||
import { db } from "../config/database.ts";
|
||||
import { config } from "../config/env.ts";
|
||||
|
||||
async function seedDatabase() {
|
||||
try {
|
||||
console.log("🔌 Connecting to database...");
|
||||
await db.connect();
|
||||
console.log("✅ Connected to database\n");
|
||||
|
||||
// 1. Seed Departments
|
||||
console.log("📁 Seeding departments...");
|
||||
const existingDepts = await db.query<{ count: number }[]>(
|
||||
"SELECT COUNT(*) as count FROM departments"
|
||||
);
|
||||
|
||||
if (existingDepts[0].count === 0) {
|
||||
await db.execute(`
|
||||
INSERT INTO departments (name) VALUES
|
||||
('Tudki'),
|
||||
('Dana'),
|
||||
('Groundnut')
|
||||
`);
|
||||
console.log(" ✅ Departments created");
|
||||
} else {
|
||||
console.log(" ℹ️ Departments already exist");
|
||||
}
|
||||
|
||||
// 2. Seed Sub-departments for Groundnut
|
||||
console.log("📂 Seeding sub-departments...");
|
||||
const groundnutDept = await db.query<{ id: number }[]>(
|
||||
"SELECT id FROM departments WHERE name = ?",
|
||||
["Groundnut"]
|
||||
);
|
||||
|
||||
let groundnutId: number | null = null;
|
||||
|
||||
if (groundnutDept.length > 0) {
|
||||
groundnutId = groundnutDept[0].id;
|
||||
const existingSubDepts = await db.query<{ count: number }[]>(
|
||||
"SELECT COUNT(*) as count FROM sub_departments WHERE department_id = ?",
|
||||
[groundnutId]
|
||||
);
|
||||
|
||||
if (existingSubDepts[0].count === 0) {
|
||||
const subDepts = [
|
||||
"Mufali Aavak Katai",
|
||||
"Mufali Aavak Dhang",
|
||||
"Dhang Se Katai",
|
||||
"Guthli Bori Silai Dhang",
|
||||
"Guthali dada Pala Tulai Silai Dhang",
|
||||
"Mufali Patthar Bori silai dhang",
|
||||
"Mufali Patthar Bori Utrai",
|
||||
"Bardana Bandal Loading Unloading",
|
||||
"Bardana Gatthi Loading",
|
||||
"Black Dana Loading/Unloading",
|
||||
"Pre Cleaning",
|
||||
"Destoner",
|
||||
"Water",
|
||||
"Decordicater",
|
||||
"Round Chalna",
|
||||
"Cleaning",
|
||||
"Round Chalna No.1"
|
||||
];
|
||||
|
||||
for (const name of subDepts) {
|
||||
await db.execute(
|
||||
"INSERT INTO sub_departments (department_id, name, primary_activity) VALUES (?, ?, ?)",
|
||||
[groundnutId, name, "Loading/Unloading"]
|
||||
);
|
||||
}
|
||||
console.log(" ✅ Sub-departments created");
|
||||
} else {
|
||||
console.log(" ℹ️ Sub-departments already exist");
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Seed SuperAdmin
|
||||
console.log("👤 Seeding SuperAdmin user...");
|
||||
const existingAdmin = await db.query<{ id: number }[]>(
|
||||
"SELECT id FROM users WHERE username = ?",
|
||||
["admin"]
|
||||
);
|
||||
|
||||
const adminPassword = await hash("admin123", config.BCRYPT_ROUNDS);
|
||||
|
||||
if (existingAdmin.length > 0) {
|
||||
await db.execute(
|
||||
"UPDATE users SET password = ?, is_active = TRUE WHERE username = ?",
|
||||
[adminPassword, "admin"]
|
||||
);
|
||||
console.log(" ✅ SuperAdmin password updated");
|
||||
} else {
|
||||
await db.execute(
|
||||
"INSERT INTO users (username, name, email, password, role, is_active) VALUES (?, ?, ?, ?, ?, ?)",
|
||||
["admin", "Super Admin", "admin@workallocate.com", adminPassword, "SuperAdmin", true]
|
||||
);
|
||||
console.log(" ✅ SuperAdmin created");
|
||||
}
|
||||
|
||||
// 4. Seed Sample Supervisors
|
||||
console.log("👥 Seeding sample supervisors...");
|
||||
const tudkiDept = await db.query<{ id: number }[]>(
|
||||
"SELECT id FROM departments WHERE name = ?",
|
||||
["Tudki"]
|
||||
);
|
||||
const danaDept = await db.query<{ id: number }[]>(
|
||||
"SELECT id FROM departments WHERE name = ?",
|
||||
["Dana"]
|
||||
);
|
||||
|
||||
const supervisorPassword = await hash("supervisor123", config.BCRYPT_ROUNDS);
|
||||
|
||||
const supervisors = [
|
||||
{ username: "supervisor_tudki", name: "Tudki Supervisor", email: "supervisor.tudki@workallocate.com", deptId: tudkiDept[0]?.id },
|
||||
{ username: "supervisor_dana", name: "Dana Supervisor", email: "supervisor.dana@workallocate.com", deptId: danaDept[0]?.id },
|
||||
{ username: "supervisor_groundnut", name: "Groundnut Supervisor", email: "supervisor.groundnut@workallocate.com", deptId: groundnutId }
|
||||
];
|
||||
|
||||
for (const sup of supervisors) {
|
||||
if (sup.deptId) {
|
||||
const existing = await db.query<{ id: number }[]>(
|
||||
"SELECT id FROM users WHERE username = ?",
|
||||
[sup.username]
|
||||
);
|
||||
if (existing.length === 0) {
|
||||
await db.execute(
|
||||
"INSERT INTO users (username, name, email, password, role, department_id, is_active) VALUES (?, ?, ?, ?, ?, ?, ?)",
|
||||
[sup.username, sup.name, sup.email, supervisorPassword, "Supervisor", sup.deptId, true]
|
||||
);
|
||||
console.log(` ✅ ${sup.name} created`);
|
||||
} else {
|
||||
console.log(` ℹ️ ${sup.name} already exists`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 5. Seed Sample Contractors
|
||||
console.log("🏗️ Seeding sample contractors...");
|
||||
const contractorPassword = await hash("contractor123", config.BCRYPT_ROUNDS);
|
||||
|
||||
const contractors = [
|
||||
{ username: "contractor1", name: "Contractor One", email: "contractor1@workallocate.com", deptId: groundnutId },
|
||||
{ username: "contractor2", name: "Contractor Two", email: "contractor2@workallocate.com", deptId: groundnutId }
|
||||
];
|
||||
|
||||
for (const con of contractors) {
|
||||
const existing = await db.query<{ id: number }[]>(
|
||||
"SELECT id FROM users WHERE username = ?",
|
||||
[con.username]
|
||||
);
|
||||
if (existing.length === 0) {
|
||||
await db.execute(
|
||||
"INSERT INTO users (username, name, email, password, role, department_id, is_active) VALUES (?, ?, ?, ?, ?, ?, ?)",
|
||||
[con.username, con.name, con.email, contractorPassword, "Contractor", con.deptId, true]
|
||||
);
|
||||
console.log(` ✅ ${con.name} created`);
|
||||
} else {
|
||||
console.log(` ℹ️ ${con.name} already exists`);
|
||||
}
|
||||
}
|
||||
|
||||
// 6. Seed Sample Employees
|
||||
console.log("👷 Seeding sample employees...");
|
||||
const contractor1 = await db.query<{ id: number }[]>(
|
||||
"SELECT id FROM users WHERE username = ?",
|
||||
["contractor1"]
|
||||
);
|
||||
const employeePassword = await hash("employee123", config.BCRYPT_ROUNDS);
|
||||
|
||||
if (contractor1.length > 0) {
|
||||
const employees = [
|
||||
{ username: "employee1", name: "Employee One", email: "employee1@workallocate.com" },
|
||||
{ username: "employee2", name: "Employee Two", email: "employee2@workallocate.com" },
|
||||
{ username: "employee3", name: "Employee Three", email: "employee3@workallocate.com" }
|
||||
];
|
||||
|
||||
for (const emp of employees) {
|
||||
const existing = await db.query<{ id: number }[]>(
|
||||
"SELECT id FROM users WHERE username = ?",
|
||||
[emp.username]
|
||||
);
|
||||
if (existing.length === 0) {
|
||||
await db.execute(
|
||||
"INSERT INTO users (username, name, email, password, role, department_id, contractor_id, is_active) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
[emp.username, emp.name, emp.email, employeePassword, "Employee", groundnutId, contractor1[0].id, true]
|
||||
);
|
||||
console.log(` ✅ ${emp.name} created`);
|
||||
} else {
|
||||
console.log(` ℹ️ ${emp.name} already exists`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 7. Seed Contractor Rates
|
||||
console.log("💰 Seeding contractor rates...");
|
||||
if (contractor1.length > 0) {
|
||||
const existingRate = await db.query<{ id: number }[]>(
|
||||
"SELECT id FROM contractor_rates WHERE contractor_id = ?",
|
||||
[contractor1[0].id]
|
||||
);
|
||||
|
||||
if (existingRate.length === 0) {
|
||||
const today = new Date().toISOString().split("T")[0];
|
||||
await db.execute(
|
||||
"INSERT INTO contractor_rates (contractor_id, rate, effective_date) VALUES (?, ?, ?)",
|
||||
[contractor1[0].id, 500.00, today]
|
||||
);
|
||||
console.log(" ✅ Contractor rates created");
|
||||
} else {
|
||||
console.log(" ℹ️ Contractor rates already exist");
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`
|
||||
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
||||
✅ Database seeding completed successfully!
|
||||
|
||||
🔑 Default Login Credentials:
|
||||
|
||||
SuperAdmin:
|
||||
Username: admin
|
||||
Password: admin123
|
||||
|
||||
Supervisor (Groundnut):
|
||||
Username: supervisor_groundnut
|
||||
Password: supervisor123
|
||||
|
||||
Contractor:
|
||||
Username: contractor1
|
||||
Password: contractor123
|
||||
|
||||
Employee:
|
||||
Username: employee1
|
||||
Password: employee123
|
||||
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
||||
`);
|
||||
|
||||
} catch (error) {
|
||||
console.error("❌ Error seeding database:", (error as Error).message);
|
||||
Deno.exit(1);
|
||||
} finally {
|
||||
await db.close();
|
||||
}
|
||||
}
|
||||
|
||||
await seedDatabase();
|
||||
Reference in New Issue
Block a user