164 lines
3.8 KiB
TypeScript
164 lines
3.8 KiB
TypeScript
import apiClient from "@/lib/api/client";
|
|
|
|
// ─── Types ────────────────────────────────────────────────────────────────────
|
|
|
|
export interface SocialMedia {
|
|
facebook: string;
|
|
instagram: string;
|
|
telegram: string;
|
|
}
|
|
|
|
export interface Client {
|
|
id: number;
|
|
email: string;
|
|
name: string;
|
|
firstName: string;
|
|
lastName: string;
|
|
username: string;
|
|
phone: string;
|
|
avatar: string;
|
|
socialMedia: SocialMedia;
|
|
lightMode: number;
|
|
role: string;
|
|
active: boolean;
|
|
createdAt: string;
|
|
updatedAt: string;
|
|
}
|
|
|
|
export interface ClientDetail extends Client {
|
|
roles: ClientRole[];
|
|
permissions: ClientPermission[];
|
|
branches: ClientBranch[];
|
|
loanOrders: ClientLoanOrder[];
|
|
cardOrders: ClientCardOrder[];
|
|
auditLogs: ClientAuditLog[];
|
|
}
|
|
|
|
export interface ClientRole {
|
|
id: number;
|
|
code: string;
|
|
name: string;
|
|
guardName: string;
|
|
}
|
|
|
|
export interface ClientPermission {
|
|
id: number;
|
|
name: string;
|
|
guardName: string;
|
|
}
|
|
|
|
export interface ClientBranch {
|
|
id: number;
|
|
name: string;
|
|
region: string;
|
|
district: string;
|
|
uniqueCode: string;
|
|
isActive: boolean;
|
|
billingUsernameIbr: string;
|
|
billingUsernameSber: string;
|
|
billingUsernameVisa: string;
|
|
}
|
|
|
|
export interface ClientLoanOrder {
|
|
id: number;
|
|
name: string;
|
|
amount: number;
|
|
status: string;
|
|
createdAt: string;
|
|
}
|
|
|
|
export interface ClientCardOrder {
|
|
id: number;
|
|
name: string;
|
|
amount: number;
|
|
status: string;
|
|
createdAt: string;
|
|
}
|
|
|
|
export interface ClientAuditLog {
|
|
id: number;
|
|
actionName: string;
|
|
performedBy: string;
|
|
target: string;
|
|
status: string;
|
|
createdAt: string;
|
|
}
|
|
|
|
export interface ClientsListResponse {
|
|
count: number;
|
|
users: Client[];
|
|
}
|
|
|
|
export interface ClientsListParams {
|
|
page?: number;
|
|
limit?: number;
|
|
search?: string;
|
|
isActive?: string;
|
|
}
|
|
|
|
export interface CreateClientPayload {
|
|
active: boolean;
|
|
avatar: string;
|
|
email: string;
|
|
firstName: string;
|
|
lastName: string;
|
|
lightMode: number;
|
|
name: string;
|
|
password: string;
|
|
phone: string;
|
|
role: string;
|
|
socialMedia: SocialMedia;
|
|
username: string;
|
|
}
|
|
|
|
export interface UpdateClientPayload {
|
|
active?: boolean;
|
|
avatar?: string;
|
|
email?: string;
|
|
firstName?: string;
|
|
lastName?: string;
|
|
lightMode?: number;
|
|
name?: string;
|
|
phone?: string;
|
|
role?: string;
|
|
socialMedia?: SocialMedia;
|
|
username?: string;
|
|
}
|
|
|
|
// ─── API Functions ────────────────────────────────────────────────────────────
|
|
|
|
export const clientsApi = {
|
|
/** GET /users/clients — backend filters by `username` */
|
|
getClients: async (params: ClientsListParams = {}, signal?: AbortSignal): Promise<ClientsListResponse> => {
|
|
const { page, limit, search, isActive } = params;
|
|
const { data } = await apiClient.get<ClientsListResponse>("/users/clients", {
|
|
params: { page, limit, username: search, isActive },
|
|
signal,
|
|
});
|
|
return data;
|
|
},
|
|
|
|
/** GET /users/:id */
|
|
getClientById: async (id: number): Promise<ClientDetail> => {
|
|
const { data } = await apiClient.get<ClientDetail>(`/users/${id}`);
|
|
return data;
|
|
},
|
|
|
|
/** POST /users — same entity & payload as all users */
|
|
createClient: async (payload: CreateClientPayload): Promise<Client> => {
|
|
const { data } = await apiClient.post<Client>("/users", payload);
|
|
return data;
|
|
},
|
|
|
|
/** PUT /users/:id */
|
|
updateClient: async (id: number, payload: UpdateClientPayload): Promise<Client> => {
|
|
const { data } = await apiClient.put<Client>(`/users/${id}`, payload);
|
|
return data;
|
|
},
|
|
|
|
/** DELETE /users/:id */
|
|
deleteClient: async (id: number): Promise<void> => {
|
|
await apiClient.delete(`/users/${id}`);
|
|
},
|
|
};
|