Stage 6
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
-module(handler_report_by_id).
|
||||
-include("records.hrl").
|
||||
|
||||
-export([init/2]).
|
||||
|
||||
init(Req, Opts) ->
|
||||
handle(Req, Opts).
|
||||
|
||||
handle(Req, _Opts) ->
|
||||
case cowboy_req:method(Req) of
|
||||
<<"PUT">> -> resolve_report(Req);
|
||||
_ -> send_error(Req, 405, <<"Method not allowed">>)
|
||||
end.
|
||||
|
||||
%% PUT /v1/admin/reports/:id - рассмотрение жалобы
|
||||
resolve_report(Req) ->
|
||||
case handler_auth:authenticate(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
|
||||
#{<<"action">> := Action} ->
|
||||
ActionAtom = case Action of
|
||||
<<"review">> -> reviewed;
|
||||
<<"dismiss">> -> dismissed;
|
||||
_ -> undefined
|
||||
end,
|
||||
case ActionAtom of
|
||||
undefined ->
|
||||
send_error(Req2, 400, <<"Invalid action. Use 'review' or 'dismiss'">>);
|
||||
_ ->
|
||||
case logic_moderation:resolve_report(AdminId, ReportId, ActionAtom) of
|
||||
{ok, Report} ->
|
||||
Response = report_to_json(Report),
|
||||
send_json(Req2, 200, Response);
|
||||
{error, access_denied} ->
|
||||
send_error(Req2, 403, <<"Admin access required">>);
|
||||
{error, already_resolved} ->
|
||||
send_error(Req2, 409, <<"Report already resolved">>);
|
||||
{error, not_found} ->
|
||||
send_error(Req2, 404, <<"Report not found">>);
|
||||
{error, _} ->
|
||||
send_error(Req2, 500, <<"Internal server error">>)
|
||||
end
|
||||
end;
|
||||
_ ->
|
||||
send_error(Req2, 400, <<"Missing action field">>)
|
||||
catch
|
||||
_:_ ->
|
||||
send_error(Req2, 400, <<"Invalid JSON format">>)
|
||||
end;
|
||||
{error, Code, Message, Req1} ->
|
||||
send_error(Req1, Code, Message)
|
||||
end.
|
||||
|
||||
%% Вспомогательные функции
|
||||
report_to_json(Report) ->
|
||||
#{
|
||||
id => Report#report.id,
|
||||
reporter_id => Report#report.reporter_id,
|
||||
target_type => Report#report.target_type,
|
||||
target_id => Report#report.target_id,
|
||||
reason => Report#report.reason,
|
||||
status => Report#report.status,
|
||||
created_at => datetime_to_iso8601(Report#report.created_at),
|
||||
resolved_at => case Report#report.resolved_at of
|
||||
undefined -> null;
|
||||
Dt -> datetime_to_iso8601(Dt)
|
||||
end,
|
||||
resolved_by => Report#report.resolved_by
|
||||
}.
|
||||
|
||||
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])).
|
||||
|
||||
send_json(Req, Status, Data) ->
|
||||
Body = jsx:encode(Data),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
|
||||
send_error(Req, Status, Message) ->
|
||||
Body = jsx:encode(#{error => Message}),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
Reference in New Issue
Block a user