from odoo import models, fields, api from odoo.exceptions import UserError class PushNotificationWizard(models.TransientModel): _name = 'mapan.push.wizard' _description = 'Send Mobile App Notification' title = fields.Char(string='Notification Title', required=True) body = fields.Text(string='Notification Body', required=True) recipient_type = fields.Selection([ ('all', 'All Activated Members'), ('specific', 'Specific Members') ], string='Send To', default='all', required=True) partner_ids = fields.Many2many( 'res.partner', string='Recipients', domain=[('user_ids.share', '=', True), ('user_ids.active', '=', True)], ) def action_send_push(self): if self.recipient_type == 'specific' and not self.partner_ids: raise UserError('Please select at least one recipient, or switch to "All Activated Members".') if self.recipient_type == 'all': # Leave partner_ids empty → is_global=True → visible to all app users self.env['mapan.app.notification'].create({ 'title': self.title, 'body': self.body, }) target_msg = 'all activated app members' else: # Specific recipients only self.env['mapan.app.notification'].create({ 'title': self.title, 'body': self.body, 'partner_ids': [(6, 0, self.partner_ids.ids)], }) target_msg = f'{len(self.partner_ids)} selected member(s)' return { 'type': 'ir.actions.client', 'tag': 'display_notification', 'params': { 'title': 'Notification Sent', 'message': f'Notification staged for {target_msg}.', 'type': 'success', 'sticky': False, } }