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 | |
| parent | 3b2f11da025094d676b2eea0b32794852dc9f748 (diff) | |
Wati update contact attribute
| -rwxr-xr-x | indoteknik_custom/models/__init__.py | 1 | ||||
| -rw-r--r-- | indoteknik_custom/models/wati_api.py | 8 | ||||
| -rw-r--r-- | indoteknik_custom/models/wati_contact.py | 55 | ||||
| -rwxr-xr-x | indoteknik_custom/security/ir.model.access.csv | 1 |
4 files changed, 61 insertions, 4 deletions
diff --git a/indoteknik_custom/models/__init__.py b/indoteknik_custom/models/__init__.py index b45cd8c5..bba017e7 100755 --- a/indoteknik_custom/models/__init__.py +++ b/indoteknik_custom/models/__init__.py @@ -52,3 +52,4 @@ from . import midtrans from . import ip_lookup from . import wati from . import wati_api +from . import wati_contact diff --git a/indoteknik_custom/models/wati_api.py b/indoteknik_custom/models/wati_api.py index 156483f6..476f542d 100644 --- a/indoteknik_custom/models/wati_api.py +++ b/indoteknik_custom/models/wati_api.py @@ -19,7 +19,7 @@ class WatiApi(models.Model): def http_get(self, endpoint: str, url_params: dict): try: url_params = urllib.parse.urlencode(url_params) - url = "%s/%s?%s" % (self.api_url(), endpoint, url_params) + url = self.api_url() + endpoint + "?%s" % url_params headers = self.api_header() response = requests.get(url, headers=headers) return response.json() @@ -28,7 +28,7 @@ class WatiApi(models.Model): def http_post(self, endpoint: str, data: dict): try: - url = "%s/%s" % (self.api_url(), endpoint) + url = self.api_url() + endpoint headers = self.api_header() response = requests.post(url, headers=headers, json=data) return response.json() @@ -38,13 +38,13 @@ class WatiApi(models.Model): def _test(self, method): res = False if method == 'GET': - res = self.http_get('v1/getContacts', { + res = self.http_get('/api/v1/getContacts', { "name": 'Darren', "pageSize": 10, "pageNumber": 1 }) elif method == 'POST': - res = self.http_post('v1/updateContactAttributes/6281290696017', { + res = self.http_post('/api/v1/updateContactAttributes/6281290696017', { "customParams": [{"name": "name", "value": "Darren"}] }) 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 diff --git a/indoteknik_custom/security/ir.model.access.csv b/indoteknik_custom/security/ir.model.access.csv index 8109cefe..3f762c13 100755 --- a/indoteknik_custom/security/ir.model.access.csv +++ b/indoteknik_custom/security/ir.model.access.csv @@ -35,5 +35,6 @@ access_ip_lookup,access.ip.lookup,model_ip_lookup,,1,1,1,1 access_ip_lookup_line,access.ip.lookup.line,model_ip_lookup_line,,1,1,1,1 access_wati_notification,access.wati.notification,model_wati_notification,,1,1,1,1 access_wati_api,access.wati.api,model_wati_api,,1,1,1,1 +access_wati_contact,access.wati.contact,model_wati_contact,,1,1,1,1 access_user_company_request,access.user.company.request,model_user_company_request,,1,1,1,1 access_res_partner_company_type,access.res.partner.company_type,model_res_partner_company_type,,1,1,1,1
\ No newline at end of file |
