api: add book action to recurring fillslots endpoint (#60255)

This commit is contained in:
Valentin Deniaud 2022-01-06 11:01:08 +01:00
parent ad019b0956
commit 58ade5be61
3 changed files with 36 additions and 9 deletions

View File

@ -245,7 +245,7 @@ class AgendaOrSubscribedSlugsSerializer(AgendaOrSubscribedSlugsMixin, serializer
class RecurringFillslotsQueryStringSerializer(AgendaOrSubscribedSlugsSerializer):
action = serializers.ChoiceField(required=True, choices=['update'])
action = serializers.ChoiceField(required=True, choices=['update', 'book'])
class RecurringEventsListSerializer(AgendaOrSubscribedSlugsSerializer):

View File

@ -1628,6 +1628,11 @@ class RecurringFillslots(APIView):
payload['slots'], start_datetime, end_datetime, user_external_id
)
events_to_unbook = self.get_events_to_unbook(agendas, events_to_book)
elif data['action'] == 'book':
events_to_book = self.get_event_recurrences(
payload['slots'], start_datetime, end_datetime, user_external_id
)
events_to_unbook = []
events_to_book = events_to_book.exclude(booking__user_external_id=user_external_id)

View File

@ -2146,7 +2146,8 @@ def test_fillslot_past_events_recurring_event(app, user):
assert resp.json['err_desc'] == 'event %s is cancelled' % first_recurrence.slug
def test_recurring_events_api_fillslots(app, user, freezer):
@pytest.mark.parametrize('action', ['book', 'update'])
def test_recurring_events_api_fillslots(app, user, freezer, action):
freezer.move_to('2021-09-06 12:00')
agenda = Agenda.objects.create(label='Foo bar', kind='events')
Desk.objects.create(agenda=agenda, slug='_exceptions_holder')
@ -2175,7 +2176,7 @@ def test_recurring_events_api_fillslots(app, user, freezer):
assert len(resp.json['data']) == 5
app.authorization = ('Basic', ('john.doe', 'password'))
fillslots_url = '/api/agendas/recurring-events/fillslots/?agendas=%s&action=update' % agenda.slug
fillslots_url = '/api/agendas/recurring-events/fillslots/?agendas=%s&action=%s' % (agenda.slug, action)
params = {'user_external_id': 'user_id'}
# Book Monday and Thursday of first event and Sunday of second event
params['slots'] = 'foo-bar@event:0,foo-bar@event:3,foo-bar@sunday-event:6'
@ -2324,11 +2325,21 @@ def test_recurring_events_api_fillslots_change_bookings(app, user, freezer):
assert Booking.objects.filter(event__start_datetime__week_day=2).count() == 52
assert Booking.objects.filter(event__start_datetime__week_day=5).count() == 52
# Book Friday without changing other bookings
params['slots'] = 'foo-bar@event:4'
resp = app.post_json(fillslots_url.replace('update', 'book'), params=params)
assert resp.json['booking_count'] == 52
assert resp.json['cancelled_booking_count'] == 0
assert Booking.objects.count() == 156
assert Booking.objects.filter(event__start_datetime__week_day=2).count() == 52
assert Booking.objects.filter(event__start_datetime__week_day=5).count() == 52
assert Booking.objects.filter(event__start_datetime__week_day=6).count() == 52
# Change booking to Monday and Tuesday
params['slots'] = 'foo-bar@event:0,foo-bar@event:1'
resp = app.post_json(fillslots_url, params=params)
assert resp.json['booking_count'] == 52
assert resp.json['cancelled_booking_count'] == 52
assert resp.json['cancelled_booking_count'] == 104
assert Booking.objects.count() == 104
assert Booking.objects.filter(event__start_datetime__week_day=2).count() == 52
assert Booking.objects.filter(event__start_datetime__week_day=3).count() == 52
@ -2527,9 +2538,9 @@ def test_recurring_events_api_fillslots_multiple_agendas(app, user):
assert len(resp.json['data']) == 6
app.authorization = ('Basic', ('john.doe', 'password'))
fillslots_url = '/api/agendas/recurring-events/fillslots/?action=update&agendas=%s'
fillslots_url = '/api/agendas/recurring-events/fillslots/?action=%s&agendas=%s'
params = {'user_external_id': 'user_id', 'slots': 'first-agenda@a:0,first-agenda@a:5,second-agenda@c:3'}
resp = app.post_json(fillslots_url % 'first-agenda,second-agenda', params=params)
resp = app.post_json(fillslots_url % ('update', 'first-agenda,second-agenda'), params=params)
assert resp.json['booking_count'] == 13
assert Booking.objects.count() == 13
@ -2537,18 +2548,29 @@ def test_recurring_events_api_fillslots_multiple_agendas(app, user):
assert Booking.objects.filter(event__primary_event=event_b).count() == 0
assert Booking.objects.filter(event__primary_event=event_c).count() == 4
# add bookings
params = {'user_external_id': 'user_id', 'slots': 'first-agenda@a:2,second-agenda@c:2'}
resp = app.post_json(fillslots_url % ('book', 'first-agenda,second-agenda'), params=params)
assert resp.json['booking_count'] == 8
assert resp.json['cancelled_booking_count'] == 0
assert Booking.objects.count() == 21
assert Booking.objects.filter(event__primary_event=event_a).count() == 13
assert Booking.objects.filter(event__primary_event=event_b).count() == 0
assert Booking.objects.filter(event__primary_event=event_c).count() == 8
# update bookings
params = {'user_external_id': 'user_id', 'slots': 'first-agenda@b:1'}
resp = app.post_json(fillslots_url % 'first-agenda,second-agenda', params=params)
resp = app.post_json(fillslots_url % ('update', 'first-agenda,second-agenda'), params=params)
assert resp.json['booking_count'] == 5
assert resp.json['cancelled_booking_count'] == 13
assert resp.json['cancelled_booking_count'] == 21
assert Booking.objects.filter(event__primary_event=event_a).count() == 0
assert Booking.objects.filter(event__primary_event=event_b).count() == 5
assert Booking.objects.filter(event__primary_event=event_c).count() == 0
# error if slot's agenda is not in querystring
resp = app.post_json(fillslots_url % 'second-agenda', params=params, status=400)
resp = app.post_json(fillslots_url % ('update', 'second-agenda'), params=params, status=400)
assert resp.json['err'] == 1
assert resp.json['errors']['slots'] == [
'Events from the following agendas cannot be booked: first-agenda'