85 lines
3.2 KiB
Erlang
Executable File
85 lines
3.2 KiB
Erlang
Executable File
%%%-------------------------------------------------------------------
|
|
%%% @doc Логика специалистов commercial-календаря.
|
|
%%% @end
|
|
%%%-------------------------------------------------------------------
|
|
-module(logic_calendar_specialist).
|
|
-include("records.hrl").
|
|
|
|
-export([list/2, add/5, update/4, remove/3, to_json/1]).
|
|
|
|
-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
|
|
{ok, Cal} ->
|
|
case logic_calendar:can_access(ActorId, Cal) of
|
|
true -> {ok, core_calendar_specialist:list_by_calendar(CalendarId)};
|
|
false -> {error, access_denied}
|
|
end;
|
|
Error -> Error
|
|
end.
|
|
|
|
-spec add(OwnerId :: binary(), CalendarId :: binary(), UserId :: binary(),
|
|
Name :: binary(), Specs :: [binary()]) ->
|
|
{ok, #calendar_specialist{}} |
|
|
{error, not_found | access_denied | not_commercial | user_not_found |
|
|
already_exists | term()}.
|
|
add(OwnerId, CalendarId, UserId, Name, Specs) ->
|
|
case require_owner_commercial(OwnerId, CalendarId) of
|
|
{ok, _} ->
|
|
case core_user:get_by_id(UserId) of
|
|
{ok, _} ->
|
|
core_calendar_specialist:create(CalendarId, UserId, Name, Specs);
|
|
{error, _} ->
|
|
{error, user_not_found}
|
|
end;
|
|
Error -> Error
|
|
end.
|
|
|
|
-spec update(OwnerId :: binary(), CalendarId :: binary(), UserId :: binary(),
|
|
Updates :: [{atom(), term()}]) ->
|
|
{ok, #calendar_specialist{}} | {error, not_found | access_denied | not_commercial | term()}.
|
|
update(OwnerId, CalendarId, UserId, Updates) ->
|
|
case require_owner_commercial(OwnerId, CalendarId) of
|
|
{ok, _} ->
|
|
core_calendar_specialist:update(CalendarId, UserId, Updates);
|
|
Error -> Error
|
|
end.
|
|
|
|
-spec remove(OwnerId :: binary(), CalendarId :: binary(), UserId :: binary()) ->
|
|
ok | {error, not_found | access_denied | not_commercial | term()}.
|
|
remove(OwnerId, CalendarId, UserId) ->
|
|
case require_owner_commercial(OwnerId, CalendarId) of
|
|
{ok, _} ->
|
|
core_calendar_specialist:delete(CalendarId, UserId);
|
|
Error -> Error
|
|
end.
|
|
|
|
-spec to_json(#calendar_specialist{}) -> map().
|
|
to_json(S) ->
|
|
#{
|
|
id => S#calendar_specialist.id,
|
|
calendar_id => S#calendar_specialist.calendar_id,
|
|
user_id => S#calendar_specialist.user_id,
|
|
name => S#calendar_specialist.name,
|
|
specialization => S#calendar_specialist.specialization,
|
|
status => S#calendar_specialist.status,
|
|
added_at => handler_utils:datetime_to_iso8601(S#calendar_specialist.added_at),
|
|
updated_at => handler_utils:datetime_to_iso8601(S#calendar_specialist.updated_at)
|
|
}.
|
|
|
|
%%%===================================================================
|
|
%%% Internal
|
|
%%%===================================================================
|
|
|
|
require_owner_commercial(OwnerId, CalendarId) ->
|
|
case core_calendar:get_by_id(CalendarId) of
|
|
{ok, #calendar{owner_id = OwnerId, type = commercial, status = active} = Cal} ->
|
|
{ok, Cal};
|
|
{ok, #calendar{owner_id = OwnerId, type = personal}} ->
|
|
{error, not_commercial};
|
|
{ok, _} ->
|
|
{error, access_denied};
|
|
Error -> Error
|
|
end.
|