fix: improve error handling for existing contacts and simplify remoteJid processing

This commit is contained in:
Vitordotpy 2025-11-28 16:32:06 -03:00
parent baff4e8f5e
commit faed3f4574

View File

@ -346,12 +346,8 @@ export class ChatwootService {
return contact; return contact;
} catch (error) { } catch (error) {
if ( if ((error.status === 422 || error.response?.status === 422) && jid) {
(error.status === 422 || error.response?.status === 422) && this.logger.warn(`Contact with identifier ${jid} creation failed (422). Checking if it already exists...`);
(error.message?.includes('taken') || error.response?.data?.message?.includes('taken')) &&
jid
) {
this.logger.warn(`Contact with identifier ${jid} already exists, trying to find it...`);
const existingContact = await this.findContactByIdentifier(instance, jid); const existingContact = await this.findContactByIdentifier(instance, jid);
if (existingContact) { if (existingContact) {
const contactId = existingContact.id; const contactId = existingContact.id;
@ -2535,7 +2531,7 @@ export class ChatwootService {
if (!remoteJid) { if (!remoteJid) {
return ''; return '';
} }
return remoteJid.replace(/:\d+/, '').replace('@s.whatsapp.net', '').replace('@g.us', '').replace('@lid', ''); return remoteJid.replace(/:\d+/, '').split('@')[0];
} }
public startImportHistoryMessages(instance: InstanceDto) { public startImportHistoryMessages(instance: InstanceDto) {