Skip to content

Commit 3df4e8d

Browse files
committed
Changed label update to chat instead of contacts
1 parent a2622cb commit 3df4e8d

File tree

4 files changed

+83
-21
lines changed

4 files changed

+83
-21
lines changed

src/whatsapp/models/chat.model.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export class ChatRaw {
77
id?: string;
88
owner: string;
99
lastMsgTimestamp?: number;
10+
labels?: string[];
1011
}
1112

1213
type ChatRawBoolean<T> = {
@@ -18,6 +19,7 @@ const chatSchema = new Schema<ChatRaw>({
1819
_id: { type: String, _id: true },
1920
id: { type: String, required: true, minlength: 1 },
2021
owner: { type: String, required: true, minlength: 1 },
22+
labels: { type: [String], default: [] },
2123
});
2224

2325
export const ChatModel = dbserver?.model(ChatRaw.name, chatSchema, 'chats');

src/whatsapp/models/contact.model.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ export class ContactRaw {
88
id?: string;
99
profilePictureUrl?: string;
1010
owner: string;
11-
labels?: string[];
1211
}
1312

1413
type ContactRawBoolean<T> = {
@@ -22,7 +21,6 @@ const contactSchema = new Schema<ContactRaw>({
2221
id: { type: String, required: true, minlength: 1 },
2322
profilePictureUrl: { type: String, minlength: 1 },
2423
owner: { type: String, required: true, minlength: 1 },
25-
labels: { type: [String], default: [] },
2624
});
2725

2826
export const ContactModel = dbserver?.model(ContactRaw.name, contactSchema, 'contacts');

src/whatsapp/repository/chat.repository.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,63 @@ export class ChatRepository extends Repository {
115115
return { error: error?.toString() };
116116
}
117117
}
118+
119+
public async update(data: ChatRaw[], instanceName: string, saveDb = false): Promise<IInsert> {
120+
try {
121+
this.logger.verbose('updating chats');
122+
123+
if (data.length === 0) {
124+
this.logger.verbose('no chats to update');
125+
return;
126+
}
127+
128+
if (this.dbSettings.ENABLED && saveDb) {
129+
this.logger.verbose('updating chats in db');
130+
131+
const chats = data.map((chat) => {
132+
return {
133+
updateOne: {
134+
filter: { id: chat.id },
135+
update: { ...chat },
136+
upsert: true,
137+
},
138+
};
139+
});
140+
141+
const { nModified } = await this.chatModel.bulkWrite(chats);
142+
143+
this.logger.verbose('chats updated in db: ' + nModified + ' chats');
144+
return { insertCount: nModified };
145+
}
146+
147+
this.logger.verbose('updating chats in store');
148+
149+
const store = this.configService.get<StoreConf>('STORE');
150+
151+
if (store.CONTACTS) {
152+
this.logger.verbose('updating chats in store');
153+
data.forEach((chat) => {
154+
this.writeStore({
155+
path: join(this.storePath, 'chats', instanceName),
156+
fileName: chat.id,
157+
data: chat,
158+
});
159+
this.logger.verbose(
160+
'chats updated in store in path: ' + join(this.storePath, 'chats', instanceName) + '/' + chat.id,
161+
);
162+
});
163+
164+
this.logger.verbose('chats updated in store: ' + data.length + ' chats');
165+
166+
return { insertCount: data.length };
167+
}
168+
169+
this.logger.verbose('chats not updated');
170+
return { insertCount: 0 };
171+
} catch (error) {
172+
return error;
173+
} finally {
174+
data = undefined;
175+
}
176+
}
118177
}

src/whatsapp/services/whatsapp.service.ts

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2209,32 +2209,34 @@ export class WAStartupService {
22092209
) => {
22102210
this.logger.verbose('Sending data to webhook in event LABELS_ASSOCIATION');
22112211

2212-
// Atualiza labels no contato
2213-
const contact = await this.repository.contact.find({
2214-
where: {
2215-
owner: this.instance.name,
2216-
id: data.association.chatId,
2217-
},
2218-
});
2219-
if (contact.length > 0) {
2220-
let labels = [...contact[0].labels];
2221-
if (data.type === 'remove') {
2222-
labels = labels.filter((label) => label !== data.association.labelId);
2223-
} else if (data.type === 'add') {
2224-
labels = [...labels, data.association.labelId];
2212+
// Atualiza labels nos chats
2213+
if (database.SAVE_DATA.CHATS) {
2214+
const chats = await this.repository.chat.find({
2215+
where: {
2216+
owner: this.instance.name,
2217+
},
2218+
});
2219+
const chat = chats.find((c) => c.id === data.association.chatId);
2220+
if (chat) {
2221+
let labels = [...chat.labels];
2222+
if (data.type === 'remove') {
2223+
labels = labels.filter((label) => label !== data.association.labelId);
2224+
} else if (data.type === 'add') {
2225+
labels = [...labels, data.association.labelId];
2226+
}
2227+
await this.repository.chat.update(
2228+
[{ id: chat.id, owner: this.instance.name, labels }],
2229+
this.instance.name,
2230+
database.SAVE_DATA.CHATS,
2231+
);
22252232
}
2226-
await this.repository.contact.update(
2227-
[{ ...contact[0], labels }],
2228-
this.instance.name,
2229-
database.SAVE_DATA.CONTACTS,
2230-
);
22312233
}
22322234

22332235
// Envia dados para o webhook
22342236
this.sendDataWebhook(Events.LABELS_ASSOCIATION, {
22352237
instance: this.instance.name,
22362238
type: data.type,
2237-
jid: data.association.chatId,
2239+
chatId: data.association.chatId,
22382240
labelId: data.association.labelId,
22392241
});
22402242
},
@@ -2244,6 +2246,7 @@ export class WAStartupService {
22442246
this.logger.verbose('Initializing event handler');
22452247
this.client.ev.process(async (events) => {
22462248
if (!this.endSession) {
2249+
this.logger.verbose(`Event received: ${Object.keys(events).join(', ')}`);
22472250
const database = this.configService.get<Database>('DATABASE');
22482251
const settings = await this.findSettings();
22492252

0 commit comments

Comments
 (0)