Refactor admin stats API and charts data models for consistency

- Updated `AdminStatsResponse` with streamlined type annotations and added `AdminStatsData` type definition.
- Renamed chart data model fields (`totalUsers` → `total_users`, `activeUsers` → `active_users`, `members` → `value`, etc.) for alignment with backend naming conventions.
- Adjusted related test files to reflect updated data model structure.
- Improved readability of `AdminPage` component by reformatting destructuring in `useQuery`.
This commit is contained in:
Felipe Cardoso
2025-11-24 12:44:45 +01:00
parent 2e4700ae9b
commit acfe59c8b3
4 changed files with 40 additions and 29 deletions

View File

@@ -19,7 +19,11 @@ import { useQuery } from '@tanstack/react-query';
import { getAdminStats } from '@/lib/api/admin'; import { getAdminStats } from '@/lib/api/admin';
export default function AdminPage() { export default function AdminPage() {
const { data: stats, isLoading, error } = useQuery({ const {
data: stats,
isLoading,
error,
} = useQuery({
queryKey: ['admin', 'stats'], queryKey: ['admin', 'stats'],
queryFn: async () => { queryFn: async () => {
const response = await getAdminStats(); const response = await getAdminStats();

View File

@@ -2,44 +2,51 @@ import { apiClient } from './client';
import type { Options } from './generated/sdk.gen'; import type { Options } from './generated/sdk.gen';
export interface UserGrowthData { export interface UserGrowthData {
date: string; date: string;
total_users: number; total_users: number;
active_users: number; active_users: number;
} }
export interface OrgDistributionData { export interface OrgDistributionData {
name: string; name: string;
value: number; value: number;
} }
export interface UserStatusData { export interface UserStatusData {
name: string; name: string;
value: number; value: number;
} }
export interface AdminStatsResponse { export interface AdminStatsResponse {
user_growth: UserGrowthData[]; user_growth: UserGrowthData[];
organization_distribution: OrgDistributionData[]; organization_distribution: OrgDistributionData[];
user_status: UserStatusData[]; user_status: UserStatusData[];
} }
export type AdminStatsData = {
body?: never;
path?: never;
query?: never;
url: '/api/v1/admin/stats';
};
/** /**
* Admin: Get Dashboard Stats * Admin: Get Dashboard Stats
* *
* Get aggregated statistics for the admin dashboard (admin only) * Get aggregated statistics for the admin dashboard (admin only)
*/ */
export const getAdminStats = <ThrowOnError extends boolean = false>( export const getAdminStats = <ThrowOnError extends boolean = false>(
options?: Options<any, ThrowOnError> options?: Options<AdminStatsData, ThrowOnError>
) => { ) => {
return (options?.client ?? apiClient).get<AdminStatsResponse, any, ThrowOnError>({ return (options?.client ?? apiClient).get<AdminStatsResponse, unknown, ThrowOnError>({
responseType: 'json', responseType: 'json',
security: [ security: [
{ {
scheme: 'bearer', scheme: 'bearer',
type: 'http', type: 'http',
}, },
], ],
url: '/api/v1/admin/stats', url: '/api/v1/admin/stats',
...options, ...options,
}); });
}; };

View File

@@ -19,9 +19,9 @@ jest.mock('recharts', () => {
describe('OrganizationDistributionChart', () => { describe('OrganizationDistributionChart', () => {
const mockData: OrganizationDistributionData[] = [ const mockData: OrganizationDistributionData[] = [
{ name: 'Engineering', members: 45, activeMembers: 42 }, { name: 'Engineering', value: 45 },
{ name: 'Marketing', members: 28, activeMembers: 25 }, { name: 'Marketing', value: 28 },
{ name: 'Sales', members: 35, activeMembers: 33 }, { name: 'Sales', value: 35 },
]; ];
it('renders chart card with title and description', () => { it('renders chart card with title and description', () => {

View File

@@ -19,9 +19,9 @@ jest.mock('recharts', () => {
describe('UserGrowthChart', () => { describe('UserGrowthChart', () => {
const mockData: UserGrowthData[] = [ const mockData: UserGrowthData[] = [
{ date: 'Jan 1', totalUsers: 100, activeUsers: 80 }, { date: 'Jan 1', total_users: 100, active_users: 80 },
{ date: 'Jan 2', totalUsers: 105, activeUsers: 85 }, { date: 'Jan 2', total_users: 105, active_users: 85 },
{ date: 'Jan 3', totalUsers: 110, activeUsers: 90 }, { date: 'Jan 3', total_users: 110, active_users: 90 },
]; ];
it('renders chart card with title and description', () => { it('renders chart card with title and description', () => {