feat: create default personal calendar on email verification. Refs EventHub/EventHubBack#49

This commit is contained in:
2026-07-20 11:17:31 +03:00
parent 2440c50330
commit 7c1fe1940d
4 changed files with 80 additions and 6 deletions
+38 -1
View File
@@ -2,7 +2,7 @@
-include("records.hrl").
-export([create_calendar/3, create_calendar/4, get_calendar/2, list_calendars/1,
update_calendar/3, delete_calendar/2]).
update_calendar/3, delete_calendar/2, ensure_default_calendar/1]).
-export([can_access/2, can_edit/2]).
-export([admin_list_all/0, admin_get_by_id/1, admin_update/2, admin_delete/1]).
@@ -50,6 +50,43 @@ create_calendar(UserId, Title, Description, Confirmation, Type) ->
{error, user_not_found}
end.
%% @doc Создаёт дефолтный personal-календарь после активации пользователя.
%% Идемпотентно: если у владельца уже есть active personal — ok.
-spec ensure_default_calendar(UserId :: binary()) -> ok | {error, term()}.
ensure_default_calendar(UserId) ->
case has_active_personal_calendar(UserId) of
true ->
ok;
false ->
case core_user:get_by_id(UserId) of
{ok, User} ->
Title = default_calendar_title(User),
case create_calendar(UserId, Title, <<>>, manual, personal) of
{ok, _} -> ok;
Error -> Error
end;
Error ->
Error
end
end.
has_active_personal_calendar(UserId) ->
case core_calendar:list_by_owner(UserId) of
{ok, Calendars} ->
lists:any(
fun(#calendar{type = personal}) -> true;
(_) -> false
end,
Calendars);
_ ->
false
end.
default_calendar_title(#user{nickname = Nick}) when is_binary(Nick), byte_size(Nick) > 0 ->
Nick;
default_calendar_title(_) ->
<<"Мой календарь">>.
%% Получение календаря с проверкой доступа
get_calendar(UserId, CalendarId) ->
case core_calendar:get_by_id(CalendarId) of