import json import logging from odoo import http from odoo.http import request from .base import EncoachMixin _logger = logging.getLogger(__name__) class ApprovalsController(EncoachMixin, http.Controller): @http.route('/api/approval-workflows', type='http', auth='public', methods=['GET', 'OPTIONS'], csrf=False, cors='*') def list_workflows(self, **kwargs): try: user = self._authenticate() if not user: return self._error_response('Unauthorized', 401) workflows = request.env['encoach.approval.workflow'].sudo().search([]) return self._json_response([{ 'id': w.id, 'examId': w.exam_id.id if w.exam_id else None, 'creatorId': w.creator_id.id if w.creator_id else None, 'status': w.status, 'modules': w.modules, 'steps': w.steps, } for w in workflows]) except Exception: _logger.exception("List approval workflows error") return self._error_response('Internal server error', 500) @http.route('/api/approval-workflows', type='http', auth='public', methods=['POST', 'OPTIONS'], csrf=False, cors='*') def create_workflow(self, **kwargs): try: user = self._authenticate() if not user: return self._error_response('Unauthorized', 401) body = self._get_json_body() vals = { 'creator_id': user.id, 'status': body.get('status', 'pending'), } if body.get('examId'): vals['exam_id'] = int(body['examId']) if 'modules' in body: vals['modules'] = body['modules'] if 'steps' in body: vals['steps'] = body['steps'] wf = request.env['encoach.approval.workflow'].sudo().create(vals) return self._json_response({ 'id': wf.id, 'examId': wf.exam_id.id if wf.exam_id else None, 'status': wf.status, }) except Exception: _logger.exception("Create approval workflow error") return self._error_response('Internal server error', 500) @http.route('/api/approval-workflows/', type='http', auth='public', methods=['PATCH', 'OPTIONS'], csrf=False, cors='*') def update_workflow(self, wid, **kwargs): try: user = self._authenticate() if not user: return self._error_response('Unauthorized', 401) wf = request.env['encoach.approval.workflow'].sudo().browse(wid) if not wf.exists(): return self._error_response('Workflow not found', 404) body = self._get_json_body() vals = {} if 'status' in body: vals['status'] = body['status'] if 'modules' in body: vals['modules'] = body['modules'] if 'steps' in body: vals['steps'] = body['steps'] if vals: wf.write(vals) return self._json_response({ 'id': wf.id, 'status': wf.status, }) except Exception: _logger.exception("Update approval workflow error") return self._error_response('Internal server error', 500) @http.route('/api/approval-workflows/', type='http', auth='public', methods=['DELETE', 'OPTIONS'], csrf=False, cors='*') def delete_workflow(self, wid, **kwargs): try: user = self._authenticate() if not user: return self._error_response('Unauthorized', 401) wf = request.env['encoach.approval.workflow'].sudo().browse(wid) if not wf.exists(): return self._error_response('Workflow not found', 404) wf.unlink() return self._json_response({'ok': True}) except Exception: _logger.exception("Delete approval workflow error") return self._error_response('Internal server error', 500)