Stage 8
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
-module(core_calendar).
|
||||
-include("records.hrl").
|
||||
|
||||
-export([create/4, get_by_id/1, list_by_owner/1, update/2, delete/1]).
|
||||
-export([create/4, create/5, get_by_id/1, list_by_owner/1, update/2, delete/1]).
|
||||
-export([generate_id/0]).
|
||||
|
||||
%% Создание календаря
|
||||
@@ -32,6 +32,34 @@ create(OwnerId, Title, Description, Confirmation) ->
|
||||
{aborted, Reason} -> {error, Reason}
|
||||
end.
|
||||
|
||||
%% Создание календаря с типом и политикой
|
||||
create(OwnerId, Title, Description, Confirmation, Type) ->
|
||||
Id = generate_id(),
|
||||
Calendar = #calendar{
|
||||
id = Id,
|
||||
owner_id = OwnerId,
|
||||
title = Title,
|
||||
description = Description,
|
||||
tags = [],
|
||||
type = Type,
|
||||
confirmation = Confirmation,
|
||||
rating_avg = 0.0,
|
||||
rating_count = 0,
|
||||
status = active,
|
||||
created_at = calendar:universal_time(),
|
||||
updated_at = calendar:universal_time()
|
||||
},
|
||||
|
||||
F = fun() ->
|
||||
mnesia:write(Calendar),
|
||||
{ok, Calendar}
|
||||
end,
|
||||
|
||||
case mnesia:transaction(F) of
|
||||
{atomic, Result} -> Result;
|
||||
{aborted, Reason} -> {error, Reason}
|
||||
end.
|
||||
|
||||
%% Получение календаря по ID
|
||||
get_by_id(Id) ->
|
||||
case mnesia:dirty_read(calendar, Id) of
|
||||
|
||||
@@ -0,0 +1,143 @@
|
||||
-module(core_subscription).
|
||||
-include("records.hrl").
|
||||
|
||||
-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]).
|
||||
|
||||
-define(TRIAL_DAYS, 30).
|
||||
|
||||
%% Создание подписки
|
||||
create(UserId, Plan, TrialUsed) ->
|
||||
Id = generate_id(),
|
||||
Now = calendar:universal_time(),
|
||||
|
||||
{StartDate, EndDate} = case TrialUsed of
|
||||
true ->
|
||||
% Платная подписка
|
||||
DurationMonths = plan_to_months(Plan),
|
||||
End = add_months(Now, DurationMonths),
|
||||
{Now, End};
|
||||
false ->
|
||||
% Пробный период
|
||||
End = add_days(Now, ?TRIAL_DAYS),
|
||||
{Now, End}
|
||||
end,
|
||||
|
||||
Subscription = #subscription{
|
||||
id = Id,
|
||||
user_id = UserId,
|
||||
plan = Plan,
|
||||
status = active,
|
||||
trial_used = TrialUsed,
|
||||
started_at = StartDate,
|
||||
expires_at = EndDate,
|
||||
created_at = Now,
|
||||
updated_at = Now
|
||||
},
|
||||
|
||||
F = fun() ->
|
||||
mnesia:write(Subscription),
|
||||
{ok, Subscription}
|
||||
end,
|
||||
|
||||
case mnesia:transaction(F) of
|
||||
{atomic, Result} -> Result;
|
||||
{aborted, Reason} -> {error, Reason}
|
||||
end.
|
||||
|
||||
%% Получение подписки по ID
|
||||
get_by_id(Id) ->
|
||||
case mnesia:dirty_read(subscription, Id) of
|
||||
[] -> {error, not_found};
|
||||
[Subscription] -> {ok, Subscription}
|
||||
end.
|
||||
|
||||
%% Получение активной подписки пользователя
|
||||
get_active_by_user(UserId) ->
|
||||
Match = #subscription{user_id = UserId, status = active, _ = '_'},
|
||||
case mnesia:dirty_match_object(Match) of
|
||||
[] -> {error, not_found};
|
||||
[Subscription] -> {ok, Subscription}
|
||||
end.
|
||||
|
||||
%% Список всех подписок пользователя
|
||||
list_by_user(UserId) ->
|
||||
Match = #subscription{user_id = UserId, _ = '_'},
|
||||
Subscriptions = mnesia:dirty_match_object(Match),
|
||||
{ok, lists:sort(fun(A, B) -> A#subscription.created_at >= B#subscription.created_at end, Subscriptions)}.
|
||||
|
||||
%% Список всех подписок (для админов)
|
||||
list_all() ->
|
||||
Match = #subscription{_ = '_'},
|
||||
Subscriptions = mnesia:dirty_match_object(Match),
|
||||
{ok, Subscriptions}.
|
||||
|
||||
%% Обновление статуса подписки
|
||||
update_status(Id, Status) when Status =:= active; Status =:= expired; Status =:= cancelled ->
|
||||
F = fun() ->
|
||||
case mnesia:read(subscription, Id) of
|
||||
[] ->
|
||||
{error, not_found};
|
||||
[Subscription] ->
|
||||
Updated = Subscription#subscription{
|
||||
status = Status,
|
||||
updated_at = calendar:universal_time()
|
||||
},
|
||||
mnesia:write(Updated),
|
||||
{ok, Updated}
|
||||
end
|
||||
end,
|
||||
|
||||
case mnesia:transaction(F) of
|
||||
{atomic, Result} -> Result;
|
||||
{aborted, Reason} -> {error, Reason}
|
||||
end.
|
||||
|
||||
%% Проверка истёкших подписок (вызывается периодически)
|
||||
check_expired() ->
|
||||
Now = calendar:universal_time(),
|
||||
Match = #subscription{status = active, _ = '_'},
|
||||
ActiveSubscriptions = mnesia:dirty_match_object(Match),
|
||||
|
||||
lists:foreach(fun(Sub) ->
|
||||
case Sub#subscription.expires_at < Now of
|
||||
true ->
|
||||
update_status(Sub#subscription.id, expired),
|
||||
% Если у пользователя нет другой активной подписки, понижаем календари
|
||||
case get_active_by_user(Sub#subscription.user_id) of
|
||||
{error, not_found} ->
|
||||
downgrade_user_calendars(Sub#subscription.user_id);
|
||||
_ -> ok
|
||||
end;
|
||||
false -> ok
|
||||
end
|
||||
end, ActiveSubscriptions).
|
||||
|
||||
%% Понижение календарей пользователя до personal при истечении подписки
|
||||
downgrade_user_calendars(UserId) ->
|
||||
Match = #calendar{owner_id = UserId, type = commercial, _ = '_'},
|
||||
Calendars = mnesia:dirty_match_object(Match),
|
||||
lists:foreach(fun(Cal) ->
|
||||
core_calendar:update(Cal#calendar.id, [{type, personal}])
|
||||
end, Calendars).
|
||||
|
||||
%% Внутренние функции
|
||||
generate_id() ->
|
||||
base64:encode(crypto:strong_rand_bytes(16), #{mode => urlsafe, padding => false}).
|
||||
|
||||
plan_to_months(monthly) -> 1;
|
||||
plan_to_months(quarterly) -> 3;
|
||||
plan_to_months(biannual) -> 6;
|
||||
plan_to_months(annual) -> 12;
|
||||
plan_to_months(trial) -> 1. % пробный период на 1 месяц
|
||||
|
||||
add_months(DateTime, Months) ->
|
||||
Seconds = calendar:datetime_to_gregorian_seconds(DateTime),
|
||||
Days = Seconds div 86400,
|
||||
NewDays = Days + (Months * 30),
|
||||
calendar:gregorian_seconds_to_datetime(NewDays * 86400).
|
||||
|
||||
add_days(DateTime, Days) ->
|
||||
Seconds = calendar:datetime_to_gregorian_seconds(DateTime),
|
||||
calendar:gregorian_seconds_to_datetime(Seconds + (Days * 86400)).
|
||||
Reference in New Issue
Block a user