fix(api): resolve public calendar GET by unique short_name.
CI / test (push) Successful in 7m28s
CI / deploy-ift (push) Successful in 2m46s
CI / e2e-ift (push) Successful in 1m26s
CI / deploy-stage (push) Successful in 2m4s
CI / e2e-stage (push) Successful in 2m4s

This commit is contained in:
2026-08-14 15:00:41 +03:00
parent 658cbda4fe
commit 3d7ae0d372
8 changed files with 67 additions and 18 deletions
+24 -1
View File
@@ -1,6 +1,7 @@
-module(core_calendar).
-include("records.hrl").
-export([create/4, create/5, get_by_id/1, list_by_owner/1, update/2, delete/1]).
-export([create/4, create/5, get_by_id/1, get_by_short_name/1, get_by_id_or_short_name/1,
list_by_owner/1, update/2, delete/1]).
-export([count_calendars/0, list_all/0]).
-export([freeze/2, unfreeze/2]).
-export([count_calendars_by_date/2]).
@@ -78,6 +79,28 @@ get_by_id(Id) ->
[Calendar] -> {ok, Calendar}
end.
%% Unique public slug. Empty name is not a slug (many Default calendars).
-spec get_by_short_name(Name :: binary()) -> {ok, #calendar{}} | {error, not_found}.
get_by_short_name(<<>>) ->
{error, not_found};
get_by_short_name(Name) when is_binary(Name) ->
case mnesia:dirty_index_read(calendar, Name, #calendar.short_name) of
[] ->
{error, not_found};
Rows ->
case [C || C <- Rows, C#calendar.status =:= active] of
[C | _] -> {ok, C};
[] -> {error, not_found}
end
end.
-spec get_by_id_or_short_name(IdOrSlug :: binary()) -> {ok, #calendar{}} | {error, not_found}.
get_by_id_or_short_name(IdOrSlug) ->
case get_by_id(IdOrSlug) of
{ok, C} -> {ok, C};
{error, not_found} -> get_by_short_name(IdOrSlug)
end.
%%%-------------------------------------------------------------------
%%% @doc Список активных календарей владельца.
%%% @end
+2 -1
View File
@@ -121,7 +121,8 @@ get_calendar(Req) ->
case logic_calendar:get_calendar(UserId, CalendarId) of
{ok, Calendar} ->
Json0 = handler_utils:calendar_to_json(Calendar),
Following = UserId =/= <<>> andalso logic_calendar_follow:is_following(UserId, CalendarId),
Following = UserId =/= <<>> andalso
logic_calendar_follow:is_following(UserId, Calendar#calendar.id),
handler_utils:send_json(Req1, 200, Json0#{following => Following});
{error, access_denied} ->
handler_utils:send_error(Req1, 403, <<"Access denied">>);
+1 -1
View File
@@ -162,7 +162,7 @@ calendar_has_content(CalendarId) ->
%% Получение календаря с проверкой доступа
get_calendar(UserId, CalendarId) ->
case core_calendar:get_by_id(CalendarId) of
case core_calendar:get_by_id_or_short_name(CalendarId) of
{ok, Calendar} ->
case can_access(UserId, Calendar) of
true -> {ok, Calendar};
+5 -6
View File
@@ -15,7 +15,7 @@
-spec follow(UserId :: binary(), CalendarId :: binary()) ->
{ok, #calendar_follow{}} | {error, term()}.
follow(UserId, CalendarId) ->
case core_calendar:get_by_id(CalendarId) of
case core_calendar:get_by_id_or_short_name(CalendarId) of
{error, not_found} ->
{error, not_found};
{ok, #calendar{owner_id = UserId}} ->
@@ -27,7 +27,7 @@ follow(UserId, CalendarId) ->
true ->
case Calendar#calendar.status of
active ->
core_calendar_follow:follow(CalendarId, UserId);
core_calendar_follow:follow(Calendar#calendar.id, UserId);
_ ->
{error, not_found}
end
@@ -40,14 +40,13 @@ follow(UserId, CalendarId) ->
%%%-------------------------------------------------------------------
-spec unfollow(UserId :: binary(), CalendarId :: binary()) -> ok | {error, term()}.
unfollow(UserId, CalendarId) ->
case core_calendar:get_by_id(CalendarId) of
case core_calendar:get_by_id_or_short_name(CalendarId) of
{error, not_found} ->
%% всё равно снимаем локальный follow, если был
core_calendar_follow:unfollow(CalendarId, UserId);
{ok, #calendar{owner_id = UserId}} ->
{error, own_calendar};
{ok, _} ->
core_calendar_follow:unfollow(CalendarId, UserId)
{ok, Calendar} ->
core_calendar_follow:unfollow(Calendar#calendar.id, UserId)
end.
%%%-------------------------------------------------------------------
+2 -5
View File
@@ -10,12 +10,9 @@
-spec list(ActorId :: binary(), CalendarId :: binary()) ->
{ok, [#calendar_specialist{}]} | {error, not_found | access_denied}.
list(ActorId, CalendarId) ->
case core_calendar:get_by_id(CalendarId) of
case logic_calendar:get_calendar(ActorId, CalendarId) of
{ok, Cal} ->
case logic_calendar:can_access(ActorId, Cal) of
true -> {ok, core_calendar_specialist:list_by_calendar(CalendarId)};
false -> {error, access_denied}
end;
{ok, core_calendar_specialist:list_by_calendar(Cal#calendar.id)};
Error -> Error
end.
+4 -4
View File
@@ -25,7 +25,7 @@ create_event(UserId, CalendarId, Title, StartTime, Duration, Description) ->
{reject, Words} ->
{error, {content_banned, Words}};
{ok, Action, [Title2, Desc2], Words} ->
case core_event:create(CalendarId, Title2, StartTime, Duration) of
case core_event:create(Calendar#calendar.id, Title2, StartTime, Duration) of
{ok, Event} ->
case Desc2 of
<<>> -> ok;
@@ -64,7 +64,7 @@ create_recurring_event(UserId, CalendarId, Title, StartTime, Duration, RRule, De
{reject, Words} ->
{error, {content_banned, Words}};
{ok, Action, [Title2, Desc2], Words} ->
case core_event:create_recurring(CalendarId, Title2, StartTime, Duration, RRule) of
case core_event:create_recurring(Calendar#calendar.id, Title2, StartTime, Duration, RRule) of
{ok, Event} ->
case Desc2 of
<<>> -> ok;
@@ -184,8 +184,8 @@ get_event(UserId, EventId) ->
%% Список событий календаря
list_events(UserId, CalendarId) ->
case logic_calendar:get_calendar(UserId, CalendarId) of
{ok, _} ->
core_event:list_by_calendar(CalendarId);
{ok, Calendar} ->
core_event:list_by_calendar(Calendar#calendar.id);
Error ->
Error
end.