Skip to content

Commit

Permalink
Merge PR #1533 into 16.0
Browse files Browse the repository at this point in the history
Signed-off-by simahawk
  • Loading branch information
shopinvader-git-bot committed Jul 25, 2024
2 parents eb2df78 + 0e7e83e commit 9c87fd8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions shopinvader_api_cart/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
* Laurent Mignon <[email protected]>
* Stéphane Bidoul <[email protected]>
* Marie Lejeune <[email protected]>
* Simone Orsi <[email protected]>
18 changes: 18 additions & 0 deletions shopinvader_api_cart/routers/cart.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Copyright 2022 ACSONE SA/NV
# Copyright 2024 Camptocamp (http://www.camptocamp.com).
# @author Simone Orsi <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from collections import OrderedDict
from typing import Annotated
Expand Down Expand Up @@ -56,6 +58,22 @@ def sync(
return Sale.from_sale_order(cart) if cart else Response(status_code=204)


@cart_router.post("/new", status_code=201)
def new(
data: CartSyncInput,
env: Annotated[api.Environment, Depends(authenticated_partner_env)],
partner: Annotated["ResPartner", Depends(authenticated_partner)],
) -> Sale | None:
"""Create a new cart on demand.
You can use this endpoint to create multiple carts for the same customer.
"""
cart = env["shopinvader_api_cart.cart_router.helper"]._sync_cart(
partner, None, None, data.transactions
)
return Sale.from_sale_order(cart) if cart else Response(status_code=204)


@cart_router.post("/update/{uuid}", deprecated=True)
@cart_router.post("/{uuid}/update")
@cart_router.post("/current/update")
Expand Down
27 changes: 27 additions & 0 deletions shopinvader_api_cart/tests/test_shopinvader_api_cart.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Copyright 2022 ACSONE SA/NV
# Copyright 2024 Camptocamp (http://www.camptocamp.com).
# @author Simone Orsi <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

import json
Expand All @@ -7,6 +9,7 @@
from requests import Response

from odoo.exceptions import AccessError, MissingError
from odoo.tests.common import RecordCapturer

from ..routers.cart import cart_router
from ..schemas import CartTransaction
Expand Down Expand Up @@ -517,3 +520,27 @@ def test_update_new_cart(self) -> None:
self.assertEqual(response.status_code, status.HTTP_200_OK)
info = response.json()
self.assertTrue(self.env["sale.order"].browse(info["id"]).exists())

def test_sync_new_cart(self) -> None:
# A cart is already existing
so = self.env["sale.order"]._create_empty_cart(
self.default_fastapi_authenticated_partner.id
)
data = {
"transactions": [
{"uuid": self.trans_uuid_1, "product_id": self.product_1.id, "qty": 1}
]
}
with RecordCapturer(
self.env["sale.order"],
[("partner_id", "=", self.default_fastapi_authenticated_partner.id)],
) as capt:
for __ in range(1, 5):
# Get 4 new carts
with self._create_test_client(router=cart_router) as test_client:
response: Response = test_client.post(
"/new", content=json.dumps(data)
)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertTrue(capt.records[-1].id > so.id)
self.assertEqual(len(capt.records), 4)

0 comments on commit 9c87fd8

Please sign in to comment.