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 prestadores = sequelize.define( 'prestadores', { id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true, }, descricao_perfil: { type: DataTypes.TEXT, }, cidade: { type: DataTypes.TEXT, }, estado: { type: DataTypes.TEXT, }, avaliacao_media: { type: DataTypes.DECIMAL, }, perfil_completo: { type: DataTypes.BOOLEAN, allowNull: false, defaultValue: false, }, importHash: { type: DataTypes.STRING(255), allowNull: true, unique: true, }, }, { timestamps: true, paranoid: true, freezeTableName: true, }, ); prestadores.associate = (db) => { db.prestadores.belongsToMany(db.categorias, { as: 'categorias', foreignKey: { name: 'prestadores_categoriasId', }, constraints: false, through: 'prestadoresCategoriasCategorias', }); db.prestadores.belongsToMany(db.categorias, { as: 'categorias_filter', foreignKey: { name: 'prestadores_categoriasId', }, constraints: false, through: 'prestadoresCategoriasCategorias', }); /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity db.prestadores.hasMany(db.propostas, { as: 'propostas_prestador', foreignKey: { name: 'prestadorId', }, constraints: false, }); db.prestadores.hasMany(db.avaliacoes, { as: 'avaliacoes_prestador', foreignKey: { name: 'prestadorId', }, constraints: false, }); //end loop db.prestadores.belongsTo(db.users, { as: 'usuario', foreignKey: { name: 'usuarioId', }, constraints: false, }); db.prestadores.belongsTo(db.users, { as: 'createdBy', }); db.prestadores.belongsTo(db.users, { as: 'updatedBy', }); }; return prestadores; };