2026-02-05 07:06:31 +00:00

172 lines
2.2 KiB
JavaScript

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 chats = sequelize.define(
'chats',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
chat_type: {
type: DataTypes.ENUM,
values: [
"direct",
"community"
],
},
last_message_at: {
type: DataTypes.DATE,
},
chat_status: {
type: DataTypes.ENUM,
values: [
"active",
"archived",
"closed"
],
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
chats.associate = (db) => {
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
db.chats.hasMany(db.matches, {
as: 'matches_chat',
foreignKey: {
name: 'chatId',
},
constraints: false,
});
db.chats.hasMany(db.chat_participants, {
as: 'chat_participants_chat',
foreignKey: {
name: 'chatId',
},
constraints: false,
});
db.chats.hasMany(db.messages, {
as: 'messages_chat',
foreignKey: {
name: 'chatId',
},
constraints: false,
});
db.chats.hasMany(db.money_transfers, {
as: 'money_transfers_chat',
foreignKey: {
name: 'chatId',
},
constraints: false,
});
//end loop
db.chats.belongsTo(db.matches, {
as: 'direct_match',
foreignKey: {
name: 'direct_matchId',
},
constraints: false,
});
db.chats.belongsTo(db.communities, {
as: 'community',
foreignKey: {
name: 'communityId',
},
constraints: false,
});
db.chats.belongsTo(db.users, {
as: 'createdBy',
});
db.chats.belongsTo(db.users, {
as: 'updatedBy',
});
};
return chats;
};