diff options
| author | Rafi Zadanly <zadanlyr@gmail.com> | 2023-02-27 17:01:50 +0700 |
|---|---|---|
| committer | Rafi Zadanly <zadanlyr@gmail.com> | 2023-02-27 17:01:50 +0700 |
| commit | 241df23ed0bc2e0aefdc032900753b04399d190d (patch) | |
| tree | 1d80d9e214258474c726d5559c1bc43c17341902 /indoteknik_custom/models/wati_contact.py | |
| parent | 3b2f11da025094d676b2eea0b32794852dc9f748 (diff) | |
Wati update contact attribute
Diffstat (limited to 'indoteknik_custom/models/wati_contact.py')
| -rw-r--r-- | indoteknik_custom/models/wati_contact.py | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/indoteknik_custom/models/wati_contact.py b/indoteknik_custom/models/wati_contact.py new file mode 100644 index 00000000..bdef0e04 --- /dev/null +++ b/indoteknik_custom/models/wati_contact.py @@ -0,0 +1,55 @@ +from odoo import models +import logging +import math + +_logger = logging.getLogger('[Update Attribute Wati Contact]') + + +class WatiContact(models.Model): + _name = 'wati.contact' + + def update_attribute_wati_contact(self): + wati_api = self.env['wati.api'] + res_partner = self.env['res.partner'] + total_synced = 0 + + has_next = True + page_number = 0 + while has_next: + page_number += 1 + wati_contacts = wati_api.http_get('/api/v1/getContacts', { + 'pageSize': 1, + 'pageNumber': page_number + }) + if wati_contacts['result'] != 'success': + continue + page_size = wati_contacts['link']['pageSize'] + total = wati_contacts['link']['total'] + page_total = math.ceil(total / page_size) + offset = (page_number - 1) * page_size + current = 0 + _logger.info('=== Page: %s | Page Total: %s' % (str(page_number), str(page_total))) + for contact in wati_contacts['contact_list']: + current += 1 + _logger.info('= Process: %s/%s' % (str(offset + current), str(total))) + params = [('mobile', '=', contact['phone'])] + partner = res_partner.search(params, limit=1) + if not partner: + continue + + company = partner.parent_id or partner + attributes = [{'name': 'perusahaan', 'value': company.name}] + email = partner.email or company.email + if email: + attributes.append({'name': 'email', 'value': email}) + sales = partner.user_id or company.user_id + if sales: + attributes.append({'name': 'sales', 'value': sales.name}) + attributes.append({'name': 'sync', 'value': 'true'}) + + endpoint = '/api/v1/updateContactAttributes/%s' % contact['phone'] + wati_api.http_post(endpoint, {'customParams': attributes}) + total_synced += 1 + has_next = wati_contacts['link']['nextPage'] is not None + _logger.info('=== Total Synced: %s ===' % str(total_synced)) + return |
