Skip to content

Commit 14d10c0

Browse files
committed
chore: chatwoot verbose logs
1 parent 88c1830 commit 14d10c0

File tree

3 files changed

+56
-19
lines changed

3 files changed

+56
-19
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* Update baileys version
1919
* Update in Baileys version that fixes timeout when updating profile picture
2020
* Adjusts for fix timeout error on send status message
21+
* Chatwoot verbose logs
2122

2223
# 2.1.1 (2024-09-22 10:31)
2324

src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,10 +1195,11 @@ export class BaileysStartupService extends ChannelStartupService {
11951195
);
11961196
}
11971197

1198-
this.prismaRepository.contact.updateMany({
1199-
where: { remoteJid: received.key.remoteJid, instanceId: this.instanceId },
1200-
data: contactRaw,
1201-
});
1198+
if (this.configService.get<Database>('DATABASE').SAVE_DATA.CONTACTS)
1199+
await this.prismaRepository.contact.create({
1200+
data: contactRaw,
1201+
});
1202+
12021203
return;
12031204
}
12041205

src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -538,26 +538,37 @@ export class ChatwootService {
538538

539539
public async createConversation(instance: InstanceDto, body: any) {
540540
try {
541+
this.logger.verbose('--- Start createConversation ---');
542+
this.logger.verbose(`Instance: ${JSON.stringify(instance)}`);
543+
this.logger.verbose(`Body: ${JSON.stringify(body)}`);
544+
541545
const client = await this.clientCw(instance);
542546

543547
if (!client) {
544-
this.logger.warn('client not found');
548+
this.logger.warn(`Client not found for instance: ${JSON.stringify(instance)}`);
545549
return null;
546550
}
547551

548552
const cacheKey = `${instance.instanceName}:createConversation-${body.key.remoteJid}`;
553+
this.logger.verbose(`Cache key: ${cacheKey}`);
554+
549555
if (await this.cache.has(cacheKey)) {
556+
this.logger.verbose(`Cache hit for key: ${cacheKey}`);
550557
const conversationId = (await this.cache.get(cacheKey)) as number;
558+
this.logger.verbose(`Cached conversation ID: ${conversationId}`);
551559
let conversationExists: conversation | boolean;
552560
try {
553561
conversationExists = await client.conversations.get({
554562
accountId: this.provider.accountId,
555563
conversationId: conversationId,
556564
});
565+
this.logger.verbose(`Conversation exists: ${JSON.stringify(conversationExists)}`);
557566
} catch (error) {
567+
this.logger.error(`Error getting conversation: ${error}`);
558568
conversationExists = false;
559569
}
560570
if (!conversationExists) {
571+
this.logger.verbose('Conversation does not exist, re-calling createConversation');
561572
this.cache.delete(cacheKey);
562573
return await this.createConversation(instance, body);
563574
}
@@ -566,30 +577,37 @@ export class ChatwootService {
566577
}
567578

568579
const isGroup = body.key.remoteJid.includes('@g.us');
580+
this.logger.verbose(`Is group: ${isGroup}`);
569581

570582
const chatId = isGroup ? body.key.remoteJid : body.key.remoteJid.split('@')[0];
583+
this.logger.verbose(`Chat ID: ${chatId}`);
571584

572585
let nameContact: string;
573586

574587
nameContact = !body.key.fromMe ? body.pushName : chatId;
588+
this.logger.verbose(`Name contact: ${nameContact}`);
575589

576590
const filterInbox = await this.getInbox(instance);
577591

578592
if (!filterInbox) {
579-
this.logger.warn('inbox not found');
593+
this.logger.warn(`Inbox not found for instance: ${JSON.stringify(instance)}`);
580594
return null;
581595
}
582596

583597
if (isGroup) {
598+
this.logger.verbose('Processing group conversation');
584599
const group = await this.waMonitor.waInstances[instance.instanceName].client.groupMetadata(chatId);
600+
this.logger.verbose(`Group metadata: ${JSON.stringify(group)}`);
585601

586602
nameContact = `${group.subject} (GROUP)`;
587603

588604
const picture_url = await this.waMonitor.waInstances[instance.instanceName].profilePicture(
589605
body.key.participant.split('@')[0],
590606
);
607+
this.logger.verbose(`Participant profile picture URL: ${JSON.stringify(picture_url)}`);
591608

592609
const findParticipant = await this.findContact(instance, body.key.participant.split('@')[0]);
610+
this.logger.verbose(`Found participant: ${JSON.stringify(findParticipant)}`);
593611

594612
if (findParticipant) {
595613
if (!findParticipant.name || findParticipant.name === chatId) {
@@ -612,8 +630,10 @@ export class ChatwootService {
612630
}
613631

614632
const picture_url = await this.waMonitor.waInstances[instance.instanceName].profilePicture(chatId);
633+
this.logger.verbose(`Contact profile picture URL: ${JSON.stringify(picture_url)}`);
615634

616635
let contact = await this.findContact(instance, chatId);
636+
this.logger.verbose(`Found contact: ${JSON.stringify(contact)}`);
617637

618638
if (contact) {
619639
if (!body.key.fromMe) {
@@ -630,8 +650,10 @@ export class ChatwootService {
630650
)
631651
: false);
632652

633-
const contactNeedsUpdate = pictureNeedsUpdate || nameNeedsUpdate;
634-
if (contactNeedsUpdate) {
653+
this.logger.verbose(`Picture needs update: ${pictureNeedsUpdate}`);
654+
this.logger.verbose(`Name needs update: ${nameNeedsUpdate}`);
655+
656+
if (pictureNeedsUpdate || nameNeedsUpdate) {
635657
contact = await this.updateContact(instance, contact.id, {
636658
...(nameNeedsUpdate && { name: nameContact }),
637659
...(waProfilePictureFile === '' && { avatar: null }),
@@ -653,38 +675,50 @@ export class ChatwootService {
653675
}
654676

655677
if (!contact) {
656-
this.logger.warn('contact not found');
678+
this.logger.warn('Contact not created or found');
657679
return null;
658680
}
659681

660682
const contactId = contact?.payload?.id || contact?.payload?.contact?.id || contact?.id;
683+
this.logger.verbose(`Contact ID: ${contactId}`);
661684

662685
const contactConversations = (await client.contacts.listConversations({
663686
accountId: this.provider.accountId,
664687
id: contactId,
665688
})) as any;
689+
this.logger.verbose(`Contact conversations: ${JSON.stringify(contactConversations)}`);
690+
691+
if (!contactConversations || !contactConversations.payload) {
692+
this.logger.error('No conversations found or payload is undefined');
693+
return null;
694+
}
666695

667-
if (contactConversations?.payload?.length) {
696+
if (contactConversations.payload.length) {
668697
let conversation: any;
669698
if (this.provider.reopenConversation) {
670699
conversation = contactConversations.payload.find((conversation) => conversation.inbox_id == filterInbox.id);
700+
this.logger.verbose(`Found conversation in reopenConversation mode: ${JSON.stringify(conversation)}`);
671701

672702
if (this.provider.conversationPending) {
673-
await client.conversations.toggleStatus({
674-
accountId: this.provider.accountId,
675-
conversationId: conversation.id,
676-
data: {
677-
status: 'pending',
678-
},
679-
});
703+
if (conversation) {
704+
await client.conversations.toggleStatus({
705+
accountId: this.provider.accountId,
706+
conversationId: conversation.id,
707+
data: {
708+
status: 'pending',
709+
},
710+
});
711+
}
680712
}
681713
} else {
682714
conversation = contactConversations.payload.find(
683715
(conversation) => conversation.status !== 'resolved' && conversation.inbox_id == filterInbox.id,
684716
);
717+
this.logger.verbose(`Found conversation: ${JSON.stringify(conversation)}`);
685718
}
686719

687720
if (conversation) {
721+
this.logger.verbose(`Returning existing conversation ID: ${conversation.id}`);
688722
this.cache.set(cacheKey, conversation.id);
689723
return conversation.id;
690724
}
@@ -705,14 +739,15 @@ export class ChatwootService {
705739
});
706740

707741
if (!conversation) {
708-
this.logger.warn('conversation not found');
742+
this.logger.warn('Conversation not created or found');
709743
return null;
710744
}
711745

746+
this.logger.verbose(`New conversation created with ID: ${conversation.id}`);
712747
this.cache.set(cacheKey, conversation.id);
713748
return conversation.id;
714749
} catch (error) {
715-
this.logger.error(error);
750+
this.logger.error(`Error in createConversation: ${error}`);
716751
}
717752
}
718753

0 commit comments

Comments
 (0)