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 floors = sequelize.define( 'floors', { id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true, }, floor_num: { type: DataTypes.INTEGER, }, is_mezzanine: { type: DataTypes.BOOLEAN, allowNull: false, defaultValue: false, }, floor_plan_height: { type: DataTypes.DECIMAL, }, floor_plan_width: { type: DataTypes.DECIMAL, }, importHash: { type: DataTypes.STRING(255), allowNull: true, unique: true, }, }, { timestamps: true, paranoid: true, freezeTableName: true, }, ); floors.associate = (db) => { db.floors.belongsToMany(db.floor_layouts, { as: 'floor_layouts', foreignKey: { name: 'floors_floor_layoutsId', }, constraints: false, through: 'floorsFloor_layoutsFloor_layouts', }); db.floors.belongsToMany(db.floor_layouts, { as: 'floor_layouts_filter', foreignKey: { name: 'floors_floor_layoutsId', }, constraints: false, through: 'floorsFloor_layoutsFloor_layouts', }); /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity db.floors.hasMany(db.floor_layouts, { as: 'floor_layouts_floor', foreignKey: { name: 'floorId', }, constraints: false, }); //end loop db.floors.belongsTo(db.buildings, { as: 'building', foreignKey: { name: 'buildingId', }, constraints: false, }); db.floors.belongsTo(db.companies, { as: 'companies', foreignKey: { name: 'companiesId', }, constraints: false, }); db.floors.hasMany(db.file, { as: 'floor_plan_image', foreignKey: 'belongsToId', constraints: false, scope: { belongsTo: db.floors.getTableName(), belongsToColumn: 'floor_plan_image', }, }); db.floors.hasMany(db.file, { as: 'published_image', foreignKey: 'belongsToId', constraints: false, scope: { belongsTo: db.floors.getTableName(), belongsToColumn: 'published_image', }, }); db.floors.belongsTo(db.users, { as: 'createdBy', }); db.floors.belongsTo(db.users, { as: 'updatedBy', }); }; return floors; };