Files
EventHubBack/src/handlers/admin/admin_handler_report_by_id.erl
T
aleksey 17b2e617d7
Test & Deploy to Snapdeploy / build-test-deploy (push) Failing after 24m46s
bagofix
2026-06-05 23:20:03 +03:00

126 lines
4.5 KiB
Erlang

%%%-------------------------------------------------------------------
%%% @doc Административный обработчик конкретной жалобы.
%%% GET – получить жалобу по ID.
%%% PUT – обновить статус жалобы.
%%% @end
%%%-------------------------------------------------------------------
-module(admin_handler_report_by_id).
-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) ->
case cowboy_req:method(Req) of
<<"GET">> -> get_report(Req);
<<"PUT">> -> update_report(Req);
_ -> handler_utils:send_error(Req, 405, <<"Method not allowed">>)
end.
-spec trails() -> [map()].
trails() ->
BaseParams = [
#{ name => <<"id">>, in => <<"path">>, description => <<"Report ID">>, required => true, schema => #{type => string} }
],
[
#{ % GET
path => <<"/v1/admin/reports/:id">>,
method => <<"GET">>,
description => <<"Get report by ID (admin)">>,
tags => [<<"Reports">>],
parameters => BaseParams,
responses => #{
200 => #{ description => <<"Report details">>, content => #{<<"application/json">> => #{schema => report_schema()}} },
404 => #{description => <<"Report not found">>}
}
},
#{ % PUT
path => <<"/v1/admin/reports/:id">>,
method => <<"PUT">>,
description => <<"Update report status (admin)">>,
tags => [<<"Reports">>],
parameters => BaseParams,
requestBody => #{
required => true,
content => #{<<"application/json">> => #{schema => report_update_schema()}}
},
responses => #{
200 => #{description => <<"Updated report">>},
404 => #{description => <<"Report not found">>}
}
}
].
report_schema() ->
#{
type => object,
properties => #{
id => #{type => string},
reporter_id => #{type => string},
target_type => #{type => string, enum => [<<"calendar">>, <<"event">>, <<"review">>]},
target_id => #{type => string},
reason => #{type => string},
status => #{type => string, enum => [<<"pending">>, <<"reviewed">>, <<"dismissed">>]},
created_at => #{type => string, format => <<"date-time">>},
resolved_at => #{type => string, format => <<"date-time">>, nullable => true},
resolved_by => #{type => string, nullable => true}
}
}.
report_update_schema() ->
#{
type => object,
properties => #{
status => #{type => string, enum => [<<"reviewed">>, <<"dismissed">>]}
}
}.
%%% Internal functions
get_report(Req) ->
case handler_utils:auth_admin(Req) of
{ok, AdminId, Req1} ->
ReportId = cowboy_req:binding(id, Req1),
case logic_report:get_report(AdminId, ReportId) of
{ok, Report} ->
handler_utils:send_json(Req1, 200, handler_utils:report_to_json(Report));
{error, not_found} ->
handler_utils:send_error(Req1, 404, <<"Report not found">>);
{error, _} ->
handler_utils:send_error(Req1, 500, <<"Internal server error">>)
end;
{error, Code, Msg, Req1} ->
handler_utils:send_error(Req1, Code, Msg)
end.
update_report(Req) ->
case handler_utils:auth_admin(Req) of
{ok, AdminId, Req1} ->
ReportId = cowboy_req:binding(id, Req1),
{ok, Body, Req2} = cowboy_req:read_body(Req1),
try jsx:decode(Body, [return_maps]) of
#{<<"status">> := StatusBin} ->
% Преобразуем бинарный статус в атом
StatusAtom = try binary_to_existing_atom(StatusBin, utf8)
catch _:_ -> StatusBin
end,
case logic_report:update_report_status(AdminId, ReportId, StatusAtom) of
{ok, Report} ->
% Аудит изменения статуса жалобы
admin_utils:log_admin_action(AdminId, <<"update_report_status">>, <<"report">>, ReportId, <<>>, Req2),
handler_utils:send_json(Req2, 200, handler_utils:report_to_json(Report));
{error, not_found} ->
handler_utils:send_error(Req2, 404, <<"Report not found">>);
{error, _} ->
handler_utils:send_error(Req2, 500, <<"Internal server error">>)
end;
_ ->
handler_utils:send_error(Req2, 400, <<"Missing status field">>)
catch
_:_ -> handler_utils:send_error(Req1, 400, <<"Invalid JSON">>)
end;
{error, Code, Msg, Req1} ->
handler_utils:send_error(Req1, Code, Msg)
end.