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)).
|
||||
|
||||
%%%===================================================================
|
||||
%%% Новые обёртки для админки
|
||||
%%%===================================================================
|
||||
|
||||
@@ -3,7 +3,8 @@
|
||||
-export([create_booking/2, confirm_booking/2, confirm_booking/3,
|
||||
cancel_booking/2, cancel_booking/3, get_booking/2,
|
||||
list_bookings/2, list_user_bookings/1, delete_booking/2,
|
||||
list_bookings_admin/0, get_booking_admin/1, list_event_bookings/1]).
|
||||
list_bookings_admin/0, get_booking_admin/1,
|
||||
list_event_bookings/1, list_event_bookings/2]).
|
||||
|
||||
%%%-------------------------------------------------------------------
|
||||
%%% @doc Создание бронирования со статусом `pending`.
|
||||
@@ -151,13 +152,37 @@ get_booking_admin(BookingId) ->
|
||||
core_booking:get_by_id(BookingId).
|
||||
|
||||
%%%-------------------------------------------------------------------
|
||||
%%% @doc Список бронирований события (административный).
|
||||
%%% @doc Список бронирований события (административный / внутренний).
|
||||
%%% @end
|
||||
%%%-------------------------------------------------------------------
|
||||
-spec list_event_bookings(EventId :: binary()) -> {ok, [#booking{}]}.
|
||||
list_event_bookings(EventId) ->
|
||||
core_booking:list_by_event(EventId).
|
||||
|
||||
%%%-------------------------------------------------------------------
|
||||
%%% @doc Список бронирований события для владельца календаря (или admin).
|
||||
%%% Возвращает полный список заявок без фильтрации по участнику.
|
||||
%%% @end
|
||||
%%%-------------------------------------------------------------------
|
||||
-spec list_event_bookings(UserId :: binary(), EventId :: binary()) ->
|
||||
{ok, [#booking{}]} | {error, not_found | access_denied}.
|
||||
list_event_bookings(UserId, EventId) ->
|
||||
case core_event:get_by_id(EventId) of
|
||||
{ok, Event} ->
|
||||
case core_calendar:get_by_id(Event#event.calendar_id) of
|
||||
{ok, Calendar} ->
|
||||
case admin_utils:is_admin(UserId)
|
||||
orelse Calendar#calendar.owner_id =:= UserId of
|
||||
true -> core_booking:list_by_event(EventId);
|
||||
false -> {error, access_denied}
|
||||
end;
|
||||
{error, not_found} ->
|
||||
{error, not_found}
|
||||
end;
|
||||
{error, not_found} ->
|
||||
{error, not_found}
|
||||
end.
|
||||
|
||||
%%%-------------------------------------------------------------------
|
||||
%%% @doc Список всех бронирований (административный).
|
||||
%%% @end
|
||||
|
||||
@@ -6,8 +6,6 @@
|
||||
-export([check_user_subscription/1, can_create_commercial_calendar/1]).
|
||||
-export([handle_expired_subscriptions/0]).
|
||||
|
||||
-define(TRIAL_DAYS, 30).
|
||||
|
||||
%% ============ Управление подписками ============
|
||||
|
||||
%% Начать пробный период (вызывается при первой попытке создать commercial календарь)
|
||||
@@ -22,6 +20,7 @@ start_trial(UserId) ->
|
||||
true ->
|
||||
{error, trial_already_used};
|
||||
false ->
|
||||
% trial_used=true — флаг; длительность из plan=trial (~1 месяц)
|
||||
case core_subscription:create(UserId, trial, true) of
|
||||
{ok, Subscription} ->
|
||||
{ok, Subscription};
|
||||
@@ -34,7 +33,7 @@ start_trial(UserId) ->
|
||||
activate_subscription(UserId, Plan, PaymentInfo) ->
|
||||
case process_payment(PaymentInfo, plan_price(Plan)) of
|
||||
ok ->
|
||||
% Проверяем, была ли у пользователя хоть одна подписка
|
||||
% Флаг trial_used: была ли уже любая подписка (не влияет на длительность)
|
||||
{ok, AllSubs} = core_subscription:list_by_user(UserId),
|
||||
TrialUsed = length(AllSubs) > 0,
|
||||
|
||||
@@ -45,7 +44,7 @@ activate_subscription(UserId, Plan, PaymentInfo) ->
|
||||
_ -> ok
|
||||
end,
|
||||
|
||||
% Создаём новую подписку
|
||||
% Длительность всегда из Plan (plan_to_months), TrialUsed — только флаг
|
||||
case core_subscription:create(UserId, Plan, TrialUsed) of
|
||||
{ok, Subscription} ->
|
||||
{ok, Subscription};
|
||||
|
||||
Reference in New Issue
Block a user