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 contests = sequelize.define( 'contests', { id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true, }, title: { type: DataTypes.TEXT, }, description: { type: DataTypes.TEXT, }, type: { type: DataTypes.ENUM, values: [ 'direct', 'direct_internal', 'direct_external', 'internal_external', 'internal', 'external', ], }, places_non_handicapped: { type: DataTypes.INTEGER, }, places_handicapped: { type: DataTypes.INTEGER, }, end_date: { type: DataTypes.DATE, }, importHash: { type: DataTypes.STRING(255), allowNull: true, unique: true, }, }, { timestamps: true, paranoid: true, freezeTableName: true, }, ); contests.associate = (db) => { db.contests.belongsToMany(db.corps, { as: 'corps', foreignKey: { name: 'contests_corpsId', }, constraints: false, through: 'contestsCorpsCorps', }); db.contests.belongsToMany(db.corps, { as: 'corps_filter', foreignKey: { name: 'contests_corpsId', }, constraints: false, through: 'contestsCorpsCorps', }); /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity db.contests.hasMany(db.applications, { as: 'applications_contest', foreignKey: { name: 'contestId', }, constraints: false, }); //end loop db.contests.belongsTo(db.ministries, { as: 'ministry', foreignKey: { name: 'ministryId', }, constraints: false, }); db.contests.belongsTo(db.users, { as: 'createdBy', }); db.contests.belongsTo(db.users, { as: 'updatedBy', }); }; return contests; };