-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add module to disable Gainsight for all subtenants (#34)
- Loading branch information
Showing
3 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
src/modules/disable-gainsight/disable-gainsight-action.factory.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters