Рефакторинг обработчиков. Часть 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
+113 -49
View File
@@ -1,35 +1,114 @@
%%%-------------------------------------------------------------------
%%% @doc Обработчик подписки текущего пользователя (клиентский API).
%%%
%%% GET – получить информацию о подписке.
%%% POST – активировать подписку или начать пробный период.
%%% @end
%%%-------------------------------------------------------------------
-module(handler_subscription).
-include("records.hrl").
-behaviour(cowboy_handler).
-export([init/2]).
-export([trails/0]).
-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() ->
[
#{ % GET
path => <<"/v1/subscription">>,
method => <<"GET">>,
description => <<"Get current user subscription">>,
tags => [<<"Subscription">>],
responses => #{
200 => #{
description => <<"Subscription details">>,
content => #{<<"application/json">> => #{schema => subscription_schema()}}
},
401 => #{description => <<"Unauthorized">>}
}
},
#{ % POST
path => <<"/v1/subscription">>,
method => <<"POST">>,
description => <<"Activate subscription or start trial">>,
tags => [<<"Subscription">>],
requestBody => #{
required => true,
content => #{<<"application/json">> => #{schema => #{
type => object,
required => [<<"action">>],
properties => #{
action => #{type => string, enum => [<<"start_trial">>, <<"activate">>]},
plan => #{type => string, enum => [<<"monthly">>, <<"quarterly">>, <<"biannual">>, <<"annual">>]},
payment_info => #{type => object, description => <<"Payment information">>}
}
}}}
},
responses => #{
201 => #{description => <<"Subscription activated or trial started">>},
400 => #{description => <<"Invalid action or JSON">>},
402 => #{description => <<"Payment failed">>},
409 => #{description => <<"Already has active subscription">>}
}
}
].
subscription_schema() ->
#{
type => object,
properties => #{
id => #{type => string},
user_id => #{type => string},
plan => #{type => string, enum => [<<"monthly">>, <<"quarterly">>, <<"biannual">>, <<"annual">>]},
status => #{type => string, enum => [<<"active">>, <<"expired">>, <<"cancelled">>]},
trial_used => #{type => boolean},
started_at => #{type => string, format => <<"date-time">>},
expires_at => #{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">> -> get_subscription(Req);
<<"GET">> -> get_subscription(Req);
<<"POST">> -> create_subscription(Req);
_ -> send_error(Req, 405, <<"Method not allowed">>)
_ -> handler_utils:send_error(Req, 405, <<"Method not allowed">>)
end.
%% GET /v1/subscription - получить подписку текущего пользователя
%% @doc GET /v1/subscription получить подписку текущего пользователя.
-spec get_subscription(cowboy_req:req()) -> {ok, binary(), cowboy_req:req()}.
get_subscription(Req) ->
case handler_auth:authenticate(Req) of
case handler_utils:auth_user(Req) of
{ok, UserId, Req1} ->
case logic_subscription:get_user_subscription(UserId) of
{ok, Subscription} ->
send_json(Req1, 200, Subscription);
handler_utils:send_json(Req1, 200, subscription_to_json(Subscription));
{error, _} ->
send_error(Req1, 500, <<"Internal server error">>)
handler_utils:send_error(Req1, 500, <<"Internal server error">>)
end;
{error, Code, Message, Req1} ->
send_error(Req1, Code, Message)
handler_utils:send_error(Req1, Code, Message)
end.
%% POST /v1/subscription - активировать подписку
%% @doc POST /v1/subscription активировать подписку.
-spec create_subscription(cowboy_req:req()) -> {ok, binary(), cowboy_req:req()}.
create_subscription(Req) ->
case handler_auth:authenticate(Req) of
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
@@ -39,11 +118,11 @@ create_subscription(Req) ->
case logic_subscription:start_trial(UserId) of
{ok, Subscription} ->
Response = subscription_to_json(Subscription),
send_json(Req2, 201, Response);
handler_utils:send_json(Req2, 201, Response);
{error, already_has_subscription} ->
send_error(Req2, 409, <<"Already has active subscription">>);
handler_utils:send_error(Req2, 409, <<"Already has active subscription">>);
{error, _} ->
send_error(Req2, 500, <<"Internal server error">>)
handler_utils:send_error(Req2, 500, <<"Internal server error">>)
end;
#{<<"action">> := <<"activate">>, <<"plan">> := PlanBin} ->
Plan = parse_plan(PlanBin),
@@ -51,54 +130,39 @@ create_subscription(Req) ->
case logic_subscription:activate_subscription(UserId, Plan, PaymentInfo) of
{ok, Subscription} ->
Response = subscription_to_json(Subscription),
send_json(Req2, 201, Response);
handler_utils:send_json(Req2, 201, Response);
{error, payment_failed} ->
send_error(Req2, 402, <<"Payment failed">>);
handler_utils:send_error(Req2, 402, <<"Payment failed">>);
{error, _} ->
send_error(Req2, 500, <<"Internal server error">>)
handler_utils:send_error(Req2, 500, <<"Internal server error">>)
end;
_ ->
send_error(Req2, 400, <<"Invalid action. Use 'start_trial' or 'activate'">>)
handler_utils:send_error(Req2, 400, <<"Invalid action. Use 'start_trial' or 'activate'">>)
end;
_ ->
send_error(Req2, 400, <<"Invalid JSON">>)
handler_utils:send_error(Req2, 400, <<"Invalid JSON">>)
catch
_:_ ->
send_error(Req2, 400, <<"Invalid JSON format">>)
_:_ -> handler_utils:send_error(Req2, 400, <<"Invalid JSON format">>)
end;
{error, Code, Message, Req1} ->
send_error(Req1, Code, Message)
handler_utils:send_error(Req1, Code, Message)
end.
%% Вспомогательные функции
parse_plan(<<"monthly">>) -> monthly;
%%%===================================================================
%%% Внутренние функции
%%%===================================================================
%% @private Преобразует бинарное имя плана в атом.
-spec parse_plan(binary()) -> monthly | quarterly | biannual | annual.
parse_plan(<<"monthly">>) -> monthly;
parse_plan(<<"quarterly">>) -> quarterly;
parse_plan(<<"biannual">>) -> biannual;
parse_plan(<<"annual">>) -> annual;
parse_plan(_) -> monthly.
parse_plan(<<"biannual">>) -> biannual;
parse_plan(<<"annual">>) -> annual;
parse_plan(_) -> monthly.
%% @private Формирует JSON-представление подписки (поддерживает как запись, так и карту).
-spec subscription_to_json(#subscription{} | map()) -> map().
subscription_to_json(Subscription) when is_tuple(Subscription) ->
#{
id => Subscription#subscription.id,
plan => Subscription#subscription.plan,
status => Subscription#subscription.status,
trial_used => Subscription#subscription.trial_used,
started_at => datetime_to_iso8601(Subscription#subscription.started_at),
expires_at => datetime_to_iso8601(Subscription#subscription.expires_at)
};
handler_utils:subscription_to_json(Subscription);
subscription_to_json(Subscription) when is_map(Subscription) ->
Subscription.
datetime_to_iso8601({{Year, Month, Day}, {Hour, Minute, Second}}) ->
iolist_to_binary(io_lib:format("~4..0B-~2..0B-~2..0BT~2..0B:~2..0B:~2..0BZ",
[Year, Month, Day, Hour, Minute, Second])).
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, []}.
Subscription.