mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-12-09 01:49:37 -06:00
Merge pull request #2162 from moothz/fix/saveOnWhatsappCache-remoteJid-failed-constraint
fix: Erro ao enviar mensagem para grupos (remoteJid failed constraint)
This commit is contained in:
commit
27be03ea95
@ -65,73 +65,117 @@ interface ISaveOnWhatsappCacheParams {
|
|||||||
lid?: 'lid' | undefined;
|
lid?: 'lid' | undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function normalizeJid(jid: string | null | undefined): string | null {
|
||||||
|
if (!jid) return null;
|
||||||
|
return jid.startsWith('+') ? jid.slice(1) : jid;
|
||||||
|
}
|
||||||
|
|
||||||
export async function saveOnWhatsappCache(data: ISaveOnWhatsappCacheParams[]) {
|
export async function saveOnWhatsappCache(data: ISaveOnWhatsappCacheParams[]) {
|
||||||
if (configService.get<Database>('DATABASE').SAVE_DATA.IS_ON_WHATSAPP) {
|
if (!configService.get<Database>('DATABASE').SAVE_DATA.IS_ON_WHATSAPP) {
|
||||||
for (const item of data) {
|
return;
|
||||||
const remoteJid = item.remoteJid.startsWith('+') ? item.remoteJid.slice(1) : item.remoteJid;
|
}
|
||||||
|
|
||||||
// TODO: Buscar registro existente PRIMEIRO para preservar dados
|
// Processa todos os itens em paralelo para melhor performance
|
||||||
const allJids = [remoteJid];
|
const processingPromises = data.map(async (item) => {
|
||||||
|
try {
|
||||||
const altJid =
|
const remoteJid = normalizeJid(item.remoteJid);
|
||||||
item.remoteJidAlt && item.remoteJidAlt.includes('@lid')
|
if (!remoteJid) {
|
||||||
? item.remoteJidAlt.startsWith('+')
|
logger.warn('[saveOnWhatsappCache] Item skipped, missing remoteJid.');
|
||||||
? item.remoteJidAlt.slice(1)
|
return;
|
||||||
: item.remoteJidAlt
|
|
||||||
: null;
|
|
||||||
|
|
||||||
if (altJid) {
|
|
||||||
allJids.push(altJid);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const expandedJids = allJids.flatMap((jid) => getAvailableNumbers(jid));
|
const altJidNormalized = normalizeJid(item.remoteJidAlt);
|
||||||
|
const lidAltJid = altJidNormalized && altJidNormalized.includes('@lid') ? altJidNormalized : null;
|
||||||
|
|
||||||
|
const baseJids = [remoteJid]; // Garante que o remoteJid esteja na lista inicial
|
||||||
|
if (lidAltJid) {
|
||||||
|
baseJids.push(lidAltJid);
|
||||||
|
}
|
||||||
|
|
||||||
|
const expandedJids = baseJids.flatMap((jid) => getAvailableNumbers(jid));
|
||||||
|
|
||||||
|
// 1. Busca entrada por jidOptions e também remoteJid
|
||||||
|
// Às vezes acontece do remoteJid atual NÃO ESTAR no jidOptions ainda, ocasionando o erro:
|
||||||
|
// 'Unique constraint failed on the fields: (`remoteJid`)'
|
||||||
|
// Isso acontece principalmente em grupos que possuem o número do criador no ID (ex.: '559911223345-1234567890@g.us')
|
||||||
const existingRecord = await prismaRepository.isOnWhatsapp.findFirst({
|
const existingRecord = await prismaRepository.isOnWhatsapp.findFirst({
|
||||||
where: {
|
where: {
|
||||||
OR: expandedJids.map((jid) => ({ jidOptions: { contains: jid } })),
|
OR: [
|
||||||
|
...expandedJids.map((jid) => ({ jidOptions: { contains: jid } })),
|
||||||
|
{ remoteJid: remoteJid }, // TODO: Descobrir o motivo que causa o remoteJid não estar (às vezes) incluso na lista de jidOptions
|
||||||
|
],
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
logger.verbose(`Register exists: ${existingRecord ? existingRecord.remoteJid : 'não not found'}`);
|
|
||||||
|
|
||||||
const finalJidOptions = [...expandedJids];
|
|
||||||
|
|
||||||
if (existingRecord?.jidOptions) {
|
|
||||||
const existingJids = existingRecord.jidOptions.split(',');
|
|
||||||
// TODO: Adicionar JIDs existentes que não estão na lista atual
|
|
||||||
existingJids.forEach((jid) => {
|
|
||||||
if (!finalJidOptions.includes(jid)) {
|
|
||||||
finalJidOptions.push(jid);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Se tiver remoteJidAlt com @lid novo, adicionar
|
|
||||||
if (altJid && !finalJidOptions.includes(altJid)) {
|
|
||||||
finalJidOptions.push(altJid);
|
|
||||||
}
|
|
||||||
|
|
||||||
const uniqueNumbers = Array.from(new Set(finalJidOptions));
|
|
||||||
|
|
||||||
logger.verbose(
|
logger.verbose(
|
||||||
`Saving: remoteJid=${remoteJid}, jidOptions=${uniqueNumbers.join(',')}, lid=${item.lid === 'lid' || item.remoteJid?.includes('@lid') ? 'lid' : null}`,
|
`[saveOnWhatsappCache] Register exists for [${expandedJids.join(',')}]? => ${existingRecord ? existingRecord.remoteJid : 'Not found'}`,
|
||||||
);
|
);
|
||||||
|
|
||||||
await prismaRepository.isOnWhatsapp.upsert({
|
// 2. Unifica todos os JIDs usando um Set para garantir valores únicos
|
||||||
where: { remoteJid: remoteJid },
|
const finalJidOptions = new Set(expandedJids);
|
||||||
|
|
||||||
update: {
|
if (lidAltJid) {
|
||||||
jidOptions: uniqueNumbers.join(','),
|
finalJidOptions.add(lidAltJid);
|
||||||
lid: item.lid === 'lid' || item.remoteJid?.includes('@lid') ? 'lid' : null,
|
}
|
||||||
},
|
|
||||||
create: {
|
if (existingRecord?.jidOptions) {
|
||||||
remoteJid: remoteJid,
|
existingRecord.jidOptions.split(',').forEach((jid) => finalJidOptions.add(jid));
|
||||||
jidOptions: uniqueNumbers.join(','),
|
}
|
||||||
lid: item.lid === 'lid' || item.remoteJid?.includes('@lid') ? 'lid' : null,
|
|
||||||
},
|
// 3. Prepara o payload final
|
||||||
});
|
// Ordena os JIDs para garantir consistência na string final
|
||||||
|
const sortedJidOptions = [...finalJidOptions].sort();
|
||||||
|
const newJidOptionsString = sortedJidOptions.join(',');
|
||||||
|
const newLid = item.lid === 'lid' || item.remoteJid?.includes('@lid') ? 'lid' : null;
|
||||||
|
|
||||||
|
const dataPayload = {
|
||||||
|
remoteJid: remoteJid,
|
||||||
|
jidOptions: newJidOptionsString,
|
||||||
|
lid: newLid,
|
||||||
|
};
|
||||||
|
|
||||||
|
// 4. Decide entre Criar ou Atualizar
|
||||||
|
if (existingRecord) {
|
||||||
|
// Compara a string de JIDs ordenada existente com a nova
|
||||||
|
const existingJidOptionsString = existingRecord.jidOptions
|
||||||
|
? existingRecord.jidOptions.split(',').sort().join(',')
|
||||||
|
: '';
|
||||||
|
|
||||||
|
const isDataSame =
|
||||||
|
existingRecord.remoteJid === dataPayload.remoteJid &&
|
||||||
|
existingJidOptionsString === dataPayload.jidOptions &&
|
||||||
|
existingRecord.lid === dataPayload.lid;
|
||||||
|
|
||||||
|
if (isDataSame) {
|
||||||
|
logger.verbose(`[saveOnWhatsappCache] Data for ${remoteJid} is already up-to-date. Skipping update.`);
|
||||||
|
return; // Pula para o próximo item
|
||||||
|
}
|
||||||
|
|
||||||
|
// Os dados são diferentes, então atualiza
|
||||||
|
logger.verbose(
|
||||||
|
`[saveOnWhatsappCache] Register exists, updating: remoteJid=${remoteJid}, jidOptions=${dataPayload.jidOptions}, lid=${dataPayload.lid}`,
|
||||||
|
);
|
||||||
|
await prismaRepository.isOnWhatsapp.update({
|
||||||
|
where: { id: existingRecord.id },
|
||||||
|
data: dataPayload,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
// Cria nova entrada
|
||||||
|
logger.verbose(
|
||||||
|
`[saveOnWhatsappCache] Register does not exist, creating: remoteJid=${remoteJid}, jidOptions=${dataPayload.jidOptions}, lid=${dataPayload.lid}`,
|
||||||
|
);
|
||||||
|
await prismaRepository.isOnWhatsapp.create({
|
||||||
|
data: dataPayload,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
// Loga o erro mas não para a execução dos outros promises
|
||||||
|
logger.error(`[saveOnWhatsappCache] Error processing item for ${item.remoteJid}: `, e);
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
|
|
||||||
|
// Espera todas as operações paralelas terminarem
|
||||||
|
await Promise.allSettled(processingPromises);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getOnWhatsappCache(remoteJids: string[]) {
|
export async function getOnWhatsappCache(remoteJids: string[]) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user