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 machines = sequelize.define( 'machines', { id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true, }, asset_tag: { type: DataTypes.TEXT, }, name: { type: DataTypes.TEXT, }, status: { type: DataTypes.ENUM, values: [ "available", "running", "down", "maintenance", "retired" ], }, manufacturer: { type: DataTypes.TEXT, }, model: { type: DataTypes.TEXT, }, serial_number: { type: DataTypes.TEXT, }, commissioned_on: { type: DataTypes.DATE, }, notes: { type: DataTypes.TEXT, }, importHash: { type: DataTypes.STRING(255), allowNull: true, unique: true, }, }, { timestamps: true, paranoid: true, freezeTableName: true, }, ); machines.associate = (db) => { /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity db.machines.hasMany(db.maintenance_work_orders, { as: 'maintenance_work_orders_machine', foreignKey: { name: 'machineId', }, constraints: false, }); db.machines.hasMany(db.production_operations, { as: 'production_operations_machine', foreignKey: { name: 'machineId', }, constraints: false, }); //end loop db.machines.belongsTo(db.work_centers, { as: 'work_center', foreignKey: { name: 'work_centerId', }, constraints: false, }); db.machines.hasMany(db.file, { as: 'manuals', foreignKey: 'belongsToId', constraints: false, scope: { belongsTo: db.machines.getTableName(), belongsToColumn: 'manuals', }, }); db.machines.hasMany(db.file, { as: 'photos', foreignKey: 'belongsToId', constraints: false, scope: { belongsTo: db.machines.getTableName(), belongsToColumn: 'photos', }, }); db.machines.belongsTo(db.users, { as: 'createdBy', }); db.machines.belongsTo(db.users, { as: 'updatedBy', }); }; return machines; };