fix: persist specialist_id on event create; 402 commercial without active sub. Fixes EventHub/EventHubBack#61
This commit is contained in:
@@ -409,9 +409,26 @@ register_and_login(Email, Password) ->
|
||||
|
||||
-spec create_calendar(binary(), map()) -> binary().
|
||||
create_calendar(Token, Params) ->
|
||||
Type = maps:get(type, Params, maps:get(<<"type">>, Params, <<"personal">>)),
|
||||
case Type of
|
||||
<<"commercial">> -> ensure_commercial_subscription(Token);
|
||||
commercial -> ensure_commercial_subscription(Token);
|
||||
_ -> ok
|
||||
end,
|
||||
#{<<"id">> := CalId} = client_post(<<"/v1/calendars">>, Token, Params),
|
||||
CalId.
|
||||
|
||||
-spec ensure_commercial_subscription(binary()) -> ok.
|
||||
ensure_commercial_subscription(Token) ->
|
||||
case client_request(post, <<"/v1/subscription">>, Token,
|
||||
jsx:encode(#{action => <<"start_trial">>})) of
|
||||
{ok, 201, _, _} -> ok;
|
||||
{ok, 200, _, _} -> ok;
|
||||
{ok, 409, _, _} -> ok; %% already has subscription / trial used with active
|
||||
{ok, 400, _, _} -> ok; %% trial_already_used — may still have active paid
|
||||
_ -> ok
|
||||
end.
|
||||
|
||||
-spec create_event(binary(), binary(), map()) -> binary().
|
||||
create_event(Token, CalId, Params) ->
|
||||
Path = <<"/v1/calendars/", CalId/binary, "/events">>,
|
||||
|
||||
@@ -31,6 +31,7 @@ logic_calendar_test_() ->
|
||||
fun cleanup/1,
|
||||
[
|
||||
{"Create calendar test", fun test_create_calendar/0},
|
||||
{"Create commercial requires subscription", fun test_create_commercial_gate/0},
|
||||
{"Get calendar test", fun test_get_calendar/0},
|
||||
{"List calendars test", fun test_list_calendars/0},
|
||||
{"Update calendar test", fun test_update_calendar/0},
|
||||
@@ -66,6 +67,18 @@ test_create_calendar() ->
|
||||
?assertEqual(personal, Calendar#calendar.type),
|
||||
?assertEqual(Confirmation, Calendar#calendar.confirmation).
|
||||
|
||||
test_create_commercial_gate() ->
|
||||
UserId = create_test_user(),
|
||||
?assertMatch({error, subscription_required},
|
||||
logic_calendar:create_calendar(UserId, <<"Studio">>, <<>>, manual, commercial)),
|
||||
{ok, Personal} = logic_calendar:create_calendar(UserId, <<"Personal">>, <<>>, manual, personal),
|
||||
?assertEqual(personal, Personal#calendar.type),
|
||||
?assertMatch({error, subscription_required},
|
||||
logic_calendar:update_calendar(UserId, Personal#calendar.id, [{type, commercial}])),
|
||||
{ok, _} = logic_subscription:start_trial(UserId),
|
||||
{ok, Commercial} = logic_calendar:create_calendar(UserId, <<"Studio">>, <<>>, manual, commercial),
|
||||
?assertEqual(commercial, Commercial#calendar.type).
|
||||
|
||||
test_get_calendar() ->
|
||||
UserId = create_test_user(),
|
||||
{ok, Calendar} = logic_calendar:create_calendar(UserId, <<"Test">>, <<"">>, manual),
|
||||
@@ -92,6 +105,7 @@ test_list_calendars() ->
|
||||
test_update_calendar() ->
|
||||
UserId = create_test_user(),
|
||||
{ok, Calendar} = logic_calendar:create_calendar(UserId, <<"Original">>, <<"">>, manual),
|
||||
{ok, _} = logic_subscription:start_trial(UserId),
|
||||
|
||||
Updates = [{title, <<"Updated">>}, {type, commercial}, {confirmation, auto}],
|
||||
{ok, Updated} = logic_calendar:update_calendar(UserId, Calendar#calendar.id, Updates),
|
||||
@@ -122,6 +136,7 @@ test_access_control() ->
|
||||
?assertNot(logic_calendar:can_edit(OtherId, PersonalCalendar)),
|
||||
?assertNot(logic_calendar:can_access(OtherId, PersonalCalendar)),
|
||||
|
||||
{ok, _} = logic_subscription:start_trial(OwnerId),
|
||||
{ok, CommercialCalendar} = logic_calendar:update_calendar(OwnerId, PersonalCalendar#calendar.id, [{type, commercial}]),
|
||||
?assert(logic_calendar:can_access(OtherId, CommercialCalendar)),
|
||||
?assertNot(logic_calendar:can_edit(OtherId, CommercialCalendar)),
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
-include_lib("eunit/include/eunit.hrl").
|
||||
-include("records.hrl").
|
||||
|
||||
-define(TABLES, [user, calendar, event]).
|
||||
-define(TABLES, [user, calendar, event, calendar_specialist, subscription]).
|
||||
|
||||
setup() ->
|
||||
eh_test_support:start_mnesia(),
|
||||
@@ -24,7 +24,8 @@ logic_event_test_() ->
|
||||
{"List events test", fun test_list_events/0},
|
||||
{"Update event test", fun test_update_event/0},
|
||||
{"Delete event test", fun test_delete_event/0},
|
||||
{"Event time validation test", fun test_time_validation/0}
|
||||
{"Event time validation test", fun test_time_validation/0},
|
||||
{"Persist specialist_id on update", fun test_specialist_id_persist/0}
|
||||
]}.
|
||||
|
||||
create_test_user_and_calendar() ->
|
||||
@@ -43,6 +44,26 @@ create_test_user_and_calendar() ->
|
||||
{ok, Calendar} = logic_calendar:create_calendar(UserId, <<"Test Calendar">>, <<"">>, manual),
|
||||
{UserId, Calendar#calendar.id}.
|
||||
|
||||
create_commercial_with_specialist() ->
|
||||
OwnerId = base64:encode(crypto:strong_rand_bytes(12), #{padding => false}),
|
||||
SpecId = base64:encode(crypto:strong_rand_bytes(12), #{padding => false}),
|
||||
Owner = #user{
|
||||
id = OwnerId, email = <<"owner@ex.com">>, password_hash = <<"hash">>,
|
||||
role = user, status = active,
|
||||
created_at = calendar:universal_time(), updated_at = calendar:universal_time()
|
||||
},
|
||||
Spec = #user{
|
||||
id = SpecId, email = <<"spec@ex.com">>, password_hash = <<"hash">>,
|
||||
role = user, status = active,
|
||||
created_at = calendar:universal_time(), updated_at = calendar:universal_time()
|
||||
},
|
||||
mnesia:dirty_write(Owner),
|
||||
mnesia:dirty_write(Spec),
|
||||
{ok, _} = logic_subscription:start_trial(OwnerId),
|
||||
{ok, Cal} = logic_calendar:create_calendar(OwnerId, <<"Studio">>, <<>>, manual, commercial),
|
||||
{ok, _} = core_calendar_specialist:create(Cal#calendar.id, SpecId, <<"Spec">>, []),
|
||||
{OwnerId, SpecId, Cal#calendar.id}.
|
||||
|
||||
add_days(DateTime, Days) ->
|
||||
Sec = calendar:datetime_to_gregorian_seconds(DateTime) + Days * 86400,
|
||||
calendar:gregorian_seconds_to_datetime(Sec).
|
||||
@@ -102,3 +123,14 @@ test_time_validation() ->
|
||||
|
||||
FutureTime = eh_test_support:future_start(),
|
||||
?assertEqual(ok, logic_event:validate_event_time(FutureTime)).
|
||||
|
||||
test_specialist_id_persist() ->
|
||||
{OwnerId, SpecId, CalendarId} = create_commercial_with_specialist(),
|
||||
StartTime = eh_test_support:future_start(),
|
||||
{ok, Event} = logic_event:create_event(OwnerId, CalendarId, <<"Slot">>, StartTime, 60),
|
||||
?assertEqual(<<>>, Event#event.specialist_id),
|
||||
{ok, Updated} = logic_event:update_event(OwnerId, Event#event.id, [{specialist_id, SpecId}]),
|
||||
?assertEqual(SpecId, Updated#event.specialist_id),
|
||||
BadSpec = base64:encode(crypto:strong_rand_bytes(12), #{padding => false}),
|
||||
?assertMatch({error, invalid_specialist},
|
||||
logic_event:update_event(OwnerId, Event#event.id, [{specialist_id, BadSpec}])).
|
||||
|
||||
@@ -18,7 +18,7 @@ logic_subscription_test_() ->
|
||||
{"Check subscription - free", fun test_check_free/0},
|
||||
{"Check subscription - trial", fun test_check_trial/0},
|
||||
{"Check subscription - paid", fun test_check_paid/0},
|
||||
{"Can create commercial - free (auto-trial)", fun test_can_create_commercial_free/0},
|
||||
{"Can create commercial - free (no auto-trial)", fun test_can_create_commercial_free/0},
|
||||
{"Can create commercial - trial", fun test_can_create_commercial_trial/0},
|
||||
{"Can create commercial - paid", fun test_can_create_commercial_paid/0},
|
||||
{"Get user subscription - free", fun test_get_user_subscription_free/0},
|
||||
@@ -127,9 +127,9 @@ test_check_paid() ->
|
||||
{ok, active, monthly} = logic_subscription:check_user_subscription(UserId).
|
||||
|
||||
test_can_create_commercial_free() ->
|
||||
% Новый пользователь автоматически получает пробный период при проверке
|
||||
% Без активной подписки/trial create commercial запрещён (trial — явно через start_trial)
|
||||
UserId = create_test_user(),
|
||||
?assert(logic_subscription:can_create_commercial_calendar(UserId)).
|
||||
?assertNot(logic_subscription:can_create_commercial_calendar(UserId)).
|
||||
|
||||
test_can_create_commercial_trial() ->
|
||||
UserId = create_test_user(),
|
||||
|
||||
Reference in New Issue
Block a user