Рефакторинг обработчиков. Часть 2 #21

This commit is contained in:
2026-05-11 21:51:45 +03:00
parent 6403f061df
commit 61bb44ab4a
31 changed files with 8391 additions and 1480 deletions
+78 -47
View File
@@ -1,56 +1,87 @@
%%%-------------------------------------------------------------------
%%% @doc Обработчик профиля текущего пользователя (клиентский API).
%%%
%%% GET – получить информацию о своём профиле.
%%% @end
%%%-------------------------------------------------------------------
-module(handler_user_me).
-include("records.hrl").
-behaviour(cowboy_handler).
-export([init/2]).
-export([trails/0]).
init(Req, Opts) -> handle(Req, Opts).
-include("records.hrl").
%%% cowboy_handler callback
-spec init(cowboy_req:req(), any()) -> {ok, cowboy_req:req(), any()}.
init(Req, Opts) ->
handle(Req, Opts).
%%% Swagger metadata
-spec trails() -> [map()].
trails() ->
[
#{
path => <<"/v1/user/me">>,
method => <<"GET">>,
description => <<"Get current user profile">>,
tags => [<<"Users">>],
responses => #{
200 => #{
description => <<"User profile">>,
content => #{<<"application/json">> => #{schema => user_schema()}}
},
401 => #{description => <<"Unauthorized">>},
404 => #{description => <<"User not found">>}
}
}
].
user_schema() ->
#{
type => object,
properties => #{
id => #{type => string},
email => #{type => string, format => <<"email">>},
role => #{type => string, enum => [<<"user">>, <<"bot">>]},
status => #{type => string, enum => [<<"active">>, <<"frozen">>, <<"deleted">>]},
reason => #{type => string, nullable => true},
nickname => #{type => string, nullable => true},
avatar_url => #{type => string, nullable => true},
timezone => #{type => string, nullable => true},
language => #{type => string, nullable => true},
social_links => #{type => array, items => #{type => string}, nullable => true},
phone => #{type => string, nullable => true},
preferences => #{type => object, nullable => true},
last_login => #{type => string, format => <<"date-time">>},
created_at => #{type => string, format => <<"date-time">>},
updated_at => #{type => string, format => <<"date-time">>}
}
}.
%%%===================================================================
%%% HTTP-методы
%%%===================================================================
%% @private
-spec handle(cowboy_req:req(), any()) -> {ok, cowboy_req:req(), any()}.
handle(Req, _Opts) ->
case cowboy_req:method(Req) of
<<"GET">> ->
case authenticate(Req) of
{ok, UserId, Req1} ->
case core_user:get_by_id(UserId) of
{ok, User} ->
Response = #{
id => User#user.id,
email => User#user.email,
role => User#user.role,
status => User#user.status,
created_at => User#user.created_at,
updated_at => User#user.updated_at
},
send_json(Req1, 200, Response);
{error, not_found} ->
send_error(Req1, 404, <<"User not found">>)
end;
{error, Code, Message, Req1} ->
send_error(Req1, Code, Message)
end;
_ ->
send_error(Req, 405, <<"Method not allowed">>)
<<"GET">> -> get_me(Req);
_ -> handler_utils:send_error(Req, 405, <<"Method not allowed">>)
end.
authenticate(Req) ->
case cowboy_req:parse_header(<<"authorization">>, Req) of
{bearer, Token} ->
case logic_auth:verify_jwt(Token) of
{ok, UserId, _Role} -> % ← теперь возвращается {ok, UserId, Role}
{ok, UserId, Req};
{error, expired} ->
{error, 401, <<"Token expired">>, Req};
{error, _} ->
{error, 401, <<"Invalid token">>, Req}
%% @doc GET /v1/user/me — получение профиля текущего пользователя.
-spec get_me(cowboy_req:req()) -> {ok, binary(), cowboy_req:req()}.
get_me(Req) ->
case handler_utils:auth_user(Req) of
{ok, UserId, Req1} ->
case core_user:get_by_id(UserId) of
{ok, User} ->
handler_utils:send_json(Req1, 200, handler_utils:user_to_json(User));
{error, not_found} ->
handler_utils:send_error(Req1, 404, <<"User not found">>)
end;
_ ->
{error, 401, <<"Missing or invalid Authorization header">>, Req}
end.
send_json(Req, Status, Data) ->
Body = jsx:encode(Data),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.
send_error(Req, Status, Message) ->
Body = jsx:encode(#{error => Message}),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.
{error, Code, Message, Req1} ->
handler_utils:send_error(Req1, Code, Message)
end.