Skip to content

Commit 47912a1

Browse files
arpandhakalrrojan
authored andcommitted
fix(OUT-2097) : fetched clients in batches of max 5000 limit until the desired limit is reached. Max_LIMIT_CLIENT_COUNT is set to 5000.
1 parent e1d11f0 commit 47912a1

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

src/constants/users.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
export const MAX_FETCH_ASSIGNEE_COUNT = 15_000
2+
3+
export const MAX_LIMIT_CLIENT_COUNT = 5_000 //used to specify max fetching limit while querying for clients in copilot API.

src/utils/CopilotAPI.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { withRetry } from '@/app/api/core/utils/withRetry'
22
import { copilotAPIKey as apiKey, APP_ID } from '@/config'
33
import { API_DOMAIN } from '@/constants/domains'
4+
import { MAX_LIMIT_CLIENT_COUNT } from '@/constants/users'
45
import {
56
ClientRequest,
67
ClientResponse,
@@ -126,7 +127,30 @@ export class CopilotAPI {
126127

127128
async _getClients(args: CopilotListArgs & { companyId?: string } = {}) {
128129
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 })
130154
}
131155

132156
async _updateClient(id: string, requestBody: ClientRequest): Promise<ClientResponse> {

0 commit comments

Comments
 (0)