Skip to content

Commit 06ac1f1

Browse files
authored
Merge pull request #414 from openITCOCKPIT/ITC-3617
ITC-3617 Refactor checking of duplicate service name to keep the last…
2 parents 7f55863 + f3ce8ef commit 06ac1f1

File tree

8 files changed

+102
-80
lines changed

8 files changed

+102
-80
lines changed

src/app/pages/hosts/hosts-add/hosts-add.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ <h5 cCardTitle>{{ t('Create new host') }}</h5>
166166
<fa-icon [icon]="['fas', 'exclamation-triangle']"></fa-icon>
167167
{{
168168
t('A host with the name {0} already exists. Duplicate host names could lead to confusion.', {
169-
'0': post.name
169+
'0': hostnameCheckedForDuplicates
170170
})
171171
}}
172172

src/app/pages/hosts/hosts-add/hosts-add.component.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ export class HostsAddComponent implements OnInit, OnDestroy {
135135
isHostnameInUse: false
136136
};
137137

138+
public hostnameCheckedForDuplicates: string = '';
139+
140+
138141
public hosttemplates: SelectKeyValue[] = [];
139142
public hostgroups: SelectKeyValue[] = [];
140143
public timeperiods: SelectKeyValue[] = [];
@@ -417,6 +420,8 @@ export class HostsAddComponent implements OnInit, OnDestroy {
417420
this.subscriptions.add(this.HostsService.checkForDuplicateHostname(this.post.name)
418421
.subscribe((result) => {
419422
this.data.isHostnameInUse = result;
423+
// Ensure that the host name in the warning box will not change until the user changes the name again
424+
this.hostnameCheckedForDuplicates = this.post.name;
420425
this.cdr.markForCheck();
421426
})
422427
);

src/app/pages/hosts/hosts-edit/hosts-edit.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ <h5 cCardTitle>{{ t('Edit host:') }}
204204
<fa-icon [icon]="['fas', 'exclamation-triangle']"></fa-icon>
205205
{{
206206
t('A host with the name {0} already exists. Duplicate host names could lead to confusion.', {
207-
'0': post.name
207+
'0': hostnameCheckedForDuplicates
208208
})
209209
}}
210210

src/app/pages/hosts/hosts-edit/hosts-edit.component.ts

Lines changed: 78 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, inject, OnDestroy, OnInit } from '@angular/core';
22

33
import {
4-
AlertComponent,
5-
CardBodyComponent,
6-
CardComponent,
7-
CardFooterComponent,
8-
CardHeaderComponent,
9-
CardTitleDirective,
10-
DropdownComponent,
11-
DropdownItemDirective,
12-
DropdownMenuDirective,
13-
DropdownToggleDirective,
14-
FormCheckComponent,
15-
FormCheckInputDirective,
16-
FormCheckLabelDirective,
17-
FormControlDirective,
18-
FormDirective,
19-
FormLabelDirective,
20-
InputGroupComponent,
21-
InputGroupTextDirective,
22-
NavComponent,
23-
NavItemComponent
4+
AlertComponent,
5+
CardBodyComponent,
6+
CardComponent,
7+
CardFooterComponent,
8+
CardHeaderComponent,
9+
CardTitleDirective,
10+
DropdownComponent,
11+
DropdownItemDirective,
12+
DropdownMenuDirective,
13+
DropdownToggleDirective,
14+
FormCheckComponent,
15+
FormCheckInputDirective,
16+
FormCheckLabelDirective,
17+
FormControlDirective,
18+
FormDirective,
19+
FormLabelDirective,
20+
InputGroupComponent,
21+
InputGroupTextDirective,
22+
NavComponent,
23+
NavItemComponent
2424
} from '@coreui/angular';
2525
import { BackButtonDirective } from '../../../directives/back-button.directive';
2626
import {
@@ -69,59 +69,59 @@ import { HistoryService } from '../../../history.service';
6969
@Component({
7070
selector: 'oitc-hosts-edit',
7171
imports: [
72-
AlertComponent,
73-
BackButtonDirective,
74-
CardBodyComponent,
75-
CardComponent,
76-
CardFooterComponent,
77-
CardHeaderComponent,
78-
CardTitleDirective,
79-
CheckAttemptsInputComponent,
80-
DropdownComponent,
81-
DropdownItemDirective,
82-
DropdownMenuDirective,
83-
DropdownToggleDirective,
84-
FaIconComponent,
85-
FormCheckComponent,
86-
FormCheckInputDirective,
87-
FormCheckLabelDirective,
88-
FormControlDirective,
89-
FormDirective,
90-
FormErrorDirective,
91-
FormFeedbackComponent,
92-
FormLabelDirective,
93-
FormsModule,
94-
HumanTimeComponent,
95-
InputGroupComponent,
96-
InputGroupTextDirective,
97-
IntervalInputComponent,
98-
LabelLinkComponent,
99-
MacrosComponent,
100-
MultiSelectComponent,
101-
NavComponent,
102-
NavItemComponent,
103-
NgForOf,
104-
NgIf,
105-
NgSelectModule,
106-
PermissionDirective,
107-
PriorityComponent,
108-
ReactiveFormsModule,
109-
RequiredIconComponent,
110-
SelectComponent,
111-
TemplateDiffBtnComponent,
112-
TemplateDiffComponent,
113-
TranslocoDirective,
114-
TranslocoPipe,
115-
TrueFalseDirective,
116-
XsButtonDirective,
117-
RouterLink,
118-
ObjectUuidComponent,
119-
NgClass,
120-
FakeSelectComponent,
121-
UiBlockerComponent,
122-
FormLoaderComponent,
123-
AsyncPipe
124-
],
72+
AlertComponent,
73+
BackButtonDirective,
74+
CardBodyComponent,
75+
CardComponent,
76+
CardFooterComponent,
77+
CardHeaderComponent,
78+
CardTitleDirective,
79+
CheckAttemptsInputComponent,
80+
DropdownComponent,
81+
DropdownItemDirective,
82+
DropdownMenuDirective,
83+
DropdownToggleDirective,
84+
FaIconComponent,
85+
FormCheckComponent,
86+
FormCheckInputDirective,
87+
FormCheckLabelDirective,
88+
FormControlDirective,
89+
FormDirective,
90+
FormErrorDirective,
91+
FormFeedbackComponent,
92+
FormLabelDirective,
93+
FormsModule,
94+
HumanTimeComponent,
95+
InputGroupComponent,
96+
InputGroupTextDirective,
97+
IntervalInputComponent,
98+
LabelLinkComponent,
99+
MacrosComponent,
100+
MultiSelectComponent,
101+
NavComponent,
102+
NavItemComponent,
103+
NgForOf,
104+
NgIf,
105+
NgSelectModule,
106+
PermissionDirective,
107+
PriorityComponent,
108+
ReactiveFormsModule,
109+
RequiredIconComponent,
110+
SelectComponent,
111+
TemplateDiffBtnComponent,
112+
TemplateDiffComponent,
113+
TranslocoDirective,
114+
TranslocoPipe,
115+
TrueFalseDirective,
116+
XsButtonDirective,
117+
RouterLink,
118+
ObjectUuidComponent,
119+
NgClass,
120+
FakeSelectComponent,
121+
UiBlockerComponent,
122+
FormLoaderComponent,
123+
AsyncPipe
124+
],
125125
templateUrl: './hosts-edit.component.html',
126126
styleUrl: './hosts-edit.component.css',
127127
changeDetection: ChangeDetectionStrategy.OnPush
@@ -155,6 +155,9 @@ export class HostsEditComponent implements OnInit, OnDestroy {
155155
disableInheritance: false
156156
};
157157

158+
public hostnameCheckedForDuplicates: string = '';
159+
160+
158161
public hosttemplates: SelectKeyValue[] = [];
159162
public hostgroups: SelectKeyValue[] = [];
160163
public timeperiods: SelectKeyValue[] = [];
@@ -410,6 +413,8 @@ export class HostsEditComponent implements OnInit, OnDestroy {
410413
this.subscriptions.add(this.HostsService.checkForDuplicateHostname(this.post.name, [this.id])
411414
.subscribe((result) => {
412415
this.data.isHostnameInUse = result;
416+
// Ensure that the host name in the warning box will not change until the user changes the name again
417+
this.hostnameCheckedForDuplicates = this.post.name;
413418
this.cdr.markForCheck();
414419
})
415420
);

src/app/pages/services/services-add/services-add.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ <h5 cCardTitle>{{ t('Create new service') }}</h5>
131131
<fa-icon [icon]="['fas', 'exclamation-triangle']"></fa-icon>
132132
{{
133133
t('A service with the name {0} already exists on this host. Duplicate service names could lead to confusion.', {
134-
'0': post.name
134+
'0': servicenameCheckedForDuplicates
135135
})
136136
}}
137137

src/app/pages/services/services-add/services-add.component.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import { BackButtonDirective } from '../../../directives/back-button.directive';
2121
import {
2222
CheckAttemptsInputComponent
2323
} from '../../../layouts/coreui/check-attempts-input/check-attempts-input.component';
24-
import { CoreuiComponent } from '../../../layouts/coreui/coreui.component';
2524
import { FaIconComponent } from '@fortawesome/angular-fontawesome';
2625
import { FormErrorDirective } from '../../../layouts/coreui/form-error.directive';
2726
import { FormFeedbackComponent } from '../../../layouts/coreui/form-feedback/form-feedback.component';
@@ -136,6 +135,8 @@ export class ServicesAddComponent {
136135
areContactsInheritedFromServicetemplate: false,
137136
};
138137

138+
public servicenameCheckedForDuplicates: string = '';
139+
139140
public servicetemplates: SelectKeyValue[] = [];
140141
public servicegroups: SelectKeyValue[] = [];
141142
public timeperiods: SelectKeyValue[] = [];
@@ -380,6 +381,8 @@ export class ServicesAddComponent {
380381
public checkForDuplicateServicename() {
381382
const existingServicesNames: string[] = Object.values(this.existingServices);
382383
this.data.isServicenameInUse = existingServicesNames.includes(this.post.name);
384+
// Ensure that the service name in the warning box will not change until the user changes the name again
385+
this.servicenameCheckedForDuplicates = this.post.name;
383386
this.cdr.markForCheck();
384387
}
385388

src/app/pages/services/services-edit/services-edit.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ <h5 cCardTitle>
187187
<fa-icon [icon]="['fas', 'exclamation-triangle']"></fa-icon>
188188
{{
189189
t('A service with the name {0} already exists on this host. Duplicate service names could lead to confusion.', {
190-
'0': post.name
190+
'0': servicenameCheckedForDuplicates
191191
})
192192
}}
193193

src/app/pages/services/services-edit/services-edit.component.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import { BackButtonDirective } from '../../../directives/back-button.directive';
2121
import {
2222
CheckAttemptsInputComponent
2323
} from '../../../layouts/coreui/check-attempts-input/check-attempts-input.component';
24-
import { CoreuiComponent } from '../../../layouts/coreui/coreui.component';
2524
import { FaIconComponent } from '@fortawesome/angular-fontawesome';
2625
import { FormErrorDirective } from '../../../layouts/coreui/form-error.directive';
2726
import { FormFeedbackComponent } from '../../../layouts/coreui/form-feedback/form-feedback.component';
@@ -142,14 +141,15 @@ export class ServicesEditComponent {
142141
areContactsInheritedFromHost: false,
143142
areContactsInheritedFromServicetemplate: false,
144143
};
144+
public servicenameCheckedForDuplicates: string = '';
145145

146146
public servicetemplates: SelectKeyValue[] = [];
147147
public servicegroups: SelectKeyValue[] = [];
148148
public timeperiods: SelectKeyValue[] = [];
149149
public checkperiods: SelectKeyValue[] = [];
150150
public contacts: SelectKeyValue[] = [];
151151
public contactgroups: SelectKeyValue[] = [];
152-
public existingServices: object = {}
152+
public existingServices: Record<number, string> = {};
153153
public isSlaHost: boolean = false;
154154

155155
public errors: GenericValidationError | null = null;
@@ -326,8 +326,17 @@ export class ServicesEditComponent {
326326
}
327327

328328
public checkForDuplicateServicename() {
329+
// Remove current service from existing services to avoid false positive
330+
const serviceId = this.post.id;
331+
if (serviceId && this.existingServices) {
332+
if (this.existingServices.hasOwnProperty(serviceId)) {
333+
delete this.existingServices[serviceId];
334+
}
335+
}
336+
329337
const existingServicesNames: string[] = Object.values(this.existingServices);
330338
this.data.isServicenameInUse = existingServicesNames.includes(this.post.name);
339+
this.servicenameCheckedForDuplicates = this.post.name;
331340
this.cdr.markForCheck();
332341
}
333342

0 commit comments

Comments
 (0)