feat: add input validation and support for global push notifications in wizard

This commit is contained in:
Suherdy Yacob 2026-06-13 21:53:57 +07:00
parent a37e49e3ed
commit 8737c8a661

View File

@ -18,25 +18,31 @@ class PushNotificationWizard(models.TransientModel):
)
def action_send_push(self):
# Determine target partners based on recipient type
partners = self.partner_ids if self.recipient_type == 'specific' else self.env['res.partner']
if self.recipient_type == 'specific' and not self.partner_ids:
raise UserError('Please select at least one recipient, or switch to "All Activated Members".')
# Create the notification record in the DB instead of calling Firebase
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,
'partner_ids': [(6, 0, partners.ids)]
})
recipient_count = len(partners)
target_msg = f"{recipient_count} selected partners" if self.recipient_type == 'specific' else "all app users"
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': 'App Notification Dispatched',
'message': f'Message successfully staged for {target_msg}!',
'title': 'Notification Sent',
'message': f'Notification staged for {target_msg}.',
'type': 'success',
'sticky': False,
}