admin area
This commit is contained in:
145
apps/website/lib/api/admin/AdminApiClient.ts
Normal file
145
apps/website/lib/api/admin/AdminApiClient.ts
Normal file
@@ -0,0 +1,145 @@
|
||||
import { BaseApiClient } from '../base/BaseApiClient';
|
||||
import type { ErrorReporter } from '@/lib/interfaces/ErrorReporter';
|
||||
import type { Logger } from '@/lib/interfaces/Logger';
|
||||
|
||||
export interface UserDto {
|
||||
id: string;
|
||||
email: string;
|
||||
displayName: string;
|
||||
roles: string[];
|
||||
status: string;
|
||||
isSystemAdmin: boolean;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
lastLoginAt?: Date;
|
||||
primaryDriverId?: string;
|
||||
}
|
||||
|
||||
export interface UserListResponse {
|
||||
users: UserDto[];
|
||||
total: number;
|
||||
page: number;
|
||||
limit: number;
|
||||
totalPages: number;
|
||||
}
|
||||
|
||||
export interface ListUsersQuery {
|
||||
role?: string;
|
||||
status?: string;
|
||||
email?: string;
|
||||
search?: string;
|
||||
page?: number;
|
||||
limit?: number;
|
||||
sortBy?: 'email' | 'displayName' | 'createdAt' | 'lastLoginAt' | 'status';
|
||||
sortDirection?: 'asc' | 'desc';
|
||||
}
|
||||
|
||||
export interface DashboardStats {
|
||||
totalUsers: number;
|
||||
activeUsers: number;
|
||||
suspendedUsers: number;
|
||||
deletedUsers: number;
|
||||
systemAdmins: number;
|
||||
recentLogins: number;
|
||||
newUsersToday: number;
|
||||
userGrowth: {
|
||||
label: string;
|
||||
value: number;
|
||||
color: string;
|
||||
}[];
|
||||
roleDistribution: {
|
||||
label: string;
|
||||
value: number;
|
||||
color: string;
|
||||
}[];
|
||||
statusDistribution: {
|
||||
active: number;
|
||||
suspended: number;
|
||||
deleted: number;
|
||||
};
|
||||
activityTimeline: {
|
||||
date: string;
|
||||
newUsers: number;
|
||||
logins: number;
|
||||
}[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Admin API Client
|
||||
*
|
||||
* Provides methods for admin operations like user management.
|
||||
* Only accessible to users with Owner or Super Admin roles.
|
||||
*/
|
||||
export class AdminApiClient extends BaseApiClient {
|
||||
/**
|
||||
* List all users with filtering, sorting, and pagination
|
||||
* Requires Owner or Super Admin role
|
||||
*/
|
||||
async listUsers(query: ListUsersQuery = {}): Promise<UserListResponse> {
|
||||
const params = new URLSearchParams();
|
||||
|
||||
if (query.role) params.append('role', query.role);
|
||||
if (query.status) params.append('status', query.status);
|
||||
if (query.email) params.append('email', query.email);
|
||||
if (query.search) params.append('search', query.search);
|
||||
if (query.page) params.append('page', query.page.toString());
|
||||
if (query.limit) params.append('limit', query.limit.toString());
|
||||
if (query.sortBy) params.append('sortBy', query.sortBy);
|
||||
if (query.sortDirection) params.append('sortDirection', query.sortDirection);
|
||||
|
||||
return this.get<UserListResponse>(`/admin/users?${params.toString()}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a single user by ID
|
||||
* Requires Owner or Super Admin role
|
||||
*/
|
||||
async getUser(userId: string): Promise<UserDto> {
|
||||
return this.get<UserDto>(`/admin/users/${userId}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update user roles
|
||||
* Requires Owner role only
|
||||
*/
|
||||
async updateUserRoles(userId: string, roles: string[]): Promise<UserDto> {
|
||||
return this.patch<UserDto>(`/admin/users/${userId}/roles`, { roles });
|
||||
}
|
||||
|
||||
/**
|
||||
* Update user status (activate/suspend/delete)
|
||||
* Requires Owner or Super Admin role
|
||||
*/
|
||||
async updateUserStatus(userId: string, status: string): Promise<UserDto> {
|
||||
return this.patch<UserDto>(`/admin/users/${userId}/status`, { status });
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a user (soft delete)
|
||||
* Requires Owner or Super Admin role
|
||||
*/
|
||||
async deleteUser(userId: string): Promise<void> {
|
||||
return this.delete(`/admin/users/${userId}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new user
|
||||
* Requires Owner or Super Admin role
|
||||
*/
|
||||
async createUser(userData: {
|
||||
email: string;
|
||||
displayName: string;
|
||||
roles: string[];
|
||||
primaryDriverId?: string;
|
||||
}): Promise<UserDto> {
|
||||
return this.post<UserDto>(`/admin/users`, userData);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get dashboard statistics
|
||||
* Requires Owner or Super Admin role
|
||||
*/
|
||||
async getDashboardStats(): Promise<DashboardStats> {
|
||||
return this.get<DashboardStats>(`/admin/dashboard/stats`);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user