Skip to content

Commit a42bc98

Browse files
committed
fix: add/remove saving on db and improve add query for startup case
1 parent ecbbc5b commit a42bc98

File tree

2 files changed

+56
-30
lines changed

2 files changed

+56
-30
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"class-validator": "^0.14.1",
6060
"compression": "^1.7.4",
6161
"cors": "^2.8.5",
62+
"cuid": "^3.0.0",
6263
"dayjs": "^1.11.7",
6364
"dotenv": "^16.4.5",
6465
"eventemitter2": "^6.4.9",

src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

Lines changed: 55 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ import { LabelAssociation } from 'baileys/lib/Types/LabelAssociation';
125125
import { spawn } from 'child_process';
126126
import { isArray, isBase64, isURL } from 'class-validator';
127127
import { randomBytes } from 'crypto';
128+
import cuid from 'cuid';
128129
import EventEmitter2 from 'eventemitter2';
129130
import ffmpeg from 'fluent-ffmpeg';
130131
import FormData from 'form-data';
@@ -1573,43 +1574,17 @@ export class BaileysStartupService extends ChannelStartupService {
15731574
database: Database,
15741575
) => {
15751576
this.logger.info(
1576-
`labels as sociation - ${data?.association?.chatId} (${data.type}): ${data?.association?.labelId}`,
1577+
`labels association - ${data?.association?.chatId} (${data.type}-${data?.association?.type}): ${data?.association?.labelId}`,
15771578
);
15781579
if (database.SAVE_DATA.CHATS) {
15791580
const instanceId = this.instanceId;
15801581
const chatId = data.association.chatId;
15811582
const labelId = data.association.labelId;
15821583

15831584
if (data.type === 'add') {
1584-
// Adicionar o label ao array JSONB
1585-
await this.prismaRepository.$executeRawUnsafe(
1586-
`UPDATE "Chat"
1587-
SET "labels" = (SELECT to_jsonb(array_agg(DISTINCT elem))
1588-
FROM (SELECT jsonb_array_elements_text("labels") AS elem
1589-
UNION
1590-
SELECT $1::text AS elem) sub)
1591-
WHERE "instanceId" = $2
1592-
AND "remoteJid" = $3`,
1593-
labelId,
1594-
instanceId,
1595-
chatId,
1596-
);
1585+
await this.addLabel(labelId, instanceId, chatId);
15971586
} else if (data.type === 'remove') {
1598-
// Usar consulta SQL bruta para remover o label
1599-
await this.prismaRepository.$executeRawUnsafe(
1600-
`UPDATE "Chat"
1601-
SET "labels" = COALESCE(
1602-
(SELECT jsonb_agg(elem)
1603-
FROM jsonb_array_elements_text("labels") AS elem
1604-
WHERE elem <> $1),
1605-
'[]' ::jsonb
1606-
)
1607-
WHERE "instanceId" = $2
1608-
AND "remoteJid" = $3;`,
1609-
labelId,
1610-
instanceId,
1611-
chatId,
1612-
);
1587+
await this.removeLabel(labelId, instanceId, chatId);
16131588
}
16141589
}
16151590

@@ -3886,7 +3861,7 @@ export class BaileysStartupService extends ChannelStartupService {
38863861
}));
38873862
}
38883863

3889-
public async handleLabel(data: HandleLabelDto) {
3864+
public async handleLabel(data: HandleLabelDto, instanceId: string) {
38903865
const whatsappContact = await this.whatsappNumber({ numbers: [data.number] });
38913866
if (whatsappContact.length === 0) {
38923867
throw new NotFoundException('Number not found');
@@ -3899,11 +3874,13 @@ export class BaileysStartupService extends ChannelStartupService {
38993874
try {
39003875
if (data.action === 'add') {
39013876
await this.client.addChatLabel(contact.jid, data.labelId);
3877+
await this.addLabel(data.labelId, instanceId, contact.jid);
39023878

39033879
return { numberJid: contact.jid, labelId: data.labelId, add: true };
39043880
}
39053881
if (data.action === 'remove') {
39063882
await this.client.removeChatLabel(contact.jid, data.labelId);
3883+
await this.removeLabel(data.labelId, instanceId, contact.jid);
39073884

39083885
return { numberJid: contact.jid, labelId: data.labelId, remove: true };
39093886
}
@@ -4352,4 +4329,52 @@ export class BaileysStartupService extends ChannelStartupService {
43524329

43534330
return unreadMessages;
43544331
}
4332+
4333+
private async addLabel(labelId: string, instanceId: string, chatId: string) {
4334+
const id = cuid();
4335+
4336+
await this.prismaRepository.$executeRawUnsafe(
4337+
`INSERT INTO "Chat" ("id", "instanceId", "remoteJid", "labels", "createdAt", "updatedAt")
4338+
VALUES ($4, $2, $3, to_jsonb(ARRAY[$1]::text[]), NOW(), NOW()) ON CONFLICT ("instanceId", "remoteJid")
4339+
DO
4340+
UPDATE
4341+
SET "labels" = (
4342+
SELECT to_jsonb(array_agg(DISTINCT elem))
4343+
FROM (
4344+
SELECT jsonb_array_elements_text("Chat"."labels") AS elem
4345+
UNION
4346+
SELECT $1::text AS elem
4347+
) sub
4348+
),
4349+
"updatedAt" = NOW();`,
4350+
labelId,
4351+
instanceId,
4352+
chatId,
4353+
id,
4354+
);
4355+
}
4356+
4357+
private async removeLabel(labelId: string, instanceId: string, chatId: string) {
4358+
const id = cuid();
4359+
4360+
await this.prismaRepository.$executeRawUnsafe(
4361+
`INSERT INTO "Chat" ("id", "instanceId", "remoteJid", "labels", "createdAt", "updatedAt")
4362+
VALUES ($4, $2, $3, '[]'::jsonb, NOW(), NOW()) ON CONFLICT ("instanceId", "remoteJid")
4363+
DO
4364+
UPDATE
4365+
SET "labels" = COALESCE (
4366+
(
4367+
SELECT jsonb_agg(elem)
4368+
FROM jsonb_array_elements_text("Chat"."labels") AS elem
4369+
WHERE elem <> $1
4370+
),
4371+
'[]'::jsonb
4372+
),
4373+
"updatedAt" = NOW();`,
4374+
labelId,
4375+
instanceId,
4376+
chatId,
4377+
id,
4378+
);
4379+
}
43554380
}

0 commit comments

Comments
 (0)