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 inspections = sequelize.define( 'inspections', { id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true, }, inspection_number: { type: DataTypes.TEXT, }, inspected_date: { type: DataTypes.DATE, }, result: { type: DataTypes.ENUM, values: [ "pass", "fail", "rework" ], }, defects_found: { type: DataTypes.TEXT, }, severity: { type: DataTypes.ENUM, values: [ "minor", "major", "critical" ], }, importHash: { type: DataTypes.STRING(255), allowNull: true, unique: true, }, }, { timestamps: true, paranoid: true, freezeTableName: true, }, ); inspections.associate = (db) => { /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity //end loop db.inspections.belongsTo(db.work_orders, { as: 'work_order', foreignKey: { name: 'work_orderId', }, constraints: false, }); db.inspections.belongsTo(db.users, { as: 'inspector', foreignKey: { name: 'inspectorId', }, constraints: false, }); db.inspections.hasMany(db.file, { as: 'attachments', foreignKey: 'belongsToId', constraints: false, scope: { belongsTo: db.inspections.getTableName(), belongsToColumn: 'attachments', }, }); db.inspections.belongsTo(db.users, { as: 'createdBy', }); db.inspections.belongsTo(db.users, { as: 'updatedBy', }); }; return inspections; };