Статистика для дашборда #7
This commit is contained in:
@@ -13,16 +13,8 @@ get_stats(Req) ->
|
||||
{ok, AdminId, Req1} ->
|
||||
case admin_utils:is_admin(AdminId) of
|
||||
true ->
|
||||
Stats = #{
|
||||
users => count_users(),
|
||||
calendars => count_calendars(),
|
||||
events => count_events(),
|
||||
bookings => count_bookings(),
|
||||
reviews => count_reviews(),
|
||||
reports => count_reports(),
|
||||
tickets => count_tickets(),
|
||||
subscriptions => count_subscriptions()
|
||||
},
|
||||
{ok, Admin} = core_admin:get_by_id(AdminId),
|
||||
Stats = logic_stats:get_stats(Admin#admin.role, AdminId),
|
||||
send_json(Req1, 200, Stats);
|
||||
false ->
|
||||
send_error(Req1, 403, <<"Admin access required">>)
|
||||
@@ -31,15 +23,6 @@ get_stats(Req) ->
|
||||
send_error(Req1, Code, Message)
|
||||
end.
|
||||
|
||||
count_users() -> length(mnesia:dirty_match_object(#user{_ = '_'})).
|
||||
count_calendars() -> length(mnesia:dirty_match_object(#calendar{_ = '_'})).
|
||||
count_events() -> length(mnesia:dirty_match_object(#event{is_instance = false, _ = '_'})).
|
||||
count_bookings() -> length(mnesia:dirty_match_object(#booking{_ = '_'})).
|
||||
count_reviews() -> length(mnesia:dirty_match_object(#review{_ = '_'})).
|
||||
count_reports() -> length(mnesia:dirty_match_object(#report{_ = '_'})).
|
||||
count_tickets() -> length(mnesia:dirty_match_object(#ticket{_ = '_'})).
|
||||
count_subscriptions() -> length(mnesia:dirty_match_object(#subscription{_ = '_'})).
|
||||
|
||||
send_json(Req, Status, Data) ->
|
||||
Body = jsx:encode(Data),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
|
||||
@@ -13,8 +13,8 @@ list_users(Req) ->
|
||||
{ok, AdminId, Req1} ->
|
||||
case admin_utils:is_admin(AdminId) of
|
||||
true ->
|
||||
Users = core_user:list_users(),
|
||||
send_json(Req1, 200, [user_to_json(U) || U <- Users]);
|
||||
{ok, Users} = core_user:list_users(),
|
||||
send_json(Req1, 200, [user_to_map(U) || U <- Users]);
|
||||
false ->
|
||||
send_error(Req1, 403, <<"Admin access required">>)
|
||||
end;
|
||||
@@ -22,14 +22,23 @@ list_users(Req) ->
|
||||
send_error(Req1, Code, Message)
|
||||
end.
|
||||
|
||||
user_to_json(U) ->
|
||||
user_to_map(User) when is_map(User) ->
|
||||
#{
|
||||
id => U#user.id,
|
||||
email => U#user.email,
|
||||
role => U#user.role,
|
||||
status => U#user.status,
|
||||
created_at => datetime_to_iso8601(U#user.created_at),
|
||||
updated_at => datetime_to_iso8601(U#user.updated_at)
|
||||
id => maps:get(id, User),
|
||||
email => maps:get(email, User),
|
||||
role => maps:get(role, User, <<"user">>),
|
||||
status => maps:get(status, User, <<"active">>),
|
||||
created_at => datetime_to_iso8601(maps:get(created_at, User)),
|
||||
updated_at => datetime_to_iso8601(maps:get(updated_at, User))
|
||||
};
|
||||
user_to_map(User) ->
|
||||
#{
|
||||
id => User#user.id,
|
||||
email => User#user.email,
|
||||
role => atom_to_binary(User#user.role, utf8),
|
||||
status => atom_to_binary(User#user.status, utf8),
|
||||
created_at => datetime_to_iso8601(User#user.created_at),
|
||||
updated_at => datetime_to_iso8601(User#user.updated_at)
|
||||
}.
|
||||
|
||||
datetime_to_iso8601({{Y,M,D},{H,Min,S}}) ->
|
||||
|
||||
@@ -22,7 +22,7 @@ init(Req, _Opts) ->
|
||||
case logic_auth:verify_jwt(Token) of
|
||||
{ok, UserId, Role} ->
|
||||
io:format("[ADMIN_WS] UserId: ~s, Role: ~s~n", [UserId, Role]),
|
||||
case admin_utils:is_admin(Role) of
|
||||
case lists:member(Role, [<<"admin">>, <<"superadmin">>, <<"moderator">>, <<"support">>]) of
|
||||
true ->
|
||||
io:format("[ADMIN_WS] Admin access granted~n"),
|
||||
{cowboy_websocket, Req, #state{admin_id = UserId}};
|
||||
|
||||
Reference in New Issue
Block a user