From 0df38956baa8f9f8c7e27edc7018578bb97a61ac Mon Sep 17 00:00:00 2001 From: Chris Lovering Date: Mon, 16 Sep 2024 14:15:06 +0100 Subject: Reduce the time voucher rows are locked when creating orders --- thallium-backend/src/routes/orders.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/thallium-backend/src/routes/orders.py b/thallium-backend/src/routes/orders.py index af659bd..f52a2f9 100644 --- a/thallium-backend/src/routes/orders.py +++ b/thallium-backend/src/routes/orders.py @@ -21,8 +21,6 @@ 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", @@ -31,6 +29,9 @@ async def create_order(request: Request, db: DBSession, client: PrintfulClient, ) resp.raise_for_status() cost = OrderCosts.model_validate(resp.json()["result"]["costs"]) + + stmt = select(DBVoucher).where(DBVoucher.id == voucher.id).with_for_update() + db_voucher = await db.scalar(stmt) if cost.total > db_voucher.balance: raise HTTPException( status_code=400, -- cgit v1.2.3