( tenantId: string | null, page?: number, pageSize?: number )
| 43 | * If page and pageSize are not provided, returns all users |
| 44 | */ |
| 45 | export async function listUsers( |
| 46 | tenantId: string | null, |
| 47 | page?: number, |
| 48 | pageSize?: number |
| 49 | ): Promise<{ users: User[]; total: number; totalPages?: number }> { |
| 50 | if (!tenantId) return { users: [], total: 0 }; |
| 51 | |
| 52 | try { |
| 53 | const requestBody: any = { |
| 54 | tenant_id: tenantId, |
| 55 | sort_by: "created_at", |
| 56 | sort_order: "desc", |
| 57 | }; |
| 58 | |
| 59 | // Only include pagination parameters if both are provided |
| 60 | if (page !== undefined && pageSize !== undefined) { |
| 61 | requestBody.page = page; |
| 62 | requestBody.page_size = pageSize; |
| 63 | } |
| 64 | |
| 65 | const response = await fetchWithAuth(API_ENDPOINTS.users.list, { |
| 66 | method: "POST", |
| 67 | body: JSON.stringify(requestBody), |
| 68 | }); |
| 69 | |
| 70 | const result: UserListResponse = await response.json(); |
| 71 | return { |
| 72 | users: result.data, |
| 73 | // Support both paginated (pagination.total) and non-paginated (root total) responses |
| 74 | total: result.pagination?.total ?? result.total ?? 0, |
| 75 | totalPages: result.pagination?.total_pages, |
| 76 | }; |
| 77 | } catch (error) { |
| 78 | if (error instanceof ApiError) { |
| 79 | throw error; |
| 80 | } |
| 81 | throw new ApiError(500, "Failed to fetch users"); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Get user details by user ID |
no test coverage detected