File: //home/arjun/projects/good-life-be/seeders/adminSeeder.cjs
const { v4: uuidv4 } = require('uuid');
const bcrypt = require('bcrypt');
module.exports = {
async up(queryInterface) {
// Default admin user data
const adminUser = {
id: uuidv4(),
first_name: 'Admin',
second_name: 'User',
role: 'admin',
is_deleted: false,
is_verified: true,
verification_token: null,
createdAt: new Date(),
updatedAt: new Date(),
};
const adminLogin = {
id: uuidv4(),
email: 'admin@spericorn.com', // Default admin email
password: await bcrypt.hash('Admin@123', 10), // Default admin password (hashed)
user_id: adminUser.id,
createdAt: new Date(),
updatedAt: new Date(),
};
// Check if an admin user already exists
const existingAdmin = await queryInterface.sequelize.query(
'SELECT * FROM users WHERE role = :role AND is_deleted = false',
{
replacements: { role: 'admin' },
type: queryInterface.sequelize.QueryTypes.SELECT,
}
);
if (existingAdmin.length === 0) {
// Insert the admin user and login data
await queryInterface.bulkInsert('users', [adminUser]);
await queryInterface.bulkInsert('logins', [adminLogin]);
console.log('Admin user and login created successfully.');
} else {
console.log('Admin user already exists. Seeder skipped.');
}
},
async down(queryInterface) {
// Rollback logic to delete the admin user and login
await queryInterface.bulkDelete('logins', { email: 'admin@example.com' });
await queryInterface.bulkDelete('users', { role: 'admin' });
},
};