All files / services users.service.ts

100% Statements 23/23
100% Branches 0/0
100% Functions 8/8
100% Lines 23/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124                                                                                                                          4x 36x 30x           4x 3x 2x           4x 4x 3x           4x 3x 2x           4x       2x 2x           4x 2x 2x           4x 2x 2x           4x 3x    
/**
 * Users API service
 * Handles API calls for user management (superadmin only)
 */
 
import { apiClient } from '@/lib/api';
 
export interface User {
  id: string;
  email: string;
  account_id: string;
  account_code: string;
  account_name: string;
  role_id: number;
  role_name: string;
  is_active: boolean;
  created_at: string;
  last_login: string | null;
  email_verified: boolean;
  email_verified_at: string | null;
}
 
export type UserListItem = User;
 
export interface UserListResponse {
  total: number;
  items: UserListItem[];
}
 
export interface UserListParams {
  account_id?: string;
  role_id?: number;
  is_active?: boolean;
  search?: string;
  skip?: number;
  limit?: number;
  sort?: string;
}
 
export interface CreateUserRequest {
  email: string;
  password: string;
  account_id: string;
  role_id: number;
  is_active?: boolean;
}
 
export interface UpdateUserRequest {
  email?: string;
  role_id?: number;
  is_active?: boolean;
}
 
export interface PasswordResetRequest {
  new_password?: string;
  send_reset_link?: boolean;
}
 
/**
 * Get list of users with optional filters
 */
export const getUsers = async (params?: UserListParams): Promise<UserListResponse> => {
  const response = await apiClient.get<UserListResponse>('/users', { params });
  return response.data;
};
 
/**
 * Get a user by ID
 */
export const getUser = async (userId: string): Promise<User> => {
  const response = await apiClient.get<User>(`/users/${userId}`);
  return response.data;
};
 
/**
 * Create a new user
 */
export const createUser = async (data: CreateUserRequest): Promise<User> => {
  const response = await apiClient.post<User>('/users', data);
  return response.data;
};
 
/**
 * Update a user
 */
export const updateUser = async (userId: string, data: UpdateUserRequest): Promise<User> => {
  const response = await apiClient.patch<User>(`/users/${userId}`, data);
  return response.data;
};
 
/**
 * Reset a user's password
 */
export const resetUserPassword = async (
  userId: string,
  data: PasswordResetRequest
): Promise<{ message: string }> => {
  const response = await apiClient.put<{ message: string }>(`/users/${userId}/password`, data);
  return response.data;
};
 
/**
 * Manually verify a user's email
 */
export const verifyUser = async (userId: string): Promise<{ message: string }> => {
  const response = await apiClient.post<{ message: string }>(`/users/${userId}/verify`);
  return response.data;
};
 
/**
 * Resend verification email to a user
 */
export const resendUserVerification = async (userId: string): Promise<{ message: string }> => {
  const response = await apiClient.post<{ message: string }>(`/users/${userId}/resend-verification`);
  return response.data;
};
 
/**
 * Deactivate a user (soft delete)
 */
export const deactivateUser = async (userId: string): Promise<void> => {
  await apiClient.delete(`/users/${userId}`);
};