mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-24 17:38:40 -06:00
fix: avoid concurrency cases in label handler
This commit is contained in:
parent
cefe3ef6c3
commit
ecbbc5b090
@ -1529,6 +1529,8 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
|
|
||||||
private readonly labelHandle = {
|
private readonly labelHandle = {
|
||||||
[Events.LABELS_EDIT]: async (label: Label) => {
|
[Events.LABELS_EDIT]: async (label: Label) => {
|
||||||
|
this.sendDataWebhook(Events.LABELS_EDIT, { ...label, instance: this.instance.name });
|
||||||
|
|
||||||
const labelsRepository = await this.prismaRepository.label.findMany({
|
const labelsRepository = await this.prismaRepository.label.findMany({
|
||||||
where: { instanceId: this.instanceId },
|
where: { instanceId: this.instanceId },
|
||||||
});
|
});
|
||||||
@ -1563,7 +1565,6 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
create: labelData,
|
create: labelData,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
this.sendDataWebhook(Events.LABELS_EDIT, { ...label, instance: this.instance.name });
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -1571,26 +1572,44 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
data: { association: LabelAssociation; type: 'remove' | 'add' },
|
data: { association: LabelAssociation; type: 'remove' | 'add' },
|
||||||
database: Database,
|
database: Database,
|
||||||
) => {
|
) => {
|
||||||
|
this.logger.info(
|
||||||
|
`labels as sociation - ${data?.association?.chatId} (${data.type}): ${data?.association?.labelId}`,
|
||||||
|
);
|
||||||
if (database.SAVE_DATA.CHATS) {
|
if (database.SAVE_DATA.CHATS) {
|
||||||
const chats = await this.prismaRepository.chat.findMany({
|
const instanceId = this.instanceId;
|
||||||
where: { instanceId: this.instanceId },
|
const chatId = data.association.chatId;
|
||||||
});
|
const labelId = data.association.labelId;
|
||||||
const chat = chats.find((c) => c.remoteJid === data.association.chatId);
|
|
||||||
if (chat) {
|
|
||||||
const labelsArray = Array.isArray(chat.labels) ? chat.labels.map((event) => String(event)) : [];
|
|
||||||
let labels = [...labelsArray];
|
|
||||||
|
|
||||||
if (data.type === 'remove') {
|
if (data.type === 'add') {
|
||||||
labels = labels.filter((label) => label !== data.association.labelId);
|
// Adicionar o label ao array JSONB
|
||||||
} else if (data.type === 'add') {
|
await this.prismaRepository.$executeRawUnsafe(
|
||||||
labels = [...labels, data.association.labelId];
|
`UPDATE "Chat"
|
||||||
}
|
SET "labels" = (SELECT to_jsonb(array_agg(DISTINCT elem))
|
||||||
await this.prismaRepository.chat.update({
|
FROM (SELECT jsonb_array_elements_text("labels") AS elem
|
||||||
where: { id: chat.id },
|
UNION
|
||||||
data: {
|
SELECT $1::text AS elem) sub)
|
||||||
labels,
|
WHERE "instanceId" = $2
|
||||||
},
|
AND "remoteJid" = $3`,
|
||||||
});
|
labelId,
|
||||||
|
instanceId,
|
||||||
|
chatId,
|
||||||
|
);
|
||||||
|
} else if (data.type === 'remove') {
|
||||||
|
// Usar consulta SQL bruta para remover o label
|
||||||
|
await this.prismaRepository.$executeRawUnsafe(
|
||||||
|
`UPDATE "Chat"
|
||||||
|
SET "labels" = COALESCE(
|
||||||
|
(SELECT jsonb_agg(elem)
|
||||||
|
FROM jsonb_array_elements_text("labels") AS elem
|
||||||
|
WHERE elem <> $1),
|
||||||
|
'[]' ::jsonb
|
||||||
|
)
|
||||||
|
WHERE "instanceId" = $2
|
||||||
|
AND "remoteJid" = $3;`,
|
||||||
|
labelId,
|
||||||
|
instanceId,
|
||||||
|
chatId,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -4229,6 +4248,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
throw new BadRequestException('Unable to leave the group', error.toString());
|
throw new BadRequestException('Unable to leave the group', error.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async templateMessage() {
|
public async templateMessage() {
|
||||||
throw new Error('Method not available in the Baileys service');
|
throw new Error('Method not available in the Baileys service');
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user