diff options
| author | Rafi Zadanly <zadanlyr@gmail.com> | 2023-07-26 10:25:10 +0700 |
|---|---|---|
| committer | Rafi Zadanly <zadanlyr@gmail.com> | 2023-07-26 10:25:10 +0700 |
| commit | 5ee4d54bdd5f8585bbf220dae31bb5aa2da41703 (patch) | |
| tree | a44e2ea50440ea810a499cbf288081ddb51376d1 /indoteknik_api/controllers/api_v1 | |
| parent | 1469c56e6c0964ad279c3dfc621be8551407363b (diff) | |
Update post cart API
Diffstat (limited to 'indoteknik_api/controllers/api_v1')
| -rw-r--r-- | indoteknik_api/controllers/api_v1/cart.py | 22 |
1 files changed, 17 insertions, 5 deletions
diff --git a/indoteknik_api/controllers/api_v1/cart.py b/indoteknik_api/controllers/api_v1/cart.py index d712be4d..5a60fd42 100644 --- a/indoteknik_api/controllers/api_v1/cart.py +++ b/indoteknik_api/controllers/api_v1/cart.py @@ -33,6 +33,7 @@ class Cart(controller.Controller): @http.route(PREFIX_USER + 'cart/create-or-update', auth='public', methods=['POST', 'OPTIONS'], csrf=False) @controller.Controller.must_authorized() def create_or_update_cart(self, user_id, **kw): + # Convert input values to appropriate types user_id = int(user_id) product_id = int(kw.get('product_id', 0)) qty = int(kw.get('qty', 0)) @@ -43,25 +44,36 @@ class Cart(controller.Controller): is_selected = is_selected in ('true', True) + # Check required fields if not user_id or not product_id or not qty: return self.response(code=400, description='user_id, product_id and qty is required') - query = [('user_id', '=', user_id), ('product_id', '=', product_id)] - cart = request.env['website.user.cart'].search(query, limit=1) + website_user_cart = request.env['website.user.cart'] + + # Remove previous 'buy' entries for the user + user_query = ('user_id', '=', user_id) + website_user_cart.search([user_query, ('source', '=', 'buy')]).unlink() + + # Prepare query to find existing cart entry for the product + query = [user_query, ('product_id', '=', product_id), ('source', '=', 'add_to_cart')] + cart = website_user_cart.search(query, limit=1) result = {} data_to_update = { 'qty': qty, 'is_selected': is_selected, 'program_line_id': program_line_id } + if source: data_to_update['source'] = source - - if cart: + + if cart and source in (None, 'add_to_cart'): + # Update existing cart entry cart.write(data_to_update) result['id'] = cart.id else: - create = request.env['website.user.cart'].create({ + # Create a new cart entry if it doesn't exist + create = website_user_cart.create({ 'user_id': user_id, 'product_id': product_id, **data_to_update |
