feat(waitlist): optional event waitlist with FIFO promote.
CI / test (push) Successful in 7m12s
CI / deploy-ift (push) Successful in 2m44s
CI / e2e-ift (push) Successful in 1m28s
CI / deploy-stage (push) Successful in 2m3s
CI / e2e-stage (push) Successful in 1m24s

Commercial calendars opt in via settings.waitlist_enabled; join when full;
auto-promote on cancel/decline/expire with email and in-app notify.

Refs EventHub/EventHubBack#72
This commit is contained in:
2026-08-14 21:44:42 +03:00
parent 29111cd514
commit fb8869fa86
15 changed files with 730 additions and 9 deletions
+128
View File
@@ -0,0 +1,128 @@
%%%-------------------------------------------------------------------
%%% @doc Waitlist for events (Back#72).
%%% POST/DELETE/GET /v1/events/:id/waitlist
%%% @end
%%%-------------------------------------------------------------------
-module(handler_event_waitlist).
-behaviour(cowboy_handler).
-export([init/2, trails/0]).
-spec init(cowboy_req:req(), any()) -> {ok, cowboy_req:req(), any()}.
init(Req, _Opts) ->
case cowboy_req:method(Req) of
<<"POST">> -> join(Req);
<<"DELETE">> -> leave(Req);
<<"GET">> -> get_status(Req);
_ -> handler_utils:send_error(Req, 405, <<"Method not allowed">>)
end.
-spec trails() -> [map()].
trails() ->
[
#{
path => <<"/v1/events/:id/waitlist">>,
method => <<"POST">>,
description => <<"Join event waitlist (commercial, waitlist_enabled)">>,
tags => [<<"Waitlist">>],
responses => #{
200 => #{description => <<"Joined">>},
400 => #{description => <<"not_full / already_* / waitlist_disabled">>},
401 => #{description => <<"Unauthorized">>},
403 => #{description => <<"Forbidden">>},
404 => #{description => <<"Not found">>}
}
},
#{
path => <<"/v1/events/:id/waitlist">>,
method => <<"DELETE">>,
description => <<"Leave event waitlist">>,
tags => [<<"Waitlist">>],
responses => #{
200 => #{description => <<"Left">>},
401 => #{description => <<"Unauthorized">>},
404 => #{description => <<"Not on waitlist">>}
}
},
#{
path => <<"/v1/events/:id/waitlist">>,
method => <<"GET">>,
description => <<"Own waitlist status, or full list for owner/specialist">>,
tags => [<<"Waitlist">>],
responses => #{
200 => #{description => <<"Status">>},
401 => #{description => <<"Unauthorized">>},
404 => #{description => <<"Not found">>}
}
}
].
join(Req) ->
EventId = cowboy_req:binding(id, Req),
case handler_utils:auth_user(Req) of
{ok, UserId, Req1} ->
case logic_waitlist:join(UserId, EventId) of
{ok, Body} ->
handler_utils:send_json(Req1, 200, Body);
{error, not_found} ->
handler_utils:send_error(Req1, 404, <<"Event not found">>);
{error, personal_calendar} ->
handler_utils:send_error(Req1, 403, <<"personal_calendar">>);
{error, subscription_inactive} ->
handler_utils:send_error(Req1, 403, <<"subscription_inactive">>);
{error, own_event} ->
handler_utils:send_error(Req1, 403, <<"own_event">>);
{error, waitlist_disabled} ->
handler_utils:send_error(Req1, 400, <<"waitlist_disabled">>);
{error, not_full} ->
handler_utils:send_error(Req1, 400, <<"not_full">>);
{error, already_booked} ->
handler_utils:send_error(Req1, 400, <<"already_booked">>);
{error, already_on_waitlist} ->
handler_utils:send_error(Req1, 400, <<"already_on_waitlist">>);
{error, event_not_active} ->
handler_utils:send_error(Req1, 400, <<"event_not_active">>);
{error, Reason} when is_atom(Reason) ->
handler_utils:send_error(Req1, 400, atom_to_binary(Reason, utf8));
{error, _} ->
handler_utils:send_error(Req1, 400, <<"Invalid request">>)
end;
{error, Code, Message, Req1} ->
handler_utils:send_error(Req1, Code, Message)
end.
leave(Req) ->
EventId = cowboy_req:binding(id, Req),
case handler_utils:auth_user(Req) of
{ok, UserId, Req1} ->
case logic_waitlist:leave(UserId, EventId) of
ok ->
handler_utils:send_json(Req1, 200, #{ok => true});
{error, not_found} ->
handler_utils:send_error(Req1, 404, <<"Not on waitlist">>)
end;
{error, Code, Message, Req1} ->
handler_utils:send_error(Req1, Code, Message)
end.
get_status(Req) ->
EventId = cowboy_req:binding(id, Req),
case handler_utils:auth_user(Req) of
{ok, UserId, Req1} ->
case logic_waitlist:list_for_manager(UserId, EventId) of
{ok, Body} ->
handler_utils:send_json(Req1, 200, Body);
{error, access_denied} ->
case logic_waitlist:status_for_user(UserId, EventId) of
{ok, Body} ->
handler_utils:send_json(Req1, 200, Body);
{error, not_found} ->
handler_utils:send_error(Req1, 404, <<"Event not found">>);
{error, _} ->
handler_utils:send_error(Req1, 403, <<"Forbidden">>)
end;
{error, not_found} ->
handler_utils:send_error(Req1, 404, <<"Event not found">>)
end;
{error, Code, Message, Req1} ->
handler_utils:send_error(Req1, Code, Message)
end.