diff options
author | 2024-09-13 19:38:02 +0100 | |
---|---|---|
committer | 2024-09-13 19:38:02 +0100 | |
commit | 1cf3fce85817d4520cd9e98c5c456bf693cc787b (patch) | |
tree | b8def6a2164ac53401d74f7a3ad654aba556246b | |
parent | Also accept a quantity for order items (diff) |
Set the external_id of the printful order to the voucher id
-rw-r--r-- | thallium-backend/src/dto/orders.py | 5 | ||||
-rw-r--r-- | thallium-backend/src/routes/orders.py | 5 |
2 files changed, 7 insertions, 3 deletions
diff --git a/thallium-backend/src/dto/orders.py b/thallium-backend/src/dto/orders.py index f4864ab..848e7fc 100644 --- a/thallium-backend/src/dto/orders.py +++ b/thallium-backend/src/dto/orders.py @@ -2,6 +2,8 @@ from decimal import Decimal from pydantic import BaseModel +from src.dto import Voucher + class OrderRecipient(BaseModel): """Information about the recipient of the order.""" @@ -35,9 +37,10 @@ class OrderCreate(BaseModel): recipient: OrderRecipient items: list[OrderItem] - def as_printful_payload(self) -> dict: + def as_printful_payload(self, voucher: Voucher) -> dict: """Return this order in the format used by Printful's API.""" return { + "external_id": voucher.id, "recipient": self.recipient.model_dump(), "items": [item.model_dump() for item in self.items], } diff --git a/thallium-backend/src/routes/orders.py b/thallium-backend/src/routes/orders.py index c5126a8..20682c0 100644 --- a/thallium-backend/src/routes/orders.py +++ b/thallium-backend/src/routes/orders.py @@ -20,11 +20,12 @@ async def create_order(request: Request, db: DBSession, client: PrintfulClient, If the voucher does not have enough funds, the order is cancelled. """ - resp = await client.post("/orders", json=order.as_printful_payload(), params={"confirm": False}) + voucher: Voucher = request.state.voucher + + resp = await client.post("/orders", json=order.as_printful_payload(voucher), params={"confirm": False}) resp.raise_for_status() submitted_order = Order.model_validate(resp.json()["result"]) - voucher: Voucher = request.state.voucher stmt = select(DBVoucher).where(DBVoucher.id == voucher.id).with_for_update() db_voucher = await db.scalar(stmt) if submitted_order.costs.total > db_voucher.balance: |