import { Component, OnInit } from "@angular/core"; import { ActivatedRoute } from "@angular/router"; import { combineLatest, Observable, startWith, switchMap } from "rxjs"; import { OrganizationService } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction"; import { DialogService } from "@bitwarden/components"; import { ServiceAccountSecretsDetailsView, ServiceAccountView, } from "../models/view/service-account.view"; import { AccessPolicyService } from "../shared/access-policies/access-policy.service"; import { ServiceAccountDeleteDialogComponent, ServiceAccountDeleteOperation, } from "./dialog/service-account-delete-dialog.component"; import { OperationType, ServiceAccountDialogComponent, ServiceAccountOperation, } from "./dialog/service-account-dialog.component"; import { ServiceAccountService } from "./service-account.service"; @Component({ selector: "sm-service-accounts", templateUrl: "./service-accounts.component.html", }) export class ServiceAccountsComponent implements OnInit { protected serviceAccounts$: Observable; protected search: string; private organizationId: string; private organizationEnabled: boolean; constructor( private route: ActivatedRoute, private dialogService: DialogService, private accessPolicyService: AccessPolicyService, private serviceAccountService: ServiceAccountService, private organizationService: OrganizationService ) {} ngOnInit() { this.serviceAccounts$ = combineLatest([ this.route.params, this.serviceAccountService.serviceAccount$.pipe(startWith(null)), this.accessPolicyService.serviceAccountAccessPolicyChanges$.pipe(startWith(null)), ]).pipe( switchMap(async ([params]) => { this.organizationId = params.organizationId; this.organizationEnabled = this.organizationService.get(params.organizationId)?.enabled; return await this.getServiceAccounts(); }) ); } openNewServiceAccountDialog() { this.dialogService.open(ServiceAccountDialogComponent, { data: { organizationId: this.organizationId, operation: OperationType.Add, organizationEnabled: this.organizationEnabled, }, }); } openEditServiceAccountDialog(serviceAccountId: string) { this.dialogService.open(ServiceAccountDialogComponent, { data: { organizationId: this.organizationId, serviceAccountId: serviceAccountId, operation: OperationType.Edit, organizationEnabled: this.organizationEnabled, }, }); } openDeleteDialog(event: ServiceAccountView[]) { this.dialogService.open( ServiceAccountDeleteDialogComponent, { data: { serviceAccounts: event, }, } ); } private async getServiceAccounts(): Promise { return await this.serviceAccountService.getServiceAccounts(this.organizationId, true); } }