feat: PATCH /v1/user/me — профиль и смена пароля. Refs EventHub/EventHubBack#48
CI / test (push) Failing after 9m43s
CI / deploy-ift (push) Has been skipped
CI / e2e-ift (push) Has been skipped
CI / deploy-stage (push) Has been skipped
CI / e2e-stage (push) Has been skipped

This commit is contained in:
2026-07-19 23:26:34 +03:00
parent 5b638de450
commit 0934e2c413
6 changed files with 652 additions and 25 deletions
+86 -5
View File
@@ -1,7 +1,8 @@
%%%-------------------------------------------------------------------
%%% @doc Обработчик профиля текущего пользователя (клиентский API).
%%%
%%% GET – получить информацию о своём профиле.
%%% GET – получить информацию о своём профиле.
%%% PATCH – частичное обновление профиля / смена пароля.
%%% @end
%%%-------------------------------------------------------------------
-module(handler_user_me).
@@ -34,6 +35,26 @@ trails() ->
401 => #{description => <<"Unauthorized">>},
404 => #{description => <<"User not found">>}
}
},
#{
path => <<"/v1/user/me">>,
method => <<"PATCH">>,
description => <<"Update current user profile (partial) or change password">>,
tags => [<<"Users">>],
requestBody => #{
required => true,
content => #{<<"application/json">> => #{schema => user_update_schema()}}
},
responses => #{
200 => #{
description => <<"Updated profile">>,
content => #{<<"application/json">> => #{schema => user_schema()}}
},
400 => #{description => <<"Invalid body / unknown field">>},
401 => #{description => <<"Unauthorized">>},
403 => #{description => <<"Wrong current password or account not active">>},
404 => #{description => <<"User not found">>}
}
}
].
@@ -49,7 +70,7 @@ user_schema() ->
nickname => #{type => string, nullable => true},
avatar_url => #{type => string, nullable => true},
timezone => #{type => string, nullable => true},
language => #{type => string, nullable => true},
language => #{type => string, enum => [<<"ru">>, <<"en">>], nullable => true},
social_links => #{type => array, items => #{type => string}, nullable => true},
phone => #{type => string, nullable => true},
preferences => #{type => object, nullable => true},
@@ -59,6 +80,23 @@ user_schema() ->
}
}.
user_update_schema() ->
#{
type => object,
properties => #{
language => #{type => string, enum => [<<"ru">>, <<"en">>]},
nickname => #{type => string, nullable => true},
timezone => #{type => string, nullable => true},
phone => #{type => string, nullable => true},
avatar_url => #{type => string, nullable => true},
preferences => #{type => object, nullable => true},
password => #{type => string, format => <<"password">>,
description => <<"New password; requires current_password">>},
current_password => #{type => string, format => <<"password">>,
description => <<"Current password; required with password">>}
}
}.
%%%===================================================================
%%% HTTP-методы
%%%===================================================================
@@ -67,8 +105,9 @@ user_schema() ->
-spec handle(cowboy_req:req(), any()) -> {ok, cowboy_req:req(), any()}.
handle(Req, _Opts) ->
case cowboy_req:method(Req) of
<<"GET">> -> get_me(Req);
_ -> handler_utils:send_error(Req, 405, <<"Method not allowed">>)
<<"GET">> -> get_me(Req);
<<"PATCH">> -> patch_me(Req);
_ -> handler_utils:send_error(Req, 405, <<"Method not allowed">>)
end.
%% @doc GET /v1/user/me — получение профиля текущего пользователя.
@@ -84,4 +123,46 @@ get_me(Req) ->
end;
{error, Code, Message, Req1} ->
handler_utils:send_error(Req1, Code, Message)
end.
end.
%% @doc PATCH /v1/user/me — частичное обновление профиля / смена пароля.
-spec patch_me(cowboy_req:req()) -> {ok, binary(), cowboy_req:req()}.
patch_me(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
Decoded when is_map(Decoded) ->
case logic_user:update_me(UserId, Decoded) of
{ok, User} ->
handler_utils:send_json(Req2, 200, handler_utils:user_to_json(User));
{error, not_found} ->
handler_utils:send_error(Req2, 404, <<"User not found">>);
{error, wrong_password} ->
handler_utils:send_error(Req2, 403, <<"Wrong current password">>);
{error, forbidden} ->
handler_utils:send_error(Req2, 403, <<"Account is not active">>);
{error, unknown_field} ->
handler_utils:send_error(Req2, 400, <<"Unknown field">>);
{error, empty_body} ->
handler_utils:send_error(Req2, 400, <<"Empty update body">>);
{error, password_pair_required} ->
handler_utils:send_error(Req2, 400,
<<"password and current_password must be sent together">>);
{error, invalid_password} ->
handler_utils:send_error(Req2, 400, <<"Invalid password fields">>);
{error, invalid_language} ->
handler_utils:send_error(Req2, 400, <<"language must be ru or en">>);
{error, Reason} when is_atom(Reason) ->
handler_utils:send_error(Req2, 400, atom_to_binary(Reason, utf8));
{error, _} ->
handler_utils:send_error(Req2, 400, <<"Invalid request">>)
end;
_ ->
handler_utils:send_error(Req2, 400, <<"Invalid JSON">>)
catch
_:_ -> handler_utils:send_error(Req2, 400, <<"Invalid JSON format">>)
end;
{error, Code, Message, Req1} ->
handler_utils:send_error(Req1, Code, Message)
end.