From 01f7037a38147a82223372086cec7ad8765b4f32 Mon Sep 17 00:00:00 2001 From: Carlos Garcia Date: Fri, 24 Apr 2026 22:20:09 -0400 Subject: [PATCH] fix(addon): create AI bot as internal user; use post_migrate_hook - Bot needs to be a res.users to appear in Discuss DM search - post_migrate_hook runs on both install and -u update - Idempotent: skips creation if user already exists Co-Authored-By: Claude Sonnet 4.6 --- addons/activeblue_ai/__init__.py | 36 +++++++++++++++------------- addons/activeblue_ai/__manifest__.py | 3 ++- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/addons/activeblue_ai/__init__.py b/addons/activeblue_ai/__init__.py index 83cffb9..0df9f4b 100644 --- a/addons/activeblue_ai/__init__.py +++ b/addons/activeblue_ai/__init__.py @@ -4,20 +4,22 @@ import logging _logger = logging.getLogger(__name__) -def _create_ai_bot_partner(env): - """Create the ActiveBlue AI bot partner via ORM so all field defaults are applied.""" - XID = 'activeblue_ai.partner_activeblue_ai' - if not env.ref(XID, raise_if_not_found=False): - partner = env['res.partner'].create({ - 'name': 'ActiveBlue AI', - 'active': True, - 'partner_share': False, - }) - env['ir.model.data'].create({ - 'module': 'activeblue_ai', - 'name': 'partner_activeblue_ai', - 'model': 'res.partner', - 'res_id': partner.id, - 'noupdate': True, - }) - _logger.info('Created ActiveBlue AI bot partner id=%d', partner.id) +def _ensure_ai_bot_user(env): + """Create the ActiveBlue AI internal user so it appears in Discuss DM search.""" + if env['res.users'].search([('login', '=', 'activeblue_ai_bot')]): + return + user = env['res.users'].create({ + 'name': 'ActiveBlue AI', + 'login': 'activeblue_ai_bot', + 'groups_id': [(6, 0, [env.ref('base.group_user').id])], + 'share': False, + 'active': True, + }) + env['ir.model.data'].create({ + 'module': 'activeblue_ai', + 'name': 'partner_activeblue_ai', + 'model': 'res.partner', + 'res_id': user.partner_id.id, + 'noupdate': True, + }) + _logger.info('Created ActiveBlue AI bot user id=%d', user.id) diff --git a/addons/activeblue_ai/__manifest__.py b/addons/activeblue_ai/__manifest__.py index 54ef607..306adcb 100644 --- a/addons/activeblue_ai/__manifest__.py +++ b/addons/activeblue_ai/__manifest__.py @@ -34,5 +34,6 @@ CRM, sales, project management, eLearning, expenses, and HR. 'installable': True, 'application': True, 'auto_install': False, - 'post_init_hook': '_create_ai_bot_partner', + 'post_init_hook': '_ensure_ai_bot_user', + 'post_migrate_hook': '_ensure_ai_bot_user', }