55 lines
1.8 KiB
JavaScript
55 lines
1.8 KiB
JavaScript
const { v4: uuid } = require("uuid");
|
|
|
|
module.exports = {
|
|
async up(queryInterface) {
|
|
const createdAt = new Date();
|
|
const updatedAt = new Date();
|
|
|
|
// Get role IDs
|
|
const roles = await queryInterface.sequelize.query(
|
|
`SELECT id, name FROM roles`,
|
|
{ type: queryInterface.sequelize.QueryTypes.SELECT }
|
|
);
|
|
|
|
const getRoleId = (name) => roles.find(r => r.name === name)?.id;
|
|
|
|
// Create permissions for REVIEWS
|
|
const permissions = [
|
|
{ id: uuid(), name: 'CREATE_REVIEWS', createdAt, updatedAt },
|
|
{ id: uuid(), name: 'READ_REVIEWS', createdAt, updatedAt },
|
|
{ id: uuid(), name: 'UPDATE_REVIEWS', createdAt, updatedAt },
|
|
{ id: uuid(), name: 'DELETE_REVIEWS', createdAt, updatedAt },
|
|
];
|
|
|
|
await queryInterface.bulkInsert('permissions', permissions);
|
|
|
|
const getPermissionId = (name) => permissions.find(p => p.name === name)?.id;
|
|
|
|
const rolePermissions = [];
|
|
|
|
const adminId = getRoleId('Administrator');
|
|
if (adminId) {
|
|
permissions.forEach(p => {
|
|
rolePermissions.push({ createdAt, updatedAt, roles_permissionsId: adminId, permissionId: p.id });
|
|
});
|
|
}
|
|
|
|
const customerId = getRoleId('Customer');
|
|
if (customerId) {
|
|
rolePermissions.push({ createdAt, updatedAt, roles_permissionsId: customerId, permissionId: getPermissionId('CREATE_REVIEWS') });
|
|
rolePermissions.push({ createdAt, updatedAt, roles_permissionsId: customerId, permissionId: getPermissionId('READ_REVIEWS') });
|
|
}
|
|
|
|
const publicId = getRoleId('Public');
|
|
if (publicId) {
|
|
rolePermissions.push({ createdAt, updatedAt, roles_permissionsId: publicId, permissionId: getPermissionId('READ_REVIEWS') });
|
|
}
|
|
|
|
await queryInterface.bulkInsert('rolesPermissionsPermissions', rolePermissions);
|
|
},
|
|
|
|
async down(queryInterface) {
|
|
// Reverse logic if needed
|
|
}
|
|
};
|