fix: owner event bookings list and paid plan durations from plan_to_months.
Fixes EventHub/EventHubBack#51 Fixes EventHub/EventHubBack#52 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -8,35 +8,27 @@
|
||||
-export([count_subscriptions_by_plan/0, count_subscriptions_by_status/0,
|
||||
count_trial_subscriptions/0, get_ending_paid_subscriptions/1]).
|
||||
|
||||
-define(TRIAL_DAYS, 30).
|
||||
|
||||
%%%-------------------------------------------------------------------
|
||||
%%% @doc Создание подписки.
|
||||
%%% `TrialUsed` – `true`, если подписка платная; `false` для пробного периода.
|
||||
%%% `TrialUsed` – флаг (использован ли trial); на длительность не влияет.
|
||||
%%% Длительность всегда считается из `Plan` через `plan_to_months/1`.
|
||||
%%% Все поля записи инициализированы, `undefined` не возникает.
|
||||
%%% @end
|
||||
%%%-------------------------------------------------------------------
|
||||
-spec create(UserId :: binary(), Plan :: monthly | quarterly | biannual | annual,
|
||||
-spec create(UserId :: binary(), Plan :: monthly | quarterly | biannual | annual | trial,
|
||||
TrialUsed :: boolean()) -> {ok, #subscription{}} | {error, term()}.
|
||||
create(UserId, Plan, TrialUsed) ->
|
||||
Id = infra_utils:generate_id(16),
|
||||
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,
|
||||
DurationMonths = plan_to_months(Plan),
|
||||
EndDate = add_months(Now, DurationMonths),
|
||||
Subscription = #subscription{
|
||||
id = Id,
|
||||
user_id = UserId,
|
||||
plan = Plan,
|
||||
status = active,
|
||||
trial_used = TrialUsed,
|
||||
started_at = StartDate,
|
||||
started_at = Now,
|
||||
expires_at = EndDate,
|
||||
created_at = Now,
|
||||
updated_at = Now
|
||||
@@ -167,11 +159,6 @@ add_months(DateTime, Months) ->
|
||||
NewDays = Days + (Months * 30),
|
||||
calendar:gregorian_seconds_to_datetime(NewDays * 86400).
|
||||
|
||||
-spec add_days(calendar:datetime(), pos_integer()) -> calendar:datetime().
|
||||
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