Ajustes da integracao com chatwoot e correcoes de erros na integracao com o s3

This commit is contained in:
Éder Costa 2025-04-19 10:53:45 -03:00
parent a6304d3eec
commit a657b92c54
7 changed files with 2496 additions and 2389 deletions

View File

@ -3,7 +3,7 @@ FROM node:20-alpine AS builder
RUN apk update && \
apk add git ffmpeg wget curl bash openssl
LABEL version="2.2.3.3" description="Api to control whatsapp features through http requests."
LABEL version="2.2.3.17" description="Api to control whatsapp features through http requests."
LABEL maintainer="Davidson Gomes" git="https://github.com/DavidsonGomes"
LABEL contact="contato@atendai.com"

7
package-lock.json generated
View File

@ -1,12 +1,12 @@
{
"name": "evolution-api",
"version": "2.2.3.3",
"version": "2.2.3.17",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "evolution-api",
"version": "2.2.3.3",
"version": "2.2.3.17",
"license": "Apache-2.0",
"dependencies": {
"@adiwajshing/keyed-db": "^0.2.4",
@ -55,6 +55,7 @@
"sharp": "^0.32.6",
"socket.io": "^4.8.1",
"socket.io-client": "^4.8.1",
"source-map-support": "^0.5.21",
"tsup": "^8.3.5"
},
"devDependencies": {
@ -10848,7 +10849,6 @@
"version": "0.6.1",
"resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz",
"integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==",
"dev": true,
"engines": {
"node": ">=0.10.0"
}
@ -10857,7 +10857,6 @@
"version": "0.5.21",
"resolved": "https://registry.npmjs.org/source-map-support/-/source-map-support-0.5.21.tgz",
"integrity": "sha512-uBHU3L3czsIyYXKX88fdrGovxdSCoTGDRZ6SYXtSRxLZUzHg5P/66Ht6uoUlHu9EZod+inXhKo3qQgwXUT/y1w==",
"dev": true,
"dependencies": {
"buffer-from": "^1.0.0",
"source-map": "^0.6.0"

View File

@ -1,13 +1,13 @@
{
"name": "evolution-api",
"version": "2.2.3.3",
"version": "2.2.3.17",
"description": "Rest api for communication with WhatsApp",
"main": "./dist/main.js",
"type": "commonjs",
"scripts": {
"build": "tsc --noEmit && tsup",
"start": "tsnd -r tsconfig-paths/register --files --transpile-only ./src/main.ts",
"start:prod": "node dist/main",
"start:prod": "node --enable-source-maps -r source-map-support/register dist/main.js",
"dev:server": "tsnd -r tsconfig-paths/register --files --transpile-only --respawn --ignore-watch node_modules ./src/main.ts",
"test": "tsnd -r tsconfig-paths/register --files --transpile-only --respawn --ignore-watch node_modules ./test/all.test.ts",
"lint": "eslint --fix --ext .ts src",
@ -95,6 +95,7 @@
"sharp": "^0.32.6",
"socket.io": "^4.8.1",
"socket.io-client": "^4.8.1",
"source-map-support": "^0.5.21",
"tsup": "^8.3.5"
},
"devDependencies": {

View File

@ -295,53 +295,85 @@
avatar_url?: string,
jid?: string,
) {
const client = await this.clientCw(instance);
this.logger.verbose(
`[ChatwootService][createContact] start instance=${instance.instanceName} phone=${phoneNumber}`
);
// 1) obter cliente
const client = await this.clientCw(instance);
if (!client) {
this.logger.warn('client not found');
this.logger.warn(
`[ChatwootService][createContact] client not found for instance=${instance.instanceName}`
);
return null;
}
this.logger.verbose(`[ChatwootService][createContact] client obtained`);
let data: any = {};
// 2) montar payload
const data: any = { inbox_id: inboxId, name: name || phoneNumber, avatar_url };
if (!isGroup) {
data = {
inbox_id: inboxId,
name: name || phoneNumber,
identifier: jid,
avatar_url: avatar_url,
};
if ((jid && jid.includes('@')) || !jid) {
data['phone_number'] = `+${phoneNumber}`;
}
data.identifier = jid;
data.phone_number = `+${phoneNumber}`;
} else {
data = {
inbox_id: inboxId,
name: name || phoneNumber,
identifier: phoneNumber,
avatar_url: avatar_url,
};
data.identifier = phoneNumber;
}
this.logger.verbose(
`[ChatwootService][createContact] payload=${JSON.stringify(data)}`
);
const contact = await client.contacts.create({
// 3) criar no Chatwoot
let rawResponse: any;
try {
rawResponse = await client.contacts.create({
accountId: this.provider.accountId,
data,
});
this.logger.verbose(
`[ChatwootService][createContact] raw create response=${JSON.stringify(rawResponse)}`
);
} catch (err) {
this.logger.error(
`[ChatwootService][createContact] error creating contact: ${err}`
);
throw err;
}
if (!contact) {
this.logger.warn('contact not found');
// 4) extrair o contactId dos dois possíveis formatos
// - legacy: { id: number, ... }
// - nova versão: { payload: { contact: { id: number, ... } } }
const maybePayload = rawResponse.payload?.contact;
const contactObj = maybePayload ?? rawResponse;
const contactId = contactObj.id as number | undefined;
if (!contactId) {
this.logger.error(
`[ChatwootService][createContact] no id found in response; raw=${JSON.stringify(rawResponse)}`
);
return null;
}
this.logger.verbose(
`[ChatwootService][createContact] created contact id=${contactId}`
);
const findContact = await this.findContact(instance, phoneNumber);
const contactId = findContact?.id;
// 5) adicionar label
try {
this.logger.verbose(
`[ChatwootService][createContact] adding label=${this.provider.nameInbox} to contactId=${contactId}`
);
await this.addLabelToContact(this.provider.nameInbox, contactId);
return contact;
} catch (err) {
this.logger.error(
`[ChatwootService][createContact] error addLabelToContact: ${err}`
);
}
// 6) retornar objeto com .id para ser usado pelo createConversation
return { id: contactId, ...contactObj };
}
public async updateContact(instance: InstanceDto, id: number, data: any) {
const client = await this.clientCw(instance);
@ -406,51 +438,78 @@
}
public async findContact(instance: InstanceDto, phoneNumber: string) {
const client = await this.clientCw(instance);
this.logger.verbose(
`[ChatwootService][findContact] start for instance=${instance.instanceName}, phoneNumber=${phoneNumber}`
);
const client = await this.clientCw(instance);
if (!client) {
this.logger.warn('client not found');
this.logger.warn(
`[ChatwootService][findContact] client not found for instance=${instance.instanceName}`
);
return null;
}
let query: any;
const isGroup = phoneNumber.includes('@g.us');
const query = isGroup ? phoneNumber : `+${phoneNumber}`;
if (!isGroup) {
query = `+${phoneNumber}`;
} else {
query = phoneNumber;
}
let contact: any;
this.logger.verbose(
`[ChatwootService][findContact] isGroup=${isGroup}, query=${query}`
);
let response: any;
try {
if (isGroup) {
contact = await client.contacts.search({
response = await client.contacts.search({
accountId: this.provider.accountId,
q: query,
});
} else {
contact = await chatwootRequest(this.getClientCwConfig(), {
response = await chatwootRequest(this.getClientCwConfig(), {
method: 'POST',
url: `/api/v1/accounts/${this.provider.accountId}/contacts/filter`,
body: {
payload: this.getFilterPayload(query),
},
body: { payload: this.getFilterPayload(query) },
});
}
if (!contact && contact?.payload?.length === 0) {
this.logger.warn('contact not found');
this.logger.verbose(
`[ChatwootService][findContact] raw response: ${JSON.stringify(response)}`
);
} catch (error) {
this.logger.error(
`[ChatwootService][findContact] error during API call: ${error.message}`
);
return null;
}
if (!isGroup) {
return contact.payload.length > 1 ? this.findContactInContactList(contact.payload, query) : contact.payload[0];
const payload = response.payload || [];
this.logger.verbose(
`[ChatwootService][findContact] payload length: ${payload.length}`
);
if (payload.length === 0) {
this.logger.warn(
`[ChatwootService][findContact] contact not found for query=${query}`
);
return null;
}
let found: any;
if (isGroup) {
found = payload.find((c: any) => c.identifier === query);
} else {
return contact.payload.find((contact) => contact.identifier === query);
found = payload.length > 1
? this.findContactInContactList(payload, query)
: payload[0];
}
this.logger.verbose(
`[ChatwootService][findContact] returning contact: ${JSON.stringify(found)}`
);
return found;
}
private async mergeBrazilianContacts(contacts: any[]) {
try {
const contact = await chatwootRequest(this.getClientCwConfig(), {
@ -542,227 +601,215 @@
return filterPayload;
}
public async createConversation(instance: InstanceDto, body: any) {
try {
this.logger.verbose('--- Start createConversation ---');
this.logger.verbose(`Instance: ${JSON.stringify(instance)}`);
private pendingCreateConv = new Map<string, Promise<number>>();
public async createConversation(instance: InstanceDto, body: any): Promise<number> {
const remoteJid = body.key.remoteJid as string;
this.logger.verbose("[createConversation] Iniciando para remoteJid=" + remoteJid);
// 0) Se já está criando, reutiliza a promise
if (this.pendingCreateConv.has(remoteJid)) {
this.logger.verbose("[createConversation] Ja em criacao para " + remoteJid + ", retornando promise existente");
return this.pendingCreateConv.get(remoteJid)!;
}
let triedRecovery = false;
const cacheKey = instance.instanceName + ":createConversation-" + remoteJid;
const p = (async (): Promise<number> => {
try {
this.logger.verbose("[createConversation] Chamando _createConversation pela primeira vez");
return await this._createConversation(instance, body);
} catch (err) {
this.logger.error("[createConversation] Erro na primeira tentativa: " + err);
if (!triedRecovery) {
triedRecovery = true;
this.logger.warn("[createConversation] Tentando recuperacao: limpando cache e recriando conversa");
await this.cache.delete(cacheKey);
this.logger.verbose("[createConversation] Cache deletado para chave=" + cacheKey);
return await this._createConversation(instance, body);
}
this.logger.error("[createConversation] Ja tentei recuperacao, repassando erro");
throw err;
}
})();
this.pendingCreateConv.set(remoteJid, p);
try {
const convId = await p;
this.logger.verbose("[createConversation] Concluido para " + remoteJid + ", convId=" + convId);
return convId;
} finally {
this.pendingCreateConv.delete(remoteJid);
this.logger.verbose("[createConversation] Removido pendingCreateConv para " + remoteJid);
}
}
private async _createConversation(instance: InstanceDto, body: any): Promise<number> {
const remoteJid = body.key.remoteJid as string;
const cacheKey = instance.instanceName + ":createConversation-" + remoteJid;
this.logger.verbose("[_createConversation] Start para remoteJid=" + remoteJid);
// 1) Cliente Chatwoot
const client = await this.clientCw(instance);
if (!client) {
this.logger.warn(`Client not found for instance: ${JSON.stringify(instance)}`);
return null;
this.logger.error("[_createConversation] Client Chatwoot nao encontrado para " + instance.instanceName);
throw new Error("Client not found for instance: " + instance.instanceName);
}
this.logger.verbose("[_createConversation] Client Chatwoot obtido");
// 2) Cache
const hasCache = await this.cache.has(cacheKey);
this.logger.verbose("[_createConversation] Cache check para key=" + cacheKey + ": " + hasCache);
if (hasCache) {
const cachedId = (await this.cache.get(cacheKey)) as number;
this.logger.verbose("[_createConversation] Usando ID em cache=" + cachedId);
return cachedId;
}
const cacheKey = `${instance.instanceName}:createConversation-${body.key.remoteJid}`;
this.logger.verbose(`Cache key: ${cacheKey}`);
if (await this.cache.has(cacheKey)) {
this.logger.verbose(`Cache hit for key: ${cacheKey}`);
const conversationId = (await this.cache.get(cacheKey)) as number;
this.logger.verbose(`Cached conversation ID: ${conversationId}`);
let conversationExists: conversation | boolean;
try {
conversationExists = await client.conversations.get({
accountId: this.provider.accountId,
conversationId: conversationId,
});
this.logger.verbose(`Conversation exists: ${JSON.stringify(conversationExists)}`);
} catch (error) {
this.logger.error(`Error getting conversation: ${error}`);
conversationExists = false;
}
if (!conversationExists) {
this.logger.verbose('Conversation does not exist, re-calling createConversation');
this.cache.delete(cacheKey);
return await this.createConversation(instance, body);
}
return conversationId;
}
const isGroup = body.key.remoteJid.includes('@g.us');
this.logger.verbose(`Is group: ${isGroup}`);
const chatId = isGroup ? body.key.remoteJid : body.key.remoteJid.split('@')[0];
this.logger.verbose(`Chat ID: ${chatId}`);
let nameContact: string;
nameContact = !body.key.fromMe ? body.pushName : chatId;
this.logger.verbose(`Name contact: ${nameContact}`);
// 3) Inbox
const filterInbox = await this.getInbox(instance);
if (!filterInbox) {
this.logger.warn(`Inbox not found for instance: ${JSON.stringify(instance)}`);
return null;
this.logger.error("[_createConversation] Inbox nao encontrada para " + instance.instanceName);
throw new Error("Inbox not found for instance: " + instance.instanceName);
}
this.logger.verbose("[_createConversation] Inbox encontrada: id=" + filterInbox.id);
if (isGroup) {
this.logger.verbose('Processing group conversation');
const group = await this.waMonitor.waInstances[instance.instanceName].client.groupMetadata(chatId);
this.logger.verbose(`Group metadata: ${JSON.stringify(group)}`);
nameContact = `${group.subject} (GROUP)`;
const picture_url = await this.waMonitor.waInstances[instance.instanceName].profilePicture(
body.key.participant.split('@')[0],
);
this.logger.verbose(`Participant profile picture URL: ${JSON.stringify(picture_url)}`);
const findParticipant = await this.findContact(instance, body.key.participant.split('@')[0]);
this.logger.verbose(`Found participant: ${JSON.stringify(findParticipant)}`);
if (findParticipant) {
if (!findParticipant.name || findParticipant.name === chatId) {
await this.updateContact(instance, findParticipant.id, {
name: body.pushName,
avatar_url: picture_url.profilePictureUrl || null,
});
}
} else {
await this.createContact(
instance,
body.key.participant.split('@')[0],
filterInbox.id,
false,
body.pushName,
picture_url.profilePictureUrl || null,
body.key.participant,
);
}
}
const picture_url = await this.waMonitor.waInstances[instance.instanceName].profilePicture(chatId);
this.logger.verbose(`Contact profile picture URL: ${JSON.stringify(picture_url)}`);
// 4) Contato
const isGroup = remoteJid.includes("@g.us");
const chatId = isGroup ? remoteJid : remoteJid.split("@")[0];
this.logger.verbose("[_createConversation] isGroup=" + isGroup + ", chatId=" + chatId);
let contact = await this.findContact(instance, chatId);
this.logger.verbose(`Found contact: ${JSON.stringify(contact)}`);
if (contact) {
if (!body.key.fromMe) {
const waProfilePictureFile =
picture_url?.profilePictureUrl?.split('#')[0].split('?')[0].split('/').pop() || '';
const chatwootProfilePictureFile = contact?.thumbnail?.split('#')[0].split('?')[0].split('/').pop() || '';
const pictureNeedsUpdate = waProfilePictureFile !== chatwootProfilePictureFile;
const nameNeedsUpdate =
!contact.name ||
contact.name === chatId ||
(`+${chatId}`.startsWith('+55')
? this.getNumbers(`+${chatId}`).some(
(v) => contact.name === v || contact.name === v.substring(3) || contact.name === v.substring(1),
)
: false);
this.logger.verbose(`Picture needs update: ${pictureNeedsUpdate}`);
this.logger.verbose(`Name needs update: ${nameNeedsUpdate}`);
if (pictureNeedsUpdate || nameNeedsUpdate) {
contact = await this.updateContact(instance, contact.id, {
...(nameNeedsUpdate && { name: nameContact }),
...(waProfilePictureFile === '' && { avatar: null }),
...(pictureNeedsUpdate && { avatar_url: picture_url?.profilePictureUrl }),
});
}
}
this.logger.verbose("[_createConversation] Contato encontrado: id=" + contact.id);
} else {
const jid = body.key.remoteJid;
this.logger.verbose("[_createConversation] Contato nao existe, criando...");
const name = isGroup
? (await this.waMonitor.waInstances[instance.instanceName].client.groupMetadata(chatId)).subject + " (GROUP)"
: body.pushName || chatId;
const pictureUrl = (await this.waMonitor.waInstances[instance.instanceName].profilePicture(chatId)).profilePictureUrl;
contact = await this.createContact(
instance,
chatId,
filterInbox.id,
isGroup,
nameContact,
picture_url.profilePictureUrl || null,
jid,
name,
pictureUrl || null,
isGroup ? remoteJid : undefined
);
}
if (!contact) {
this.logger.warn('Contact not created or found');
return null;
this.logger.error("[_createConversation] Falha ao criar contato para " + chatId);
throw new Error("Nao conseguiu criar contato para conversa");
}
this.logger.verbose("[_createConversation] Contato criado: id=" + contact.id);
}
const contactId = (contact.id ?? contact.payload?.contact?.id) as number;
const contactId = contact?.payload?.id || contact?.payload?.contact?.id || contact?.id;
this.logger.verbose(`Contact ID: ${contactId}`);
const contactConversations = (await client.contacts.listConversations({
// 5) Listar conversas existentes
this.logger.verbose("[_createConversation] Chamando listConversations para contactId=" + contactId);
const listResp: any = await client.contacts.listConversations({
accountId: this.provider.accountId,
id: contactId,
})) as any;
this.logger.verbose(`Contact conversations: ${JSON.stringify(contactConversations)}`);
});
this.logger.verbose("[_createConversation] listConversations raw: " + JSON.stringify(listResp));
if (!contactConversations || !contactConversations.payload) {
this.logger.error('No conversations found or payload is undefined');
return null;
}
let conversations: any[] = [];
if (Array.isArray(listResp)) conversations = listResp;
else if (Array.isArray(listResp.payload)) conversations = listResp.payload;
else if (Array.isArray(listResp.data?.payload)) conversations = listResp.data.payload;
else if (Array.isArray(listResp.data)) conversations = listResp.data;
this.logger.verbose("[_createConversation] Encontradas " + conversations.length + " conversas");
if (contactConversations.payload.length) {
let conversation: any;
// 6) Filtrar conversa aberta ou pendente
let conv = null;
if (this.provider.reopenConversation) {
conversation = contactConversations.payload.find((conversation) => conversation.inbox_id == filterInbox.id);
this.logger.verbose(`Found conversation in reopenConversation mode: ${JSON.stringify(conversation)}`);
if (conversation && this.provider.conversationPending && conversation.status !== 'open') {
if (conversation) {
this.logger.verbose("[_createConversation] reopenConversation=true, buscando inbox_id=" + filterInbox.id);
conv = conversations.find(c => c.inbox_id === filterInbox.id);
if (conv && this.provider.conversationPending && conv.status !== "pending") {
this.logger.verbose("[_createConversation] Reabrindo conversa " + conv.id + " para status=pending");
await client.conversations.toggleStatus({
accountId: this.provider.accountId,
conversationId: conversation.id,
data: {
status: 'pending',
},
conversationId: conv.id,
data: { status: "pending" },
});
}
}
else if (!conversation) {
this.logger.warn('Conversation not found, creating a new one');
this.cache.delete(cacheKey);
return await this.createConversation(instance, body);
}
} else {
conversation = contactConversations.payload.find(
(conversation) => conversation.status !== 'resolved' && conversation.inbox_id == filterInbox.id,
);
this.logger.verbose(`Found conversation: ${JSON.stringify(conversation)}`);
this.logger.verbose("[_createConversation] reopenConversation=false, buscando status!=resolved");
conv = conversations.find(c => c.status !== "resolved" && c.inbox_id === filterInbox.id);
}
if (conversation) {
this.logger.verbose(`Returning existing conversation ID: ${conversation.id}`);
this.cache.set(cacheKey, conversation.id);
return conversation.id;
}
if (conv) {
this.logger.verbose("[_createConversation] Usando conversa existente id=" + conv.id);
this.cache.set(cacheKey, conv.id, 5 * 60);
return conv.id;
}
const data = {
// 7) Criar nova conversa
this.logger.verbose("[_createConversation] Nenhuma conversa encontrada, criando nova...");
const payload: any = {
contact_id: contactId.toString(),
inbox_id: filterInbox.id.toString(),
...(this.provider.conversationPending ? { status: "pending" } : {}),
};
if (this.provider.conversationPending) {
data['status'] = 'pending';
}
const conversation = await client.conversations.create({
try {
const newConv = await client.conversations.create({
accountId: this.provider.accountId,
data,
data: payload,
});
if (!newConv?.id) {
this.logger.error("[_createConversation] create retornou sem ID");
throw new Error("Falha ao criar nova conversa: resposta sem ID");
}
this.logger.verbose("[_createConversation] Nova conversa criada id=" + newConv.id);
this.cache.set(cacheKey, newConv.id, 5 * 60);
return newConv.id;
if (!conversation) {
this.logger.warn('Conversation not created or found');
return null;
} catch (err: any) {
this.logger.error("[_createConversation] Erro ao criar conversa: " + err);
this.logger.warn("[_createConversation] Tentando recuperar conversa via listConversations novamente");
const retryList: any = await client.contacts.listConversations({
accountId: this.provider.accountId,
id: contactId,
});
this.logger.verbose("[_createConversation] retry listConversations raw: " + JSON.stringify(retryList));
let retryConvs: any[] = [];
if (Array.isArray(retryList)) retryConvs = retryList;
else if (Array.isArray(retryList.payload)) retryConvs = retryList.payload;
else if (Array.isArray(retryList.data?.payload)) retryConvs = retryList.data.payload;
else if (Array.isArray(retryList.data)) retryConvs = retryList.data;
this.logger.verbose("[_createConversation] retry encontrou " + retryConvs.length + " conversas");
const recovered = retryConvs.find(c => c.inbox_id === filterInbox.id);
if (recovered) {
this.logger.verbose("[_createConversation] Recuperou conversa existente id=" + recovered.id);
this.cache.set(cacheKey, recovered.id, 5 * 60);
return recovered.id;
}
this.logger.verbose(`New conversation created with ID: ${conversation.id}`);
this.cache.set(cacheKey, conversation.id);
return conversation.id;
} catch (error) {
this.logger.error(`Error in createConversation: ${error}`);
this.logger.error("[_createConversation] Nao recuperou conversa, repassando erro");
throw err;
}
}
public async getInbox(instance: InstanceDto): Promise<inbox | null> {
const cacheKey = `${instance.instanceName}:getInbox`;
if (await this.cache.has(cacheKey)) {
@ -2216,8 +2263,7 @@
}
if (event === 'messages.edit') {
const editedText = `${
body?.editedMessage?.conversation || body?.editedMessage?.extendedTextMessage?.text
const editedText = `${body?.editedMessage?.conversation || body?.editedMessage?.extendedTextMessage?.text
}\n\n_\`${i18next.t('cw.message.edited')}.\`_`;
const message = await this.getMessageByKeyId(instance, body?.key?.id);
const key = message.key as {
@ -2458,17 +2504,26 @@
})
).reduce((acc: Map<string, ContactModel>, contact: ContactModel) => acc.set(contact.id, contact), new Map());
recentContacts.forEach(async (contact) => {
if (contactsWithProfilePicture.has(contact.identifier)) {
client.contacts.update({
for (const c of recentContacts) {
const pic = contactsWithProfilePicture.get(c.identifier);
if (!pic) continue;
try {
await client.contacts.update({
accountId: this.provider.accountId,
id: contact.id,
id: c.id,
data: {
avatar_url: contactsWithProfilePicture.get(contact.identifier).profilePictureUrl || null,
avatar_url: pic.profilePictureUrl || null,
},
});
this.logger.verbose(`Avatar atualizado para o contato ${c.id}`);
} catch (err) {
this.logger.error(`Falha ao atualizar avatar do contato ${c.id}: ${err}`);
}
});
}
} catch (error) {
this.logger.error(`Error on update avatar in recent conversations: ${error.toString()}`);
}

View File

@ -93,8 +93,8 @@ class ChatwootImport {
return 0;
}
let contactsChunk: Contact[] = this.sliceIntoChunks(contacts, 3000);
while (contactsChunk.length > 0) {
const contactBatches = this.sliceIntoChunks(contacts, 3000);
for (const contactsChunk of contactBatches) {
const labelSql = `SELECT id FROM labels WHERE title = '${provider.nameInbox}' AND account_id = ${provider.accountId} LIMIT 1`;
let labelId = (await pgClient.query(labelSql))?.rows[0]?.id;
@ -158,7 +158,6 @@ class ChatwootImport {
await pgClient.query(sqlInsertLabel, [tagId, 'Contact', 'labels']);
contactsChunk = this.sliceIntoChunks(contacts, 3000);
}
this.deleteHistoryContacts(instance);
@ -265,9 +264,9 @@ class ChatwootImport {
// Processamento das mensagens em batches
const batchSize = 4000;
let messagesChunk: Message[] = this.sliceIntoChunks(messagesOrdered, batchSize);
let messagesChunks = this.sliceIntoChunks(messagesOrdered, batchSize);
let batchNumber = 1;
while (messagesChunk.length > 0) {
for (const messagesChunk of messagesChunks) {
this.logger.info(
`[importHistoryMessages] Processando batch ${batchNumber} com ${messagesChunk.length} mensagens.`
);
@ -350,7 +349,7 @@ class ChatwootImport {
}
}
batchNumber++;
messagesChunk = this.sliceIntoChunks(messagesOrdered, batchSize);
}
this.deleteHistoryMessages(instance);
@ -367,7 +366,7 @@ class ChatwootImport {
this.logger.info(
`[importHistoryMessages] Iniciando importação de contatos do histórico para a instância "${instance.instanceName}".`
);
this.importHistoryContacts(instance, providerData);
await this.importHistoryContacts(instance, providerData);
this.logger.info(
`[importHistoryMessages] Concluída a importação de mensagens para a instância "${instance.instanceName}". Total importado: ${totalMessagesImported}.`
@ -416,136 +415,154 @@ class ChatwootImport {
): Promise<Map<string, FksChatwoot>> {
const pgClient = postgresClient.getChatwootConnection();
const resultMap = new Map<string, FksChatwoot>();
try {
// Para cada telefone presente
for (const rawPhoneNumber of messagesByPhoneNumber.keys()) {
// Obtém as duas versões normalizadas do número (com e sem nono dígito)
const [normalizedWith, normalizedWithout] = this.normalizeBrazilianPhoneNumberOptions(rawPhoneNumber);
const phoneTimestamp = phoneNumbersWithTimestamp.get(rawPhoneNumber);
if (!phoneTimestamp) {
this.logger.warn(`Timestamp não encontrado para o telefone ${rawPhoneNumber}`);
// Se preferir interromper, lance um erro:
throw new Error(`Timestamp não encontrado para o telefone ${rawPhoneNumber}`);
for (const rawPhone of messagesByPhoneNumber.keys()) {
// 1) Normalizar telefone e gerar JIDs
const [normalizedWith, normalizedWithout] =
this.normalizeBrazilianPhoneNumberOptions(rawPhone);
const jidWith = normalizedWith.replace(/^\+/, '') + '@s.whatsapp.net';
const jidWithout = normalizedWithout.replace(/^\+/, '') + '@s.whatsapp.net';
const ts = phoneNumbersWithTimestamp.get(rawPhone);
if (!ts) {
this.logger.warn(`Timestamp não encontrado para ${rawPhone}`);
throw new Error(`Timestamp não encontrado para ${rawPhone}`);
}
// --- Etapa 1: Buscar ou Inserir o Contato ---
let contact;
try {
this.logger.verbose(`Buscando contato para: ${normalizedWith} OU ${normalizedWithout}`);
const selectContactQuery = `
// 2) Buscar ou inserir Contact (agora incluindo identifier)
let contact: { id: number; phone_number: string };
{
const selectContact = `
SELECT id, phone_number
FROM contacts
WHERE account_id = $1
AND (phone_number = $2 OR phone_number = $3)
AND (
phone_number = $2
OR phone_number = $3
OR identifier = $4
OR identifier = $5
)
LIMIT 1
`;
const contactRes = await pgClient.query(selectContactQuery, [
const res = await pgClient.query(selectContact, [
provider.accountId,
normalizedWith,
normalizedWithout
normalizedWithout,
jidWith,
jidWithout
]);
if (contactRes.rowCount > 0) {
contact = contactRes.rows[0];
this.logger.verbose(`Contato encontrado: ${JSON.stringify(contact)}`);
if (res.rowCount) {
contact = res.rows[0];
this.logger.verbose(`Contato existente: ${JSON.stringify(contact)}`);
} else {
this.logger.verbose(`Contato não encontrado. Inserindo novo contato para ${normalizedWith}`);
const insertContactQuery = `
INSERT INTO contacts (name, phone_number, account_id, identifier, created_at, updated_at)
VALUES (REPLACE($2, '+', ''), $2, $1, CONCAT(REPLACE($2, '+', ''), '@s.whatsapp.net'),
to_timestamp($3), to_timestamp($4))
const insertContact = `
INSERT INTO contacts
(name, phone_number, account_id, identifier, created_at, updated_at)
VALUES
(
REPLACE($2, '+', ''),
$2,
$1,
$5, -- agora é $5
to_timestamp($3),
to_timestamp($4)
)
RETURNING id, phone_number
`;
const insertRes = await pgClient.query(insertContactQuery, [
provider.accountId,
normalizedWith,
phoneTimestamp.first,
phoneTimestamp.last,
const insertRes = await pgClient.query(insertContact, [
provider.accountId, // $1
normalizedWith, // $2
ts.first, // $3
ts.last, // $4
jidWith // $5
]);
contact = insertRes.rows[0];
this.logger.verbose(`Novo contato inserido: ${JSON.stringify(contact)}`);
this.logger.verbose(`Contato inserido: ${JSON.stringify(contact)}`);
}
} catch (error) {
this.logger.error(`Erro ao recuperar/inserir contato para ${rawPhoneNumber}: ${error}`);
throw error;
}
// --- Etapa 2: Buscar ou Inserir a Conversa (e o Contact_inboxes) ---
let conversation;
try {
this.logger.verbose(`Buscando conversa para o contato (ID: ${contact.id}) na caixa ${inbox.id}`);
const selectConversationQuery = `
SELECT con.id AS conversation_id, con.contact_id
FROM conversations con
JOIN contact_inboxes ci ON ci.contact_id = con.contact_id AND ci.inbox_id = $2
WHERE con.account_id = $1 AND con.inbox_id = $2 AND con.contact_id = $3
LIMIT 1
`;
const convRes = await pgClient.query(selectConversationQuery, [provider.accountId, inbox.id, contact.id]);
if (convRes.rowCount > 0) {
conversation = convRes.rows[0];
this.logger.verbose(`Conversa encontrada: ${JSON.stringify(conversation)}`);
} else {
this.logger.verbose(`Nenhuma conversa encontrada para o contato ${contact.id}. Verificando contact_inboxes.`);
// 3) Buscar ou inserir ContactInbox
let contactInboxId: number;
const selectContactInboxQuery = `
{
const selectCi = `
SELECT id
FROM contact_inboxes
WHERE contact_id = $1 AND inbox_id = $2
WHERE contact_id = $1
AND inbox_id = $2
LIMIT 1
`;
const ciRes = await pgClient.query(selectContactInboxQuery, [contact.id, inbox.id]);
if (ciRes.rowCount > 0) {
const ciRes = await pgClient.query(selectCi, [
contact.id,
inbox.id
]);
if (ciRes.rowCount) {
contactInboxId = ciRes.rows[0].id;
this.logger.verbose(`contact_inbox encontrado: ${contactInboxId}`);
this.logger.verbose(`Contact_inbox existente: ${contactInboxId}`);
} else {
this.logger.verbose(`Contact_inbox não encontrado para o contato ${contact.id}. Inserindo novo contact_inbox.`);
const insertContactInboxQuery = `
INSERT INTO contact_inboxes (contact_id, inbox_id, source_id, created_at, updated_at)
VALUES ($1, $2, gen_random_uuid(), NOW(), NOW())
const insertCi = `
INSERT INTO contact_inboxes
(contact_id, inbox_id, source_id, created_at, updated_at)
VALUES
($1, $2, gen_random_uuid(), NOW(), NOW())
RETURNING id
`;
const ciInsertRes = await pgClient.query(insertContactInboxQuery, [contact.id, inbox.id]);
contactInboxId = ciInsertRes.rows[0].id;
this.logger.verbose(`Novo contact_inbox inserido com ID: ${contactInboxId}`);
const insertRes = await pgClient.query(insertCi, [
contact.id,
inbox.id
]);
contactInboxId = insertRes.rows[0].id;
this.logger.verbose(`Contact_inbox inserido: ${contactInboxId}`);
}
}
this.logger.verbose(`Inserindo conversa para o contato ${contact.id} com contact_inbox ${contactInboxId}`);
const insertConversationQuery = `
// 4) Buscar ou inserir Conversation
let conversationId: number;
{
const selectConv = `
SELECT id
FROM conversations
WHERE account_id = $1
AND inbox_id = $2
AND contact_id = $3
LIMIT 1
`;
const convRes = await pgClient.query(selectConv, [
provider.accountId,
inbox.id,
contact.id
]);
if (convRes.rowCount) {
conversationId = convRes.rows[0].id;
this.logger.verbose(`Conversa existente: ${conversationId}`);
} else {
const insertConv = `
INSERT INTO conversations
(account_id, inbox_id, status, contact_id, contact_inbox_id, uuid, last_activity_at, created_at, updated_at)
(account_id, inbox_id, status, contact_id, contact_inbox_id, uuid,
last_activity_at, created_at, updated_at)
VALUES
($1, $2, 0, $3, $4, gen_random_uuid(), NOW(), NOW(), NOW())
RETURNING id AS conversation_id, contact_id
RETURNING id
`;
const convInsertRes = await pgClient.query(insertConversationQuery, [
const insertRes = await pgClient.query(insertConv, [
provider.accountId,
inbox.id,
contact.id,
contactInboxId,
contactInboxId
]);
conversation = convInsertRes.rows[0];
this.logger.verbose(`Nova conversa inserida: ${JSON.stringify(conversation)}`);
conversationId = insertRes.rows[0].id;
this.logger.verbose(`Conversa inserida: ${conversationId}`);
}
} catch (error) {
this.logger.error(`Erro ao recuperar/inserir conversa para o contato ${contact.id}: ${error}`);
throw error;
}
// --- Etapa 3: Mapeia o resultado para o Map ---
const fks: FksChatwoot = {
resultMap.set(rawPhone, {
phone_number: normalizedWith,
contact_id: contact.id,
conversation_id: conversation.conversation_id || conversation.id
};
resultMap.set(normalizedWith, fks);
this.logger.verbose(`Resultado mapeado para ${normalizedWith}: ${JSON.stringify(fks)}`);
} // fim for
} catch (error) {
this.logger.error(`Erro geral no processamento: ${error}`);
throw error; // Propaga o erro para que o método pare
contact_id: String(contact.id),
conversation_id: String(conversationId)
});
}
return resultMap;
}
@ -567,8 +584,6 @@ class ChatwootImport {
@ -681,8 +696,12 @@ class ChatwootImport {
}
}
public sliceIntoChunks(arr: any[], chunkSize: number) {
return arr.splice(0, chunkSize);
public sliceIntoChunks<T>(arr: T[], chunkSize: number): T[][] {
const chunks: T[][] = [];
for (let i = 0; i < arr.length; i += chunkSize) {
chunks.push(arr.slice(i, i + chunkSize));
}
return chunks;
}
public isGroup(remoteJid: string) {

View File

@ -40,7 +40,8 @@ const bucketExists = async () => {
};
const setBucketPolicy = async () => {
if (minioClient) {
if (!minioClient) return;
const policy = {
Version: '2012-10-17',
Statement: [
@ -52,10 +53,25 @@ const setBucketPolicy = async () => {
},
],
};
try {
await minioClient.setBucketPolicy(bucketName, JSON.stringify(policy));
console.log(`[S3 Service] Bucket policy aplicada em ${bucketName}`);
} catch (err: any) {
// MinIO não implementa esse endpoint
if (err.code === 'NotImplemented') {
console.warn(
`[S3 Service] setBucketPolicy não suportado por este endpoint, ignorando (bucket=${bucketName})`
);
} else {
// qualquer outro erro real, relança
console.error('[S3 Service] Erro ao aplicar bucket policy', err);
throw err;
}
}
};
const createBucket = async () => {
if (minioClient) {
try {

View File

@ -42,7 +42,10 @@ export class CacheService {
if (!this.cache) {
return;
}
this.cache.set(key, value, ttl);
const effectiveTtl = ttl ?? (2 * 60 * 60);
this.cache.set(key, value, effectiveTtl);
}
public async hSet(key: string, field: string, value: any) {
@ -69,6 +72,20 @@ export class CacheService {
if (!this.cache) {
return;
}
// Verifica se a chave é realmente uma string
if (typeof key !== 'string') {
this.logger.error(
`Invalid cache key type: expected string but received ${typeof key}. Key content: ${JSON.stringify(key)}. Stack trace: ${new Error().stack}`
);
} else {
// Opcional: se a chave contiver quebras de linha, pode ser um sinal de que há um vCard em vez de um simples identificador
if (key.includes('\n')) {
this.logger.error(
`Invalid cache key format (contains newline characters): ${key}. Stack trace: ${new Error().stack}`
);
}
}
// Chama a implementação real do delete
return this.cache.delete(key);
}