This commit is contained in:
2026-04-21 15:06:57 +03:00
parent fc6ef8de7e
commit a4a7daa5e0
10 changed files with 1084 additions and 0 deletions

View 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).