46 lines
1.3 KiB
Erlang
46 lines
1.3 KiB
Erlang
-module(admin_handler_ticket_stats).
|
|
-behaviour(cowboy_handler).
|
|
-export([init/2]).
|
|
|
|
-include("records.hrl"). % ← добавлено
|
|
|
|
init(Req, _Opts) ->
|
|
case cowboy_req:method(Req) of
|
|
<<"GET">> -> get_stats(Req);
|
|
_ -> send_error(Req, 405, <<"Method not allowed">>)
|
|
end.
|
|
|
|
get_stats(Req) ->
|
|
case auth_admin(Req) of
|
|
{ok, _AdminId, Req1} ->
|
|
Stats = core_ticket:stats(),
|
|
send_json(Req1, 200, Stats);
|
|
{error, Code, Message, Req1} ->
|
|
send_error(Req1, Code, Message)
|
|
end.
|
|
|
|
auth_admin(Req) ->
|
|
case handler_auth:authenticate(Req) of
|
|
{ok, AdminId, Req1} ->
|
|
case admin_utils:is_admin(AdminId) of
|
|
true -> {ok, AdminId, Req1};
|
|
false -> {error, 403, <<"Admin access required">>, Req1}
|
|
end;
|
|
{error, Code, Message, Req1} ->
|
|
{error, Code, Message, Req1}
|
|
end.
|
|
|
|
send_json(Req, Status, Data) ->
|
|
Headers = #{
|
|
<<"content-type">> => <<"application/json">>,
|
|
<<"access-control-allow-origin">> => <<"*">>,
|
|
<<"access-control-expose-headers">> => <<"Content-Range">>
|
|
},
|
|
Body = jsx:encode(Data),
|
|
cowboy_req:reply(Status, Headers, Body, Req),
|
|
{ok, Body, []}.
|
|
|
|
send_error(Req, Status, Message) ->
|
|
Body = jsx:encode(#{error => Message}),
|
|
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
|
{ok, Body, []}. |