diff --git a/src/whatsapp/models/chat.model.ts b/src/whatsapp/models/chat.model.ts index 57a263fbf..9e713a13d 100644 --- a/src/whatsapp/models/chat.model.ts +++ b/src/whatsapp/models/chat.model.ts @@ -7,6 +7,7 @@ export class ChatRaw { id?: string; owner: string; lastMsgTimestamp?: number; + labels?: string[]; } type ChatRawBoolean = { @@ -18,6 +19,7 @@ const chatSchema = new Schema({ _id: { type: String, _id: true }, id: { type: String, required: true, minlength: 1 }, owner: { type: String, required: true, minlength: 1 }, + labels: { type: [String], default: [] }, }); export const ChatModel = dbserver?.model(ChatRaw.name, chatSchema, 'chats'); diff --git a/src/whatsapp/models/contact.model.ts b/src/whatsapp/models/contact.model.ts index b42dc7d52..a915bb196 100644 --- a/src/whatsapp/models/contact.model.ts +++ b/src/whatsapp/models/contact.model.ts @@ -8,7 +8,6 @@ export class ContactRaw { id?: string; profilePictureUrl?: string; owner: string; - labels?: string[]; } type ContactRawBoolean = { @@ -22,7 +21,6 @@ const contactSchema = new Schema({ id: { type: String, required: true, minlength: 1 }, profilePictureUrl: { type: String, minlength: 1 }, owner: { type: String, required: true, minlength: 1 }, - labels: { type: [String], default: [] }, }); export const ContactModel = dbserver?.model(ContactRaw.name, contactSchema, 'contacts'); diff --git a/src/whatsapp/repository/chat.repository.ts b/src/whatsapp/repository/chat.repository.ts index 99cb61e8c..5f2f50ce6 100644 --- a/src/whatsapp/repository/chat.repository.ts +++ b/src/whatsapp/repository/chat.repository.ts @@ -115,4 +115,63 @@ export class ChatRepository extends Repository { return { error: error?.toString() }; } } + + public async update(data: ChatRaw[], instanceName: string, saveDb = false): Promise { + try { + this.logger.verbose('updating chats'); + + if (data.length === 0) { + this.logger.verbose('no chats to update'); + return; + } + + if (this.dbSettings.ENABLED && saveDb) { + this.logger.verbose('updating chats in db'); + + const chats = data.map((chat) => { + return { + updateOne: { + filter: { id: chat.id }, + update: { ...chat }, + upsert: true, + }, + }; + }); + + const { nModified } = await this.chatModel.bulkWrite(chats); + + this.logger.verbose('chats updated in db: ' + nModified + ' chats'); + return { insertCount: nModified }; + } + + this.logger.verbose('updating chats in store'); + + const store = this.configService.get('STORE'); + + if (store.CONTACTS) { + this.logger.verbose('updating chats in store'); + data.forEach((chat) => { + this.writeStore({ + path: join(this.storePath, 'chats', instanceName), + fileName: chat.id, + data: chat, + }); + this.logger.verbose( + 'chats updated in store in path: ' + join(this.storePath, 'chats', instanceName) + '/' + chat.id, + ); + }); + + this.logger.verbose('chats updated in store: ' + data.length + ' chats'); + + return { insertCount: data.length }; + } + + this.logger.verbose('chats not updated'); + return { insertCount: 0 }; + } catch (error) { + return error; + } finally { + data = undefined; + } + } } diff --git a/src/whatsapp/services/whatsapp.service.ts b/src/whatsapp/services/whatsapp.service.ts index 77c8808a7..9b4a11f10 100644 --- a/src/whatsapp/services/whatsapp.service.ts +++ b/src/whatsapp/services/whatsapp.service.ts @@ -2209,32 +2209,34 @@ export class WAStartupService { ) => { this.logger.verbose('Sending data to webhook in event LABELS_ASSOCIATION'); - // Atualiza labels no contato - const contact = await this.repository.contact.find({ - where: { - owner: this.instance.name, - id: data.association.chatId, - }, - }); - if (contact.length > 0) { - let labels = [...contact[0].labels]; - if (data.type === 'remove') { - labels = labels.filter((label) => label !== data.association.labelId); - } else if (data.type === 'add') { - labels = [...labels, data.association.labelId]; + // Atualiza labels nos chats + if (database.SAVE_DATA.CHATS) { + const chats = await this.repository.chat.find({ + where: { + owner: this.instance.name, + }, + }); + const chat = chats.find((c) => c.id === data.association.chatId); + if (chat) { + let labels = [...chat.labels]; + if (data.type === 'remove') { + labels = labels.filter((label) => label !== data.association.labelId); + } else if (data.type === 'add') { + labels = [...labels, data.association.labelId]; + } + await this.repository.chat.update( + [{ id: chat.id, owner: this.instance.name, labels }], + this.instance.name, + database.SAVE_DATA.CHATS, + ); } - await this.repository.contact.update( - [{ ...contact[0], labels }], - this.instance.name, - database.SAVE_DATA.CONTACTS, - ); } // Envia dados para o webhook this.sendDataWebhook(Events.LABELS_ASSOCIATION, { instance: this.instance.name, type: data.type, - jid: data.association.chatId, + chatId: data.association.chatId, labelId: data.association.labelId, }); }, @@ -2244,6 +2246,7 @@ export class WAStartupService { this.logger.verbose('Initializing event handler'); this.client.ev.process(async (events) => { if (!this.endSession) { + this.logger.verbose(`Event received: ${Object.keys(events).join(', ')}`); const database = this.configService.get('DATABASE'); const settings = await this.findSettings();