feat: GET /v1/user/booking-requests + search image_url. Fixes EventHub/EventHubBack#59
This commit is contained in:
@@ -2,7 +2,8 @@
|
||||
-include("records.hrl").
|
||||
-export([create_booking/2, confirm_booking/2, confirm_booking/3,
|
||||
cancel_booking/2, cancel_booking/3, get_booking/2,
|
||||
list_bookings/2, list_user_bookings/1, delete_booking/2,
|
||||
list_bookings/2, list_user_bookings/1, list_user_booking_requests/1,
|
||||
delete_booking/2,
|
||||
list_bookings_admin/0, get_booking_admin/1,
|
||||
list_event_bookings/1, list_event_bookings/2,
|
||||
process_timeout_bookings/0, cancel_pending_for_owner/1,
|
||||
@@ -165,6 +166,71 @@ list_bookings(EventId, UserId) ->
|
||||
list_user_bookings(UserId) ->
|
||||
core_booking:list_by_user(UserId).
|
||||
|
||||
%%%-------------------------------------------------------------------
|
||||
%%% @doc Pending bookings the user can confirm/decline as owner or specialist.
|
||||
%%% Returns `{ok, [{Booking, Event, Role}]}` where Role is `owner` | `specialist`.
|
||||
%%% @end
|
||||
%%%-------------------------------------------------------------------
|
||||
-spec list_user_booking_requests(UserId :: binary()) ->
|
||||
{ok, [{#booking{}, #event{}, owner | specialist}]}.
|
||||
list_user_booking_requests(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}.
|
||||
|
||||
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
|
||||
{ok, Events} -> [E#event.id || E <- Events];
|
||||
_ -> []
|
||||
end
|
||||
end, Cals);
|
||||
_ ->
|
||||
[]
|
||||
end.
|
||||
|
||||
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
|
||||
{ok, Events} ->
|
||||
[E#event.id || E <- Events,
|
||||
is_binary(E#event.specialist_id),
|
||||
E#event.specialist_id =/= <<>>,
|
||||
E#event.specialist_id =:= UserId];
|
||||
_ ->
|
||||
[]
|
||||
end
|
||||
end, Specs).
|
||||
|
||||
collect_pending(EventIds, Role) ->
|
||||
lists:flatmap(fun(EventId) ->
|
||||
case core_booking:list_by_event(EventId) of
|
||||
{ok, Bookings} ->
|
||||
case core_event:get_by_id(EventId) of
|
||||
{ok, Event} ->
|
||||
[{B, Event, Role} || B <- Bookings, B#booking.status =:= pending];
|
||||
_ ->
|
||||
[]
|
||||
end;
|
||||
_ ->
|
||||
[]
|
||||
end
|
||||
end, EventIds).
|
||||
|
||||
sort_requests(Items) ->
|
||||
lists:sort(fun({A, _, _}, {B, _, _}) ->
|
||||
A#booking.created_at >= B#booking.created_at
|
||||
end, Items).
|
||||
|
||||
-spec delete_booking(BookingId :: binary(), UserId :: binary()) ->
|
||||
ok | {error, not_found | access_denied}.
|
||||
delete_booking(BookingId, UserId) ->
|
||||
|
||||
Reference in New Issue
Block a user