File size: 4,063 Bytes
4327358
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import ChatwootClient, {
  contact_update,
  public_contact_create_update_payload,
} from '@figuro/chatwoot-sdk';
import type { contact } from '@figuro/chatwoot-sdk/dist/models/contact';
import type { generic_id } from '@figuro/chatwoot-sdk/dist/models/generic_id';
import { ILogger } from '@waha/apps/app_sdk/ILogger';
import {
  ChatWootAPIConfig,
  ChatWootInboxAPI,
} from '@waha/apps/chatwoot/client/interfaces';
import { isJidCusFormat } from '@waha/utils/wa';
import * as lodash from 'lodash';

import { AttributeKey } from '../const';

export interface ContactResponse {
  data: generic_id & contact;
  sourceId: string;
}

export class ContactAPI {
  constructor(
    private config: ChatWootAPIConfig,
    private accountAPI: ChatwootClient,
    protected inboxAPI: ChatWootInboxAPI,
    private logger: ILogger,
  ) {}

  async searchByAnyID(chatId: string): Promise<ContactResponse | null> {
    const payload: any[] = [
      {
        attribute_key: AttributeKey.WA_CHAT_ID,
        filter_operator: 'equal_to',
        values: [chatId],
        attribute_model: 'standard',
        custom_attribute_type: '',
        query_operator: 'OR',
      },
      {
        attribute_key: AttributeKey.WA_JID,
        filter_operator: 'equal_to',
        values: [chatId],
        attribute_model: 'standard',
        custom_attribute_type: '',
        query_operator: 'OR',
      },
      {
        attribute_key: AttributeKey.WA_LID,
        filter_operator: 'equal_to',
        values: [chatId],
        attribute_model: 'standard',
        custom_attribute_type: '',
        query_operator: 'OR',
      },
      {
        attribute_key: 'identifier',
        filter_operator: 'equal_to',
        values: [chatId],
        attribute_model: 'standard',
        custom_attribute_type: '',
      },
    ];

    if (isJidCusFormat(chatId)) {
      // Search by phone
      let phone_number = chatId.split('@')[0];
      phone_number = phone_number.replace('+', '');
      payload[payload.length - 1].query_operator = 'OR';
      payload.push({
        attribute_key: 'phone_number',
        filter_operator: 'equal_to',
        values: [phone_number],
      });
    }

    const response: any = await this.accountAPI.contacts.filter({
      accountId: this.config.accountId,
      payload: payload as any,
    });

    const contacts = response.payload;
    if (contacts.length == 0) {
      return null;
    }
    const contact = contacts[0];
    const inboxes = lodash.filter(contact.contact_inboxes, {
      inbox: { id: this.config.inboxId },
    });
    if (inboxes.length == 0) {
      return null;
    }
    return {
      data: contact,
      sourceId: inboxes[0].source_id,
    };
  }

  public updateCustomAttributes(
    contact: generic_id & contact,
    attributes: any,
  ) {
    const update: contact_update = {
      custom_attributes: { ...contact.custom_attributes, ...attributes },
    };
    return this.accountAPI.contacts.update({
      id: contact.id,
      accountId: this.config.accountId,
      data: update,
    });
  }

  public async create(
    chatId: string,
    payload: public_contact_create_update_payload,
  ): Promise<ContactResponse> {
    const contact = await this.inboxAPI.contacts.create({
      inboxIdentifier: this.config.inboxIdentifier,
      data: payload,
    });
    this.logger.info(
      `Created contact for chat.id: ${chatId}, contact.id: ${contact.source_id}`,
    );
    const response: any = await this.accountAPI.contacts.get({
      accountId: this.config.accountId,
      id: contact.id,
    });
    return {
      data: response.payload,
      sourceId: contact.source_id,
    };
  }

  public updateAvatarUrlSafe(contactId, avatarUrl: string) {
    this.accountAPI.contacts
      .update({
        accountId: this.config.accountId,
        id: contactId,
        data: {
          avatar_url: avatarUrl,
        },
      })
      .catch((e) => {
        this.logger.warn(
          'Error updating avatar_url for contact.id: ' + contactId,
        );
        this.logger.warn(e);
      });
  }
}