import { apiRequest } from "@/shared/api/request"; import { API_ENDPOINTS, API_OPERATIONS, apiEndpointPath } from "@/shared/api/generated/endpoints"; import type { AdminUserDto, ApiPage, CreateUserResultDto, EntityId, FinanceScopeAssignmentDto, PageQuery, ResetPasswordResultDto, UserFormPayload, UserFinanceScopeDto, UserAppScopeDto, UserStatus, UserUpdatePayload } from "@/shared/api/types"; export function listUsers(query?: PageQuery): Promise> { return apiRequest>(apiEndpointPath(API_OPERATIONS.listUsers), { query }); } export function getUser(id: EntityId): Promise { return apiRequest(apiEndpointPath(API_OPERATIONS.getUser, { id })); } export function createUser(payload: UserFormPayload): Promise { const endpoint = API_ENDPOINTS.createUser; return apiRequest(apiEndpointPath(API_OPERATIONS.createUser), { body: payload, method: endpoint.method }); } export function updateUser(id: EntityId, payload: UserUpdatePayload): Promise { const endpoint = API_ENDPOINTS.updateUser; return apiRequest(apiEndpointPath(API_OPERATIONS.updateUser, { id }), { body: payload, method: endpoint.method }); } export function updateUserStatus(id: EntityId, status: UserStatus): Promise { const endpoint = API_ENDPOINTS.updateUserStatus; return apiRequest(apiEndpointPath(API_OPERATIONS.updateUserStatus, { id }), { body: { status }, method: endpoint.method }); } export function resetUserPassword(id: EntityId, password?: string): Promise { const endpoint = API_ENDPOINTS.resetUserPassword; return apiRequest(apiEndpointPath(API_OPERATIONS.resetUserPassword, { id }), { body: { password }, method: endpoint.method }); } export function batchUpdateUserStatus(ids: EntityId[], status: UserStatus): Promise { const endpoint = API_ENDPOINTS.batchUpdateUserStatus; return apiRequest(apiEndpointPath(API_OPERATIONS.batchUpdateUserStatus), { body: { ids, status }, method: endpoint.method }); } export function exportUsers(query?: PageQuery): Promise { return apiRequest(apiEndpointPath(API_OPERATIONS.exportUsers), { query, raw: true }); } export interface FinanceScopeCatalogDto { all?: boolean; apps?: Array<{ appCode: string; appName?: string; logoUrl?: string }>; regions?: Array<{ appCode: string; countries?: string[]; name?: string; regionCode?: string; regionId: number }>; countries?: Array<{ appCode: string; countryCode: string; countryDisplayName?: string; countryName?: string; regionId?: number }>; scopes?: UserFinanceScopeDto[]; } export function listUserFinanceScopes(id: EntityId): Promise<{ items: UserFinanceScopeDto[]; total: number }> { return apiRequest<{ items: UserFinanceScopeDto[]; total: number }>(apiEndpointPath(API_OPERATIONS.listUserFinanceScopes, { id })); } export function replaceUserFinanceScopes(id: EntityId, scopes: UserFinanceScopeDto[]): Promise<{ items: UserFinanceScopeDto[]; total: number }> { return apiRequest<{ items: UserFinanceScopeDto[]; total: number }, { scopes: UserFinanceScopeDto[] }>(apiEndpointPath(API_OPERATIONS.replaceUserFinanceScopes, { id }), { body: { scopes }, method: "PUT" }); } export function getFinanceScopeCatalog(): Promise { return apiRequest(apiEndpointPath(API_OPERATIONS.getFinanceScope)); } export function listFinanceScopeAssignments(): Promise<{ items: FinanceScopeAssignmentDto[]; total: number }> { return apiRequest<{ items: FinanceScopeAssignmentDto[]; total: number }>(apiEndpointPath(API_OPERATIONS.listFinanceScopeAssignments)); } export function getUserAppScopes(id: EntityId): Promise { return apiRequest(apiEndpointPath(API_OPERATIONS.getUserAppScopes, { id })); } export function replaceUserAppScopes(id: EntityId, scope: UserAppScopeDto): Promise { return apiRequest(apiEndpointPath(API_OPERATIONS.replaceUserAppScopes, { id }), { body: scope, method: "PUT" }); }