# -*- coding: utf-8 -*- from odoo import models, fields, api, _ class SocialCommerceDashboard(models.Model): _name = 'social.commerce.dashboard' _description = 'Social Commerce Admin Dashboard' name = fields.Char(string='Dashboard', default='Social Commerce Overview') company_id = fields.Many2one('res.company', string='Company', default=lambda self: self.env.company) # Meta Metrics meta_connection_status = fields.Selection([ ('untested', 'Not Tested'), ('connected', 'Connected'), ('error', 'Connection Error') ], string='Meta Connection Status', compute='_compute_metrics') meta_catalog_id = fields.Char(string='Meta Catalog ID', compute='_compute_metrics') meta_last_sync = fields.Datetime(string='Last Catalog Sync', compute='_compute_metrics') meta_synced_count = fields.Integer(string='Products Synced', compute='_compute_metrics') meta_pending_count = fields.Integer(string='Products Pending', compute='_compute_metrics') meta_failed_count = fields.Integer(string='Products Failed', compute='_compute_metrics') # WhatsApp Metrics whatsapp_connection_status = fields.Selection([ ('untested', 'Not Tested'), ('connected', 'Connected'), ('error', 'Connection Error') ], string='WhatsApp Connection Status', compute='_compute_metrics') whatsapp_webhook_verified = fields.Boolean(string='Webhook Verified', compute='_compute_metrics') whatsapp_phone = fields.Char(string='Business Number', compute='_compute_metrics') whatsapp_sent_count = fields.Integer(string='Notifications Sent', compute='_compute_metrics') whatsapp_failed_count = fields.Integer(string='Failed Notifications', compute='_compute_metrics') whatsapp_inbound_count = fields.Integer(string='Customer Inquiries', compute='_compute_metrics') last_inbound_message = fields.Text(string='Last Customer Message', compute='_compute_metrics') last_inbound_date = fields.Datetime(string='Last Message Date', compute='_compute_metrics') def _compute_metrics(self): queue_model = self.env['social.meta.sync.queue'] notif_model = self.env['social.whatsapp.notification'] msg_model = self.env['social.whatsapp.message'] product_model = self.env['product.product'] for rec in self: comp = rec.company_id or self.env.company # Meta rec.meta_connection_status = comp.meta_connection_status rec.meta_catalog_id = comp.meta_catalog_id or 'Not Configured' last_synced_prod = product_model.search([ ('meta_last_sync', '!=', False), ('company_id', 'in', (False, comp.id)) ], order='meta_last_sync desc', limit=1) rec.meta_last_sync = last_synced_prod.meta_last_sync if last_synced_prod else False rec.meta_synced_count = product_model.search_count([ ('meta_sync_status', '=', 'synced'), ('company_id', 'in', (False, comp.id)) ]) rec.meta_pending_count = queue_model.search_count([ ('state', 'in', ('pending', 'processing')), ('company_id', '=', comp.id) ]) rec.meta_failed_count = queue_model.search_count([ ('state', '=', 'failed'), ('company_id', '=', comp.id) ]) # WhatsApp rec.whatsapp_connection_status = comp.whatsapp_connection_status rec.whatsapp_webhook_verified = comp.whatsapp_webhook_verified rec.whatsapp_phone = comp.whatsapp_business_phone_number or 'Not Configured' rec.whatsapp_sent_count = notif_model.search_count([ ('state', 'in', ('sent', 'delivered', 'read')), ('company_id', '=', comp.id) ]) rec.whatsapp_failed_count = notif_model.search_count([ ('state', '=', 'failed'), ('company_id', '=', comp.id) ]) rec.whatsapp_inbound_count = msg_model.search_count([ ('direction', '=', 'inbound'), ('company_id', '=', comp.id) ]) last_msg = msg_model.search([ ('direction', '=', 'inbound'), ('company_id', '=', comp.id) ], order='timestamp desc', limit=1) if last_msg: rec.last_inbound_message = f"{last_msg.from_phone}: {last_msg.body or ''}" rec.last_inbound_date = last_msg.timestamp else: rec.last_inbound_message = "No incoming customer messages yet." rec.last_inbound_date = False def action_test_meta_connection(self): return self.env['social.meta.catalog'].test_connection(self.company_id) def action_test_whatsapp_connection(self): return self.env['social.whatsapp.api'].test_connection(self.company_id) def action_sync_products_now(self): return self.env['social.meta.sync.queue'].action_sync_all_published_products(self.company_id) def action_retry_failed_all(self): # 1. Retry failed Meta queue items meta_items = self.env['social.meta.sync.queue'].search([ ('state', '=', 'failed'), ('company_id', '=', self.company_id.id) ]) if meta_items: meta_items.action_retry() self.env['social.meta.sync.queue'].process_queue(limit=50) # 2. Retry failed WhatsApp notifications wa_notifs = self.env['social.whatsapp.notification'].search([ ('state', '=', 'failed'), ('company_id', '=', self.company_id.id) ]) if wa_notifs: wa_notifs.action_retry() return { 'type': 'ir.actions.client', 'tag': 'display_notification', 'params': { 'title': _("Retry Initiated"), 'message': _("Retrying %d failed Meta catalog items and %d failed WhatsApp notifications.") % (len(meta_items), len(wa_notifs)), 'type': 'success', 'sticky': False, } } def action_view_sync_logs(self): return self.env["ir.actions.actions"]._for_xml_id("dine360_meta_social.action_social_meta_sync_queue") def action_view_whatsapp_logs(self): return self.env["ir.actions.actions"]._for_xml_id("dine360_meta_social.action_social_whatsapp_notification") def action_view_inbound_messages(self): return self.env["ir.actions.actions"]._for_xml_id("dine360_meta_social.action_social_whatsapp_message")