|
1 | 1 | import { withRetry } from '@/app/api/core/utils/withRetry' |
2 | 2 | import { copilotAPIKey as apiKey, APP_ID } from '@/config' |
3 | 3 | import { API_DOMAIN } from '@/constants/domains' |
| 4 | +import { MAX_LIMIT_CLIENT_COUNT } from '@/constants/users' |
4 | 5 | import { |
5 | 6 | ClientRequest, |
6 | 7 | ClientResponse, |
@@ -126,7 +127,30 @@ export class CopilotAPI { |
126 | 127 |
|
127 | 128 | async _getClients(args: CopilotListArgs & { companyId?: string } = {}) { |
128 | 129 | console.info('CopilotAPI#_getClients', this.token) |
129 | | - return ClientsResponseSchema.parse(await this.copilot.listClients(args)) |
| 130 | + const maxLimit = MAX_LIMIT_CLIENT_COUNT |
| 131 | + const requestedLimit = args.limit || maxLimit |
| 132 | + let clients: ClientResponse[] = [] |
| 133 | + let nextToken: string | undefined = undefined |
| 134 | + |
| 135 | + if (requestedLimit <= maxLimit) { |
| 136 | + return ClientsResponseSchema.parse(await this.copilot.listClients(args)) |
| 137 | + } |
| 138 | + |
| 139 | + //fetching client data in batches of MAX_LIMIT_CLIENT_COUNT instead of fetching it as a whole. |
| 140 | + do { |
| 141 | + const remaining = requestedLimit - clients.length |
| 142 | + const fetchLimit = Math.min(maxLimit, remaining) |
| 143 | + const response = await this.copilot.listClients({ |
| 144 | + ...args, |
| 145 | + limit: fetchLimit, |
| 146 | + nextToken, |
| 147 | + }) |
| 148 | + const parsedData = ClientsResponseSchema.parse(response)?.data ?? [] |
| 149 | + clients.push(...parsedData) |
| 150 | + nextToken = response?.nextToken || undefined |
| 151 | + } while (nextToken && clients.length < requestedLimit) |
| 152 | + |
| 153 | + return ClientsResponseSchema.parse({ data: clients }) |
130 | 154 | } |
131 | 155 |
|
132 | 156 | async _updateClient(id: string, requestBody: ClientRequest): Promise<ClientResponse> { |
|
0 commit comments