40014-vm/backend/src/db/models/conversations.js
2026-05-15 19:51:06 +00:00

156 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 conversations = sequelize.define(
'conversations',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
title: {
type: DataTypes.TEXT,
},
summary: {
type: DataTypes.TEXT,
},
status: {
type: DataTypes.ENUM,
values: [
"active",
"archived",
"deleted"
],
},
is_pinned: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
last_message_at: {
type: DataTypes.DATE,
},
client_context_json: {
type: DataTypes.TEXT,
},
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,
});
db.conversations.hasMany(db.usage_events, {
as: 'usage_events_conversation',
foreignKey: {
name: 'conversationId',
},
constraints: false,
});
//end loop
db.conversations.belongsTo(db.users, {
as: 'user',
foreignKey: {
name: 'userId',
},
constraints: false,
});
db.conversations.belongsTo(db.agents, {
as: 'agent',
foreignKey: {
name: 'agentId',
},
constraints: false,
});
db.conversations.belongsTo(db.users, {
as: 'createdBy',
});
db.conversations.belongsTo(db.users, {
as: 'updatedBy',
});
};
return conversations;
};