Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1 Load all tenant options #2

Merged
merged 2 commits into from
Dec 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 39 additions & 7 deletions tenant-option-management/tenant-option-management.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@ 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';
options: ITenantOption[];
}
@Injectable()
export class TenantOptionManagementService {
constructor(private inventory: InventoryService, private tenantOption: TenantOptionsService) {}
private readonly MAX_PAGE_SIZE = 2000;

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

async getConfiguration(): Promise<TenantOptionConfiguration> {
const { data } = await this.inventory.list({
Expand Down Expand Up @@ -62,12 +69,37 @@ 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);

this.alertService.danger('Failed to load tenant options', (error as Error).message);

return [];
}
}

async deleteOption(row: TenantOptionRow) {
Expand Down