import Sequelize, {Op} from "sequelize"; import jsonType from "@vostro/graphql-types/lib/json"; import {validateFindOptions, validateMutation} from "@vostro/c2-utils/lib/auth"; import {GraphQLString, GraphQLInputObjectType} from "graphql"; import { C2Utils } from '@vostro/c2-utils/lib/types'; import {fromGlobalId} from "graphql-relay"; /** * This is a Event Log Model * @classdesc Event Log Model * @name EventLogModel * @class * @memberof Core * @property {string} event * @property {string} data * @property {UserModel} user */ export default { name: "EventLog", define: { action: {type: Sequelize.STRING, allowNull: false}, description: {type: Sequelize.STRING}, changeset: {type: Sequelize.JSONB}, source: {type: Sequelize.STRING}, model: {type: Sequelize.STRING}, }, relationships: [ {type: "belongsTo", model: "User", name: "user", options: {foreignKey: "userId", target: "id"}}, ], override: { changeset: { type: { name: "EventLogData", fields: { previous: {type: jsonType}, current: {type: jsonType}, }, }, output(result: any) { return result.changeset; }, input(_field: any) { throw new Error("Unable to query"); }, }, }, options: { tableName: "event-logs", classMethods: {}, instanceMethods: {}, hooks: { beforeFind: [async function beforeFind(options: C2Utils.FindOptions) { return validateFindOptions("EventLog", options, undefined, undefined, true); }], beforeCreate: [async function beforeCreate(instance: any, options: C2Utils.FindOptions) { return validateMutation("EventLog", "create", options, instance, undefined, true); }], beforeUpdate: [async function beforeUpdate(instance: any, options: C2Utils.FindOptions) { return validateMutation("EventLog", "update", options, instance, undefined, true); }], beforeDestroy: [async function beforeDestroy(instance: any, options: C2Utils.FindOptions) { return validateMutation("EventLog", "destroy", options, instance, undefined, true); }], }, }, whereOperatorTypes: { changesetFind: new GraphQLInputObjectType({ name: "EventLogWhereOperator", fields: { model: { type: GraphQLString, }, value: { type: GraphQLString, }, }, }), descriptionFind: new GraphQLInputObjectType({ name: "DescriptionFindWhereOperator", fields: { model: { type: GraphQLString, }, value: { type: GraphQLString, }, }, }), }, whereOperators: { async changesetFind(args: any) : Promise { const {changesetFind} = args; const {model, value} = changesetFind; const userId = fromGlobalId(value).id; return { [Op.and]: { [Op.or]: [ { changeset: { current: { userId: { [Op.eq]: userId, }, }, }, }, { [Op.and]: [ { model: { [Op.eq]: model, }, changeset: { current: { id: { [Op.eq]: userId, }, }, }, }, ], }, ], }, }; }, async descriptionFind(args: any): Promise { const data = args?.descriptionFind; const {model, value} = data || {}; return { model: model, description: { [Op.iLike]: `%${value}%`, }, }; }, }, };