fix: persist specialist_id on event create; 402 commercial without active sub. Fixes EventHub/EventHubBack#61
This commit is contained in:
@@ -117,23 +117,20 @@ create_calendar(Req) ->
|
||||
Confirmation = parse_confirmation(maps:get(<<"confirmation">>, Decoded, <<"manual">>)),
|
||||
Tags = maps:get(<<"tags">>, Decoded, []),
|
||||
Type = parse_type(maps:get(<<"type">>, Decoded, <<"personal">>)),
|
||||
case Type of
|
||||
commercial ->
|
||||
case logic_subscription:can_create_commercial_calendar(UserId) of
|
||||
true -> ok;
|
||||
false ->
|
||||
handler_utils:send_error(Req2, 402, <<"Subscription required for commercial calendar">>),
|
||||
throw(stop)
|
||||
end;
|
||||
personal -> ok
|
||||
end,
|
||||
case logic_calendar:create_calendar(UserId, Title, Description, Confirmation) of
|
||||
case logic_calendar:create_calendar(UserId, Title, Description, Confirmation, Type) of
|
||||
{ok, Calendar} ->
|
||||
Updates = [{tags, Tags}, {type, Type}],
|
||||
core_calendar:update(Calendar#calendar.id, Updates),
|
||||
{ok, Updated} = core_calendar:get_by_id(Calendar#calendar.id),
|
||||
Updated = case Tags of
|
||||
[] -> Calendar;
|
||||
_ ->
|
||||
case logic_calendar:update_calendar(UserId, Calendar#calendar.id, [{tags, Tags}]) of
|
||||
{ok, C2} -> C2;
|
||||
_ -> Calendar
|
||||
end
|
||||
end,
|
||||
Response = calendar_to_json(Updated),
|
||||
handler_utils:send_json(Req2, 201, Response);
|
||||
{error, subscription_required} ->
|
||||
handler_utils:send_error(Req2, 402, <<"Subscription required for commercial calendar">>);
|
||||
{error, user_inactive} ->
|
||||
handler_utils:send_error(Req2, 403, <<"User account is not active">>);
|
||||
{error, {content_banned, _Words}} ->
|
||||
@@ -147,7 +144,6 @@ create_calendar(Req) ->
|
||||
_ ->
|
||||
handler_utils:send_error(Req2, 400, <<"Invalid JSON">>)
|
||||
catch
|
||||
throw:stop -> ok;
|
||||
_:_ -> handler_utils:send_error(Req2, 400, <<"Invalid JSON format">>)
|
||||
end;
|
||||
{error, Code, Message, Req1} ->
|
||||
|
||||
@@ -188,6 +188,8 @@ update_event(Req) ->
|
||||
handler_utils:send_error(Req2, 400, <<"Event cannot be in the past">>);
|
||||
{error, {content_banned, _}} ->
|
||||
handler_utils:send_error(Req2, 400, <<"Content contains banned words">>);
|
||||
{error, invalid_specialist} ->
|
||||
handler_utils:send_error(Req2, 400, <<"Invalid specialist_id for this calendar">>);
|
||||
{error, _} ->
|
||||
handler_utils:send_error(Req2, 500, <<"Internal server error">>)
|
||||
end;
|
||||
|
||||
@@ -140,6 +140,7 @@ event_create_schema() ->
|
||||
lon => #{type => number, format => float}
|
||||
}
|
||||
},
|
||||
specialist_id => #{type => string, nullable => true},
|
||||
recurrence => #{type => object, description => <<"Recurrence rule (RFC 5545)">>}
|
||||
}
|
||||
}.
|
||||
@@ -176,10 +177,7 @@ create_event(Req) ->
|
||||
undefined ->
|
||||
case logic_event:create_event(UserId, CalendarId, Title, StartTime, Duration, Description) of
|
||||
{ok, Event} ->
|
||||
update_event_fields(UserId, Event#event.id, Location, Decoded),
|
||||
{ok, UpdatedEvent} = core_event:get_by_id(Event#event.id),
|
||||
Response = handler_utils:event_to_json(UpdatedEvent),
|
||||
handler_utils:send_json(Req2, 201, Response);
|
||||
finish_create_event(Req2, UserId, Event, Location, Decoded);
|
||||
{error, access_denied} ->
|
||||
handler_utils:send_error(Req2, 403, <<"Access denied">>);
|
||||
{error, not_found} ->
|
||||
@@ -194,10 +192,7 @@ create_event(Req) ->
|
||||
RRule ->
|
||||
case logic_event:create_recurring_event(UserId, CalendarId, Title, StartTime, Duration, RRule, Description) of
|
||||
{ok, Event} ->
|
||||
update_event_fields(UserId, Event#event.id, Location, Decoded),
|
||||
{ok, UpdatedEvent} = core_event:get_by_id(Event#event.id),
|
||||
Response = handler_utils:event_to_json(UpdatedEvent),
|
||||
handler_utils:send_json(Req2, 201, Response);
|
||||
finish_create_event(Req2, UserId, Event, Location, Decoded);
|
||||
{error, invalid_rrule} ->
|
||||
handler_utils:send_error(Req2, 400, <<"Invalid recurrence rule">>);
|
||||
{error, access_denied} ->
|
||||
@@ -262,6 +257,21 @@ list_events(Req) ->
|
||||
%%% Вспомогательные функции
|
||||
%%%===================================================================
|
||||
|
||||
finish_create_event(Req, UserId, Event, Location, Decoded) ->
|
||||
case update_event_fields(UserId, Event#event.id, Location, Decoded) of
|
||||
ok ->
|
||||
{ok, UpdatedEvent} = core_event:get_by_id(Event#event.id),
|
||||
Response = handler_utils:event_to_json(UpdatedEvent),
|
||||
handler_utils:send_json(Req, 201, Response);
|
||||
{error, invalid_specialist} ->
|
||||
_ = logic_event:delete_event(UserId, Event#event.id),
|
||||
handler_utils:send_error(Req, 400, <<"Invalid specialist_id for this calendar">>);
|
||||
{error, _} ->
|
||||
{ok, UpdatedEvent} = core_event:get_by_id(Event#event.id),
|
||||
Response = handler_utils:event_to_json(UpdatedEvent),
|
||||
handler_utils:send_json(Req, 201, Response)
|
||||
end.
|
||||
|
||||
update_event_fields(UserId, EventId, Location, Decoded) ->
|
||||
Updates = [],
|
||||
Updates1 = case Location of undefined -> Updates; _ -> [{location, Location} | Updates] end,
|
||||
@@ -269,12 +279,18 @@ update_event_fields(UserId, EventId, Location, Decoded) ->
|
||||
Updates3 = case maps:get(<<"tags">>, Decoded, undefined) of undefined -> Updates2; Tags -> [{tags, Tags} | Updates2] end,
|
||||
%% description already applied in create_event/6 when present
|
||||
Updates4 = case maps:get(<<"online_link">>, Decoded, undefined) of undefined -> Updates3; Link -> [{online_link, Link} | Updates3] end,
|
||||
case Updates4 of
|
||||
Updates5 = case maps:get(<<"specialist_id">>, Decoded, undefined) of
|
||||
undefined -> Updates4;
|
||||
null -> [{specialist_id, <<>>} | Updates4];
|
||||
SpecId when is_binary(SpecId) -> [{specialist_id, SpecId} | Updates4];
|
||||
_ -> Updates4
|
||||
end,
|
||||
case Updates5 of
|
||||
[] -> ok;
|
||||
_ ->
|
||||
case logic_event:update_event(UserId, EventId, Updates4) of
|
||||
case logic_event:update_event(UserId, EventId, Updates5) of
|
||||
{ok, _} -> ok;
|
||||
_ -> ok
|
||||
{error, _} = Err -> Err
|
||||
end
|
||||
end.
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
-module(logic_calendar).
|
||||
-include("records.hrl").
|
||||
|
||||
-export([create_calendar/3, create_calendar/4, get_calendar/2, list_calendars/1,
|
||||
-export([create_calendar/3, create_calendar/4, create_calendar/5, get_calendar/2, list_calendars/1,
|
||||
update_calendar/3, delete_calendar/2, ensure_default_calendar/1]).
|
||||
-export([can_access/2, can_edit/2, booking_open/1]).
|
||||
-export([admin_list_all/0, admin_get_by_id/1, admin_update/2, admin_delete/1]).
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
|
||||
%% ============ Управление подписками ============
|
||||
|
||||
%% Начать пробный период (вызывается при первой попытке создать commercial календарь)
|
||||
%% Начать пробный период (явно через POST /v1/subscription action=start_trial)
|
||||
start_trial(UserId) ->
|
||||
case core_subscription:get_active_by_user(UserId) of
|
||||
{ok, _} ->
|
||||
@@ -103,29 +103,12 @@ check_user_subscription(UserId) ->
|
||||
{ok, free, free}
|
||||
end.
|
||||
|
||||
%% Проверить, может ли пользователь создавать коммерческие календари
|
||||
%% Если у пользователя нет активной подписки, но он ещё не использовал пробный период,
|
||||
%% автоматически запускаем пробный период
|
||||
%% Проверить, может ли пользователь создавать/апгрейдить commercial-календарь.
|
||||
%% Только при уже активной подписке/trial; trial стартует явно через start_trial/1.
|
||||
can_create_commercial_calendar(UserId) ->
|
||||
case check_user_subscription(UserId) of
|
||||
{ok, active, _} ->
|
||||
true;
|
||||
{ok, free, free} ->
|
||||
% Пользователь без подписки - проверяем, не использовал ли он уже пробный период
|
||||
{ok, AllSubs} = core_subscription:list_by_user(UserId),
|
||||
TrialUsed = lists:any(fun(S) -> S#subscription.trial_used == true orelse S#subscription.plan == trial end, AllSubs),
|
||||
case TrialUsed of
|
||||
false ->
|
||||
% Автоматически запускаем пробный период
|
||||
case start_trial(UserId) of
|
||||
{ok, _} -> true;
|
||||
_ -> false
|
||||
end;
|
||||
true ->
|
||||
false
|
||||
end;
|
||||
_ ->
|
||||
false
|
||||
{ok, active, _} -> true;
|
||||
_ -> false
|
||||
end.
|
||||
|
||||
%% ============ Обслуживание ============
|
||||
|
||||
Reference in New Issue
Block a user