first commit
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* 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/documentType.seeder.js
|
||||
|
||||
const db = require("../models");
|
||||
|
||||
const seedDocumentTypes = async () => {
|
||||
try {
|
||||
const types = [
|
||||
{ doc_type_name: "DOC_TYPE_HERE_01", description: "N/A" },
|
||||
{ doc_type_name: "DOC_TYPE_HERE_02", description: "N/A" },
|
||||
];
|
||||
|
||||
const existingDocuments = await db.DocumentType.findAll({
|
||||
attributes: ["doc_type_name"],
|
||||
});
|
||||
|
||||
const existingNames = existingDocuments.map((doc) => doc.doc_type_name);
|
||||
|
||||
const newDocuments = types.filter(
|
||||
(dt) => !existingNames.includes(dt.doc_type_name)
|
||||
);
|
||||
|
||||
if (newDocuments.length > 0) {
|
||||
await db.DocumentType.bulkCreate(newDocuments);
|
||||
|
||||
newDocuments.forEach((dt) =>
|
||||
console.log(`Document type inserted: ${dt.doc_type_name}`)
|
||||
);
|
||||
} else {
|
||||
console.log("No new document types to insert");
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Document type seeding failed:", error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = seedDocumentTypes;
|
||||
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* 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/index.js
|
||||
|
||||
const db = require("../models");
|
||||
|
||||
// Import all seeders
|
||||
const seedUsers = require("./user.seeder");
|
||||
const seedDocumentTypes = require("./documentType.seeder");
|
||||
const {log} = require("../utils/consoleLog.utill");
|
||||
|
||||
|
||||
const runSeeders = async () => {
|
||||
try {
|
||||
log("Starting database seeding...");
|
||||
|
||||
await seedUsers();
|
||||
await seedDocumentTypes();
|
||||
log("All seeders executed successfully");
|
||||
} catch (error) {
|
||||
log("Seeding process failed:", error.message);
|
||||
} finally {
|
||||
await db.sequelize.close();
|
||||
process.exit();
|
||||
}
|
||||
};
|
||||
|
||||
runSeeders();
|
||||
@@ -0,0 +1,106 @@
|
||||
/**
|
||||
* 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;
|
||||
Reference in New Issue
Block a user