summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/wati.py
diff options
context:
space:
mode:
Diffstat (limited to 'indoteknik_custom/models/wati.py')
-rw-r--r--indoteknik_custom/models/wati.py63
1 files changed, 48 insertions, 15 deletions
diff --git a/indoteknik_custom/models/wati.py b/indoteknik_custom/models/wati.py
index f3632334..a0619f83 100644
--- a/indoteknik_custom/models/wati.py
+++ b/indoteknik_custom/models/wati.py
@@ -32,28 +32,43 @@ class WatiNotification(models.Model):
]).unlink()
_logger.info('Success Cleanup WATI Notification')
- def _parse_notification(self, limit = 0):
+ def _parse_notification(self, limit=0):
domain = [('is_parsed', '=', False)]
notifications = self.search(domain, order='id', limit=limit)
notification_not_parsed_count = self.search_count(domain)
i = 0
for notification in notifications:
i += 1
- _logger.info('[Parse Notification][%s] Process: %s/%s | Not Parsed: %s' % (notification.id, i, str(limit), str(notification_not_parsed_count)))
+ _logger.info('[Parse Notification][%s] Process: %s/%s | Not Parsed: %s' %
+ (notification.id, i, str(limit), str(notification_not_parsed_count)))
+
notification_json = json.loads(notification.json_raw)
sender_name = 'Indoteknik'
if 'senderName' in notification_json:
sender_name = notification_json['senderName']
- ticket_id = notification_json['ticketId']
- date_wati = float(notification_json['timestamp'])
- date_wati = datetime.fromtimestamp(date_wati)
+ ticket_id = notification_json.get('ticketId')
+ timestamp = notification_json.get('timestamp')
+
+ if not timestamp:
+ _logger.warning('[Parse Notification][%s] Missing timestamp in notification JSON: %s' %
+ (notification.id, notification.json_raw))
+ continue # Skip this notification
+
+ try:
+ date_wati = datetime.fromtimestamp(float(timestamp))
+ except ValueError as e:
+ _logger.error('[Parse Notification][%s] Invalid timestamp format: %s. Error: %s' %
+ (notification.id, timestamp, str(e)))
+ continue
+
wati_history = self.env['wati.history'].search([('ticket_id', '=', ticket_id)], limit=1)
if wati_history:
self._create_wati_history_line(wati_history, ticket_id, sender_name, notification_json, date_wati)
else:
new_header = self._create_wati_history_header(ticket_id, sender_name, notification_json, date_wati)
self._create_wati_history_line(new_header, ticket_id, sender_name, notification_json, date_wati)
+
notification.is_parsed = True
return
@@ -217,26 +232,42 @@ class WatiHistory(models.Model):
limit = 50
wati_histories = self.env['wati.history'].search(domain, limit=limit)
count = 0
+
for wati_history in wati_histories:
count += 1
- _logger.info('[Parse Notification] Process: %s/%s' % (str(count), str(limit)))
+ _logger.info('[Parse Notification] Processing: %s/%s', count, limit)
wati_api = self.env['wati.api']
- # Perbaikan pada params 'attribute' untuk menghindari masalah "type object is not subscriptable"
+ # Perbaikan pada parameter JSON
params = {
'pageSize': 1,
'pageNumber': 1,
- 'attribute': json.dumps([{'name': "phone", 'operator': "contain", 'value': wati_history.wa_id}]),
+ 'attribute': json.dumps([
+ {'name': "phone", 'operator': "contain", 'value': wati_history.wa_id}
+ ]),
}
- wati_contacts = wati_api.http_get('/api/v1/getContacts', params)
+ try:
+ wati_contacts = wati_api.http_get('/api/v1/getContacts', params)
+ except Exception as e:
+ _logger.error('Error while calling WATI API: %s', str(e))
+ continue
+ # Validasi respons dari API
+ if not isinstance(wati_contacts, dict):
+ _logger.error('Invalid response format from WATI API: %s', wati_contacts)
+ continue
+
if wati_contacts.get('result') != 'success':
- return
+ _logger.warning('WATI API request failed with result: %s', wati_contacts.get('result'))
+ continue
contact_list = wati_contacts.get('contact_list', [])
-
+ if not contact_list:
+ _logger.info('No contacts found for WA ID: %s', wati_history.wa_id)
+ continue
+
perusahaan = email = ''
for data in contact_list:
custom_params = data.get('customParams', [])
@@ -247,12 +278,14 @@ class WatiHistory(models.Model):
perusahaan = value
elif name == 'email':
email = value
- # End inner loop
# Update wati_history fields
- wati_history.perusahaan = perusahaan
- wati_history.email = email
- wati_history.is_get_attribute = True
+ wati_history.write({
+ 'perusahaan': perusahaan,
+ 'email': email,
+ 'is_get_attribute': True,
+ })
+ _logger.info('Wati history updated: %s', wati_history.id)
# @api.onchange('last_reply_date')
# def _compute_expired_date(self):