mapan_loyalty_push/controllers/main.py

50 lines
1.7 KiB
Python

from odoo import http
from odoo.http import request
class AppNotificationController(http.Controller):
@http.route('/api/loyalty/fetch_notifications', type='jsonrpc', auth='user', methods=['POST'], csrf=False)
def fetch_notifications(self, **kw):
"""
Endpoint for the Flutter app Background Task and In-App notification center.
"""
user = request.env.user
partner = user.partner_id
last_id = kw.get('last_id', 0)
# Give them global notifications OR notifications tagged explicitly to them
domain = [
('id', '>', last_id),
'|',
('partner_ids', 'in', [partner.id]),
('is_global', '=', True)
]
notifications = request.env['mapan.app.notification'].sudo().search_read(
domain,
['id', 'title', 'body', 'create_date'],
order='create_date desc',
limit=50
)
return {
'status': 'success',
'data': notifications
}
@http.route('/api/loyalty/branches', type='jsonrpc', auth='public', methods=['POST'], csrf=False)
def fetch_branches(self, **kw):
"""
Public endpoint for the Flutter app to get branches without exposing API keys.
"""
try:
branches = request.env['res.company'].sudo().search_read(
[('parent_id', '!=', False)],
['name', 'street', 'city', 'phone'],
limit=50
)
return {'status': 'success', 'data': branches}
except Exception as e:
return {'status': 'error', 'message': str(e)}