Раздел api Календари #22

This commit is contained in:
2026-05-25 17:50:57 +03:00
parent 0de375c499
commit c2d2e934d9
8 changed files with 635 additions and 5 deletions
+50 -1
View File
@@ -4,6 +4,7 @@
-export([create_calendar/3, create_calendar/4, get_calendar/2, list_calendars/1,
update_calendar/3, delete_calendar/2]).
-export([can_access/2, can_edit/2]).
-export([admin_list_all/0, admin_get_by_id/1, admin_update/2, admin_delete/1]).
%% Создание календаря с политикой по умолчанию (manual)
create_calendar(UserId, Title, Description) ->
@@ -113,4 +114,52 @@ validate_update({confirmation, Value}) ->
{timeout, N} when is_integer(N), N > 0 -> true;
_ -> false
end;
validate_update(_) -> false.
validate_update(_) -> false.
%% ─── Административные функции ────────────────────────────────────────
-spec admin_list_all() -> {ok, [map()]} | {error, term()}.
admin_list_all() ->
Calendars = core_calendar:list_all(),
{ok, Calendars}.
-spec admin_get_by_id(binary()) -> {ok, #calendar{}} | {error, not_found}.
admin_get_by_id(CalendarId) ->
core_calendar:get_by_id(CalendarId).
-spec admin_update(binary(), map()) -> {ok, #calendar{}} | {error, term()}.
admin_update(CalendarId, Updates) when is_map(Updates) ->
%% Преобразуем map в список {атом, значение}, исключая служебные ключи
TupleList = maps:fold(fun key_to_update/3, [], Updates),
core_calendar:update(CalendarId, TupleList).
-spec admin_delete(binary()) -> ok | {error, term()}.
admin_delete(Id) ->
case core_calendar:delete(Id) of
{ok, _} -> ok;
Error -> Error
end.
key_to_update(<<"title">>, V, Acc) -> [{title, V} | Acc];
key_to_update(<<"description">>, V, Acc) -> [{description, V} | Acc];
key_to_update(<<"short_name">>, V, Acc) -> [{short_name, V} | Acc];
key_to_update(<<"category">>, V, Acc) -> [{category, V} | Acc];
key_to_update(<<"color">>, V, Acc) -> [{color, V} | Acc];
key_to_update(<<"image_url">>, V, Acc) -> [{image_url, V} | Acc];
key_to_update(<<"settings">>, V, Acc) -> [{settings, V} | Acc];
key_to_update(<<"tags">>, V, Acc) -> [{tags, V} | Acc];
key_to_update(<<"type">>, V, Acc) ->
Type = binary_to_existing_atom(V, utf8),
[{type, Type} | Acc];
key_to_update(<<"status">>, V, Acc) ->
Status = binary_to_existing_atom(V, utf8),
[{status, Status} | Acc];
key_to_update(<<"confirmation">>, V, Acc) ->
% confirmation может быть строкой "auto", "manual" или "timeout:N"
Conf = case V of
<<"auto">> -> auto;
<<"manual">> -> manual;
_ -> V % для сложных значений пока оставляем как есть
end,
[{confirmation, Conf} | Acc];
key_to_update(<<"reason">>, V, Acc) -> [{reason, V} | Acc];
key_to_update(_, _V, Acc) -> Acc.