Add validation to prevent booking two trades on the same day
This commit is contained in:
parent
8948e3533f
commit
04192799ab
2 changed files with 59 additions and 0 deletions
|
|
@ -378,6 +378,28 @@ async def create_exchange(
|
|||
slot_date = request.slot_start.date()
|
||||
validate_date_in_range(slot_date, context="book")
|
||||
|
||||
# Check if user already has a trade on this date
|
||||
existing_trade_query = select(Exchange).where(
|
||||
and_(
|
||||
Exchange.user_id == current_user.id,
|
||||
Exchange.slot_start >= datetime.combine(slot_date, time.min, tzinfo=UTC),
|
||||
Exchange.slot_start
|
||||
< datetime.combine(slot_date, time.max, tzinfo=UTC) + timedelta(days=1),
|
||||
Exchange.status == ExchangeStatus.BOOKED,
|
||||
)
|
||||
)
|
||||
existing_trade_result = await db.execute(existing_trade_query)
|
||||
existing_trade = existing_trade_result.scalar_one_or_none()
|
||||
|
||||
if existing_trade:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail=(
|
||||
f"You already have a trade booked on {slot_date.strftime('%Y-%m-%d')}. "
|
||||
f"Only one trade per day is allowed. Trade ID: {existing_trade.id}"
|
||||
),
|
||||
)
|
||||
|
||||
# Validate direction
|
||||
try:
|
||||
direction = TradeDirection(request.direction)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue