93 lines
2.3 KiB
JavaScript
93 lines
2.3 KiB
JavaScript
/* eslint-disable no-console */
|
|
const { PrismaClient } = require('@prisma/client');
|
|
const bcrypt = require('bcrypt');
|
|
|
|
const prisma = new PrismaClient();
|
|
|
|
const ensureModulePermissions = async (roleId, { code, name, sortOrder, actions }) => {
|
|
const mod = await prisma.modules.upsert({
|
|
where: { code },
|
|
update: { name, sort_order: sortOrder, is_active: true },
|
|
create: { code, name, sort_order: sortOrder, is_active: true },
|
|
});
|
|
|
|
for (const action of actions) {
|
|
const permission = await prisma.permissions.upsert({
|
|
where: { module_id_action: { module_id: mod.id, action } },
|
|
update: { is_active: true },
|
|
create: { module_id: mod.id, action, is_active: true },
|
|
});
|
|
|
|
await prisma.role_permissions.upsert({
|
|
where: {
|
|
role_id_permission_id: { role_id: roleId, permission_id: permission.id },
|
|
},
|
|
update: {},
|
|
create: { role_id: roleId, permission_id: permission.id },
|
|
});
|
|
}
|
|
};
|
|
|
|
async function main() {
|
|
const superAdminRole = await prisma.roles.findFirst({
|
|
where: { name: 'Super Admin', deleted_at: null },
|
|
});
|
|
|
|
if (!superAdminRole) {
|
|
throw new Error('Super Admin role not found. Run erp_phase1_ddl.sql first.');
|
|
}
|
|
|
|
await ensureModulePermissions(superAdminRole.id, {
|
|
code: 'REPORTS',
|
|
name: 'Reports',
|
|
sortOrder: 90,
|
|
actions: ['view', 'export'],
|
|
});
|
|
|
|
const passwordHash = await bcrypt.hash('Admin@123', 12);
|
|
|
|
const user = await prisma.users.upsert({
|
|
where: { email: 'admin@bharaterp.com' },
|
|
update: {
|
|
status: 'active',
|
|
is_active: true,
|
|
deleted_at: null,
|
|
},
|
|
create: {
|
|
employee_code: 'EMP001',
|
|
full_name: 'Super Admin',
|
|
email: 'admin@bharaterp.com',
|
|
password_hash: passwordHash,
|
|
status: 'active',
|
|
is_active: true,
|
|
},
|
|
});
|
|
|
|
await prisma.user_roles.upsert({
|
|
where: {
|
|
user_id_role_id: {
|
|
user_id: user.id,
|
|
role_id: superAdminRole.id,
|
|
},
|
|
},
|
|
update: {},
|
|
create: {
|
|
user_id: user.id,
|
|
role_id: superAdminRole.id,
|
|
},
|
|
});
|
|
|
|
console.log('Bootstrap Super Admin ready: admin@bharaterp.com / Admin@123');
|
|
}
|
|
|
|
main()
|
|
.then(async () => {
|
|
await prisma.$disconnect();
|
|
process.exit(0);
|
|
})
|
|
.catch(async (err) => {
|
|
console.error('Seed failed', err);
|
|
await prisma.$disconnect();
|
|
process.exit(1);
|
|
});
|