Skip to content

Commit

Permalink
#1 Refactor getAllOptions method to handle pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
CGuether committed Dec 15, 2023
1 parent 0c03917 commit b45373f
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions tenant-option-management/tenant-option-management.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export interface TenantOptionConfiguration extends IManagedObject {
}
@Injectable()
export class TenantOptionManagementService {
private readonly MAX_PAGE_SIZE = 2000;

constructor(private inventory: InventoryService, private tenantOption: TenantOptionsService) {}

async getConfiguration(): Promise<TenantOptionConfiguration> {
Expand Down Expand Up @@ -62,12 +64,35 @@ export class TenantOptionManagementService {
return this.tenantOption.update(option);
}

getAllOptions() {
return this.tenantOption
.list({
pageSize: 2000,
})
.then((res) => res.data.map((o) => ({ id: `${o.category}-${o.key}`, value: o.value })));
async getAllOptions(): Promise<{ id: string; value: string }[]> {
try {
const tenantOptions: ITenantOption[] = [];
const response = await this.tenantOption.list({
pageSize: this.MAX_PAGE_SIZE,
withTotalPages: true,
});

tenantOptions.push(...response.data);

for (
let currentPage = response.paging.currentPage + 1;
currentPage <= response.paging.totalPages;
currentPage++
) {
const { data } = await this.tenantOption.list({
pageSize: this.MAX_PAGE_SIZE,
currentPage: currentPage,
});

tenantOptions.push(...data);
}

return tenantOptions.map((o) => ({ id: `${o.category}-${o.key}`, value: o.value }));
} catch (error) {
console.error(error);

return undefined;
}
}

async deleteOption(row: TenantOptionRow) {
Expand Down

0 comments on commit b45373f

Please sign in to comment.