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) ->
|
||||
|
||||
@@ -325,10 +325,12 @@ format_event(Event) ->
|
||||
#location{address = Addr, lat = Lat, lon = Lon} ->
|
||||
#{address => Addr, lat => Lat, lon => Lon}
|
||||
end,
|
||||
CalendarTitle = case core_calendar:get_by_id(Event#event.calendar_id) of
|
||||
{ok, Cal} -> Cal#calendar.title;
|
||||
_ -> null
|
||||
end,
|
||||
{CalendarTitle, ImageUrl} = case core_calendar:get_by_id(Event#event.calendar_id) of
|
||||
{ok, #calendar{title = T, image_url = <<>>}} -> {T, null};
|
||||
{ok, #calendar{title = T, image_url = undefined}} -> {T, null};
|
||||
{ok, #calendar{title = T, image_url = Url}} -> {T, Url};
|
||||
_ -> {null, null}
|
||||
end,
|
||||
#{
|
||||
id => Event#event.id,
|
||||
calendar_id => Event#event.calendar_id,
|
||||
@@ -343,7 +345,8 @@ format_event(Event) ->
|
||||
capacity => Event#event.capacity,
|
||||
rating_avg => Event#event.rating_avg,
|
||||
rating_count => Event#event.rating_count,
|
||||
status => Event#event.status
|
||||
status => Event#event.status,
|
||||
image_url => ImageUrl
|
||||
}.
|
||||
|
||||
-spec format_calendars([#calendar{}]) -> [map()].
|
||||
@@ -359,6 +362,11 @@ format_calendar(Calendar) ->
|
||||
description => Calendar#calendar.description,
|
||||
type => Calendar#calendar.type,
|
||||
booking_open => logic_calendar:booking_open(Calendar),
|
||||
image_url => case Calendar#calendar.image_url of
|
||||
<<>> -> null;
|
||||
undefined -> null;
|
||||
Url -> Url
|
||||
end,
|
||||
tags => Calendar#calendar.tags,
|
||||
rating_avg => Calendar#calendar.rating_avg,
|
||||
rating_count => Calendar#calendar.rating_count,
|
||||
|
||||
Reference in New Issue
Block a user