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.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user