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 conversations = sequelize.define( 'conversations', { id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true, }, title: { type: DataTypes.TEXT, }, status: { type: DataTypes.ENUM, values: [ "open", "closed", "archived" ], }, started_at: { type: DataTypes.DATE, }, last_message_at: { type: DataTypes.DATE, }, importHash: { type: DataTypes.STRING(255), allowNull: true, unique: true, }, }, { timestamps: true, paranoid: true, freezeTableName: true, }, ); conversations.associate = (db) => { /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity db.conversations.hasMany(db.messages, { as: 'messages_conversation', foreignKey: { name: 'conversationId', }, constraints: false, }); //end loop db.conversations.belongsTo(db.projects, { as: 'project', foreignKey: { name: 'projectId', }, constraints: false, }); db.conversations.belongsTo(db.users, { as: 'started_by', foreignKey: { name: 'started_byId', }, constraints: false, }); db.conversations.belongsTo(db.users, { as: 'createdBy', }); db.conversations.belongsTo(db.users, { as: 'updatedBy', }); }; return conversations; };