feat: Implement a structured API service layer for authentication and user management, replacing the old API utility.
This commit is contained in:
79
src/services/users.service.ts
Normal file
79
src/services/users.service.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
import api, { ApiResponse } from './api';
|
||||
|
||||
// Types
|
||||
export interface User {
|
||||
id: string;
|
||||
email: string;
|
||||
role: string;
|
||||
status: string;
|
||||
emailVerified: boolean;
|
||||
authProvider: string;
|
||||
createdAt: string;
|
||||
lastLoginAt: string | null;
|
||||
profile: {
|
||||
firstName: string;
|
||||
lastName: string;
|
||||
avatar: string | null;
|
||||
phone: string | null;
|
||||
city: string | null;
|
||||
state: string | null;
|
||||
country: string | null;
|
||||
} | null;
|
||||
}
|
||||
|
||||
export interface UsersListResponse {
|
||||
users: User[];
|
||||
total: number;
|
||||
page: number;
|
||||
limit: number;
|
||||
}
|
||||
|
||||
export interface UserFilters {
|
||||
page?: number;
|
||||
limit?: number;
|
||||
role?: string;
|
||||
status?: string;
|
||||
search?: string;
|
||||
}
|
||||
|
||||
// Users Service
|
||||
class UsersService {
|
||||
private basePath = '/users';
|
||||
|
||||
async getUsers(filters: UserFilters = {}): Promise<UsersListResponse> {
|
||||
const params = new URLSearchParams();
|
||||
|
||||
if (filters.page) params.append('page', filters.page.toString());
|
||||
if (filters.limit) params.append('limit', filters.limit.toString());
|
||||
if (filters.role) params.append('role', filters.role);
|
||||
if (filters.status) params.append('status', filters.status);
|
||||
if (filters.search) params.append('search', filters.search);
|
||||
|
||||
const queryString = params.toString();
|
||||
const url = queryString ? `${this.basePath}?${queryString}` : this.basePath;
|
||||
|
||||
const response = await api.get<ApiResponse<UsersListResponse>>(url);
|
||||
return response.data.data;
|
||||
}
|
||||
|
||||
async getUserById(id: string): Promise<User> {
|
||||
const response = await api.get<ApiResponse<User>>(`${this.basePath}/${id}`);
|
||||
return response.data.data;
|
||||
}
|
||||
|
||||
async updateUser(id: string, data: Partial<User>): Promise<User> {
|
||||
const response = await api.patch<ApiResponse<User>>(`${this.basePath}/${id}`, data);
|
||||
return response.data.data;
|
||||
}
|
||||
|
||||
async deleteUser(id: string): Promise<void> {
|
||||
await api.delete(`${this.basePath}/${id}`);
|
||||
}
|
||||
|
||||
async updateUserStatus(id: string, status: string): Promise<User> {
|
||||
const response = await api.patch<ApiResponse<User>>(`${this.basePath}/${id}/status`, { status });
|
||||
return response.data.data;
|
||||
}
|
||||
}
|
||||
|
||||
export const usersService = new UsersService();
|
||||
Reference in New Issue
Block a user