feat: add cron jobs, sync engine scaffolding, log cleanup, and email templates

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
gsinghpal
2026-03-31 20:41:04 -04:00
parent 0ce599c4ac
commit 116c0b03bf
5 changed files with 201 additions and 1 deletions

View File

@@ -115,3 +115,94 @@ class WooInstance(models.Model):
'message': message,
'company_id': self.company_id.id,
})
# ------------------------------------------------------------------
# Cron entry points
# ------------------------------------------------------------------
@api.model
def _cron_sync_products(self):
"""Sync product prices and inventory for all connected instances."""
instances = self.search([('state', '=', 'connected'), ('sync_products', '=', True)])
for instance in instances:
try:
instance._sync_products()
except Exception as e:
_logger.error("Product sync failed for %s: %s", instance.name, str(e))
instance._log_sync('product', 'woo_to_odoo', instance.name, 'failed', str(e))
instance._notify_failure('product', str(e))
@api.model
def _cron_sync_orders(self):
"""Fetch new orders from all connected WooCommerce instances."""
instances = self.search([('state', '=', 'connected'), ('sync_orders', '=', True)])
for instance in instances:
try:
instance._sync_orders()
except Exception as e:
_logger.error("Order sync failed for %s: %s", instance.name, str(e))
instance._log_sync('order', 'woo_to_odoo', instance.name, 'failed', str(e))
instance._notify_failure('order', str(e))
@api.model
def _cron_sync_inventory(self):
"""Push inventory levels to WooCommerce for all connected instances."""
instances = self.search([('state', '=', 'connected'), ('sync_inventory', '=', True)])
for instance in instances:
try:
instance._sync_inventory()
except Exception as e:
_logger.error("Inventory sync failed for %s: %s", instance.name, str(e))
instance._log_sync('inventory', 'odoo_to_woo', instance.name, 'failed', str(e))
instance._notify_failure('inventory', str(e))
@api.model
def _cron_sync_customers(self):
"""Sync customer address updates to WooCommerce."""
instances = self.search([('state', '=', 'connected'), ('sync_customers', '=', True)])
for instance in instances:
try:
instance._sync_customers()
except Exception as e:
_logger.error("Customer sync failed for %s: %s", instance.name, str(e))
instance._log_sync('customer', 'odoo_to_woo', instance.name, 'failed', str(e))
instance._notify_failure('customer', str(e))
# ------------------------------------------------------------------
# Sync method placeholders (filled in during later tasks)
# ------------------------------------------------------------------
def _sync_products(self):
"""Sync products — implemented in Task 22."""
self.ensure_one()
_logger.info("Product sync for %s — not yet implemented", self.name)
def _sync_orders(self):
"""Sync orders — implemented in Task 20."""
self.ensure_one()
_logger.info("Order sync for %s — not yet implemented", self.name)
def _sync_inventory(self):
"""Sync inventory — implemented in Task 22."""
self.ensure_one()
_logger.info("Inventory sync for %s — not yet implemented", self.name)
def _sync_customers(self):
"""Sync customers — implemented in Task 25."""
self.ensure_one()
_logger.info("Customer sync for %s — not yet implemented", self.name)
def _notify_failure(self, sync_type, error_message):
"""Send email notification on sync failure."""
self.ensure_one()
if not self.notify_on_failure or not self.notify_user_ids:
return
template = self.env.ref(
'fusion_woocommerce.woo_sync_failure_notification', raise_if_not_found=False,
)
if template:
for user in self.notify_user_ids:
template.with_context(
sync_type=sync_type,
error_message=error_message,
).send_mail(self.id, force_send=True, email_values={'email_to': user.email})