Skip to content

Commit ecbbc5b

Browse files
committed
fix: avoid concurrency cases in label handler
1 parent cefe3ef commit ecbbc5b

File tree

1 file changed

+40
-20
lines changed

1 file changed

+40
-20
lines changed

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

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1529,6 +1529,8 @@ export class BaileysStartupService extends ChannelStartupService {
15291529

15301530
private readonly labelHandle = {
15311531
[Events.LABELS_EDIT]: async (label: Label) => {
1532+
this.sendDataWebhook(Events.LABELS_EDIT, { ...label, instance: this.instance.name });
1533+
15321534
const labelsRepository = await this.prismaRepository.label.findMany({
15331535
where: { instanceId: this.instanceId },
15341536
});
@@ -1563,34 +1565,51 @@ export class BaileysStartupService extends ChannelStartupService {
15631565
create: labelData,
15641566
});
15651567
}
1566-
this.sendDataWebhook(Events.LABELS_EDIT, { ...label, instance: this.instance.name });
15671568
}
15681569
},
15691570

15701571
[Events.LABELS_ASSOCIATION]: async (
15711572
data: { association: LabelAssociation; type: 'remove' | 'add' },
15721573
database: Database,
15731574
) => {
1575+
this.logger.info(
1576+
`labels as sociation - ${data?.association?.chatId} (${data.type}): ${data?.association?.labelId}`,
1577+
);
15741578
if (database.SAVE_DATA.CHATS) {
1575-
const chats = await this.prismaRepository.chat.findMany({
1576-
where: { instanceId: this.instanceId },
1577-
});
1578-
const chat = chats.find((c) => c.remoteJid === data.association.chatId);
1579-
if (chat) {
1580-
const labelsArray = Array.isArray(chat.labels) ? chat.labels.map((event) => String(event)) : [];
1581-
let labels = [...labelsArray];
1582-
1583-
if (data.type === 'remove') {
1584-
labels = labels.filter((label) => label !== data.association.labelId);
1585-
} else if (data.type === 'add') {
1586-
labels = [...labels, data.association.labelId];
1587-
}
1588-
await this.prismaRepository.chat.update({
1589-
where: { id: chat.id },
1590-
data: {
1591-
labels,
1592-
},
1593-
});
1579+
const instanceId = this.instanceId;
1580+
const chatId = data.association.chatId;
1581+
const labelId = data.association.labelId;
1582+
1583+
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+
);
1597+
} 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+
);
15941613
}
15951614
}
15961615

@@ -4229,6 +4248,7 @@ export class BaileysStartupService extends ChannelStartupService {
42294248
throw new BadRequestException('Unable to leave the group', error.toString());
42304249
}
42314250
}
4251+
42324252
public async templateMessage() {
42334253
throw new Error('Method not available in the Baileys service');
42344254
}

0 commit comments

Comments
 (0)