summaryrefslogtreecommitdiff
path: root/indoteknik_api/controllers/api_v1
diff options
context:
space:
mode:
authorRafi Zadanly <zadanlyr@gmail.com>2024-02-19 10:16:20 +0700
committerRafi Zadanly <zadanlyr@gmail.com>2024-02-19 10:16:20 +0700
commit899aa7d32ed7df8672dd7a7415500b44242dd89c (patch)
tree39f5f64810311f5f64cd9a36e5910d460a43ea28 /indoteknik_api/controllers/api_v1
parentfd275ea8c25246e349ae3e177ac6d0acdda249c6 (diff)
Fix create or update cart api
Diffstat (limited to 'indoteknik_api/controllers/api_v1')
-rw-r--r--indoteknik_api/controllers/api_v1/cart.py38
1 files changed, 21 insertions, 17 deletions
diff --git a/indoteknik_api/controllers/api_v1/cart.py b/indoteknik_api/controllers/api_v1/cart.py
index 907c8288..5948a277 100644
--- a/indoteknik_api/controllers/api_v1/cart.py
+++ b/indoteknik_api/controllers/api_v1/cart.py
@@ -52,6 +52,9 @@ class Cart(controller.Controller):
if not user_id:
return self.response(code=400, description='user_id is required')
+ if not product_id and not program_line_id:
+ return self.response(code=400, description='product_id or program_line_id is required')
+
website_user_cart = request.env['website.user.cart']
# Remove previous 'buy' entries for the user
@@ -68,32 +71,33 @@ class Cart(controller.Controller):
cart = website_user_cart.search(query, limit=1)
data_to_update = {
+ 'user_id': user_id,
'qty': qty,
'is_selected': is_selected,
'program_line_id': program_line_id,
- 'product_id': product_id
+ 'product_id': product_id,
+ 'source': source or False
}
- if program_line_id:
- data_to_update['product_id'] = False
-
- if source:
- data_to_update['source'] = source
-
- result = {}
if cart:
- # Update existing cart entry
cart.write(data_to_update)
- result['id'] = cart.id
else:
- # Create a new cart entry if it doesn't exist
- create = website_user_cart.create({
- 'user_id': user_id,
- **data_to_update
- })
- result['id'] = create.id
+ cart = website_user_cart.create(data_to_update)
- return self.response(result)
+ return self.response({
+ 'id': cart.id,
+ 'product': {
+ 'id': cart.product_id.id,
+ 'name': cart.product_id.name
+ } if cart.product_id else None,
+ 'program_line': {
+ 'id': cart.program_line_id.id,
+ 'name': cart.program_line_id.name
+ } if cart.program_line_id else None,
+ 'qty': cart.qty,
+ 'is_selected': cart.is_selected,
+ 'source': cart.source
+ })
@http.route(PREFIX_USER + 'cart', auth='public', methods=['DELETE', 'OPTIONS'], csrf=False)
@controller.Controller.must_authorized()