Skip to content

Commit

Permalink
Reduce the time voucher rows are locked when creating orders
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisLovering committed Sep 16, 2024
1 parent 4096c51 commit 0601ee9
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions thallium-backend/src/routes/orders.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ async def create_order(request: Request, db: DBSession, client: PrintfulClient,
If the voucher does not have enough funds, the order is cancelled.
"""
voucher: Voucher = request.state.voucher
stmt = select(DBVoucher).where(DBVoucher.id == voucher.id).with_for_update()
db_voucher = await db.scalar(stmt)

resp = await client.post(
"/orders/estimate-costs",
json=order.as_printful_payload(voucher),
params={"confirm": False},
)
resp.raise_for_status()

stmt = select(DBVoucher).where(DBVoucher.id == voucher.id).with_for_update()
db_voucher = await db.scalar(stmt)
cost = OrderCosts.model_validate(resp.json()["result"]["costs"])
if cost.total > db_voucher.balance:
raise HTTPException(
Expand Down

0 comments on commit 0601ee9

Please sign in to comment.