Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/modules/create_booking/app/create_booking_usecase.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from datetime import timedelta
import uuid
from typing import List

from src.shared.domain.entities.booking import Booking
from src.shared.domain.enums.sport import SPORT
from src.shared.domain.enums.type import BOOKING_TYPE
from src.shared.domain.repositories.booking_repository_interface import IBookingRepository
from src.shared.helpers.errors.usecase_errors import DuplicatedItem, InvalidSchedule
from src.shared.helpers.errors.usecase_errors import DuplicatedItem, InvalidSchedule, InvalidSchedulePeriod


class CreateBookingUsecase:
Expand Down Expand Up @@ -44,15 +45,21 @@ def __call__(self,
for material in materials:
if not isinstance(material, str):
raise ValueError("Invalid material type")



if booking_type not in [type.value for type in BOOKING_TYPE]:
raise ValueError("Invalid type enum value")

self.booking_type = BOOKING_TYPE(booking_type)

maxtime = start_date + (timedelta(weeks=12).total_seconds()*1000)
if(end_date > maxtime):
raise InvalidSchedulePeriod()

all_bookings = self.repo.get_all_bookings()

for booking in all_bookings:

if (
(
booking.start_date < end_date + (15 * 60 * 1000) #15 minutes in mseconds
Expand Down
8 changes: 7 additions & 1 deletion src/modules/update_booking/app/update_booking_usecase.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from src.shared.domain.enums.type import BOOKING_TYPE
from src.shared.domain.repositories.booking_repository_interface import IBookingRepository
from src.shared.helpers.errors.domain_errors import EntityError, EntityParameterOrderDatesError
from src.shared.helpers.errors.usecase_errors import ForbiddenAction, NoItemsFound, InvalidSchedule
from src.shared.helpers.errors.usecase_errors import ForbiddenAction, NoItemsFound, InvalidSchedule, InvalidSchedulePeriod
from datetime import timedelta

class UpdateBookingUsecase:
def __init__(self, booking_repo: IBookingRepository):
Expand Down Expand Up @@ -61,6 +62,11 @@ def __call__(self,

if Booking.validate_booking_type(booking_type) is False:
raise EntityError("type")

maxtime = start_date + (timedelta(weeks=12).total_seconds()*1000)
if(end_date > maxtime):
raise InvalidSchedulePeriod()


all_bookings = self.booking_repo.get_all_bookings()

Expand Down
13 changes: 12 additions & 1 deletion src/shared/domain/entities/booking.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from src.shared.domain.entities.court import Court
from src.shared.domain.enums.sport import SPORT
from src.shared.domain.enums.type import BOOKING_TYPE
from src.shared.helpers.errors.domain_errors import EntityError, EntityParameterOrderDatesError
from src.shared.helpers.errors.domain_errors import EntityError, EntityParameterOrderDatesError, EntitySchedulePeriodError
from datetime import timedelta

class Booking(abc.ABC):
start_date: int
Expand All @@ -25,6 +26,9 @@ def __init__(self, start_date: int, end_date: int, court_number: int, sport: SPO

if not Booking.validate_order_dates(start_date, end_date):
raise EntityParameterOrderDatesError(start_date, end_date)

# if not Booking.validate_scheduling_time(start_date, end_date):
# raise EntitySchedulePeriodError()

if not Booking.validate_court(court_number):
raise EntityError("court")
Expand Down Expand Up @@ -67,6 +71,13 @@ def validate_order_dates(start_date: int, end_date: int) -> bool:
return False
return True

# @staticmethod
# def validate_scheduling_time(start_date: int, end_date: int)-> bool:
# maxtime = start_date + (timedelta(weeks=12).total_seconds()*1000)
# if end_date>maxtime:
# return False
# return True

@staticmethod
def validate_court(court_number: int) -> bool:
if not isinstance(court_number, int):
Expand Down
4 changes: 4 additions & 0 deletions src/shared/helpers/errors/domain_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ def __init__(self, message: str):
def message(self):
return self.__message

class EntitySchedulePeriodError(BaseError):
def __init__(self):
super().__init__(f'The scheduling period must not exceed 3 months')

class EntityParameterOrderDatesError(EntityError):
def __init__(self, start_date: int, end_date: int):
super().__init__(f'Initial date {start_date} must be less than or equal to end date {end_date}')
Expand Down
4 changes: 4 additions & 0 deletions src/shared/helpers/errors/usecase_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ class NoItemsFound(BaseError):
def __init__(self, message: str):
super().__init__(f'No items found for {message}')

class InvalidSchedulePeriod(BaseError):
def __init__(self):
super().__init__(f'The scheduling period must not exceed 3 months')

class DuplicatedItem(BaseError):
def __init__(self, message: str):
super().__init__(f'The item alredy exists for this {message}')
Expand Down
22 changes: 21 additions & 1 deletion tests/modules/create_booking/app/test_create_booking_usecase.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from src.shared.domain.entities.booking import Booking
from src.shared.domain.enums.sport import SPORT
from src.shared.domain.enums.type import BOOKING_TYPE
from src.shared.helpers.errors.usecase_errors import InvalidSchedule
from src.shared.helpers.errors.usecase_errors import InvalidSchedule, InvalidSchedulePeriod
from src.shared.infra.repositories.booking_repository_mock import BookingRepositoryMock


Expand Down Expand Up @@ -82,6 +82,26 @@ def test_create_booking_usecase_invalid_materials(self):
booking_type='Training'
)

def test_create_booking_usecase_invalid_schedule_period(self):

with pytest.raises(InvalidSchedulePeriod) as e:

booking_repository = BookingRepositoryMock()

usecase = CreateBookingUsecase(booking_repository)

response = usecase(
start_date=1771897236000,
end_date=1781897236000,
court_number=1,
sport="Tennis",
user_id='c8435c66-13a4-4641-9d54-773b4b8ccc98',
materials=['Raquete', 'Bola', 'Rede', 'Tenis'],
booking_type='Training'
)

assert e.value.message == 'The scheduling period must not exceed 3 months'

def test_create_booking_usecase_invalid_schedule_overlap(self):

with pytest.raises(InvalidSchedule) as e:
Expand Down
32 changes: 29 additions & 3 deletions tests/modules/update_booking/app/test_update_booking_usecase.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

from src.modules.update_booking.app.update_booking_usecase import UpdateBookingUsecase
from src.shared.domain.enums.sport import SPORT
from src.shared.helpers.errors.domain_errors import EntityError, EntityParameterOrderDatesError, EntityParameterTimeError
from src.shared.helpers.errors.usecase_errors import ForbiddenAction, InvalidSchedule
from src.shared.helpers.errors.domain_errors import EntityError, EntityParameterOrderDatesError
from src.shared.helpers.errors.usecase_errors import ForbiddenAction, InvalidSchedule, InvalidSchedulePeriod
from src.shared.infra.repositories.booking_repository_mock import BookingRepositoryMock


Expand Down Expand Up @@ -107,13 +107,39 @@ def test_update_booking_usecase_invalid_schedule_15min_intolerance(self):
user=user,
court_number=3,
start_date=1634571899999,
end_date=1734571000000,
end_date=1634671000000,
sport=SPORT.TENNIS,
materials=['Raquete', 'Bola', 'Rede', 'Tenis']
)

assert e.value == "Court is already booked for the selected time slot or has to have 15 min tolerance"

def test_update_booking_usecase_invalid_schedule_period(self):
booking_repo = BookingRepositoryMock()
usecase = UpdateBookingUsecase(booking_repo= booking_repo)

booking_id = booking_repo.bookings[0].booking_id

user = {
'user_id': '1f25448b-3429-4c19-8287-d9e64f17bc3a',
'name': 'Nome',
'email': 'user@email.com',
'role': 'STUDENT'
}

with pytest.raises(InvalidSchedulePeriod) as e:
booking = usecase(booking_id=booking_id,
user=user,
court_number=3,
start_date=177248251500,
end_date=187248251500,
sport=SPORT.TENNIS,
materials=['Raquete', 'Bola', 'Rede', 'Tenis']
)

assert e.value.message == "The scheduling period must not exceed 3 months"


def test_update_booking_usecase_invalid_booking_id(self):
booking_repo = BookingRepositoryMock()
usecase = UpdateBookingUsecase(booking_repo=booking_repo)
Expand Down