Implement commercial calendars: booking_open, specialists, expire without type downgrade.
CI / test (push) Successful in 6m44s
CI / deploy-ift (push) Successful in 4m2s
CI / e2e-ift (push) Successful in 1m25s
CI / deploy-stage (push) Successful in 2m25s
CI / e2e-stage (push) Successful in 1m12s

Refs EventHub/EventHubBack#54
This commit is contained in:
2026-07-22 16:12:52 +03:00
parent fdb08eb453
commit af5f506866
35 changed files with 978 additions and 207 deletions
+8
View File
@@ -116,10 +116,18 @@ create_booking(Req) ->
handler_utils:send_json(Req1, 201, booking_to_json(Booking));
{error, already_booked} ->
handler_utils:send_error(Req1, 409, <<"Already booked">>);
{error, full} ->
handler_utils:send_error(Req1, 400, <<"Event is full">>);
{error, event_full} ->
handler_utils:send_error(Req1, 400, <<"Event is full">>);
{error, event_not_active} ->
handler_utils:send_error(Req1, 400, <<"Event is not active">>);
{error, personal_calendar} ->
handler_utils:send_error(Req1, 403, <<"personal_calendar">>);
{error, subscription_inactive} ->
handler_utils:send_error(Req1, 403, <<"subscription_inactive">>);
{error, own_event} ->
handler_utils:send_error(Req1, 403, <<"Cannot book own event">>);
{error, access_denied} ->
handler_utils:send_error(Req1, 403, <<"Access denied">>);
{error, not_found} ->
+7 -1
View File
@@ -139,9 +139,13 @@ update_calendar(Req) ->
Updates = convert_calendar_fields(Updates0),
case logic_calendar:update_calendar(UserId, CalendarId, Updates) of
{ok, Calendar} ->
handler_utils:send_json(Req2, 200, handler_utils:calendar_to_json(Calendar));
Json0 = handler_utils:calendar_to_json(Calendar),
Following = logic_calendar_follow:is_following(UserId, CalendarId),
handler_utils:send_json(Req2, 200, Json0#{following => Following});
{error, access_denied} ->
handler_utils:send_error(Req2, 403, <<"Access denied">>);
{error, subscription_required} ->
handler_utils:send_error(Req2, 402, <<"Subscription required for commercial calendar">>);
{error, not_found} ->
handler_utils:send_error(Req2, 404, <<"Calendar not found">>);
{error, _} ->
@@ -182,6 +186,8 @@ convert_calendar_fields(Updates) ->
-spec convert_field({binary(), term()}) -> {atom(), term()}.
convert_field({<<"title">>, Val}) -> {title, Val};
convert_field({<<"description">>, Val}) -> {description, Val};
convert_field({<<"type">>, <<"personal">>}) -> {type, personal};
convert_field({<<"type">>, <<"commercial">>}) -> {type, commercial};
convert_field({<<"type">>, Val}) -> {type, Val};
convert_field({<<"confirmation">>, <<"auto">>}) -> {confirmation, auto};
convert_field({<<"confirmation">>, <<"manual">>}) -> {confirmation, manual};
+208
View File
@@ -0,0 +1,208 @@
%%%-------------------------------------------------------------------
%%% @doc CRUD специалистов календаря.
%%%
%%% GET /v1/calendars/:id/specialists
%%% POST /v1/calendars/:id/specialists
%%% PUT /v1/calendars/:id/specialists/:user_id
%%% DELETE /v1/calendars/:id/specialists/:user_id
%%% @end
%%%-------------------------------------------------------------------
-module(handler_calendar_specialists).
-behaviour(cowboy_handler).
-export([init/2]).
-export([trails/0]).
-include("records.hrl").
-spec init(cowboy_req:req(), any()) -> {ok, cowboy_req:req(), any()}.
init(Req, Opts) ->
handle(Req, Opts).
-spec trails() -> [map()].
trails() ->
SpecSchema = #{
type => object,
properties => #{
id => #{type => string},
calendar_id => #{type => string},
user_id => #{type => string},
name => #{type => string},
specialization => #{type => array, items => #{type => string}},
status => #{type => string, enum => [<<"active">>, <<"inactive">>]}
}
},
IdParam = #{
name => <<"id">>, in => <<"path">>, required => true,
schema => #{type => string}
},
UserParam = #{
name => <<"user_id">>, in => <<"path">>, required => true,
schema => #{type => string}
},
[
#{
path => <<"/v1/calendars/:id/specialists">>,
method => <<"GET">>,
description => <<"List calendar specialists">>,
tags => [<<"Calendars">>],
parameters => [IdParam],
responses => #{
200 => #{description => <<"OK">>,
content => #{<<"application/json">> => #{schema => #{type => array, items => SpecSchema}}}}
}
},
#{
path => <<"/v1/calendars/:id/specialists">>,
method => <<"POST">>,
description => <<"Add specialist (owner, commercial)">>,
tags => [<<"Calendars">>],
parameters => [IdParam],
responses => #{201 => #{description => <<"Created">>}}
},
#{
path => <<"/v1/calendars/:id/specialists/:user_id">>,
method => <<"PUT">>,
description => <<"Update specialist">>,
tags => [<<"Calendars">>],
parameters => [IdParam, UserParam],
responses => #{200 => #{description => <<"Updated">>}}
},
#{
path => <<"/v1/calendars/:id/specialists/:user_id">>,
method => <<"DELETE">>,
description => <<"Remove specialist">>,
tags => [<<"Calendars">>],
parameters => [IdParam, UserParam],
responses => #{200 => #{description => <<"Deleted">>}}
}
].
-spec handle(cowboy_req:req(), any()) -> {ok, cowboy_req:req(), any()}.
handle(Req, _Opts) ->
Method = cowboy_req:method(Req),
case {Method, has_user_binding(Req)} of
{<<"GET">>, false} -> list_specialists(Req);
{<<"POST">>, false} -> add_specialist(Req);
{<<"PUT">>, true} -> update_specialist(Req);
{<<"DELETE">>, true} -> remove_specialist(Req);
_ ->
handler_utils:send_error(Req, 405, <<"Method not allowed">>)
end.
has_user_binding(Req) ->
cowboy_req:binding(user_id, Req) =/= undefined.
list_specialists(Req) ->
case handler_utils:auth_user(Req) of
{ok, UserId, Req1} ->
CalendarId = cowboy_req:binding(id, Req1),
case logic_calendar_specialist:list(UserId, CalendarId) of
{ok, List} ->
handler_utils:send_json(Req1, 200,
[logic_calendar_specialist:to_json(S) || S <- List]);
{error, not_found} ->
handler_utils:send_error(Req1, 404, <<"Calendar not found">>);
{error, access_denied} ->
handler_utils:send_error(Req1, 403, <<"Access denied">>);
{error, _} ->
handler_utils:send_error(Req1, 500, <<"Internal server error">>)
end;
{error, Code, Message, Req1} ->
handler_utils:send_error(Req1, Code, Message)
end.
add_specialist(Req) ->
case handler_utils:auth_user(Req) of
{ok, OwnerId, Req1} ->
CalendarId = cowboy_req:binding(id, Req1),
{ok, Body, Req2} = cowboy_req:read_body(Req1),
try jsx:decode(Body, [return_maps]) of
#{<<"user_id">> := SpecUserId} = Map when is_binary(SpecUserId) ->
Name = maps:get(<<"name">>, Map, <<>>),
Specs = maps:get(<<"specialization">>, Map, []),
Specs2 = case is_list(Specs) of true -> Specs; false -> [] end,
case logic_calendar_specialist:add(OwnerId, CalendarId, SpecUserId, Name, Specs2) of
{ok, Rec} ->
handler_utils:send_json(Req2, 201, logic_calendar_specialist:to_json(Rec));
{error, not_found} ->
handler_utils:send_error(Req2, 404, <<"Calendar not found">>);
{error, access_denied} ->
handler_utils:send_error(Req2, 403, <<"Access denied">>);
{error, not_commercial} ->
handler_utils:send_error(Req2, 400, <<"Calendar is not commercial">>);
{error, user_not_found} ->
handler_utils:send_error(Req2, 404, <<"User not found">>);
{error, already_exists} ->
handler_utils:send_error(Req2, 409, <<"Specialist already exists">>);
{error, _} ->
handler_utils:send_error(Req2, 500, <<"Internal server error">>)
end;
_ ->
handler_utils:send_error(Req2, 400, <<"user_id required">>)
catch
_:_ -> handler_utils:send_error(Req2, 400, <<"Invalid JSON">>)
end;
{error, Code, Message, Req1} ->
handler_utils:send_error(Req1, Code, Message)
end.
update_specialist(Req) ->
case handler_utils:auth_user(Req) of
{ok, OwnerId, Req1} ->
CalendarId = cowboy_req:binding(id, Req1),
SpecUserId = cowboy_req:binding(user_id, Req1),
{ok, Body, Req2} = cowboy_req:read_body(Req1),
try jsx:decode(Body, [return_maps]) of
Map when is_map(Map) ->
Updates = parse_updates(Map),
case logic_calendar_specialist:update(OwnerId, CalendarId, SpecUserId, Updates) of
{ok, Rec} ->
handler_utils:send_json(Req2, 200, logic_calendar_specialist:to_json(Rec));
{error, not_found} ->
handler_utils:send_error(Req2, 404, <<"Not found">>);
{error, access_denied} ->
handler_utils:send_error(Req2, 403, <<"Access denied">>);
{error, not_commercial} ->
handler_utils:send_error(Req2, 400, <<"Calendar is not commercial">>);
{error, _} ->
handler_utils:send_error(Req2, 500, <<"Internal server error">>)
end;
_ ->
handler_utils:send_error(Req2, 400, <<"Invalid JSON">>)
catch
_:_ -> handler_utils:send_error(Req2, 400, <<"Invalid JSON">>)
end;
{error, Code, Message, Req1} ->
handler_utils:send_error(Req1, Code, Message)
end.
remove_specialist(Req) ->
case handler_utils:auth_user(Req) of
{ok, OwnerId, Req1} ->
CalendarId = cowboy_req:binding(id, Req1),
SpecUserId = cowboy_req:binding(user_id, Req1),
case logic_calendar_specialist:remove(OwnerId, CalendarId, SpecUserId) of
ok ->
handler_utils:send_json(Req1, 200, #{status => <<"deleted">>});
{error, not_found} ->
handler_utils:send_error(Req1, 404, <<"Not found">>);
{error, access_denied} ->
handler_utils:send_error(Req1, 403, <<"Access denied">>);
{error, not_commercial} ->
handler_utils:send_error(Req1, 400, <<"Calendar is not commercial">>);
{error, _} ->
handler_utils:send_error(Req1, 500, <<"Internal server error">>)
end;
{error, Code, Message, Req1} ->
handler_utils:send_error(Req1, Code, Message)
end.
parse_updates(Map) ->
lists:filtermap(fun
({<<"name">>, V}) when is_binary(V) -> {true, {name, V}};
({<<"specialization">>, V}) when is_list(V) -> {true, {specialization, V}};
({<<"status">>, <<"active">>}) -> {true, {status, active}};
({<<"status">>, <<"inactive">>}) -> {true, {status, inactive}};
(_) -> false
end, maps:to_list(Map)).
Regular → Executable
+7 -1
View File
@@ -443,7 +443,8 @@ calendar_to_json(Calendar) ->
settings => Calendar#calendar.settings,
tags => Calendar#calendar.tags,
type => Calendar#calendar.type,
confirmation => Calendar#calendar.confirmation,
confirmation => confirmation_to_json(Calendar#calendar.confirmation),
booking_open => logic_calendar:booking_open(Calendar),
rating_avg => Calendar#calendar.rating_avg,
rating_count => Calendar#calendar.rating_count,
status => Calendar#calendar.status,
@@ -452,6 +453,11 @@ calendar_to_json(Calendar) ->
updated_at => datetime_to_iso8601(Calendar#calendar.updated_at)
}.
confirmation_to_json(auto) -> <<"auto">>;
confirmation_to_json(manual) -> <<"manual">>;
confirmation_to_json({timeout, N}) when is_integer(N) -> #{<<"timeout">> => N};
confirmation_to_json(Other) -> Other.
%% @doc Преобразует #subscription{} в JSON-карту.
-spec subscription_to_json(#subscription{}) -> map().
subscription_to_json(Subscription) ->