generated from owl-corp/python-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an endpoint to submit an order to printful
- Loading branch information
1 parent
8458a9f
commit da8fdd3
Showing
4 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
from decimal import Decimal | ||
|
||
from pydantic import BaseModel | ||
|
||
|
||
class OrderRecipient(BaseModel): | ||
"""Information about the recipient of the order.""" | ||
|
||
name: str | ||
company: str | None = None | ||
address1: str | ||
address2: str | ||
city: str | ||
state_code: str | None = None | ||
state_name: str | None = None | ||
country_code: str | ||
country_name: str | ||
zip: str | ||
phone: str | ||
email: str | ||
tax_number: str | None = None | ||
|
||
|
||
class OrderItem(BaseModel): | ||
"""Information about the items in the order.""" | ||
|
||
product_template_id: int | ||
variant_id: int | ||
|
||
|
||
class OrderCreate(BaseModel): | ||
"""Data required to create an order.""" | ||
|
||
recipient: OrderRecipient | ||
items: list[OrderItem] | ||
|
||
def as_printful_payload(self) -> dict: | ||
"""Return this order in the format used by Printful's API.""" | ||
return { | ||
"recipient": self.recipient.model_dump(), | ||
"items": [item.model_dump() for item in self.items], | ||
} | ||
|
||
|
||
class OrderCosts(BaseModel): | ||
"""All costs associated with an order.""" | ||
|
||
currency: str | ||
subtotal: Decimal | ||
discount: Decimal | ||
shipping: Decimal | ||
digitization: Decimal | ||
additional_fee: Decimal | ||
fulfillment_fee: Decimal | ||
retail_delivery_fee: Decimal | ||
tax: Decimal | ||
vat: Decimal | ||
total: Decimal | ||
|
||
|
||
class Order(OrderCreate): | ||
"""The order as returned by printful.""" | ||
|
||
id: int | ||
status: str | ||
costs: OrderCosts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import logging | ||
|
||
from fastapi import APIRouter, Depends, HTTPException, Request | ||
from sqlalchemy import select | ||
|
||
from src.auth import TokenAuth | ||
from src.dto import Order, OrderCreate, Voucher | ||
from src.orm import Voucher as DBVoucher | ||
from src.settings import DBSession, PrintfulClient | ||
|
||
router = APIRouter(prefix="/orders", tags=["Orders"], dependencies=[Depends(TokenAuth(allow_vouchers=True))]) | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
@router.post("/") | ||
async def create_order(request: Request, db: DBSession, client: PrintfulClient, order: OrderCreate) -> Order | None: | ||
""" | ||
Create the order in printful and deduct the order cost from the voucher. | ||
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}) | ||
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: | ||
await client.delete(f"/orders/{submitted_order.id}") | ||
raise HTTPException( | ||
status_code=400, | ||
detail=f"Order totals {submitted_order.costs.total}, only {db_voucher.balance} remaining on voucher.", | ||
) | ||
|
||
db_voucher.balance = db_voucher.balance - submitted_order.costs.total | ||
return submitted_order |