Stage 7
This commit is contained in:
162
src/handlers/handler_ticket_by_id.erl
Normal file
162
src/handlers/handler_ticket_by_id.erl
Normal file
@@ -0,0 +1,162 @@
|
||||
-module(handler_ticket_by_id).
|
||||
-include("records.hrl").
|
||||
|
||||
-export([init/2]).
|
||||
|
||||
init(Req, Opts) ->
|
||||
handle(Req, Opts).
|
||||
|
||||
handle(Req, _Opts) ->
|
||||
case cowboy_req:method(Req) of
|
||||
<<"GET">> -> get_ticket(Req);
|
||||
<<"PUT">> -> update_ticket(Req);
|
||||
_ -> send_error(Req, 405, <<"Method not allowed">>)
|
||||
end.
|
||||
|
||||
%% GET /v1/admin/tickets/:id - получить тикет
|
||||
get_ticket(Req) ->
|
||||
case handler_auth:authenticate(Req) of
|
||||
{ok, AdminId, Req1} ->
|
||||
TicketId = cowboy_req:binding(id, Req1),
|
||||
case logic_ticket:get_ticket(AdminId, TicketId) of
|
||||
{ok, Ticket} ->
|
||||
Response = ticket_to_json(Ticket),
|
||||
send_json(Req1, 200, Response);
|
||||
{error, access_denied} ->
|
||||
send_error(Req1, 403, <<"Admin access required">>);
|
||||
{error, not_found} ->
|
||||
send_error(Req1, 404, <<"Ticket not found">>);
|
||||
{error, _} ->
|
||||
send_error(Req1, 500, <<"Internal server error">>)
|
||||
end;
|
||||
{error, Code, Message, Req1} ->
|
||||
send_error(Req1, Code, Message)
|
||||
end.
|
||||
|
||||
%% PUT /v1/admin/tickets/:id - обновить тикет
|
||||
update_ticket(Req) ->
|
||||
case handler_auth:authenticate(Req) of
|
||||
{ok, AdminId, Req1} ->
|
||||
TicketId = cowboy_req:binding(id, Req1),
|
||||
{ok, Body, Req2} = cowboy_req:read_body(Req1),
|
||||
try jsx:decode(Body, [return_maps]) of
|
||||
Decoded when is_map(Decoded) ->
|
||||
handle_ticket_action(AdminId, TicketId, Decoded, Req2);
|
||||
_ ->
|
||||
send_error(Req2, 400, <<"Invalid JSON">>)
|
||||
catch
|
||||
_:_ ->
|
||||
send_error(Req2, 400, <<"Invalid JSON format">>)
|
||||
end;
|
||||
{error, Code, Message, Req1} ->
|
||||
send_error(Req1, Code, Message)
|
||||
end.
|
||||
|
||||
%% Обработка действий с тикетом
|
||||
handle_ticket_action(AdminId, TicketId, Body, Req) ->
|
||||
case maps:get(<<"action">>, Body, undefined) of
|
||||
<<"status">> ->
|
||||
case maps:get(<<"status">>, Body, undefined) of
|
||||
StatusBin when StatusBin =:= <<"open">>;
|
||||
StatusBin =:= <<"in_progress">>;
|
||||
StatusBin =:= <<"resolved">>;
|
||||
StatusBin =:= <<"closed">> ->
|
||||
Status = binary_to_atom(StatusBin),
|
||||
case logic_ticket:update_status(AdminId, TicketId, Status) of
|
||||
{ok, Ticket} ->
|
||||
Response = ticket_to_json(Ticket),
|
||||
send_json(Req, 200, Response);
|
||||
{error, access_denied} ->
|
||||
send_error(Req, 403, <<"Admin access required">>);
|
||||
{error, not_found} ->
|
||||
send_error(Req, 404, <<"Ticket not found">>);
|
||||
{error, _} ->
|
||||
send_error(Req, 500, <<"Internal server error">>)
|
||||
end;
|
||||
_ ->
|
||||
send_error(Req, 400, <<"Invalid status">>)
|
||||
end;
|
||||
<<"assign">> ->
|
||||
case maps:get(<<"admin_id">>, Body, undefined) of
|
||||
undefined ->
|
||||
send_error(Req, 400, <<"Missing admin_id field">>);
|
||||
AssignToId ->
|
||||
case logic_ticket:assign_ticket(AdminId, TicketId, AssignToId) of
|
||||
{ok, Ticket} ->
|
||||
Response = ticket_to_json(Ticket),
|
||||
send_json(Req, 200, Response);
|
||||
{error, access_denied} ->
|
||||
send_error(Req, 403, <<"Admin access required">>);
|
||||
{error, not_found} ->
|
||||
send_error(Req, 404, <<"Ticket not found">>);
|
||||
{error, _} ->
|
||||
send_error(Req, 500, <<"Internal server error">>)
|
||||
end
|
||||
end;
|
||||
<<"resolve">> ->
|
||||
Note = maps:get(<<"note">>, Body, <<"">>),
|
||||
case logic_ticket:resolve_ticket(AdminId, TicketId, Note) of
|
||||
{ok, Ticket} ->
|
||||
Response = ticket_to_json(Ticket),
|
||||
send_json(Req, 200, Response);
|
||||
{error, access_denied} ->
|
||||
send_error(Req, 403, <<"Admin access required">>);
|
||||
{error, not_found} ->
|
||||
send_error(Req, 404, <<"Ticket not found">>);
|
||||
{error, _} ->
|
||||
send_error(Req, 500, <<"Internal server error">>)
|
||||
end;
|
||||
<<"close">> ->
|
||||
case logic_ticket:close_ticket(AdminId, TicketId) of
|
||||
{ok, Ticket} ->
|
||||
Response = ticket_to_json(Ticket),
|
||||
send_json(Req, 200, Response);
|
||||
{error, access_denied} ->
|
||||
send_error(Req, 403, <<"Admin access required">>);
|
||||
{error, not_found} ->
|
||||
send_error(Req, 404, <<"Ticket not found">>);
|
||||
{error, _} ->
|
||||
send_error(Req, 500, <<"Internal server error">>)
|
||||
end;
|
||||
_ ->
|
||||
send_error(Req, 400, <<"Invalid action">>)
|
||||
end.
|
||||
|
||||
%% Вспомогательные функции
|
||||
ticket_to_json(Ticket) ->
|
||||
Context = try binary_to_term(Ticket#ticket.context) of
|
||||
C -> C
|
||||
catch
|
||||
_:_ -> #{}
|
||||
end,
|
||||
|
||||
#{
|
||||
id => Ticket#ticket.id,
|
||||
error_hash => Ticket#ticket.error_hash,
|
||||
error_message => Ticket#ticket.error_message,
|
||||
stacktrace => Ticket#ticket.stacktrace,
|
||||
context => Context,
|
||||
count => Ticket#ticket.count,
|
||||
first_seen => datetime_to_iso8601(Ticket#ticket.first_seen),
|
||||
last_seen => datetime_to_iso8601(Ticket#ticket.last_seen),
|
||||
status => Ticket#ticket.status,
|
||||
assigned_to => Ticket#ticket.assigned_to,
|
||||
resolution_note => Ticket#ticket.resolution_note
|
||||
}.
|
||||
|
||||
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.
|
||||
|
||||
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).
|
||||
37
src/handlers/handler_ticket_stats.erl
Normal file
37
src/handlers/handler_ticket_stats.erl
Normal file
@@ -0,0 +1,37 @@
|
||||
-module(handler_ticket_stats).
|
||||
-include("records.hrl").
|
||||
|
||||
-export([init/2]).
|
||||
|
||||
init(Req, Opts) ->
|
||||
handle(Req, Opts).
|
||||
|
||||
handle(Req, _Opts) ->
|
||||
case cowboy_req:method(Req) of
|
||||
<<"GET">> -> get_statistics(Req);
|
||||
_ -> send_error(Req, 405, <<"Method not allowed">>)
|
||||
end.
|
||||
|
||||
%% GET /v1/admin/tickets/stats - статистика по тикетам
|
||||
get_statistics(Req) ->
|
||||
case handler_auth:authenticate(Req) of
|
||||
{ok, AdminId, Req1} ->
|
||||
case logic_ticket:get_statistics(AdminId) of
|
||||
Stats when is_map(Stats) ->
|
||||
send_json(Req1, 200, Stats);
|
||||
{error, access_denied} ->
|
||||
send_error(Req1, 403, <<"Admin access required">>);
|
||||
{error, _} ->
|
||||
send_error(Req1, 500, <<"Internal server error">>)
|
||||
end;
|
||||
{error, Code, Message, Req1} ->
|
||||
send_error(Req1, Code, Message)
|
||||
end.
|
||||
|
||||
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).
|
||||
118
src/handlers/handler_tickets.erl
Normal file
118
src/handlers/handler_tickets.erl
Normal file
@@ -0,0 +1,118 @@
|
||||
-module(handler_tickets).
|
||||
-include("records.hrl").
|
||||
|
||||
-export([init/2]).
|
||||
|
||||
init(Req, Opts) ->
|
||||
handle(Req, Opts).
|
||||
|
||||
handle(Req, _Opts) ->
|
||||
case cowboy_req:method(Req) of
|
||||
<<"GET">> -> list_tickets(Req);
|
||||
<<"POST">> -> report_error(Req);
|
||||
_ -> send_error(Req, 405, <<"Method not allowed">>)
|
||||
end.
|
||||
|
||||
%% POST /v1/tickets - сообщить об ошибке (доступно всем)
|
||||
report_error(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
|
||||
#{<<"error_message">> := ErrorMessage} ->
|
||||
Stacktrace = maps:get(<<"stacktrace">>, Decoded, <<"">>),
|
||||
Context = maps:get(<<"context">>, Decoded, #{}),
|
||||
|
||||
case logic_ticket:report_error(ErrorMessage, Stacktrace, Context) of
|
||||
{ok, Ticket} ->
|
||||
Response = ticket_to_json(Ticket),
|
||||
send_json(Req2, 201, Response);
|
||||
{error, _} ->
|
||||
send_error(Req2, 500, <<"Internal server error">>)
|
||||
end;
|
||||
_ ->
|
||||
send_error(Req2, 400, <<"Missing error_message field">>)
|
||||
end;
|
||||
_ ->
|
||||
send_error(Req2, 400, <<"Invalid JSON">>)
|
||||
catch
|
||||
_:_ ->
|
||||
send_error(Req2, 400, <<"Invalid JSON format">>)
|
||||
end;
|
||||
{error, Code, Message, Req1} ->
|
||||
send_error(Req1, Code, Message)
|
||||
end.
|
||||
|
||||
%% GET /v1/admin/tickets - список тикетов (только админ)
|
||||
list_tickets(Req) ->
|
||||
case handler_auth:authenticate(Req) of
|
||||
{ok, AdminId, Req1} ->
|
||||
Qs = cowboy_req:parse_qs(Req1),
|
||||
case proplists:get_value(<<"status">>, Qs) of
|
||||
undefined ->
|
||||
case logic_ticket:list_tickets(AdminId) of
|
||||
{ok, Tickets} ->
|
||||
Response = [ticket_to_json(T) || T <- Tickets],
|
||||
send_json(Req1, 200, Response);
|
||||
{error, access_denied} ->
|
||||
send_error(Req1, 403, <<"Admin access required">>);
|
||||
{error, _} ->
|
||||
send_error(Req1, 500, <<"Internal server error">>)
|
||||
end;
|
||||
StatusBin ->
|
||||
Status = parse_status(StatusBin),
|
||||
case logic_ticket:list_tickets_by_status(AdminId, Status) of
|
||||
{ok, Tickets} ->
|
||||
Response = [ticket_to_json(T) || T <- Tickets],
|
||||
send_json(Req1, 200, Response);
|
||||
{error, access_denied} ->
|
||||
send_error(Req1, 403, <<"Admin access required">>);
|
||||
{error, _} ->
|
||||
send_error(Req1, 500, <<"Internal server error">>)
|
||||
end
|
||||
end;
|
||||
{error, Code, Message, Req1} ->
|
||||
send_error(Req1, Code, Message)
|
||||
end.
|
||||
|
||||
%% Вспомогательные функции
|
||||
parse_status(<<"open">>) -> open;
|
||||
parse_status(<<"in_progress">>) -> in_progress;
|
||||
parse_status(<<"resolved">>) -> resolved;
|
||||
parse_status(<<"closed">>) -> closed;
|
||||
parse_status(_) -> open.
|
||||
|
||||
ticket_to_json(Ticket) ->
|
||||
Context = try binary_to_term(Ticket#ticket.context) of
|
||||
C -> C
|
||||
catch
|
||||
_:_ -> #{}
|
||||
end,
|
||||
|
||||
#{
|
||||
id => Ticket#ticket.id,
|
||||
error_hash => Ticket#ticket.error_hash,
|
||||
error_message => Ticket#ticket.error_message,
|
||||
stacktrace => Ticket#ticket.stacktrace,
|
||||
context => Context,
|
||||
count => Ticket#ticket.count,
|
||||
first_seen => datetime_to_iso8601(Ticket#ticket.first_seen),
|
||||
last_seen => datetime_to_iso8601(Ticket#ticket.last_seen),
|
||||
status => Ticket#ticket.status,
|
||||
assigned_to => Ticket#ticket.assigned_to,
|
||||
resolution_note => Ticket#ticket.resolution_note
|
||||
}.
|
||||
|
||||
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