Skip to content

Commit 14f7c8d

Browse files
time comparison fix (#60)
1 parent 556cc3f commit 14f7c8d

3 files changed

Lines changed: 22 additions & 9 deletions

File tree

backend/event_ticketing_service/app/repositories/cart_repository.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
import pytz
23
from typing import List, Dict, Any
34
from fastapi import Depends
45
from fastapi import HTTPException, status
@@ -66,13 +67,6 @@ def add_item_from_detailed_sell(self, customer_id: int, ticket_type_id: int, qua
6667
detail=f"Ticket type with ID {ticket_type_id} not found",
6768
)
6869

69-
# Verify that the ticket type is available for sale
70-
if ticket_type.available_from is not None and ticket_type.available_from > datetime.now():
71-
raise HTTPException(
72-
status_code=status.HTTP_400_BAD_REQUEST,
73-
detail=f"Ticket type with ID {ticket_type_id} is not available for sale yet",
74-
)
75-
7670
# This case should ideally not happen
7771
if not ticket_type.event:
7872
raise HTTPException(
@@ -87,8 +81,24 @@ def add_item_from_detailed_sell(self, customer_id: int, ticket_type_id: int, qua
8781
detail=f"Event '{ticket_type.event.name}' is not yet active.",
8882
)
8983

84+
# Get current time in Warsaw timezone
85+
warsaw_tz = pytz.timezone('Europe/Warsaw')
86+
now_warsaw_aware = datetime.now(warsaw_tz)
87+
88+
# Verify that the ticket type is available for sale
89+
if ticket_type.available_from is not None:
90+
# Convert available_from to Warsaw timezone
91+
available_from_warsaw_aware = warsaw_tz.localize(ticket_type.available_from)
92+
93+
if available_from_warsaw_aware > now_warsaw_aware:
94+
raise HTTPException(
95+
status_code=status.HTTP_400_BAD_REQUEST,
96+
detail=f"Ticket type with ID {ticket_type_id} is not available for sale yet",
97+
)
98+
9099
# Verify that the event has not passed
91-
if ticket_type.event.end_date < datetime.now():
100+
event_end_date_warsaw_aware = warsaw_tz.localize(ticket_type.event.end_date)
101+
if event_end_date_warsaw_aware < now_warsaw_aware:
92102
raise HTTPException(
93103
status_code=status.HTTP_400_BAD_REQUEST,
94104
detail=f"Event '{ticket_type.event.name}' has already ended.",
@@ -105,7 +115,8 @@ def add_item_from_detailed_sell(self, customer_id: int, ticket_type_id: int, qua
105115
# If the item already exists in the cart, update the quantity
106116
if existing_cart_item:
107117
existing_cart_item.quantity += quantity
108-
logger.info(f"Updated quantity for ticket_type_id {ticket_type_id} in cart_id {cart.cart_id}. New quantity: {existing_cart_item.quantity}")
118+
logger.info(
119+
f"Updated quantity for ticket_type_id {ticket_type_id} in cart_id {cart.cart_id}. New quantity: {existing_cart_item.quantity}")
109120
else:
110121
existing_cart_item = CartItemModel(
111122
cart_id=cart.cart_id,

backend/event_ticketing_service/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ sendgrid==6.12.2
1212
sqlalchemy==2.0.40
1313
uvicorn==0.34.0
1414
boto3
15+
pytz

backend/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ python_dotenv==1.1.0
88
python_multipart==0.0.20
99
sqlalchemy==2.0.40
1010
uvicorn==0.34.0
11+
pytz

0 commit comments

Comments
 (0)