diff --git a/fusion-woo-odoo/fusion_woocommerce/models/woo_sync_log.py b/fusion-woo-odoo/fusion_woocommerce/models/woo_sync_log.py index 39ccadd8..aa64f480 100644 --- a/fusion-woo-odoo/fusion_woocommerce/models/woo_sync_log.py +++ b/fusion-woo-odoo/fusion_woocommerce/models/woo_sync_log.py @@ -1,5 +1,12 @@ +import logging + from odoo import api, fields, models +_logger = logging.getLogger(__name__) + +SUCCESS_RETENTION_DAYS = 30 +ERROR_RETENTION_DAYS = 90 + class WooSyncLog(models.Model): _name = 'woo.sync.log' @@ -32,11 +39,50 @@ class WooSyncLog(models.Model): @api.model def _cron_cleanup_logs(self): - """Purge sync logs older than 90 days (180 for errors).""" - cutoff_success = fields.Datetime.subtract(fields.Datetime.now(), hours=90 * 24) - cutoff_error = fields.Datetime.subtract(fields.Datetime.now(), hours=180 * 24) - self.search([ + """Purge success/conflict logs older than 30 days, errors older than 90.""" + now = fields.Datetime.now() + cutoff_success = fields.Datetime.subtract(now, days=SUCCESS_RETENTION_DAYS) + cutoff_error = fields.Datetime.subtract(now, days=ERROR_RETENTION_DAYS) + logs = self.search([ '|', '&', ('state', '!=', 'failed'), ('create_date', '<', cutoff_success), '&', ('state', '=', 'failed'), ('create_date', '<', cutoff_error), - ]).unlink() + ]) + count = len(logs) + if count: + logs.unlink() + _logger.info("WooCommerce: purged %d old sync log entries", count) + + def action_purge_old_logs(self): + """Manual purge: delete success logs > 7 days, error logs > 30 days.""" + self.env['woo.sync.log'].check_access_rights('unlink') + now = fields.Datetime.now() + cutoff_success = fields.Datetime.subtract(now, days=7) + cutoff_error = fields.Datetime.subtract(now, days=30) + logs = self.env['woo.sync.log'].search([ + '|', + '&', ('state', '!=', 'failed'), ('create_date', '<', cutoff_success), + '&', ('state', '=', 'failed'), ('create_date', '<', cutoff_error), + ]) + count = len(logs) + logs.unlink() + return { + 'type': 'ir.actions.client', + 'tag': 'display_notification', + 'params': { + 'title': 'Sync Logs Purged', + 'message': f'{count} old log entries deleted.', + 'type': 'success', + 'next': {'type': 'ir.actions.act_window_close'}, + }, + } + + @api.model + def action_clear_errors(self): + """Clear all failed sync log entries. Called from dashboard.""" + self.check_access_rights('unlink') + logs = self.search([('state', '=', 'failed')]) + count = len(logs) + logs.unlink() + _logger.info("WooCommerce: manually cleared %d error log entries", count) + return count diff --git a/fusion-woo-odoo/fusion_woocommerce/static/src/css/woo_styles.css b/fusion-woo-odoo/fusion_woocommerce/static/src/css/woo_styles.css index 51cc58eb..650693a9 100644 --- a/fusion-woo-odoo/fusion_woocommerce/static/src/css/woo_styles.css +++ b/fusion-woo-odoo/fusion_woocommerce/static/src/css/woo_styles.css @@ -406,3 +406,6 @@ outline: none; } .woo-edit-input-text { text-align: left; width: 120px; } + +/* Clear errors button inside dashboard card */ +.woo-clear-btn { font-size: 0.72rem; padding: 1px 8px; } diff --git a/fusion-woo-odoo/fusion_woocommerce/static/src/js/dashboard.js b/fusion-woo-odoo/fusion_woocommerce/static/src/js/dashboard.js index 712cf82e..68d8cb5f 100644 --- a/fusion-woo-odoo/fusion_woocommerce/static/src/js/dashboard.js +++ b/fusion-woo-odoo/fusion_woocommerce/static/src/js/dashboard.js @@ -199,12 +199,23 @@ export class WooDashboard extends Component { type: "ir.actions.act_window", name: "Sync Errors (Last 24 h)", res_model: "woo.sync.log", - view_mode: "list,form", + views: [[false, "list"], [false, "form"]], domain: [["state", "=", "failed"]], target: "current", }); } + async clearErrors() { + const count = await rpc("/web/dataset/call_kw", { + model: "woo.sync.log", + method: "action_clear_errors", + args: [], + kwargs: {}, + }); + this.state.errors24h = 0; + this.notification.add(`${count} error log entries cleared.`, { type: "success" }); + } + openConflicts() { this.actionService.doAction("fusion_woocommerce.action_woo_conflict"); } diff --git a/fusion-woo-odoo/fusion_woocommerce/static/src/xml/dashboard.xml b/fusion-woo-odoo/fusion_woocommerce/static/src/xml/dashboard.xml index 3c4e5337..df5acb3c 100644 --- a/fusion-woo-odoo/fusion_woocommerce/static/src/xml/dashboard.xml +++ b/fusion-woo-odoo/fusion_woocommerce/static/src/xml/dashboard.xml @@ -50,7 +50,16 @@