-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathupdate_booking_usecase.py
More file actions
99 lines (78 loc) · 3.92 KB
/
update_booking_usecase.py
File metadata and controls
99 lines (78 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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.domain_errors import EntityError, EntityParameterOrderDatesError
from src.shared.helpers.errors.usecase_errors import ForbiddenAction, NoItemsFound, InvalidSchedule, InvalidSchedulePeriod
from datetime import timedelta
class UpdateBookingUsecase:
def __init__(self, booking_repo: IBookingRepository):
self.booking_repo = booking_repo
def __call__(self,
booking_id: str,
user: dict,
start_date: int = None,
end_date: int = None,
court_number: int = None,
sport: SPORT = None,
materials: List[str] = None,
new_user_id: str = None,
booking_type: BOOKING_TYPE = None):
user_id = user.get('user_id')
user_role = user.get('role')
if Booking.validate_booking_id(booking_id) is False:
raise EntityError('booking_id')
existing_booking = self.booking_repo.get_booking(booking_id)
if existing_booking is None:
raise NoItemsFound('booking')
if user_role != 'ADMIN':
booking_user_id = existing_booking.user_id
if booking_user_id != user_id:
raise ForbiddenAction('user id')
start_date = start_date if start_date is not None else existing_booking.start_date
end_date = end_date if end_date is not None else existing_booking.end_date
court_number = court_number if court_number is not None else existing_booking.court_number
sport = sport if sport is not None else existing_booking.sport
materials = materials if materials is not None else existing_booking.materials
booking_type = booking_type if booking_type is not None else existing_booking.booking_type
if Booking.validate_dates(start_date, end_date) is False:
raise EntityError("date")
if Booking.validate_order_dates(start_date, end_date) is False:
raise EntityParameterOrderDatesError(start_date, end_date)
if Booking.validate_court(court_number) is False:
raise EntityError("court_number")
if Booking.validate_sport(sport) is False:
raise EntityError("sport")
if Booking.validate_materials(materials) is False:
raise EntityError("materials")
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()
for booking in all_bookings:
if (
(booking.booking_id != booking_id)
and (
(
booking.start_date < end_date + (15 * 60 * 1000) #15 minutes in mseconds
and booking.end_date > start_date - (15 * 60 * 1000) #15 minutes in mseconds
and booking.court_number == court_number
)
)
):
raise InvalidSchedule()
booking = self.booking_repo.update_booking(
booking_id=booking_id,
start_date=start_date,
end_date=end_date,
court_number=court_number,
sport=sport,
materials=materials,
booking_type=booking_type
)
if booking.user_id != existing_booking.user_id:
booking.user_id = existing_booking.user_id
return booking