Stage 4
This commit is contained in:
+39
-30
@@ -3,7 +3,7 @@
|
||||
|
||||
-export([create_event/5, create_recurring_event/6, get_event/2, list_events/2,
|
||||
update_event/3, delete_event/2]).
|
||||
-export([validate_event_time/1, get_occurrences/3, cancel_occurrence/3]).
|
||||
-export([validate_event_time/1, validate_event_time/2, get_occurrences/3, cancel_occurrence/3]).
|
||||
-export([materialize_for_booking/3]).
|
||||
|
||||
%% Создание одиночного события
|
||||
@@ -12,7 +12,7 @@ create_event(UserId, CalendarId, Title, StartTime, Duration) ->
|
||||
{ok, Calendar} ->
|
||||
case logic_calendar:can_edit(UserId, Calendar) of
|
||||
true ->
|
||||
case validate_event_time(StartTime) of
|
||||
case validate_event_time(StartTime, UserId) of
|
||||
ok ->
|
||||
core_event:create(CalendarId, Title, StartTime, Duration);
|
||||
{error, _} = Error ->
|
||||
@@ -31,7 +31,7 @@ create_recurring_event(UserId, CalendarId, Title, StartTime, Duration, RRule) ->
|
||||
{ok, Calendar} ->
|
||||
case logic_calendar:can_edit(UserId, Calendar) of
|
||||
true ->
|
||||
case validate_event_time(StartTime) of
|
||||
case validate_event_time(StartTime, UserId) of
|
||||
ok ->
|
||||
case logic_recurrence:validate_rrule(RRule) of
|
||||
true ->
|
||||
@@ -49,7 +49,6 @@ create_recurring_event(UserId, CalendarId, Title, StartTime, Duration, RRule) ->
|
||||
Error
|
||||
end.
|
||||
|
||||
%% Получение вхождений повторяющегося события в диапазоне
|
||||
%% Получение вхождений повторяющегося события в диапазоне
|
||||
get_occurrences(UserId, MasterId, RangeEnd) ->
|
||||
case get_event(UserId, MasterId) of
|
||||
@@ -61,18 +60,12 @@ get_occurrences(UserId, MasterId, RangeEnd) ->
|
||||
end,
|
||||
{ok, ParsedRule} = logic_recurrence:parse_rrule(RRuleMap),
|
||||
|
||||
% Генерируем вхождения
|
||||
Occurrences = logic_recurrence:generate_occurrences(
|
||||
Event#event.start_time, ParsedRule, RangeEnd
|
||||
),
|
||||
|
||||
% Получаем исключения
|
||||
Exceptions = get_exceptions(MasterId),
|
||||
|
||||
% Фильтруем отменённые вхождения
|
||||
ValidOccurrences = filter_cancelled(Occurrences, Exceptions),
|
||||
|
||||
% Проверяем материализованные вхождения (могут иметь изменения)
|
||||
FinalOccurrences = merge_materialized(MasterId, ValidOccurrences),
|
||||
|
||||
{ok, FinalOccurrences};
|
||||
@@ -90,7 +83,6 @@ cancel_occurrence(UserId, MasterId, OccurrenceStart) ->
|
||||
{ok, Calendar} ->
|
||||
case logic_calendar:can_edit(UserId, Calendar) of
|
||||
true ->
|
||||
% Добавляем исключение
|
||||
Exception = #recurrence_exception{
|
||||
master_id = MasterId,
|
||||
original_start = OccurrenceStart,
|
||||
@@ -144,7 +136,7 @@ update_event(UserId, EventId, Updates) ->
|
||||
{ok, Calendar} ->
|
||||
case logic_calendar:can_edit(UserId, Calendar) of
|
||||
true ->
|
||||
ValidUpdates = validate_updates(Updates),
|
||||
ValidUpdates = validate_updates(Updates, UserId),
|
||||
core_event:update(EventId, ValidUpdates);
|
||||
false ->
|
||||
{error, access_denied}
|
||||
@@ -175,36 +167,53 @@ delete_event(UserId, EventId) ->
|
||||
Error
|
||||
end.
|
||||
|
||||
%% Валидация времени события
|
||||
%% Валидация времени события (без учёта пользователя)
|
||||
validate_event_time(StartTime) ->
|
||||
Now = calendar:universal_time(),
|
||||
case StartTime > Now of
|
||||
true -> ok;
|
||||
false -> {error, event_in_past}
|
||||
validate_event_time(StartTime, undefined).
|
||||
|
||||
%% Валидация времени события с учётом роли пользователя
|
||||
validate_event_time(StartTime, UserId) ->
|
||||
case is_admin(UserId) of
|
||||
true ->
|
||||
ok;
|
||||
false ->
|
||||
Now = calendar:universal_time(),
|
||||
case StartTime > Now of
|
||||
true -> ok;
|
||||
false -> {error, event_in_past}
|
||||
end
|
||||
end.
|
||||
|
||||
%% Проверка, является ли пользователь администратором
|
||||
is_admin(undefined) -> false;
|
||||
is_admin(UserId) ->
|
||||
case core_user:get_by_id(UserId) of
|
||||
{ok, User} -> User#user.role =:= admin;
|
||||
_ -> false
|
||||
end.
|
||||
|
||||
%% Внутренние функции
|
||||
validate_updates(Updates) ->
|
||||
lists:filter(fun validate_update/1, Updates).
|
||||
validate_updates(Updates, UserId) ->
|
||||
lists:filter(fun(Update) -> validate_update(Update, UserId) end, Updates).
|
||||
|
||||
validate_update({title, Value}) when is_binary(Value) -> true;
|
||||
validate_update({description, Value}) when is_binary(Value) -> true;
|
||||
validate_update({start_time, Value}) ->
|
||||
case validate_event_time(Value) of
|
||||
validate_update({title, Value}, _) when is_binary(Value) -> true;
|
||||
validate_update({description, Value}, _) when is_binary(Value) -> true;
|
||||
validate_update({start_time, Value}, UserId) ->
|
||||
case validate_event_time(Value, UserId) of
|
||||
ok -> true;
|
||||
_ -> false
|
||||
end;
|
||||
validate_update({duration, Value}) when is_integer(Value), Value > 0 -> true;
|
||||
validate_update({specialist_id, Value}) when is_binary(Value) -> true;
|
||||
validate_update({location, Value}) ->
|
||||
validate_update({duration, Value}, _) when is_integer(Value), Value > 0 -> true;
|
||||
validate_update({specialist_id, Value}, _) when is_binary(Value) -> true;
|
||||
validate_update({location, Value}, _) ->
|
||||
case Value of
|
||||
#location{} -> true;
|
||||
_ -> false
|
||||
end;
|
||||
validate_update({tags, Value}) when is_list(Value) -> true;
|
||||
validate_update({capacity, Value}) when is_integer(Value), Value > 0 -> true;
|
||||
validate_update({online_link, Value}) when is_binary(Value) -> true;
|
||||
validate_update(_) -> false.
|
||||
validate_update({tags, Value}, _) when is_list(Value) -> true;
|
||||
validate_update({capacity, Value}, _) when is_integer(Value), Value > 0 -> true;
|
||||
validate_update({online_link, Value}, _) when is_binary(Value) -> true;
|
||||
validate_update(_, _) -> false.
|
||||
|
||||
get_exceptions(MasterId) ->
|
||||
Match = #recurrence_exception{master_id = MasterId, _ = '_'},
|
||||
|
||||
Reference in New Issue
Block a user