import Sequelize, {Op} from "sequelize"; import {validateFindOptions, validateMutation} from "@vostro/c2-utils/lib/auth"; import { getDatabase } from "@vostro/c2-utils/lib/database"; import { C2Utils } from '@vostro/c2-utils/lib/types'; export default { name: "Role", comment: "This is a role table.", comments: { fields: { name: "This is the name of the role that will be displayed.", user: "role hasMany user", rolePermission: "role permission hasMany permission", roleId: "target for user and role permission", }, }, define: { name: { type: Sequelize.STRING, allowNull: false, comment: "This is the name of the role that will be displayed.", }, }, override: {}, relationships: [{ type: "hasMany", model: "User", name: "users", options: { foreignKey: "roleId", }, }, { type: "hasMany", model: "RolePermission", name: "permissions", options: { as: "permissions", sourceKey: "id", foreignKey: "roleId", }, }], options: { tableName: "roles", hooks: { beforeFind: [async function beforeFind(options: C2Utils.FindOptions) { return validateFindOptions("Role", options, "id", async(user) => { // const db = await getDatabase(); return { [Op.eq]: user.roleId, }; }, false); }], beforeCreate: [async function beforeCreate(instance: any, options: C2Utils.FindOptions) { return validateMutation("Role", "create", options, instance, undefined, true); }], beforeUpdate: [async function beforeUpdate(instance: any, options: C2Utils.FindOptions) { return validateMutation("Role", "update", options, instance, undefined, true); }], beforeDestroy: [async function beforeDestroy(instance: any, options: C2Utils.FindOptions) { return validateMutation("Role", "destroy", options, instance, undefined, true); }], }, }, };