Files
EventHubBack/src/handlers/handler_calendars.erl
T
aleksey 331000a464
CI / test (push) Failing after 6m37s
CI / deploy-ift (push) Has been skipped
CI / e2e-ift (push) Has been skipped
CI / deploy-stage (push) Has been skipped
CI / e2e-stage (push) Has been skipped
feat(share): calendar_share invite/ACL for personal and commercial.
Co-editor and deputy grants with mirror_to_default; personal out of search; list owned+shared. Refs EventHub/EventHubBack#73
2026-08-15 23:20:11 +03:00

218 lines
8.9 KiB
Erlang

%%%-------------------------------------------------------------------
%%% @doc Обработчик маршрута `/v1/calendars`.
%%%
%%% POST – создание нового календаря (требуется подписка для commercial).
%%% GET – получение списка календарей пользователя.
%%% @end
%%%-------------------------------------------------------------------
-module(handler_calendars).
-behaviour(cowboy_handler).
-export([init/2]).
-export([trails/0]).
-include("records.hrl").
%%% cowboy_handler callback
-spec init(cowboy_req:req(), any()) -> {ok, cowboy_req:req(), any()}.
init(Req0, _State) ->
case cowboy_req:method(Req0) of
<<"POST">> -> create_calendar(Req0);
<<"GET">> -> list_calendars(Req0);
_ -> handler_utils:send_error(Req0, 405, <<"Method not allowed">>)
end.
%%% Swagger metadata
-spec trails() -> [map()].
trails() ->
[
#{ % POST
path => <<"/v1/calendars">>,
method => <<"POST">>,
description => <<"Create a new calendar">>,
tags => [<<"Calendars">>],
requestBody => #{
required => true,
content => #{<<"application/json">> => #{schema => calendar_create_schema()}}
},
responses => #{
201 => #{description => <<"Calendar created">>},
400 => #{description => <<"Missing required fields or invalid JSON">>},
402 => #{description => <<"Subscription required for commercial calendar">>},
403 => #{description => <<"User account is not active">>}
}
},
#{ % GET
path => <<"/v1/calendars">>,
method => <<"GET">>,
description => <<"List calendars of current user">>,
tags => [<<"Calendars">>],
responses => #{
200 => #{
description => <<"Array of calendars">>,
content => #{<<"application/json">> => #{schema => #{
type => array,
items => calendar_schema()
}}}
}
}
}
].
-spec calendar_schema() -> map().
calendar_schema() ->
#{
type => object,
properties => #{
id => #{type => string},
owner_id => #{type => string},
title => #{type => string},
description => #{type => string},
short_name => #{type => string, nullable => true},
category => #{type => string, nullable => true},
color => #{type => string, nullable => true},
image_url => #{type => string, nullable => true},
settings => #{type => object, nullable => true},
tags => #{type => array, items => #{type => string}},
type => #{type => string, enum => [<<"personal">>, <<"commercial">>]},
confirmation => #{type => string, description => <<"auto, manual, or {timeout, N}">>},
rating_avg => #{type => number, format => float},
rating_count => #{type => integer},
status => #{type => string, enum => [<<"active">>, <<"frozen">>, <<"deleted">>]},
reason => #{type => string, nullable => true},
created_at => #{type => string, format => <<"date-time">>},
updated_at => #{type => string, format => <<"date-time">>}
}
}.
-spec calendar_create_schema() -> map().
calendar_create_schema() ->
#{
type => object,
required => [<<"title">>],
properties => #{
title => #{type => string},
description => #{type => string},
confirmation => #{type => string, description => <<"auto, manual, or {timeout, N}">>},
tags => #{type => array, items => #{type => string}},
type => #{type => string, enum => [<<"personal">>, <<"commercial">>]}
}
}.
%%%===================================================================
%%% HTTP-методы
%%%===================================================================
%% @doc POST /v1/calendars — создание календаря.
-spec create_calendar(cowboy_req:req()) -> {ok, binary(), cowboy_req:req()}.
create_calendar(Req) ->
case handler_utils:auth_user(Req) of
{ok, UserId, Req1} ->
{ok, Body, Req2} = cowboy_req:read_body(Req1),
try jsx:decode(Body, [return_maps]) of
Decoded when is_map(Decoded) ->
case Decoded of
#{<<"title">> := Title} ->
Description = maps:get(<<"description">>, Decoded, <<"">>),
Confirmation = parse_confirmation(maps:get(<<"confirmation">>, Decoded, <<"manual">>)),
Tags = maps:get(<<"tags">>, Decoded, []),
Type = parse_type(maps:get(<<"type">>, Decoded, <<"personal">>)),
Settings = maps:get(<<"settings">>, Decoded, undefined),
case logic_calendar:create_calendar(UserId, Title, Description, Confirmation, Type) of
{ok, Calendar} ->
Extra0 = case Tags of
[] -> [];
_ -> [{tags, Tags}]
end,
Extra = case Settings of
S when is_map(S) -> [{settings, S} | Extra0];
_ -> Extra0
end,
Updated = case Extra of
[] -> Calendar;
_ ->
case logic_calendar:update_calendar(UserId, Calendar#calendar.id, Extra) of
{ok, C2} -> C2;
_ -> Calendar
end
end,
Response = calendar_to_json(Updated),
handler_utils:send_json(Req2, 201, Response);
{error, subscription_required} ->
handler_utils:send_error(Req2, 402, <<"Subscription required for commercial calendar">>);
{error, personal_exists} ->
handler_utils:send_error(Req2, 409, <<"personal_exists">>);
{error, user_inactive} ->
handler_utils:send_error(Req2, 403, <<"User account is not active">>);
{error, {content_banned, _Words}} ->
handler_utils:send_error(Req2, 400, <<"Content contains banned words">>);
{error, _} ->
handler_utils:send_error(Req2, 500, <<"Internal server error">>)
end;
_ ->
handler_utils:send_error(Req2, 400, <<"Missing required field: title">>)
end;
_ ->
handler_utils:send_error(Req2, 400, <<"Invalid JSON">>)
catch
_:_ -> handler_utils:send_error(Req2, 400, <<"Invalid JSON format">>)
end;
{error, Code, Message, Req1} ->
handler_utils:send_error(Req1, Code, Message)
end.
%% @doc GET /v1/calendars — список календарей пользователя.
-spec list_calendars(cowboy_req:req()) -> {ok, binary(), cowboy_req:req()}.
list_calendars(Req) ->
case handler_utils:auth_user(Req) of
{ok, UserId, Req1} ->
case logic_calendar:list_calendars(UserId) of
{ok, Calendars} ->
Response = [calendar_list_json(UserId, C) || C <- Calendars],
handler_utils:send_json(Req1, 200, Response);
{error, _} ->
handler_utils:send_error(Req1, 500, <<"Internal server error">>)
end;
{error, Code, Message, Req1} ->
handler_utils:send_error(Req1, Code, Message)
end.
%%%===================================================================
%%% Внутренние функции
%%%===================================================================
-spec calendar_list_json(binary(), #calendar{}) -> map().
calendar_list_json(UserId, #calendar{owner_id = UserId} = Calendar) ->
(calendar_to_json(Calendar))#{role => <<"owner">>};
calendar_list_json(UserId, Calendar) ->
Base = (calendar_to_json(Calendar))#{role => <<"shared">>},
case core_calendar_share:get(Calendar#calendar.id, UserId) of
{ok, Share} ->
Base#{
share_rights => Share#calendar_share.rights,
mirror_to_default => Share#calendar_share.mirror_to_default
};
{error, not_found} ->
Base
end.
-spec calendar_to_json(#calendar{}) -> map().
calendar_to_json(Calendar) ->
Base = handler_utils:calendar_to_json(Calendar),
Base#{confirmation => confirmation_to_json(Calendar#calendar.confirmation)}.
-spec confirmation_to_json(auto | manual | {timeout, integer()}) -> binary() | map().
confirmation_to_json(auto) -> <<"auto">>;
confirmation_to_json(manual) -> <<"manual">>;
confirmation_to_json({timeout, N}) -> #{<<"timeout">> => N}.
-spec parse_confirmation(binary() | map()) -> auto | manual | {timeout, integer()}.
parse_confirmation(<<"auto">>) -> auto;
parse_confirmation(<<"manual">>) -> manual;
parse_confirmation(#{<<"timeout">> := N}) when is_integer(N), N > 0 -> {timeout, N};
parse_confirmation(_) -> manual.
-spec parse_type(binary()) -> personal | commercial.
parse_type(<<"personal">>) -> personal;
parse_type(<<"commercial">>) -> commercial;
parse_type(_) -> personal.