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 chat_threads = sequelize.define( 'chat_threads', { id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true, }, subject: { type: DataTypes.TEXT, }, thread_type: { type: DataTypes.ENUM, values: [ "customer_coordinator", "internal_team", "partner_customer", "system" ], }, last_message_at: { type: DataTypes.DATE, }, status: { type: DataTypes.ENUM, values: [ "open", "closed", "archived" ], }, importHash: { type: DataTypes.STRING(255), allowNull: true, unique: true, }, }, { timestamps: true, paranoid: true, freezeTableName: true, }, ); chat_threads.associate = (db) => { /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity db.chat_threads.hasMany(db.chat_messages, { as: 'chat_messages_chat_thread', foreignKey: { name: 'chat_threadId', }, constraints: false, }); //end loop db.chat_threads.belongsTo(db.organizations, { as: 'organization', foreignKey: { name: 'organizationId', }, constraints: false, }); db.chat_threads.belongsTo(db.service_requests, { as: 'service_request', foreignKey: { name: 'service_requestId', }, constraints: false, }); db.chat_threads.belongsTo(db.users, { as: 'createdBy', }); db.chat_threads.belongsTo(db.users, { as: 'updatedBy', }); }; return chat_threads; };