84 lines
2.9 KiB
Erlang
84 lines
2.9 KiB
Erlang
-module(handler_admin_subscriptions).
|
|
-include("records.hrl").
|
|
|
|
-export([init/2]).
|
|
|
|
init(Req, Opts) ->
|
|
handle(Req, Opts).
|
|
|
|
handle(Req, _Opts) ->
|
|
case cowboy_req:method(Req) of
|
|
<<"GET">> -> list_subscriptions(Req);
|
|
<<"DELETE">> -> cancel_subscription(Req);
|
|
_ -> send_error(Req, 405, <<"Method not allowed">>)
|
|
end.
|
|
|
|
%% GET /v1/admin/subscriptions - список всех подписок
|
|
list_subscriptions(Req) ->
|
|
case handler_auth:authenticate(Req) of
|
|
{ok, AdminId, Req1} ->
|
|
case is_admin(AdminId) of
|
|
true ->
|
|
{ok, Subscriptions} = core_subscription:list_all(),
|
|
Response = [subscription_to_json(S) || S <- Subscriptions],
|
|
send_json(Req1, 200, Response);
|
|
false ->
|
|
send_error(Req1, 403, <<"Admin access required">>)
|
|
end;
|
|
{error, Code, Message, Req1} ->
|
|
send_error(Req1, Code, Message)
|
|
end.
|
|
|
|
%% DELETE /v1/admin/subscriptions/:id - отменить подписку
|
|
cancel_subscription(Req) ->
|
|
case handler_auth:authenticate(Req) of
|
|
{ok, AdminId, Req1} ->
|
|
SubscriptionId = cowboy_req:binding(id, Req1),
|
|
case logic_subscription:cancel_subscription(AdminId, SubscriptionId) of
|
|
{ok, Subscription} ->
|
|
Response = subscription_to_json(Subscription),
|
|
send_json(Req1, 200, Response);
|
|
{error, access_denied} ->
|
|
send_error(Req1, 403, <<"Admin access required">>);
|
|
{error, not_found} ->
|
|
send_error(Req1, 404, <<"Subscription not found">>);
|
|
{error, _} ->
|
|
send_error(Req1, 500, <<"Internal server error">>)
|
|
end;
|
|
{error, Code, Message, Req1} ->
|
|
send_error(Req1, Code, Message)
|
|
end.
|
|
|
|
%% Вспомогательные функции
|
|
is_admin(UserId) ->
|
|
case core_user:get_by_id(UserId) of
|
|
{ok, User} -> User#user.role =:= admin;
|
|
_ -> false
|
|
end.
|
|
|
|
subscription_to_json(Subscription) ->
|
|
#{
|
|
id => Subscription#subscription.id,
|
|
user_id => Subscription#subscription.user_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),
|
|
created_at => datetime_to_iso8601(Subscription#subscription.created_at),
|
|
updated_at => datetime_to_iso8601(Subscription#subscription.updated_at)
|
|
}.
|
|
|
|
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, []}. |