107 lines
2.5 KiB
JavaScript
107 lines
2.5 KiB
JavaScript
/**
|
|
* Copyright (c) 2026 Niolla
|
|
* All rights reserved.
|
|
*
|
|
* This source code is proprietary and confidential.
|
|
* Unauthorized copying, modification, distribution, or use
|
|
* of this file, via any medium, is strictly prohibited.
|
|
*/
|
|
|
|
// app/seeders/user.seeder.js
|
|
|
|
const bcrypt = require("bcrypt");
|
|
const { v4: uuidv4 } = require("uuid");
|
|
|
|
const db = require("../models");
|
|
|
|
const seedUsers = async () => {
|
|
const transaction = await db.sequelize.transaction();
|
|
|
|
try {
|
|
const users = [
|
|
{
|
|
firstName: "System",
|
|
lastName: "Administrator",
|
|
email: "team@niolla.lk",
|
|
password: "Admin@123",
|
|
accountType: "admin",
|
|
role: "Administrator",
|
|
roleID: "admin",
|
|
department: "Administration",
|
|
profile: {
|
|
theme: "light",
|
|
notificationsEnabled: true,
|
|
dob: null,
|
|
phone_number: null,
|
|
},
|
|
},
|
|
];
|
|
|
|
for (const userData of users) {
|
|
const existingUser = await db.User.findOne({
|
|
where: {
|
|
email: userData.email,
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
if (existingUser) {
|
|
console.log(`User already exists: ${userData.email}`);
|
|
continue;
|
|
}
|
|
|
|
const hashedPassword = await bcrypt.hash(userData.password, 12);
|
|
|
|
const userId = `usr_${uuidv4()}`;
|
|
const profileId = `prf_${uuidv4()}`;
|
|
|
|
const newUser = await db.User.create(
|
|
{
|
|
id: userId,
|
|
firstName: userData.firstName,
|
|
lastName: userData.lastName,
|
|
email: userData.email,
|
|
password: hashedPassword,
|
|
accountType: userData.accountType,
|
|
role: userData.role,
|
|
roleID: userData.roleID,
|
|
department: userData.department,
|
|
},
|
|
{
|
|
transaction,
|
|
},
|
|
);
|
|
|
|
await db.Profile.create(
|
|
{
|
|
profile_id: profileId,
|
|
user_id: newUser.id,
|
|
theme: userData.profile.theme,
|
|
notificationsEnabled: userData.profile.notificationsEnabled,
|
|
dob: userData.profile.dob,
|
|
phone_number: userData.profile.phone_number,
|
|
},
|
|
{
|
|
transaction,
|
|
},
|
|
);
|
|
|
|
console.log(
|
|
`User inserted: ${newUser.firstName} ${newUser.lastName} (${newUser.email})`,
|
|
);
|
|
}
|
|
|
|
await transaction.commit();
|
|
|
|
console.log("User seeding completed successfully");
|
|
} catch (error) {
|
|
await transaction.rollback();
|
|
|
|
console.error("User seeding failed:", error);
|
|
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
module.exports = seedUsers;
|