feature(chatwoot): add merge_brazil_contacts function to solve nine digit in brazilian numbers

This commit is contained in:
Deivison Lincoln 2024-05-08 15:03:37 -03:00
parent 7a0149ee23
commit dcc32479ff
8 changed files with 53 additions and 3 deletions

View File

@ -66,6 +66,7 @@ export class InstanceController {
chatwoot_conversation_pending,
chatwoot_import_contacts,
chatwoot_name_inbox,
chatwoot_merge_brazil_contacts,
chatwoot_import_messages,
chatwoot_days_limit_import_messages,
reject_call,
@ -519,6 +520,7 @@ export class InstanceController {
reopen_conversation: chatwoot_reopen_conversation || false,
conversation_pending: chatwoot_conversation_pending || false,
import_contacts: chatwoot_import_contacts ?? true,
merge_brazil_contacts: chatwoot_merge_brazil_contacts ?? false,
import_messages: chatwoot_import_messages ?? true,
days_limit_import_messages: chatwoot_days_limit_import_messages ?? 60,
auto_create: true,

View File

@ -27,6 +27,7 @@ export class InstanceDto {
chatwoot_sign_msg?: boolean;
chatwoot_reopen_conversation?: boolean;
chatwoot_conversation_pending?: boolean;
chatwoot_merge_brazil_contacts?: boolean;
chatwoot_import_contacts?: boolean;
chatwoot_import_messages?: boolean;
chatwoot_days_limit_import_messages?: number;

View File

@ -53,6 +53,7 @@ export class ChatwootController {
data.conversation_pending = false;
data.import_contacts = false;
data.import_messages = false;
data.merge_brazil_contacts = false;
data.days_limit_import_messages = 0;
data.auto_create = false;
data.name_inbox = '';

View File

@ -9,6 +9,7 @@ export class ChatwootDto {
number?: string;
reopen_conversation?: boolean;
conversation_pending?: boolean;
merge_brazil_contacts?: boolean;
import_contacts?: boolean;
import_messages?: boolean;
days_limit_import_messages?: number;

View File

@ -14,6 +14,7 @@ export class ChatwootRaw {
number?: string;
reopen_conversation?: boolean;
conversation_pending?: boolean;
merge_brazil_contacts?: boolean;
import_contacts?: boolean;
import_messages?: boolean;
days_limit_import_messages?: number;
@ -31,6 +32,7 @@ const chatwootSchema = new Schema<ChatwootRaw>({
number: { type: String, required: true },
reopen_conversation: { type: Boolean, required: true },
conversation_pending: { type: Boolean, required: true },
merge_brazil_contacts: { type: Boolean, required: true },
import_contacts: { type: Boolean, required: true },
import_messages: { type: Boolean, required: true },
days_limit_import_messages: { type: Number, required: true },

View File

@ -86,13 +86,14 @@ export class ChatwootService {
return client;
}
public getClientCwConfig(): ChatwootAPIConfig & { name_inbox: string } {
public getClientCwConfig(): ChatwootAPIConfig & { name_inbox: string; merge_brazil_contacts: boolean } {
return {
basePath: this.provider.url,
with_credentials: true,
credentials: 'include',
token: this.provider.token,
name_inbox: this.provider.name_inbox,
merge_brazil_contacts: this.provider.merge_brazil_contacts,
};
}
@ -418,10 +419,49 @@ export class ChatwootService {
}
}
private async mergeBrazilianContacts(contacts: any[]) {
try {
//sdk chatwoot não tem função merge
this.logger.verbose('merging contacts');
const contact = await chatwootRequest(this.getClientCwConfig(), {
method: 'POST',
url: `/api/v1/accounts/${this.provider.account_id}/actions/contact_merge`,
body: {
base_contact_id: contacts.find((contact) => contact.phone_number.length === 14)?.id,
mergee_contact_id: contacts.find((contact) => contact.phone_number.length === 13)?.id,
},
});
return contact;
} catch {
this.logger.error('Error merging contacts');
return null;
}
}
private findContactInContactList(contacts: any[], query: string) {
const phoneNumbers = this.getNumbers(query);
const searchableFields = this.getSearchableFields();
// eslint-disable-next-line prettier/prettier
if(contacts.length === 2 && this.getClientCwConfig().merge_brazil_contacts && query.startsWith('+55')){
const contact = this.mergeBrazilianContacts(contacts);
if (contact) {
return contact;
}
}
const phone = phoneNumbers.reduce(
(savedNumber, number) => (number.length > savedNumber.length ? number : savedNumber),
'',
);
const contact_with9 = contacts.find((contact) => contact.phone_number === phone);
if (contact_with9) {
return contact_with9;
}
for (const contact of contacts) {
for (const field of searchableFields) {
if (contact[field] && phoneNumbers.includes(contact[field])) {
@ -449,7 +489,7 @@ export class ChatwootService {
}
private getSearchableFields() {
return ['phone_number', 'identifier'];
return ['phone_number'];
}
private getFilterPayload(query: string) {
@ -463,7 +503,7 @@ export class ChatwootService {
const queryOperator = fieldsToSearch.length - 1 === index1 && numbers.length - 1 === index2 ? null : 'OR';
filterPayload.push({
attribute_key: field,
filter_operator: ['phone_number', 'identifier'].includes(field) ? 'equal_to' : 'contains',
filter_operator: 'equal_to',
values: [number.replace('+', '')],
query_operator: queryOperator,
});

View File

@ -35,6 +35,7 @@ export const chatwootSchema: JSONSchema7 = {
conversation_pending: { type: 'boolean', enum: [true, false] },
auto_create: { type: 'boolean', enum: [true, false] },
import_contacts: { type: 'boolean', enum: [true, false] },
merge_brazil_contacts: { type: 'boolean', enum: [true, false] },
import_messages: { type: 'boolean', enum: [true, false] },
days_limit_import_messages: { type: 'number' },
},

View File

@ -356,6 +356,7 @@ export class ChannelStartupService {
this.logger.verbose(`Chatwoot sign delimiter: ${data.sign_delimiter}`);
this.logger.verbose(`Chatwoot reopen conversation: ${data.reopen_conversation}`);
this.logger.verbose(`Chatwoot conversation pending: ${data.conversation_pending}`);
this.logger.verbose(`Chatwoot merge brazilian contacts: ${data.import_contacts}`);
this.logger.verbose(`Chatwoot import contacts: ${data.import_contacts}`);
this.logger.verbose(`Chatwoot import messages: ${data.import_messages}`);
this.logger.verbose(`Chatwoot days limit import messages: ${data.days_limit_import_messages}`);
@ -370,6 +371,7 @@ export class ChannelStartupService {
sign_delimiter: data.sign_delimiter || null,
reopen_conversation: data.reopen_conversation,
conversation_pending: data.conversation_pending,
merge_brazil_contacts: data.merge_brazil_contacts,
import_contacts: data.import_contacts,
import_messages: data.import_messages,
days_limit_import_messages: data.days_limit_import_messages,