From b45373f954c5034a66ade100102b9ab93b48d364 Mon Sep 17 00:00:00 2001 From: Christian Guether Date: Fri, 15 Dec 2023 11:52:02 +0100 Subject: [PATCH 1/2] #1 Refactor getAllOptions method to handle pagination --- .../tenant-option-management.service.ts | 37 ++++++++++++++++--- 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/tenant-option-management/tenant-option-management.service.ts b/tenant-option-management/tenant-option-management.service.ts index 3723c02..d53faec 100644 --- a/tenant-option-management/tenant-option-management.service.ts +++ b/tenant-option-management/tenant-option-management.service.ts @@ -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 { @@ -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) { From ad67dfcedd7081ea0dde928853d9a9c2bb0210fd Mon Sep 17 00:00:00 2001 From: Christian Guether Date: Mon, 18 Dec 2023 13:14:29 +0100 Subject: [PATCH 2/2] #1 display alert and return empty if tentant options couldn't be loaded --- .../tenant-option-management.service.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/tenant-option-management/tenant-option-management.service.ts b/tenant-option-management/tenant-option-management.service.ts index d53faec..9200a6f 100644 --- a/tenant-option-management/tenant-option-management.service.ts +++ b/tenant-option-management/tenant-option-management.service.ts @@ -2,6 +2,7 @@ import { Injectable } from '@angular/core'; import { IManagedObject, ITenantOption, InventoryService, TenantOptionsService } from '@c8y/client'; import { TenantOptionRow } from './tenant-option-management.component'; import { cloneDeep } from 'lodash'; +import { AlertService } from '@c8y/ngx-components'; export interface TenantOptionConfiguration extends IManagedObject { type: 'tenant_option_plugin_config'; @@ -11,7 +12,11 @@ export interface TenantOptionConfiguration extends IManagedObject { export class TenantOptionManagementService { private readonly MAX_PAGE_SIZE = 2000; - constructor(private inventory: InventoryService, private tenantOption: TenantOptionsService) {} + constructor( + private inventory: InventoryService, + private tenantOption: TenantOptionsService, + private alertService: AlertService + ) {} async getConfiguration(): Promise { const { data } = await this.inventory.list({ @@ -91,7 +96,9 @@ export class TenantOptionManagementService { } catch (error) { console.error(error); - return undefined; + this.alertService.danger('Failed to load tenant options', (error as Error).message); + + return []; } }