Skip to content

Commit

Permalink
Check bookings limit also in according to booking_type
Browse files Browse the repository at this point in the history
  • Loading branch information
folix-01 committed Oct 3, 2023
1 parent 02103df commit 0d2bdd8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/redturtle/prenotazioni/adapters/booker.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ def prenotazioni(self):
"""The prenotazioni context state view"""
return self.context.unrestrictedTraverse("@@prenotazioni_context_state") # noqa

def _validate_user_limit(self, fiscalcode):
def _validate_user_limit(self, data: dict):
"""Control if user did not exceed the limit yet
Args:
fiscalcode (str): User's fiscal code
data (dict): booking data
Returns:
None
Expand All @@ -57,22 +57,31 @@ def _validate_user_limit(self, fiscalcode):
if not self.context.max_bookings_allowed:
return

if len(self.search_future_bookings_by_fiscalcode(fiscalcode)) >= (
self.context.max_bookings_allowed
):
if len(
self.search_future_bookings_by_fiscalcode(
data["fiscalcode"], data["booking_type"]
)
) >= (self.context.max_bookings_allowed):
raise BookingsLimitExceded(self.context)

def search_future_bookings_by_fiscalcode(self, fiscalcode):
def search_future_bookings_by_fiscalcode(self, fiscalcode, booking_type=None):
"""Find all the future bookings registered for the same fiscalcode"""
result = []
for booking in api.portal.get_tool("portal_catalog").unrestrictedSearchResults(
query = dict(
portal_type="Prenotazione",
fiscalcode=fiscalcode,
path={"query": "/".join(self.context.getPhysicalPath())},
Date={"query": DateTime(), "range": "min"},
review_state={
"query": ("confirmed", "pending", "private"),
},
)

if booking_type:
query["booking_type"] = booking_type

for booking in api.portal.get_tool("portal_catalog").unrestrictedSearchResults(
**query
):
result.append(booking)

Expand Down Expand Up @@ -145,7 +154,7 @@ def _create(self, data, duration=-1, force_gate=""):

if fiscalcode:
params["fiscalcode"] = fiscalcode
self._validate_user_limit(fiscalcode)
self._validate_user_limit(params)

obj = api.content.create(
type="Prenotazione",
Expand Down
24 changes: 24 additions & 0 deletions src/redturtle/prenotazioni/tests/test_bookings_num_limit.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def setUp(self):
daData=date.today(),
booking_types=[
{"name": "Type A", "duration": "30"},
{"name": "Type B", "duration": "30"},
],
gates=["Gate A"],
)
Expand Down Expand Up @@ -191,3 +192,26 @@ def test_limit_exceeded_is_not_raised_if_have_refused_bookings(self):
}
)
)

def test_limit_exceeded_is_not_raised_if_different_booking_type(self):
self.create_booking(
data={
"booking_date": self.tomorrow_8_0,
"booking_type": "Type A",
"title": "foo",
"email": "[email protected]",
"fiscalcode": self.testing_fiscalcode,
}
)

self.assertTrue(
self.create_booking(
data={
"booking_date": self.tomorrow_8_0 + timedelta(days=1),
"booking_type": "Type B",
"title": "foo",
"email": "[email protected]",
"fiscalcode": self.testing_fiscalcode,
}
)
)

0 comments on commit 0d2bdd8

Please sign in to comment.