evolution-api/src/whatsapp/models/message.model.ts
jaison-x 07e8449379 fix: when deleting a message in whatsapp, delete the message in chatwoot too
The message model schema was changed. Old format in message model was field chatwootMessageId. Now we have a document chatwoot with new properties.

I cant find a simple way to create a migration function up then the old field was no migrate to new format.
2023-12-20 17:53:37 -03:00

89 lines
2.5 KiB
TypeScript

import { Schema } from 'mongoose';
import { dbserver } from '../../libs/db.connect';
import { wa } from '../types/wa.types';
class Key {
id?: string;
remoteJid?: string;
fromMe?: boolean;
participant?: string;
}
class ChatwootMessage {
messageId?: number;
inboxId?: number;
conversationId?: number;
}
export class MessageRaw {
_id?: string;
key?: Key;
pushName?: string;
participant?: string;
message?: object;
messageType?: string;
messageTimestamp?: number | Long.Long;
owner: string;
source?: 'android' | 'web' | 'ios';
source_id?: string;
source_reply_id?: string;
chatwoot?: ChatwootMessage;
}
const messageSchema = new Schema<MessageRaw>({
_id: { type: String, _id: true },
key: {
id: { type: String, required: true, minlength: 1 },
remoteJid: { type: String, required: true, minlength: 1 },
fromMe: { type: Boolean, required: true },
participant: { type: String, minlength: 1 },
},
pushName: { type: String },
participant: { type: String },
messageType: { type: String },
message: { type: Object },
source: { type: String, minlength: 3, enum: ['android', 'web', 'ios'] },
messageTimestamp: { type: Number, required: true },
owner: { type: String, required: true, minlength: 1 },
chatwoot: {
messageId: { type: Number },
inboxId: { type: Number },
conversationId: { type: Number },
},
});
messageSchema.index({ 'chatwoot.messageId': 1, owner: 1 });
messageSchema.index({ 'key.id': 1 });
messageSchema.index({ 'key.id': 1, owner: 1 });
messageSchema.index({ owner: 1 });
export const MessageModel = dbserver?.model(MessageRaw.name, messageSchema, 'messages');
export type IMessageModel = typeof MessageModel;
export class MessageUpdateRaw {
_id?: string;
remoteJid?: string;
id?: string;
fromMe?: boolean;
participant?: string;
datetime?: number;
status?: wa.StatusMessage;
owner: string;
pollUpdates?: any;
}
const messageUpdateSchema = new Schema<MessageUpdateRaw>({
_id: { type: String, _id: true },
remoteJid: { type: String, required: true, min: 1 },
id: { type: String, required: true, min: 1 },
fromMe: { type: Boolean, required: true },
participant: { type: String, min: 1 },
datetime: { type: Number, required: true, min: 1 },
status: { type: String, required: true },
owner: { type: String, required: true, min: 1 },
});
export const MessageUpModel = dbserver?.model(MessageUpdateRaw.name, messageUpdateSchema, 'messageUpdate');
export type IMessageUpModel = typeof MessageUpModel;