114 lines
2.2 KiB
JavaScript
114 lines
2.2 KiB
JavaScript
const config = require('../../config');
|
|
const providers = config.providers;
|
|
const crypto = require('crypto');
|
|
const bcrypt = require('bcrypt');
|
|
const moment = require('moment');
|
|
|
|
module.exports = function (sequelize, DataTypes) {
|
|
const events = sequelize.define(
|
|
'events',
|
|
{
|
|
id: {
|
|
type: DataTypes.UUID,
|
|
defaultValue: DataTypes.UUIDV4,
|
|
primaryKey: true,
|
|
},
|
|
|
|
event_type: {
|
|
type: DataTypes.ENUM,
|
|
|
|
values: ['Incidente', 'Cuasi-incidente', 'ObservacióndeSeguridad'],
|
|
},
|
|
|
|
description: {
|
|
type: DataTypes.TEXT,
|
|
},
|
|
|
|
event_date: {
|
|
type: DataTypes.DATE,
|
|
},
|
|
|
|
status: {
|
|
type: DataTypes.ENUM,
|
|
|
|
values: ['Abierto', 'EnInvestigación', 'Cerrado'],
|
|
},
|
|
|
|
risk_level: {
|
|
type: DataTypes.ENUM,
|
|
|
|
values: ['Bajo', 'Medio', 'Alto'],
|
|
},
|
|
|
|
importHash: {
|
|
type: DataTypes.STRING(255),
|
|
allowNull: true,
|
|
unique: true,
|
|
},
|
|
},
|
|
{
|
|
timestamps: true,
|
|
paranoid: true,
|
|
freezeTableName: true,
|
|
},
|
|
);
|
|
|
|
events.associate = (db) => {
|
|
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
|
|
|
db.events.hasMany(db.actions, {
|
|
as: 'actions_event',
|
|
foreignKey: {
|
|
name: 'eventId',
|
|
},
|
|
constraints: false,
|
|
});
|
|
|
|
//end loop
|
|
|
|
db.events.belongsTo(db.users, {
|
|
as: 'reported_by',
|
|
foreignKey: {
|
|
name: 'reported_byId',
|
|
},
|
|
constraints: false,
|
|
});
|
|
|
|
db.events.belongsTo(db.branches, {
|
|
as: 'branch',
|
|
foreignKey: {
|
|
name: 'branchId',
|
|
},
|
|
constraints: false,
|
|
});
|
|
|
|
db.events.belongsTo(db.organizations, {
|
|
as: 'organizations',
|
|
foreignKey: {
|
|
name: 'organizationsId',
|
|
},
|
|
constraints: false,
|
|
});
|
|
|
|
db.events.hasMany(db.file, {
|
|
as: 'photos',
|
|
foreignKey: 'belongsToId',
|
|
constraints: false,
|
|
scope: {
|
|
belongsTo: db.events.getTableName(),
|
|
belongsToColumn: 'photos',
|
|
},
|
|
});
|
|
|
|
db.events.belongsTo(db.users, {
|
|
as: 'createdBy',
|
|
});
|
|
|
|
db.events.belongsTo(db.users, {
|
|
as: 'updatedBy',
|
|
});
|
|
};
|
|
|
|
return events;
|
|
};
|