Skip to content

Commit 9ccdb45

Browse files
Merge pull request #587 from deivisonrpg/chatwoot-merge-contact-with-nine
feature(chatwoot): add merge_brazil_contacts function to solve nine digit in brazilian numbers
2 parents f78d360 + 8875ab1 commit 9ccdb45

File tree

9 files changed

+57
-3
lines changed

9 files changed

+57
-3
lines changed

src/api/controllers/instance.controller.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export class InstanceController {
6666
chatwoot_conversation_pending,
6767
chatwoot_import_contacts,
6868
chatwoot_name_inbox,
69+
chatwoot_merge_brazil_contacts,
6970
chatwoot_import_messages,
7071
chatwoot_days_limit_import_messages,
7172
reject_call,
@@ -519,6 +520,7 @@ export class InstanceController {
519520
reopen_conversation: chatwoot_reopen_conversation || false,
520521
conversation_pending: chatwoot_conversation_pending || false,
521522
import_contacts: chatwoot_import_contacts ?? true,
523+
merge_brazil_contacts: chatwoot_merge_brazil_contacts ?? false,
522524
import_messages: chatwoot_import_messages ?? true,
523525
days_limit_import_messages: chatwoot_days_limit_import_messages ?? 60,
524526
auto_create: true,
@@ -574,6 +576,7 @@ export class InstanceController {
574576
sign_msg: chatwoot_sign_msg || false,
575577
reopen_conversation: chatwoot_reopen_conversation || false,
576578
conversation_pending: chatwoot_conversation_pending || false,
579+
merge_brazil_contacts: chatwoot_merge_brazil_contacts ?? false,
577580
import_contacts: chatwoot_import_contacts ?? true,
578581
import_messages: chatwoot_import_messages ?? true,
579582
days_limit_import_messages: chatwoot_days_limit_import_messages || 60,

src/api/dto/instance.dto.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export class InstanceDto {
2727
chatwoot_sign_msg?: boolean;
2828
chatwoot_reopen_conversation?: boolean;
2929
chatwoot_conversation_pending?: boolean;
30+
chatwoot_merge_brazil_contacts?: boolean;
3031
chatwoot_import_contacts?: boolean;
3132
chatwoot_import_messages?: boolean;
3233
chatwoot_days_limit_import_messages?: number;

src/api/integrations/chatwoot/controllers/chatwoot.controller.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export class ChatwootController {
5353
data.conversation_pending = false;
5454
data.import_contacts = false;
5555
data.import_messages = false;
56+
data.merge_brazil_contacts = false;
5657
data.days_limit_import_messages = 0;
5758
data.auto_create = false;
5859
data.name_inbox = '';

src/api/integrations/chatwoot/dto/chatwoot.dto.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export class ChatwootDto {
99
number?: string;
1010
reopen_conversation?: boolean;
1111
conversation_pending?: boolean;
12+
merge_brazil_contacts?: boolean;
1213
import_contacts?: boolean;
1314
import_messages?: boolean;
1415
days_limit_import_messages?: number;

src/api/integrations/chatwoot/models/chatwoot.model.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export class ChatwootRaw {
1414
number?: string;
1515
reopen_conversation?: boolean;
1616
conversation_pending?: boolean;
17+
merge_brazil_contacts?: boolean;
1718
import_contacts?: boolean;
1819
import_messages?: boolean;
1920
days_limit_import_messages?: number;
@@ -31,6 +32,7 @@ const chatwootSchema = new Schema<ChatwootRaw>({
3132
number: { type: String, required: true },
3233
reopen_conversation: { type: Boolean, required: true },
3334
conversation_pending: { type: Boolean, required: true },
35+
merge_brazil_contacts: { type: Boolean, required: true },
3436
import_contacts: { type: Boolean, required: true },
3537
import_messages: { type: Boolean, required: true },
3638
days_limit_import_messages: { type: Number, required: true },

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

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,14 @@ export class ChatwootService {
8686
return client;
8787
}
8888

89-
public getClientCwConfig(): ChatwootAPIConfig & { name_inbox: string } {
89+
public getClientCwConfig(): ChatwootAPIConfig & { name_inbox: string; merge_brazil_contacts: boolean } {
9090
return {
9191
basePath: this.provider.url,
9292
with_credentials: true,
9393
credentials: 'include',
9494
token: this.provider.token,
9595
name_inbox: this.provider.name_inbox,
96+
merge_brazil_contacts: this.provider.merge_brazil_contacts,
9697
};
9798
}
9899

@@ -418,10 +419,49 @@ export class ChatwootService {
418419
}
419420
}
420421

422+
private async mergeBrazilianContacts(contacts: any[]) {
423+
try {
424+
//sdk chatwoot não tem função merge
425+
this.logger.verbose('merging contacts');
426+
const contact = await chatwootRequest(this.getClientCwConfig(), {
427+
method: 'POST',
428+
url: `/api/v1/accounts/${this.provider.account_id}/actions/contact_merge`,
429+
body: {
430+
base_contact_id: contacts.find((contact) => contact.phone_number.length === 14)?.id,
431+
mergee_contact_id: contacts.find((contact) => contact.phone_number.length === 13)?.id,
432+
},
433+
});
434+
435+
return contact;
436+
} catch {
437+
this.logger.error('Error merging contacts');
438+
return null;
439+
}
440+
}
441+
421442
private findContactInContactList(contacts: any[], query: string) {
422443
const phoneNumbers = this.getNumbers(query);
423444
const searchableFields = this.getSearchableFields();
424445

446+
// eslint-disable-next-line prettier/prettier
447+
if(contacts.length === 2 && this.getClientCwConfig().merge_brazil_contacts && query.startsWith('+55')){
448+
449+
const contact = this.mergeBrazilianContacts(contacts);
450+
if (contact) {
451+
return contact;
452+
}
453+
}
454+
455+
const phone = phoneNumbers.reduce(
456+
(savedNumber, number) => (number.length > savedNumber.length ? number : savedNumber),
457+
'',
458+
);
459+
460+
const contact_with9 = contacts.find((contact) => contact.phone_number === phone);
461+
if (contact_with9) {
462+
return contact_with9;
463+
}
464+
425465
for (const contact of contacts) {
426466
for (const field of searchableFields) {
427467
if (contact[field] && phoneNumbers.includes(contact[field])) {
@@ -449,7 +489,7 @@ export class ChatwootService {
449489
}
450490

451491
private getSearchableFields() {
452-
return ['phone_number', 'identifier'];
492+
return ['phone_number'];
453493
}
454494

455495
private getFilterPayload(query: string) {
@@ -463,7 +503,7 @@ export class ChatwootService {
463503
const queryOperator = fieldsToSearch.length - 1 === index1 && numbers.length - 1 === index2 ? null : 'OR';
464504
filterPayload.push({
465505
attribute_key: field,
466-
filter_operator: ['phone_number', 'identifier'].includes(field) ? 'equal_to' : 'contains',
506+
filter_operator: 'equal_to',
467507
values: [number.replace('+', '')],
468508
query_operator: queryOperator,
469509
});

src/api/integrations/chatwoot/validate/chatwoot.schema.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export const chatwootSchema: JSONSchema7 = {
3535
conversation_pending: { type: 'boolean', enum: [true, false] },
3636
auto_create: { type: 'boolean', enum: [true, false] },
3737
import_contacts: { type: 'boolean', enum: [true, false] },
38+
merge_brazil_contacts: { type: 'boolean', enum: [true, false] },
3839
import_messages: { type: 'boolean', enum: [true, false] },
3940
days_limit_import_messages: { type: 'number' },
4041
},

src/api/services/channel.service.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ export class ChannelStartupService {
356356
this.logger.verbose(`Chatwoot sign delimiter: ${data.sign_delimiter}`);
357357
this.logger.verbose(`Chatwoot reopen conversation: ${data.reopen_conversation}`);
358358
this.logger.verbose(`Chatwoot conversation pending: ${data.conversation_pending}`);
359+
this.logger.verbose(`Chatwoot merge brazilian contacts: ${data.import_contacts}`);
359360
this.logger.verbose(`Chatwoot import contacts: ${data.import_contacts}`);
360361
this.logger.verbose(`Chatwoot import messages: ${data.import_messages}`);
361362
this.logger.verbose(`Chatwoot days limit import messages: ${data.days_limit_import_messages}`);
@@ -370,6 +371,7 @@ export class ChannelStartupService {
370371
sign_delimiter: data.sign_delimiter || null,
371372
reopen_conversation: data.reopen_conversation,
372373
conversation_pending: data.conversation_pending,
374+
merge_brazil_contacts: data.merge_brazil_contacts,
373375
import_contacts: data.import_contacts,
374376
import_messages: data.import_messages,
375377
days_limit_import_messages: data.days_limit_import_messages,

src/docs/swagger.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2244,6 +2244,9 @@ paths:
22442244
conversation_pending:
22452245
type: boolean
22462246
description: "Indicates whether to mark conversations as pending."
2247+
merge_brazil_contacts:
2248+
type: boolean
2249+
description: "Indicates whether to merge Brazil numbers in case of numbers with and without ninth digit."
22472250
import_contacts:
22482251
type: boolean
22492252
description: "Indicates whether to import contacts from phone to Chatwoot when connecting."

0 commit comments

Comments
 (0)