mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-12-26 15:17:44 -06:00
feat(baileys,chatwoot,on-whatsapp-cache): implementações e correções na baileys e chatwoot
* corrige cache de números PN, LIDs e g.us para enviar o número correto * atualiza para os últimos commits da baileys * corrige envio de áudio e documentos via chatwoot no canal baileys * diversas correções na integração com chatwoot * corrige mensagens ignoradas no recebimento de leads
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
import { prismaRepository } from '@api/server.module';
|
||||
import { configService, Database } from '@config/env.config';
|
||||
import { Logger } from '@config/logger.config';
|
||||
import dayjs from 'dayjs';
|
||||
|
||||
const logger = new Logger('OnWhatsappCache');
|
||||
|
||||
function getAvailableNumbers(remoteJid: string) {
|
||||
const numbersAvailable: string[] = [];
|
||||
|
||||
@@ -11,6 +14,11 @@ function getAvailableNumbers(remoteJid: string) {
|
||||
|
||||
const [number, domain] = remoteJid.split('@');
|
||||
|
||||
// TODO: Se já for @lid, retornar apenas ele mesmo SEM adicionar @domain novamente
|
||||
if (domain === 'lid' || domain === 'g.us') {
|
||||
return [remoteJid]; // Retorna direto para @lid e @g.us
|
||||
}
|
||||
|
||||
// Brazilian numbers
|
||||
if (remoteJid.startsWith('55')) {
|
||||
const numberWithDigit =
|
||||
@@ -47,35 +55,89 @@ function getAvailableNumbers(remoteJid: string) {
|
||||
numbersAvailable.push(remoteJid);
|
||||
}
|
||||
|
||||
// TODO: Adiciona @domain apenas para números que não são @lid
|
||||
return numbersAvailable.map((number) => `${number}@${domain}`);
|
||||
}
|
||||
|
||||
interface ISaveOnWhatsappCacheParams {
|
||||
remoteJid: string;
|
||||
lid?: string;
|
||||
remoteJidAlt?: string;
|
||||
lid?: 'lid' | undefined;
|
||||
}
|
||||
|
||||
export async function saveOnWhatsappCache(data: ISaveOnWhatsappCacheParams[]) {
|
||||
if (configService.get<Database>('DATABASE').SAVE_DATA.IS_ON_WHATSAPP) {
|
||||
const upsertsQuery = data.map((item) => {
|
||||
for (const item of data) {
|
||||
const remoteJid = item.remoteJid.startsWith('+') ? item.remoteJid.slice(1) : item.remoteJid;
|
||||
const numbersAvailable = getAvailableNumbers(remoteJid);
|
||||
|
||||
return prismaRepository.isOnWhatsapp.upsert({
|
||||
create: {
|
||||
remoteJid: remoteJid,
|
||||
jidOptions: numbersAvailable.join(','),
|
||||
lid: item.lid,
|
||||
// TODO: Buscar registro existente PRIMEIRO para preservar dados
|
||||
const allJids = [remoteJid];
|
||||
|
||||
const altJid =
|
||||
item.remoteJidAlt && item.remoteJidAlt.includes('@lid')
|
||||
? item.remoteJidAlt.startsWith('+')
|
||||
? item.remoteJidAlt.slice(1)
|
||||
: item.remoteJidAlt
|
||||
: null;
|
||||
|
||||
if (altJid) {
|
||||
allJids.push(altJid);
|
||||
}
|
||||
|
||||
const expandedJids = allJids.flatMap((jid) => getAvailableNumbers(jid));
|
||||
|
||||
const existingRecord = await prismaRepository.isOnWhatsapp.findFirst({
|
||||
where: {
|
||||
OR: expandedJids.map((jid) => ({ jidOptions: { contains: jid } })),
|
||||
},
|
||||
update: {
|
||||
jidOptions: numbersAvailable.join(','),
|
||||
lid: item.lid,
|
||||
},
|
||||
where: { remoteJid: remoteJid },
|
||||
});
|
||||
});
|
||||
|
||||
await prismaRepository.$transaction(upsertsQuery);
|
||||
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) {
|
||||
if (!finalJidOptions.includes(altJid)) {
|
||||
finalJidOptions.push(altJid);
|
||||
}
|
||||
}
|
||||
|
||||
const uniqueNumbers = Array.from(new Set(finalJidOptions));
|
||||
|
||||
logger.verbose(
|
||||
`Saving: remoteJid=${remoteJid}, jidOptions=${uniqueNumbers.join(',')}, lid=${item.lid === 'lid' || item.remoteJid?.includes('@lid') ? 'lid' : null}`,
|
||||
);
|
||||
|
||||
if (existingRecord) {
|
||||
await prismaRepository.isOnWhatsapp.update({
|
||||
where: { id: existingRecord.id },
|
||||
data: {
|
||||
remoteJid: remoteJid,
|
||||
jidOptions: uniqueNumbers.join(','),
|
||||
lid: item.lid === 'lid' || item.remoteJid?.includes('@lid') ? 'lid' : null,
|
||||
},
|
||||
});
|
||||
} else {
|
||||
await prismaRepository.isOnWhatsapp.create({
|
||||
data: {
|
||||
remoteJid: remoteJid,
|
||||
jidOptions: uniqueNumbers.join(','),
|
||||
lid: item.lid === 'lid' || item.remoteJid?.includes('@lid') ? 'lid' : null,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user