feat(booking): studio journal and instance inbox. Refs EventHub/EventHubFront#60
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
-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,
|
||||
list_user_studio_bookings/1,
|
||||
delete_booking/2,
|
||||
list_bookings_admin/0, get_booking_admin/1,
|
||||
list_event_bookings/1, list_event_bookings/2,
|
||||
@@ -206,20 +207,31 @@ list_user_bookings(UserId) ->
|
||||
-spec list_user_booking_requests(UserId :: binary()) ->
|
||||
{ok, [{#booking{}, #event{}, owner | specialist}]}.
|
||||
list_user_booking_requests(UserId) ->
|
||||
{OwnedEventIds, SpecOnly} = managed_event_ids(UserId),
|
||||
OwnerItems = collect_pending(OwnedEventIds, owner),
|
||||
SpecItems = collect_pending(SpecOnly, specialist),
|
||||
{ok, sort_requests(OwnerItems ++ SpecItems)}.
|
||||
|
||||
-spec list_user_studio_bookings(UserId :: binary()) ->
|
||||
{ok, [{#booking{}, #event{}, owner | specialist}]}.
|
||||
list_user_studio_bookings(UserId) ->
|
||||
{OwnedEventIds, SpecOnly} = managed_event_ids(UserId),
|
||||
OwnerItems = collect_studio(OwnedEventIds, owner),
|
||||
SpecItems = collect_studio(SpecOnly, specialist),
|
||||
{ok, sort_requests(OwnerItems ++ SpecItems)}.
|
||||
|
||||
managed_event_ids(UserId) ->
|
||||
OwnedEventIds = owned_event_ids(UserId),
|
||||
SpecEventIds = specialist_event_ids(UserId),
|
||||
OwnedSet = sets:from_list(OwnedEventIds),
|
||||
SpecOnly = [E || E <- SpecEventIds, not sets:is_element(E, OwnedSet)],
|
||||
OwnerItems = collect_pending(OwnedEventIds, owner),
|
||||
SpecItems = collect_pending(SpecOnly, specialist),
|
||||
Items = sort_requests(OwnerItems ++ SpecItems),
|
||||
{ok, Items}.
|
||||
{OwnedEventIds, SpecOnly}.
|
||||
|
||||
owned_event_ids(UserId) ->
|
||||
case core_calendar:list_by_owner(UserId) of
|
||||
{ok, Cals} ->
|
||||
lists:flatmap(fun(#calendar{id = CalId}) ->
|
||||
case core_event:list_by_calendar(CalId) of
|
||||
case core_event:list_active_including_instances(CalId) of
|
||||
{ok, Events} -> [E#event.id || E <- Events];
|
||||
_ -> []
|
||||
end
|
||||
@@ -232,7 +244,7 @@ specialist_event_ids(UserId) ->
|
||||
Specs = [S || S <- core_calendar_specialist:list_by_user(UserId),
|
||||
S#calendar_specialist.status =:= active],
|
||||
lists:flatmap(fun(#calendar_specialist{calendar_id = CalId}) ->
|
||||
case core_event:list_by_calendar(CalId) of
|
||||
case core_event:list_active_including_instances(CalId) of
|
||||
{ok, Events} ->
|
||||
[E#event.id || E <- Events,
|
||||
is_binary(E#event.specialist_id),
|
||||
@@ -272,6 +284,37 @@ collect_pending(EventIds, Role) ->
|
||||
end
|
||||
end, EventIds).
|
||||
|
||||
collect_studio(EventIds, Role) ->
|
||||
NowSec = calendar:datetime_to_gregorian_seconds(calendar:universal_time()),
|
||||
lists:flatmap(fun(EventId) ->
|
||||
case core_booking:list_by_event(EventId) of
|
||||
{ok, Bookings} ->
|
||||
case core_event:get_by_id(EventId) of
|
||||
{ok, Event} ->
|
||||
lists:filtermap(fun(B) ->
|
||||
case B#booking.status of
|
||||
pending ->
|
||||
case event_started(Event, NowSec) of
|
||||
true ->
|
||||
_ = mark_expired(B#booking.id),
|
||||
false;
|
||||
false ->
|
||||
{true, {B, Event, Role}}
|
||||
end;
|
||||
confirmed ->
|
||||
{true, {B, Event, Role}};
|
||||
_ ->
|
||||
false
|
||||
end
|
||||
end, Bookings);
|
||||
_ ->
|
||||
[]
|
||||
end;
|
||||
_ ->
|
||||
[]
|
||||
end
|
||||
end, EventIds).
|
||||
|
||||
sort_requests(Items) ->
|
||||
lists:sort(fun({A, _, _}, {B, _, _}) ->
|
||||
A#booking.created_at >= B#booking.created_at
|
||||
|
||||
Reference in New Issue
Block a user