Stage 10 final

This commit is contained in:
2026-04-22 23:15:20 +03:00
parent e3a08cfa04
commit 081dcf9588
85 changed files with 2116 additions and 160 deletions

View File

@@ -3,5 +3,13 @@
-export([init/2]).
init(Req, _Opts) ->
Body = jsx:encode(#{status => <<"ok">>, service => <<"admin">>}),
cowboy_req:reply(200, #{<<"content-type">> => <<"application/json">>}, Body, Req).
case cowboy_req:method(Req) of
<<"GET">> ->
Body = jsx:encode(#{status => <<"ok">>}),
Req1 = cowboy_req:reply(200, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Req1, []};
_ ->
Body = jsx:encode(#{error => <<"Method not allowed">>}),
Req1 = cowboy_req:reply(405, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Req1, []}
end.

View File

@@ -71,8 +71,10 @@ count_subscriptions() ->
send_json(Req, Status, Data) ->
Body = jsx:encode(Data),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.
send_error(Req, Status, Message) ->
Body = jsx:encode(#{error => Message}),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.

View File

@@ -120,8 +120,10 @@ datetime_to_iso8601({{Year, Month, Day}, {Hour, Minute, Second}}) ->
send_json(Req, Status, Data) ->
Body = jsx:encode(Data),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.
send_error(Req, Status, Message) ->
Body = jsx:encode(#{error => Message}),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.

View File

@@ -50,8 +50,10 @@ datetime_to_iso8601({{Year, Month, Day}, {Hour, Minute, Second}}) ->
send_json(Req, Status, Data) ->
Body = jsx:encode(Data),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.
send_error(Req, Status, Message) ->
Body = jsx:encode(#{error => Message}),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.

View File

@@ -0,0 +1,87 @@
-module(admin_ws_handler).
-behaviour(cowboy_websocket).
-export([init/2]).
-export([websocket_init/1]).
-export([websocket_handle/2]).
-export([websocket_info/2]).
-export([terminate/3]).
-record(state, {
admin_id :: binary() | undefined
}).
init(Req, _Opts) ->
Qs = cowboy_req:parse_qs(Req),
case proplists:get_value(<<"token">>, Qs) of
undefined ->
io:format("[ADMIN_WS] Missing token~n"),
Resp = cowboy_req:reply(401, #{}, <<"Missing token">>, Req),
{ok, Resp, undefined};
Token ->
io:format("[ADMIN_WS] Token received: ~s...~n", [binary_part(Token, 0, 30)]),
case logic_auth:verify_jwt(Token) of
{ok, Claims} ->
UserId = maps:get(<<"user_id">>, Claims),
Role = maps:get(<<"role">>, Claims),
io:format("[ADMIN_WS] UserId: ~s, Role: ~s~n", [UserId, Role]),
case Role of
<<"admin">> ->
io:format("[ADMIN_WS] Admin access granted~n"),
{cowboy_websocket, Req, #state{admin_id = UserId}};
_ ->
io:format("[ADMIN_WS] Access denied: not admin~n"),
Resp = cowboy_req:reply(403, #{}, <<"Admin access required">>, Req),
{ok, Resp, undefined}
end;
{error, expired} ->
io:format("[ADMIN_WS] Token expired~n"),
Resp = cowboy_req:reply(401, #{}, <<"Token expired">>, Req),
{ok, Resp, undefined};
{error, Reason} ->
io:format("[ADMIN_WS] Invalid token: ~p~n", [Reason]),
Resp = cowboy_req:reply(401, #{}, <<"Invalid token">>, Req),
{ok, Resp, undefined}
end
end.
websocket_init(State) ->
io:format("[ADMIN_WS] WebSocket initialized for admin ~s~n", [State#state.admin_id]),
pg:join(eventhub_admin_ws, self()),
{ok, State}.
websocket_handle({text, Msg}, State) ->
io:format("[ADMIN_WS] Received: ~s~n", [Msg]),
try jsx:decode(Msg, [return_maps]) of
#{<<"action">> := <<"subscribe">>, <<"channel">> := Channel} ->
pg:join({eventhub_admin_channel, Channel}, self()),
Reply = jsx:encode(#{status => <<"subscribed">>, channel => Channel}),
{reply, {text, Reply}, State};
#{<<"action">> := <<"unsubscribe">>, <<"channel">> := Channel} ->
pg:leave({eventhub_admin_channel, Channel}, self()),
Reply = jsx:encode(#{status => <<"unsubscribed">>, channel => Channel}),
{reply, {text, Reply}, State};
#{<<"action">> := <<"ping">>} ->
{reply, {text, <<"{\"status\":\"pong\"}">>}, State};
_ ->
{ok, State}
catch
_:_ ->
{ok, State}
end;
websocket_handle(_Frame, State) ->
{ok, State}.
websocket_info({admin_notification, Type, Data}, State) ->
Msg = jsx:encode(#{
type => Type,
data => Data,
timestamp => os:system_time(seconds)
}),
{reply, {text, Msg}, State};
websocket_info(_Info, State) ->
{ok, State}.
terminate(_Reason, _Req, _State) ->
pg:leave(eventhub_admin_ws, self()),
ok.

View File

@@ -123,8 +123,10 @@ datetime_to_iso8601({{Year, Month, Day}, {Hour, Minute, Second}}) ->
send_json(Req, Status, Data) ->
Body = jsx:encode(Data),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.
send_error(Req, Status, Message) ->
Body = jsx:encode(#{error => Message}),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.

View File

@@ -84,8 +84,10 @@ datetime_to_iso8601({{Year, Month, Day}, {Hour, Minute, Second}}) ->
send_json(Req, Status, Data) ->
Body = jsx:encode(Data),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.
send_error(Req, Status, Message) ->
Body = jsx:encode(#{error => Message}),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.

View File

@@ -75,8 +75,10 @@ datetime_to_iso8601({{Year, Month, Day}, {Hour, Minute, Second}}) ->
send_json(Req, Status, Data) ->
Body = jsx:encode(Data),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.
send_error(Req, Status, Message) ->
Body = jsx:encode(#{error => Message}),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.

View File

@@ -3,17 +3,23 @@
-export([authenticate/1]).
authenticate(Req) ->
io:format("[AUTH] Starting authentication...~n"),
case cowboy_req:parse_header(<<"authorization">>, Req) of
{bearer, Token} ->
io:format("[AUTH] Bearer token found: ~s...~n", [binary_part(Token, 0, 30)]),
case logic_auth:verify_jwt(Token) of
{ok, Claims} ->
UserId = maps:get(<<"user_id">>, Claims),
io:format("[AUTH] JWT verified, UserId: ~s~n", [UserId]),
{ok, UserId, Req};
{error, expired} ->
io:format("[AUTH] JWT expired~n"),
{error, 401, <<"Token expired">>, Req};
{error, _} ->
{error, Reason} ->
io:format("[AUTH] JWT invalid: ~p~n", [Reason]),
{error, 401, <<"Invalid token">>, Req}
end;
_ ->
Other ->
io:format("[AUTH] No bearer token: ~p~n", [Other]),
{error, 401, <<"Missing or invalid Authorization header">>, Req}
end.

View File

@@ -79,8 +79,10 @@ remove_banned_word(Req) ->
%% Вспомогательные функции
send_json(Req, Status, Data) ->
Body = jsx:encode(Data),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.
send_error(Req, Status, Message) ->
Body = jsx:encode(#{error => Message}),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.

View File

@@ -121,8 +121,10 @@ datetime_to_iso8601({{Year, Month, Day}, {Hour, Minute, Second}}) ->
send_json(Req, Status, Data) ->
Body = jsx:encode(Data),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.
send_error(Req, Status, Message) ->
Body = jsx:encode(#{error => Message}),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.

View File

@@ -80,8 +80,10 @@ datetime_to_iso8601({{Year, Month, Day}, {Hour, Minute, Second}}) ->
send_json(Req, Status, Data) ->
Body = jsx:encode(Data),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.
send_error(Req, Status, Message) ->
Body = jsx:encode(#{error => Message}),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.

View File

@@ -106,8 +106,10 @@ confirmation_to_json({timeout, N}) -> #{<<"timeout">> => N}.
send_json(Req, Status, Data) ->
Body = jsx:encode(Data),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.
send_error(Req, Status, Message) ->
Body = jsx:encode(#{error => Message}),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.

View File

@@ -113,8 +113,10 @@ confirmation_to_json({timeout, N}) -> #{<<"timeout">> => N}.
send_json(Req, Status, Data) ->
Body = jsx:encode(Data),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.
send_error(Req, Status, Message) ->
Body = jsx:encode(#{error => Message}),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.

View File

@@ -172,8 +172,10 @@ datetime_to_iso8601({{Year, Month, Day}, {Hour, Minute, Second}}) ->
send_json(Req, Status, Data) ->
Body = jsx:encode(Data),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.
send_error(Req, Status, Message) ->
Body = jsx:encode(#{error => Message}),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.

View File

@@ -134,8 +134,10 @@ datetime_to_iso8601({{Year, Month, Day}, {Hour, Minute, Second}}) ->
send_json(Req, Status, Data) ->
Body = jsx:encode(Data),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.
send_error(Req, Status, Message) ->
Body = jsx:encode(#{error => Message}),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.

View File

@@ -265,8 +265,10 @@ datetime_to_iso8601({{Year, Month, Day}, {Hour, Minute, Second}}) ->
send_json(Req, Status, Data) ->
Body = jsx:encode(Data),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.
send_error(Req, Status, Message) ->
Body = jsx:encode(#{error => Message}),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.

View File

@@ -2,6 +2,17 @@
-export([init/2]).
init(Req, _Opts) ->
Body = jsx:encode(#{status => <<"ok">>}),
cowboy_req:reply(200, #{<<"content-type">> => <<"application/json">>}, Body, Req).
init(Req, Opts) ->
handle(Req, Opts).
handle(Req, _Opts) ->
case cowboy_req:method(Req) of
<<"GET">> ->
Body = jsx:encode(#{status => <<"ok">>}),
Req1 = cowboy_req:reply(200, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Req1, []};
_ ->
Body = jsx:encode(#{error => <<"Method not allowed">>}),
Req1 = cowboy_req:reply(405, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Req1, []}
end.

View File

@@ -73,8 +73,10 @@ save_refresh_token(UserId, Token, ExpiresAt) ->
send_json(Req, Status, Data) ->
Body = jsx:encode(Data),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.
send_error(Req, Status, Message) ->
Body = jsx:encode(#{error => Message}),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.

View File

@@ -84,8 +84,10 @@ delete_refresh_token(Token) ->
send_json(Req, Status, Data) ->
Body = jsx:encode(Data),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.
send_error(Req, Status, Message) ->
Body = jsx:encode(#{error => Message}),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.

View File

@@ -56,8 +56,10 @@ handle(Req, _Opts) ->
send_json(Req, Status, Data) ->
Body = jsx:encode(Data),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.
send_error(Req, Status, Message) ->
Body = jsx:encode(#{error => Message}),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.

View File

@@ -76,8 +76,10 @@ datetime_to_iso8601({{Year, Month, Day}, {Hour, Minute, Second}}) ->
send_json(Req, Status, Data) ->
Body = jsx:encode(Data),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.
send_error(Req, Status, Message) ->
Body = jsx:encode(#{error => Message}),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.

View File

@@ -13,41 +13,32 @@ handle(Req, _Opts) ->
_ -> send_error(Req, 405, <<"Method not allowed">>)
end.
%% POST /v1/reports - создание жалобы
create_report(Req) ->
case handler_auth:authenticate(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
#{<<"target_type">> := TargetTypeBin,
<<"target_id">> := TargetId,
<<"reason">> := Reason} ->
TargetType = parse_target_type(TargetTypeBin),
case logic_moderation:create_report(UserId, TargetType, TargetId, Reason) of
{ok, Report} ->
Response = report_to_json(Report),
send_json(Req2, 201, Response);
{error, target_not_found} ->
send_error(Req2, 404, <<"Target not found">>);
{error, _} ->
send_error(Req2, 500, <<"Internal server error">>)
end;
_ ->
send_error(Req2, 400, <<"Missing required fields">>)
Decoded = jsx:decode(Body, [return_maps]),
case Decoded of
#{<<"target_type">> := TargetTypeBin,
<<"target_id">> := TargetId,
<<"reason">> := Reason} ->
TargetType = parse_target_type(TargetTypeBin),
case logic_moderation:create_report(UserId, TargetType, TargetId, Reason) of
{ok, Report} ->
Response = report_to_json(Report),
send_json(Req2, 201, Response);
{error, target_not_found} ->
send_error(Req2, 404, <<"Target not found">>);
{error, _} ->
send_error(Req2, 500, <<"Internal server error">>)
end;
_ ->
send_error(Req2, 400, <<"Invalid JSON">>)
catch
_:_ ->
send_error(Req2, 400, <<"Invalid JSON format">>)
send_error(Req2, 400, <<"Missing required fields">>)
end;
{error, Code, Message, Req1} ->
send_error(Req1, Code, Message)
end.
%% GET /v1/admin/reports - список всех жалоб (админ)
list_reports(Req) ->
case handler_auth:authenticate(Req) of
{ok, AdminId, Req1} ->
@@ -79,7 +70,6 @@ list_reports(Req) ->
send_error(Req1, Code, Message)
end.
%% Вспомогательные функции
parse_target_type(<<"event">>) -> event;
parse_target_type(<<"calendar">>) -> calendar;
parse_target_type(_) -> undefined.
@@ -106,8 +96,10 @@ datetime_to_iso8601({{Year, Month, Day}, {Hour, Minute, Second}}) ->
send_json(Req, Status, Data) ->
Body = jsx:encode(Data),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
Req1 = cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Req1, []}.
send_error(Req, Status, Message) ->
Body = jsx:encode(#{error => Message}),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
Req1 = cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Req1, []}.

View File

@@ -109,8 +109,10 @@ datetime_to_iso8601({{Year, Month, Day}, {Hour, Minute, Second}}) ->
send_json(Req, Status, Data) ->
Body = jsx:encode(Data),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.
send_error(Req, Status, Message) ->
Body = jsx:encode(#{error => Message}),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.

View File

@@ -100,8 +100,10 @@ datetime_to_iso8601({{Year, Month, Day}, {Hour, Minute, Second}}) ->
send_json(Req, Status, Data) ->
Body = jsx:encode(Data),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.
send_error(Req, Status, Message) ->
Body = jsx:encode(#{error => Message}),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.

View File

@@ -97,8 +97,10 @@ parse_datetime_param(Qs, Key) ->
send_json(Req, Status, Data) ->
Body = jsx:encode(Data),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.
send_error(Req, Status, Message) ->
Body = jsx:encode(#{error => Message}),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.

View File

@@ -95,8 +95,10 @@ datetime_to_iso8601({{Year, Month, Day}, {Hour, Minute, Second}}) ->
send_json(Req, Status, Data) ->
Body = jsx:encode(Data),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.
send_error(Req, Status, Message) ->
Body = jsx:encode(#{error => Message}),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.

View File

@@ -61,7 +61,7 @@ handle_ticket_action(AdminId, TicketId, Body, Req) ->
StatusBin =:= <<"in_progress">>;
StatusBin =:= <<"resolved">>;
StatusBin =:= <<"closed">> ->
Status = binary_to_atom(StatusBin),
Status = get_binary_to_atom(StatusBin),
case logic_ticket:update_status(AdminId, TicketId, Status) of
{ok, Ticket} ->
Response = ticket_to_json(Ticket),
@@ -148,15 +148,17 @@ datetime_to_iso8601({{Year, Month, Day}, {Hour, Minute, Second}}) ->
iolist_to_binary(io_lib:format("~4..0B-~2..0B-~2..0BT~2..0B:~2..0B:~2..0BZ",
[Year, Month, Day, Hour, Minute, Second])).
binary_to_atom(<<"open">>) -> open;
binary_to_atom(<<"in_progress">>) -> in_progress;
binary_to_atom(<<"resolved">>) -> resolved;
binary_to_atom(<<"closed">>) -> closed.
get_binary_to_atom(<<"open">>) -> open;
get_binary_to_atom(<<"in_progress">>) -> in_progress;
get_binary_to_atom(<<"resolved">>) -> resolved;
get_binary_to_atom(<<"closed">>) -> closed.
send_json(Req, Status, Data) ->
Body = jsx:encode(Data),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.
send_error(Req, Status, Message) ->
Body = jsx:encode(#{error => Message}),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.

View File

@@ -30,8 +30,10 @@ get_statistics(Req) ->
send_json(Req, Status, Data) ->
Body = jsx:encode(Data),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.
send_error(Req, Status, Message) ->
Body = jsx:encode(#{error => Message}),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.

View File

@@ -16,7 +16,7 @@ handle(Req, _Opts) ->
%% POST /v1/tickets - сообщить об ошибке (доступно всем)
report_error(Req) ->
case handler_auth:authenticate(Req) of
{ok, UserId, Req1} ->
{ok, _UserId, Req1} ->
{ok, Body, Req2} = cowboy_req:read_body(Req1),
try jsx:decode(Body, [return_maps]) of
Decoded when is_map(Decoded) ->
@@ -111,8 +111,10 @@ datetime_to_iso8601({{Year, Month, Day}, {Hour, Minute, Second}}) ->
send_json(Req, Status, Data) ->
Body = jsx:encode(Data),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.
send_error(Req, Status, Message) ->
Body = jsx:encode(#{error => Message}),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.

View File

@@ -48,8 +48,10 @@ datetime_to_iso8601({{Year, Month, Day}, {Hour, Minute, Second}}) ->
send_json(Req, Status, Data) ->
Body = jsx:encode(Data),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.
send_error(Req, Status, Message) ->
Body = jsx:encode(#{error => Message}),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.

View File

@@ -50,8 +50,10 @@ authenticate(Req) ->
send_json(Req, Status, Data) ->
Body = jsx:encode(Data),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.
send_error(Req, Status, Message) ->
Body = jsx:encode(#{error => Message}),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.

View File

@@ -47,8 +47,10 @@ datetime_to_iso8601({{Year, Month, Day}, {Hour, Minute, Second}}) ->
send_json(Req, Status, Data) ->
Body = jsx:encode(Data),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.
send_error(Req, Status, Message) ->
Body = jsx:encode(#{error => Message}),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
{ok, Body, []}.

View File

@@ -0,0 +1,85 @@
-module(ws_handler).
-behaviour(cowboy_websocket).
-export([init/2]).
-export([websocket_init/1]).
-export([websocket_handle/2]).
-export([websocket_info/2]).
-export([terminate/3]).
-record(state, {
user_id :: binary() | undefined,
subscriptions = [] :: [binary()]
}).
init(Req, _Opts) ->
% Аутентификация через query параметр token
Qs = cowboy_req:parse_qs(Req),
case proplists:get_value(<<"token">>, Qs) of
undefined ->
{ok, cowboy_req:reply(401, #{}, <<"Missing token">>, Req), undefined};
Token ->
case logic_auth:verify_jwt(Token) of
{ok, Claims} ->
UserId = maps:get(<<"user_id">>, Claims),
{cowboy_websocket, Req, #state{user_id = UserId}};
{error, _} ->
{ok, cowboy_req:reply(401, #{}, <<"Invalid token">>, Req), undefined}
end
end.
websocket_init(State) ->
% Регистрируем процесс в pg для получения уведомлений
pg:join(eventhub_ws, self()),
{ok, State}.
websocket_handle({text, Msg}, State) ->
io:format("WebSocket received: ~s~n", [Msg]),
try jsx:decode(Msg, [return_maps]) of
#{<<"action">> := <<"subscribe">>, <<"calendar_id">> := CalendarId} ->
io:format("Subscribe to calendar: ~s~n", [CalendarId]),
NewSubs = [CalendarId | State#state.subscriptions],
Reply = jsx:encode(#{status => <<"subscribed">>, calendar_id => CalendarId}),
io:format("Sending reply: ~s~n", [Reply]),
{reply, {text, Reply}, State#state{subscriptions = NewSubs}};
#{<<"action">> := <<"ping">>} ->
{reply, {text, <<"{\"status\":\"pong\"}">>}, State};
Other ->
io:format("Unknown action: ~p~n", [Other]),
{ok, State}
catch
_:Error ->
io:format("Error parsing WebSocket message: ~p~n", [Error]),
{ok, State}
end;
websocket_handle(_Frame, State) ->
{ok, State}.
websocket_info({notification, Type, Data}, State) ->
case should_notify(Type, Data, State) of
true ->
Msg = jsx:encode(#{
type => Type,
data => Data,
timestamp => os:system_time(seconds)
}),
{reply, {text, Msg}, State};
false ->
{ok, State}
end;
websocket_info(_Info, State) ->
{ok, State}.
terminate(_Reason, _Req, _State) ->
pg:leave(eventhub_ws, self()),
ok.
%% Проверка, нужно ли отправлять уведомление пользователю
should_notify(calendar_update, #{calendar_id := CalId}, State) ->
lists:member(CalId, State#state.subscriptions);
should_notify(booking_update, #{user_id := UserId}, State) ->
UserId =:= State#state.user_id;
should_notify(event_update, #{calendar_id := CalId}, State) ->
lists:member(CalId, State#state.subscriptions);
should_notify(_, _, _) ->
true.