Full backend implementation with custom Odoo modules: - encoach_api: Core API, user management, JWT auth - encoach_exam: Exam generation (reading, writing, listening, speaking) - encoach_evaluate: AI-powered evaluation (writing, speaking) - encoach_training: Training tips and walkthrough - encoach_storage: File storage management - encoach_payment: Stripe, PayPal, Paymob integration - encoach_mail: Email notifications Made-with: Cursor
89 lines
3.9 KiB
Python
89 lines
3.9 KiB
Python
import logging
|
|
|
|
from odoo import http
|
|
from odoo.http import request
|
|
|
|
from .base import EncoachMixin
|
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
|
class DiscountsController(EncoachMixin, http.Controller):
|
|
|
|
@http.route('/api/discounts', type='http', auth='public', methods=['GET', 'OPTIONS'], csrf=False, cors='*')
|
|
def list_discounts(self, **kwargs):
|
|
try:
|
|
user = self._authenticate()
|
|
if not user:
|
|
return self._error_response('Unauthorized', 401)
|
|
discounts = request.env['encoach.discount'].sudo().search([])
|
|
return self._json_response([{
|
|
'id': d.id, 'code': d.code, 'percentage': d.percentage,
|
|
'expiryDate': d.expiry_date.isoformat() if d.expiry_date else None,
|
|
'maxUses': d.max_uses, 'uses': d.uses,
|
|
} for d in discounts])
|
|
except Exception:
|
|
_logger.exception("List discounts error")
|
|
return self._error_response('Internal server error', 500)
|
|
|
|
@http.route('/api/discounts', type='http', auth='public', methods=['POST', 'OPTIONS'], csrf=False, cors='*')
|
|
def create_discount(self, **kwargs):
|
|
try:
|
|
user = self._authenticate()
|
|
if not user:
|
|
return self._error_response('Unauthorized', 401)
|
|
body = self._get_json_body()
|
|
code = body.get('code', '').strip()
|
|
if not code:
|
|
return self._error_response('code is required', 400)
|
|
discount = request.env['encoach.discount'].sudo().create({
|
|
'code': code,
|
|
'percentage': body.get('percentage', 0),
|
|
'expiry_date': body.get('expiryDate'),
|
|
'max_uses': body.get('maxUses', 0),
|
|
})
|
|
return self._json_response({
|
|
'id': discount.id, 'code': discount.code,
|
|
'percentage': discount.percentage,
|
|
})
|
|
except Exception:
|
|
_logger.exception("Create discount error")
|
|
return self._error_response('Internal server error', 500)
|
|
|
|
@http.route('/api/discounts/<int:did>', type='http', auth='public', methods=['PATCH', 'OPTIONS'], csrf=False, cors='*')
|
|
def update_discount(self, did, **kwargs):
|
|
try:
|
|
user = self._authenticate()
|
|
if not user:
|
|
return self._error_response('Unauthorized', 401)
|
|
discount = request.env['encoach.discount'].sudo().browse(did)
|
|
if not discount.exists():
|
|
return self._error_response('Discount not found', 404)
|
|
body = self._get_json_body()
|
|
vals = {}
|
|
if 'code' in body: vals['code'] = body['code']
|
|
if 'percentage' in body: vals['percentage'] = body['percentage']
|
|
if 'expiryDate' in body: vals['expiry_date'] = body['expiryDate']
|
|
if 'maxUses' in body: vals['max_uses'] = body['maxUses']
|
|
if vals:
|
|
discount.write(vals)
|
|
return self._json_response({'id': discount.id, 'code': discount.code})
|
|
except Exception:
|
|
_logger.exception("Update discount error")
|
|
return self._error_response('Internal server error', 500)
|
|
|
|
@http.route('/api/discounts/<int:did>', type='http', auth='public', methods=['DELETE', 'OPTIONS'], csrf=False, cors='*')
|
|
def delete_discount(self, did, **kwargs):
|
|
try:
|
|
user = self._authenticate()
|
|
if not user:
|
|
return self._error_response('Unauthorized', 401)
|
|
discount = request.env['encoach.discount'].sudo().browse(did)
|
|
if not discount.exists():
|
|
return self._error_response('Discount not found', 404)
|
|
discount.unlink()
|
|
return self._json_response({'ok': True})
|
|
except Exception:
|
|
_logger.exception("Delete discount error")
|
|
return self._error_response('Internal server error', 500)
|