feat(booking): materialize occurrence on POST bookings. Refs EventHub/EventHubBack#67

This commit is contained in:
2026-08-14 10:52:24 +03:00
parent e3403f6ed6
commit 8a5f254c2a
5 changed files with 220 additions and 35 deletions
+35 -7
View File
@@ -1,6 +1,6 @@
-module(logic_booking).
-include("records.hrl").
-export([create_booking/2, confirm_booking/2, confirm_booking/3,
-export([create_booking/2, create_booking/3, confirm_booking/2, confirm_booking/3,
cancel_booking/2, cancel_booking/3, get_booking/2,
list_bookings/2, list_user_bookings/1, list_user_booking_requests/1,
delete_booking/2,
@@ -16,15 +16,30 @@
-spec create_booking(UserId :: binary(), EventId :: binary()) ->
{ok, #booking{}} |
{error, full | already_booked | not_found | personal_calendar |
subscription_inactive | own_event | event_not_active | access_denied}.
subscription_inactive | own_event | event_not_active | access_denied |
occurrence_start_required | invalid_occurrence}.
create_booking(UserId, EventId) ->
create_booking(UserId, EventId, undefined).
-spec create_booking(UserId :: binary(), EventId :: binary(),
OccurrenceStart :: calendar:datetime() | undefined) ->
{ok, #booking{}} |
{error, full | already_booked | not_found | personal_calendar |
subscription_inactive | own_event | event_not_active | access_denied |
occurrence_start_required | invalid_occurrence}.
create_booking(UserId, EventId, OccurrenceStart) ->
case core_event:get_by_id(EventId) of
{ok, #event{status = active} = Event} ->
case core_calendar:get_by_id(Event#event.calendar_id) of
{ok, Calendar} ->
create_on_calendar(UserId, Event, Calendar);
{error, not_found} ->
{error, not_found}
case resolve_bookable_event(Event, OccurrenceStart) of
{ok, BookEvent} ->
case core_calendar:get_by_id(BookEvent#event.calendar_id) of
{ok, Calendar} ->
create_on_calendar(UserId, BookEvent, Calendar);
{error, not_found} ->
{error, not_found}
end;
{error, _} = Err ->
Err
end;
{ok, _} ->
{error, event_not_active};
@@ -32,6 +47,19 @@ create_booking(UserId, EventId) ->
{error, not_found}
end.
resolve_bookable_event(#event{event_type = recurring}, undefined) ->
{error, occurrence_start_required};
resolve_bookable_event(#event{event_type = recurring} = Master, OccurrenceStart) ->
case logic_event:validate_occurrence(Master, OccurrenceStart) of
ok ->
logic_event:materialize_for_booking(
Master#event.id, OccurrenceStart, Master#event.specialist_id);
{error, _} = Err ->
Err
end;
resolve_bookable_event(Event, _OccurrenceStart) ->
{ok, Event}.
create_on_calendar(UserId, _Event, #calendar{owner_id = UserId}) ->
{error, own_event};
create_on_calendar(_UserId, _Event, #calendar{type = personal}) ->