/
service-account-delete-dialog.component.ts
121 lines (104 loc) · 3.74 KB
/
service-account-delete-dialog.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import { DialogRef, DIALOG_DATA } from "@angular/cdk/dialog";
import { Component, Inject } from "@angular/core";
import {
FormControl,
FormGroup,
ValidationErrors,
ValidatorFn,
AbstractControl,
} from "@angular/forms";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service";
import { DialogService } from "@bitwarden/components";
import { ServiceAccountView } from "../../models/view/service-account.view";
import {
BulkOperationStatus,
BulkStatusDetails,
BulkStatusDialogComponent,
} from "../../shared/dialogs/bulk-status-dialog.component";
import { ServiceAccountService } from "../service-account.service";
export interface ServiceAccountDeleteOperation {
serviceAccounts: ServiceAccountView[];
}
@Component({
templateUrl: "./service-account-delete-dialog.component.html",
})
export class ServiceAccountDeleteDialogComponent {
formGroup = new FormGroup({
confirmDelete: new FormControl("", [this.matchConfirmationMessageValidator()]),
});
constructor(
public dialogRef: DialogRef,
@Inject(DIALOG_DATA) public data: ServiceAccountDeleteOperation,
private serviceAccountService: ServiceAccountService,
private i18nService: I18nService,
private platformUtilsService: PlatformUtilsService,
private dialogService: DialogService,
) {}
get title() {
return this.data.serviceAccounts.length === 1
? this.i18nService.t("deleteServiceAccount")
: this.i18nService.t("deleteMachineAccounts");
}
get dialogContent() {
return this.data.serviceAccounts.length === 1
? this.i18nService.t("deleteServiceAccountDialogMessage", this.data.serviceAccounts[0].name)
: this.i18nService.t("deleteMachineAccountsDialogMessage");
}
get dialogConfirmationLabel() {
return this.i18nService.t("deleteProjectInputLabel", this.dialogConfirmationMessage);
}
submit = async () => {
this.formGroup.markAllAsTouched();
if (this.formGroup.invalid) {
return;
}
await this.delete();
this.dialogRef.close();
};
async delete() {
const bulkResponses = await this.serviceAccountService.delete(this.data.serviceAccounts);
const errors = bulkResponses.filter((response) => response.errorMessage);
if (errors.length > 0) {
this.openBulkStatusDialog(errors);
return;
}
const message =
this.data.serviceAccounts.length === 1
? "deleteServiceAccountToast"
: "deleteMachineAccountsToast";
this.platformUtilsService.showToast("success", null, this.i18nService.t(message));
}
openBulkStatusDialog(bulkStatusResults: BulkOperationStatus[]) {
this.dialogService.open<unknown, BulkStatusDetails>(BulkStatusDialogComponent, {
data: {
title: "deleteMachineAccounts",
subTitle: "machineAccounts",
columnTitle: "serviceAccountName",
message: "bulkDeleteProjectsErrorMessage",
details: bulkStatusResults,
},
});
}
private get dialogConfirmationMessage() {
return this.data.serviceAccounts?.length === 1
? this.i18nService.t("deleteProjectConfirmMessage", this.data.serviceAccounts[0].name)
: this.i18nService.t(
"deleteMachineAccountsConfirmMessage",
this.data.serviceAccounts?.length.toString(),
);
}
private matchConfirmationMessageValidator(): ValidatorFn {
return (control: AbstractControl): ValidationErrors | null => {
if (this.dialogConfirmationMessage.toLowerCase() == control.value.toLowerCase()) {
return null;
} else {
return {
confirmationDoesntMatchError: {
message: this.i18nService.t("smConfirmationRequired"),
},
};
}
};
}
}