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 contacts = sequelize.define( 'contacts', { id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true, }, first_name: { type: DataTypes.TEXT, }, last_name: { type: DataTypes.TEXT, }, email: { type: DataTypes.TEXT, }, importHash: { type: DataTypes.STRING(255), allowNull: true, unique: true, }, }, { timestamps: true, paranoid: true, freezeTableName: true, }, ); contacts.associate = (db) => { db.contacts.belongsToMany(db.skills, { as: 'skills', foreignKey: { name: 'contacts_skillsId', }, constraints: false, through: 'contactsSkillsSkills', }); db.contacts.belongsToMany(db.skills, { as: 'skills_filter', foreignKey: { name: 'contacts_skillsId', }, constraints: false, through: 'contactsSkillsSkills', }); /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity db.contacts.hasMany(db.leads, { as: 'leads_converted_to_contact', foreignKey: { name: 'converted_to_contactId', }, constraints: false, }); //end loop db.contacts.belongsTo(db.accounts, { as: 'account', foreignKey: { name: 'accountId', }, constraints: false, }); db.contacts.belongsTo(db.organisations, { as: 'organisations', foreignKey: { name: 'organisationsId', }, constraints: false, }); db.contacts.belongsTo(db.users, { as: 'createdBy', }); db.contacts.belongsTo(db.users, { as: 'updatedBy', }); }; return contacts; };