Skip to content

Commit

Permalink
add module to disable Gainsight for all subtenants (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
reey authored Nov 17, 2021
1 parent 6386960 commit 11750a2
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
67 changes: 67 additions & 0 deletions src/modules/disable-gainsight/disable-gainsight-action.factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { Injectable } from '@angular/core';
import { Client, ITenant, TenantService } from '@c8y/client';
import { ActionFactory, Action, AlertService } from '@c8y/ngx-components';
import { FakeMicroserviceService } from '@services/fake-microservice.service';
import { TenantSelectionService } from '@modules/shared/tenant-selection/tenant-selection.service';

@Injectable()
export class DisableGainsightActionFactory implements ActionFactory {
action: Action;
constructor(
private credService: FakeMicroserviceService,
private alertService: AlertService,
private tenantSelectionService: TenantSelectionService,
private tenantService: TenantService
) {
this.action = {
label: 'Disable Gainsight',
action: () => {
this.disableGainsight();
},
priority: 1,
icon: 'ban'
};
}

get(): Action {
return this.action;
}

async disableGainsight(): Promise<void> {
const credentials = await this.credService.prepareCachedDummyMicroserviceForAllSubtenants();
const tenantIds = credentials.map((tmp) => tmp.tenant);
let selectedTenantIds: string[] = [];
try {
selectedTenantIds = await this.tenantSelectionService.getTenantSelection(tenantIds);
} catch (e) {
return;
}
const filteredCredentials = credentials.filter((tmp) => selectedTenantIds.includes(tmp.tenant));
const clients = this.credService.createClients(filteredCredentials);
const promArray = clients.map((tmp) => this.disableGainsightIfNotAlreadyDone(tmp));
Promise.all(promArray).then(
(disabledTenants) => {
const tenantsWhichAlreadyWereDisabled = disabledTenants.filter((tmp) => !!tmp).length;
this.alertService.success(
`Disabled Gainsight on all selected Tenants.`,
`${tenantsWhichAlreadyWereDisabled ? `${tenantsWhichAlreadyWereDisabled} had already been disabled.` : ''}`
);
},
(error) => {
this.alertService.warning('Failed to disable Gainsight on all selected Tenants.', JSON.stringify(error));
}
);
}

async disableGainsightIfNotAlreadyDone(client: Client): Promise<boolean> {
const { data: currentTenant } = await client.tenant.current();
const customProperties = currentTenant.customProperties || {};
if (customProperties.gainsightEnabled === false) {
// already disabled
return true;
}
Object.assign(customProperties, { gainsightEnabled: false });
await this.tenantService.update({ customProperties, id: currentTenant.name } as ITenant & any);
return false;
}
}
12 changes: 12 additions & 0 deletions src/modules/disable-gainsight/disable-gainsight.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { CoreModule, HOOK_ACTION } from '@c8y/ngx-components';
import { DisableGainsightActionFactory } from './disable-gainsight-action.factory';
import { ModalModule } from 'ngx-bootstrap/modal';

@NgModule({
imports: [CommonModule, CoreModule, ModalModule],
declarations: [],
providers: [{ provide: HOOK_ACTION, useClass: DisableGainsightActionFactory, multi: true }]
})
export class DisableGainsightActionModule {}
2 changes: 2 additions & 0 deletions src/modules/subtenant-management.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { SubtenantManagementConfigService } from '@services/subtenant-management
import { TenantStatisticsModule } from './tenant-statistics/tenant-statistics.module';
import { SharedModule } from './shared/shared.module';
import { TenantAppLogsModule } from './tenant-app-logs/tenant-app-logs.module';
import { DisableGainsightActionModule } from './disable-gainsight/disable-gainsight.module';

@NgModule({
imports: [
Expand All @@ -32,6 +33,7 @@ import { TenantAppLogsModule } from './tenant-app-logs/tenant-app-logs.module';
LookupModule,
ProvisioningModule,
RestartApamaActionModule,
DisableGainsightActionModule,
TenantStatisticsModule,
TenantAppLogsModule
],
Expand Down

0 comments on commit 11750a2

Please sign in to comment.