Перенести все админские эндпоинты на порт 8445 и добавить отдельную авторизацию для админов. Часть 2. Final #3
This commit is contained in:
@@ -4,6 +4,12 @@
|
||||
-export([create/3, get_by_id/1, get_active_by_user/1, list_by_user/1, list_all/0]).
|
||||
-export([update_status/2, check_expired/0]).
|
||||
-export([generate_id/0]).
|
||||
% --------------- новые обёртки для админки ------------------
|
||||
-export([list_subscriptions/0,
|
||||
create_subscription/1,
|
||||
update_subscription/2,
|
||||
delete_subscription/1
|
||||
]).
|
||||
|
||||
-define(TRIAL_DAYS, 30).
|
||||
|
||||
@@ -140,4 +146,72 @@ add_months(DateTime, Months) ->
|
||||
|
||||
add_days(DateTime, Days) ->
|
||||
Seconds = calendar:datetime_to_gregorian_seconds(DateTime),
|
||||
calendar:gregorian_seconds_to_datetime(Seconds + (Days * 86400)).
|
||||
calendar:gregorian_seconds_to_datetime(Seconds + (Days * 86400)).
|
||||
|
||||
% ================================================================
|
||||
% Новые обёртки для совместимости с admin_handler_subscriptions
|
||||
% ================================================================
|
||||
|
||||
list_subscriptions() ->
|
||||
{ok, Subs} = list_all(),
|
||||
Subs.
|
||||
|
||||
create_subscription(Data) ->
|
||||
UserId = maps:get(<<"user_id">>, Data),
|
||||
Plan = case maps:get(<<"plan">>, Data, <<"monthly">>) of
|
||||
<<"monthly">> -> monthly;
|
||||
<<"yearly">> -> yearly;
|
||||
<<"quarterly">>-> quarterly;
|
||||
<<"biannual">> -> biannual;
|
||||
<<"annual">> -> annual;
|
||||
Other -> Other
|
||||
end,
|
||||
TrialUsed = maps:get(<<"trial_used">>, Data, false),
|
||||
create(UserId, Plan, TrialUsed).
|
||||
|
||||
update_subscription(Id, Updates) ->
|
||||
case get_by_id(Id) of
|
||||
{ok, Sub} ->
|
||||
Updated = apply_updates(Sub, Updates),
|
||||
mnesia:dirty_write(Updated),
|
||||
{ok, Updated};
|
||||
Error -> Error
|
||||
end.
|
||||
|
||||
delete_subscription(Id) ->
|
||||
case get_by_id(Id) of
|
||||
{ok, _Sub} ->
|
||||
mnesia:dirty_delete({subscription, Id}),
|
||||
{ok, deleted};
|
||||
Error -> Error
|
||||
end.
|
||||
|
||||
%% Применение обновлений к записи подписки
|
||||
apply_updates(Sub, Updates) ->
|
||||
lists:foldl(fun({Key, Value}, Acc) ->
|
||||
case Key of
|
||||
<<"status">> ->
|
||||
NewStatus = case Value of
|
||||
<<"active">> -> active;
|
||||
<<"cancelled">> -> cancelled;
|
||||
<<"expired">> -> expired;
|
||||
Other -> Other
|
||||
end,
|
||||
Acc#subscription{status = NewStatus, updated_at = calendar:universal_time()};
|
||||
<<"plan">> ->
|
||||
NewPlan = case Value of
|
||||
<<"monthly">> -> monthly;
|
||||
<<"yearly">> -> yearly;
|
||||
<<"quarterly">>-> quarterly;
|
||||
<<"biannual">> -> biannual;
|
||||
<<"annual">> -> annual;
|
||||
Other -> Other
|
||||
end,
|
||||
Acc#subscription{plan = NewPlan, updated_at = calendar:universal_time()};
|
||||
<<"trial_used">> ->
|
||||
Acc#subscription{trial_used = Value, updated_at = calendar:universal_time()};
|
||||
<<"expires_at">> ->
|
||||
Acc#subscription{expires_at = Value, updated_at = calendar:universal_time()};
|
||||
_ -> Acc
|
||||
end
|
||||
end, Sub, maps:to_list(Updates)).
|
||||
Reference in New Issue
Block a user