feat: specialist_invite API + user lookup. Fixes EventHub/EventHubBack#55
CI / test (push) Successful in 6m47s
CI / deploy-ift (push) Successful in 3m23s
CI / e2e-ift (push) Successful in 1m58s
CI / deploy-stage (push) Successful in 2m1s
CI / e2e-stage (push) Successful in 1m13s

This commit is contained in:
2026-07-22 18:52:35 +03:00
parent af5f506866
commit 571f04737d
18 changed files with 1000 additions and 6 deletions
+147
View File
@@ -0,0 +1,147 @@
%%%-------------------------------------------------------------------
%%% @doc Owner: исходящие specialist invites.
%%%
%%% GET/POST /v1/calendars/:id/specialist-invites
%%% DELETE /v1/calendars/:id/specialist-invites/:invite_id
%%% @end
%%%-------------------------------------------------------------------
-module(handler_calendar_specialist_invites).
-behaviour(cowboy_handler).
-export([init/2, trails/0]).
-include("records.hrl").
init(Req, Opts) ->
handle(Req, Opts).
trails() ->
IdParam = #{name => <<"id">>, in => <<"path">>, required => true,
schema => #{type => string}},
InviteParam = #{name => <<"invite_id">>, in => <<"path">>, required => true,
schema => #{type => string}},
[
#{path => <<"/v1/calendars/:id/specialist-invites">>, method => <<"GET">>,
description => <<"List outgoing specialist invites">>, tags => [<<"Calendars">>],
parameters => [IdParam], responses => #{200 => #{description => <<"OK">>}}},
#{path => <<"/v1/calendars/:id/specialist-invites">>, method => <<"POST">>,
description => <<"Create specialist invite">>, tags => [<<"Calendars">>],
parameters => [IdParam], responses => #{201 => #{description => <<"Created">>}}},
#{path => <<"/v1/calendars/:id/specialist-invites/:invite_id">>, method => <<"DELETE">>,
description => <<"Cancel pending invite">>, tags => [<<"Calendars">>],
parameters => [IdParam, InviteParam], responses => #{200 => #{description => <<"OK">>}}}
].
handle(Req, _Opts) ->
Method = cowboy_req:method(Req),
InviteId = cowboy_req:binding(invite_id, Req),
case {Method, InviteId} of
{<<"GET">>, undefined} -> list_invites(Req);
{<<"POST">>, undefined} -> create_invite(Req);
{<<"DELETE">>, Id} when is_binary(Id) -> cancel_invite(Req);
_ -> handler_utils:send_error(Req, 405, <<"Method not allowed">>)
end.
list_invites(Req) ->
case handler_utils:auth_user(Req) of
{ok, OwnerId, Req1} ->
CalendarId = cowboy_req:binding(id, Req1),
case logic_specialist_invite:list_outgoing(OwnerId, CalendarId) of
{ok, List} ->
handler_utils:send_json(Req1, 200,
[logic_specialist_invite:to_json(I) || I <- List]);
{error, not_found} ->
handler_utils:send_error(Req1, 404, <<"Calendar not found">>);
{error, access_denied} ->
handler_utils:send_error(Req1, 403, <<"Access denied">>);
{error, not_commercial} ->
handler_utils:send_error(Req1, 400, <<"Calendar is not commercial">>);
{error, _} ->
handler_utils:send_error(Req1, 500, <<"Internal server error">>)
end;
{error, Code, Message, Req1} ->
handler_utils:send_error(Req1, Code, Message)
end.
create_invite(Req) ->
case handler_utils:auth_user(Req) of
{ok, OwnerId, Req1} ->
CalendarId = cowboy_req:binding(id, Req1),
{ok, Body, Req2} = cowboy_req:read_body(Req1),
try jsx:decode(Body, [return_maps]) of
Map when is_map(Map) ->
case parse_target(Map) of
{error, bad_request} ->
handler_utils:send_error(Req2, 400, <<"user_id or email required">>);
Target ->
Opts = #{
name => maps:get(<<"name">>, Map, <<>>),
specialization => case maps:get(<<"specialization">>, Map, []) of
L when is_list(L) -> L;
_ -> []
end
},
case logic_specialist_invite:create(OwnerId, CalendarId, Target, Opts) of
{ok, Inv} ->
handler_utils:send_json(Req2, 201, logic_specialist_invite:to_json(Inv));
{error, Reason} ->
map_create_error(Req2, Reason)
end
end;
_ ->
handler_utils:send_error(Req2, 400, <<"Invalid JSON">>)
catch
_:_ -> handler_utils:send_error(Req2, 400, <<"Invalid JSON">>)
end;
{error, Code, Message, Req1} ->
handler_utils:send_error(Req1, Code, Message)
end.
cancel_invite(Req) ->
case handler_utils:auth_user(Req) of
{ok, OwnerId, Req1} ->
CalendarId = cowboy_req:binding(id, Req1),
InviteId = cowboy_req:binding(invite_id, Req1),
case logic_specialist_invite:cancel(OwnerId, CalendarId, InviteId) of
{ok, Inv} ->
handler_utils:send_json(Req1, 200, logic_specialist_invite:to_json(Inv));
{error, not_found} ->
handler_utils:send_error(Req1, 404, <<"Not found">>);
{error, access_denied} ->
handler_utils:send_error(Req1, 403, <<"Access denied">>);
{error, not_commercial} ->
handler_utils:send_error(Req1, 400, <<"Calendar is not commercial">>);
{error, not_pending} ->
handler_utils:send_error(Req1, 409, <<"Invite is not pending">>);
{error, _} ->
handler_utils:send_error(Req1, 500, <<"Internal server error">>)
end;
{error, Code, Message, Req1} ->
handler_utils:send_error(Req1, Code, Message)
end.
parse_target(#{<<"user_id">> := UserId}) when is_binary(UserId), UserId =/= <<>> ->
#{user_id => UserId};
parse_target(#{<<"email">> := Email}) when is_binary(Email), Email =/= <<>> ->
#{email => Email};
parse_target(_) ->
{error, bad_request}.
map_create_error(Req, not_found) ->
handler_utils:send_error(Req, 404, <<"Calendar not found">>);
map_create_error(Req, access_denied) ->
handler_utils:send_error(Req, 403, <<"Access denied">>);
map_create_error(Req, not_commercial) ->
handler_utils:send_error(Req, 400, <<"Calendar is not commercial">>);
map_create_error(Req, subscription_inactive) ->
handler_utils:send_error(Req, 403, <<"subscription_inactive">>);
map_create_error(Req, user_not_found) ->
handler_utils:send_error(Req, 404, <<"User not found">>);
map_create_error(Req, already_specialist) ->
handler_utils:send_error(Req, 409, <<"Already a specialist">>);
map_create_error(Req, already_pending) ->
handler_utils:send_error(Req, 409, <<"Invite already pending">>);
map_create_error(Req, bad_request) ->
handler_utils:send_error(Req, 400, <<"user_id or email required">>);
map_create_error(Req, _) ->
handler_utils:send_error(Req, 500, <<"Internal server error">>).
+133
View File
@@ -0,0 +1,133 @@
%%%-------------------------------------------------------------------
%%% @doc Invitee: входящие invites + accept/decline.
%%%
%%% GET /v1/user/specialist-invites
%%% POST /v1/specialist-invites/:id/accept
%%% POST /v1/specialist-invites/:id/decline
%%% POST /v1/specialist-invites/accept body: {token}
%%% @end
%%%-------------------------------------------------------------------
-module(handler_specialist_invites).
-behaviour(cowboy_handler).
-export([init/2, trails/0]).
-include("records.hrl").
init(Req, Opts) ->
handle(Req, Opts).
trails() ->
IdParam = #{name => <<"id">>, in => <<"path">>, required => true,
schema => #{type => string}},
[
#{path => <<"/v1/user/specialist-invites">>, method => <<"GET">>,
description => <<"Incoming specialist invites">>, tags => [<<"Users">>],
responses => #{200 => #{description => <<"OK">>}}},
#{path => <<"/v1/specialist-invites/accept">>, method => <<"POST">>,
description => <<"Accept invite by email token">>, tags => [<<"Users">>],
responses => #{200 => #{description => <<"OK">>}}},
#{path => <<"/v1/specialist-invites/:id/accept">>, method => <<"POST">>,
description => <<"Accept specialist invite">>, tags => [<<"Users">>],
parameters => [IdParam], responses => #{200 => #{description => <<"OK">>}}},
#{path => <<"/v1/specialist-invites/:id/decline">>, method => <<"POST">>,
description => <<"Decline specialist invite">>, tags => [<<"Users">>],
parameters => [IdParam], responses => #{200 => #{description => <<"OK">>}}}
].
handle(Req, _Opts) ->
Method = cowboy_req:method(Req),
Path = cowboy_req:path(Req),
case Method of
<<"GET">> ->
list_incoming(Req);
<<"POST">> ->
case Path of
<<"/v1/specialist-invites/accept">> ->
accept_token(Req);
_ ->
case {cowboy_req:binding(id, Req), path_action(Path)} of
{Id, accept} when is_binary(Id) -> accept_id(Req, Id);
{Id, decline} when is_binary(Id) -> decline_id(Req, Id);
_ -> handler_utils:send_error(Req, 404, <<"Not found">>)
end
end;
_ ->
handler_utils:send_error(Req, 405, <<"Method not allowed">>)
end.
path_action(Path) ->
case binary:match(Path, <<"/accept">>) of
nomatch ->
case binary:match(Path, <<"/decline">>) of
nomatch -> unknown;
_ -> decline
end;
_ -> accept
end.
list_incoming(Req) ->
case handler_utils:auth_user(Req) of
{ok, UserId, Req1} ->
{ok, List} = logic_specialist_invite:list_incoming(UserId),
handler_utils:send_json(Req1, 200,
[logic_specialist_invite:to_json(I) || I <- List]);
{error, Code, Message, Req1} ->
handler_utils:send_error(Req1, Code, Message)
end.
accept_id(Req, InviteId) ->
case handler_utils:auth_user(Req) of
{ok, UserId, Req1} ->
reply_accept(Req1, logic_specialist_invite:accept(UserId, InviteId));
{error, Code, Message, Req1} ->
handler_utils:send_error(Req1, Code, Message)
end.
decline_id(Req, InviteId) ->
case handler_utils:auth_user(Req) of
{ok, UserId, Req1} ->
case logic_specialist_invite:decline(UserId, InviteId) of
{ok, Inv} ->
handler_utils:send_json(Req1, 200, logic_specialist_invite:to_json(Inv));
{error, Reason} ->
map_decide_error(Req1, Reason)
end;
{error, Code, Message, Req1} ->
handler_utils:send_error(Req1, Code, Message)
end.
accept_token(Req) ->
case handler_utils:auth_user(Req) of
{ok, UserId, Req1} ->
{ok, Body, Req2} = cowboy_req:read_body(Req1),
try jsx:decode(Body, [return_maps]) of
#{<<"token">> := Token} when is_binary(Token), Token =/= <<>> ->
reply_accept(Req2, logic_specialist_invite:accept_by_token(UserId, Token));
_ ->
handler_utils:send_error(Req2, 400, <<"token required">>)
catch
_:_ -> handler_utils:send_error(Req2, 400, <<"Invalid JSON">>)
end;
{error, Code, Message, Req1} ->
handler_utils:send_error(Req1, Code, Message)
end.
reply_accept(Req, {ok, Inv, Spec}) ->
handler_utils:send_json(Req, 200, #{
invite => logic_specialist_invite:to_json(Inv),
specialist => logic_calendar_specialist:to_json(Spec)
});
reply_accept(Req, {error, Reason}) ->
map_decide_error(Req, Reason).
map_decide_error(Req, not_found) ->
handler_utils:send_error(Req, 404, <<"Not found">>);
map_decide_error(Req, access_denied) ->
handler_utils:send_error(Req, 403, <<"Access denied">>);
map_decide_error(Req, not_pending) ->
handler_utils:send_error(Req, 409, <<"Invite is not pending">>);
map_decide_error(Req, expired) ->
handler_utils:send_error(Req, 410, <<"Invite expired">>);
map_decide_error(Req, _) ->
handler_utils:send_error(Req, 500, <<"Internal server error">>).
+47
View File
@@ -0,0 +1,47 @@
%%%-------------------------------------------------------------------
%%% @doc GET /v1/users/lookup?q= — typeahead для specialist invite.
%%% @end
%%%-------------------------------------------------------------------
-module(handler_users_lookup).
-behaviour(cowboy_handler).
-export([init/2, trails/0]).
init(Req, Opts) ->
handle(Req, Opts).
trails() ->
[
#{
path => <<"/v1/users/lookup">>,
method => <<"GET">>,
description => <<"Lookup users by email/nickname (typeahead)">>,
tags => [<<"Users">>],
parameters => [
#{name => <<"q">>, in => <<"query">>, required => true,
schema => #{type => string}}
],
responses => #{200 => #{description => <<"OK">>}}
}
].
handle(Req, _Opts) ->
case cowboy_req:method(Req) of
<<"GET">> -> lookup(Req);
_ -> handler_utils:send_error(Req, 405, <<"Method not allowed">>)
end.
lookup(Req) ->
case handler_utils:auth_user(Req) of
{ok, _UserId, Req1} ->
Qs = cowboy_req:parse_qs(Req1),
Q = proplists:get_value(<<"q">>, Qs, <<>>),
case logic_user_lookup:lookup(Q) of
{ok, List} ->
handler_utils:send_json(Req1, 200, List);
{error, bad_request} ->
handler_utils:send_error(Req1, 400, <<"q too short">>)
end;
{error, Code, Message, Req1} ->
handler_utils:send_error(Req1, Code, Message)
end.