2023-01-23 14:54:15 +02:00
|
|
|
///
|
|
|
|
|
/// Copyright © 2016-2022 The Thingsboard Authors
|
|
|
|
|
///
|
|
|
|
|
/// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
/// you may not use this file except in compliance with the License.
|
|
|
|
|
/// You may obtain a copy of the License at
|
|
|
|
|
///
|
|
|
|
|
/// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
///
|
|
|
|
|
/// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
/// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
/// See the License for the specific language governing permissions and
|
|
|
|
|
/// limitations under the License.
|
|
|
|
|
///
|
|
|
|
|
|
2023-01-27 15:47:08 +02:00
|
|
|
import { NotificationRule, TriggerType, TriggerTypeTranslationMap } from '@shared/models/notification.models';
|
|
|
|
|
import { Component, ElementRef, Inject, OnDestroy, ViewChild } from '@angular/core';
|
2023-01-23 14:54:15 +02:00
|
|
|
import { DialogComponent } from '@shared/components/dialog.component';
|
|
|
|
|
import { Store } from '@ngrx/store';
|
|
|
|
|
import { AppState } from '@core/core.state';
|
|
|
|
|
import { Router } from '@angular/router';
|
|
|
|
|
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
|
|
|
|
|
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
|
|
|
|
|
import { NotificationService } from '@core/http/notification.service';
|
2023-01-31 02:03:51 +02:00
|
|
|
import { EntityType } from '@shared/models/entity-type.models';
|
2023-02-01 02:02:27 +02:00
|
|
|
import { deepClone, deepTrim, isDefined } from '@core/utils';
|
2023-01-27 15:47:08 +02:00
|
|
|
import { Observable, of, Subject } from 'rxjs';
|
2023-01-31 02:03:51 +02:00
|
|
|
import { map, mergeMap, share, startWith, takeUntil } from 'rxjs/operators';
|
2023-01-27 15:47:08 +02:00
|
|
|
import { StepperOrientation, StepperSelectionEvent } from '@angular/cdk/stepper';
|
|
|
|
|
import { MatStepper } from '@angular/material/stepper';
|
|
|
|
|
import { MediaBreakpoints } from '@shared/models/constants';
|
|
|
|
|
import { BreakpointObserver } from '@angular/cdk/layout';
|
|
|
|
|
import { MatChipInputEvent, MatChipList } from '@angular/material/chips';
|
|
|
|
|
import { COMMA, ENTER, SEMICOLON } from '@angular/cdk/keycodes';
|
|
|
|
|
import {
|
|
|
|
|
AlarmSeverity,
|
2023-01-31 02:03:51 +02:00
|
|
|
alarmSeverityTranslations,
|
|
|
|
|
AlarmStatus,
|
|
|
|
|
alarmStatusTranslations
|
2023-01-27 15:47:08 +02:00
|
|
|
} from '@shared/models/alarm.models';
|
|
|
|
|
import { MatAutocomplete, MatAutocompleteSelectedEvent } from '@angular/material/autocomplete';
|
|
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
|
|
|
|
import { TruncatePipe } from '@shared/pipe/truncate.pipe';
|
2023-01-23 14:54:15 +02:00
|
|
|
|
|
|
|
|
export interface RuleNotificationDialogData {
|
|
|
|
|
rule?: NotificationRule;
|
|
|
|
|
isAdd?: boolean;
|
2023-02-01 02:02:27 +02:00
|
|
|
isCopy?: boolean;
|
2023-01-23 14:54:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
selector: 'tb-rule-notification-dialog',
|
|
|
|
|
templateUrl: './rule-notification-dialog.component.html',
|
|
|
|
|
styleUrls: ['rule-notification-dialog.component.scss']
|
|
|
|
|
})
|
|
|
|
|
export class RuleNotificationDialogComponent extends
|
|
|
|
|
DialogComponent<RuleNotificationDialogComponent, NotificationRule> implements OnDestroy {
|
|
|
|
|
|
2023-01-27 15:47:08 +02:00
|
|
|
@ViewChild('addNotificationRule', {static: true}) addNotificationRule: MatStepper;
|
|
|
|
|
|
|
|
|
|
@ViewChild('severitiesChipList') severitiesChipList: MatChipList;
|
|
|
|
|
@ViewChild('severityAutocomplete') severityAutocomplete: MatAutocomplete;
|
|
|
|
|
@ViewChild('severityInput') severityInput: ElementRef<HTMLInputElement>;
|
|
|
|
|
|
|
|
|
|
readonly separatorKeysCodes: number[] = [ENTER, COMMA, SEMICOLON];
|
|
|
|
|
|
|
|
|
|
stepperOrientation: Observable<StepperOrientation>;
|
|
|
|
|
|
2023-01-23 14:54:15 +02:00
|
|
|
ruleNotificationForm: FormGroup;
|
2023-01-27 15:47:08 +02:00
|
|
|
alarmTemplateForm: FormGroup;
|
|
|
|
|
deviceInactivityTemplateForm: FormGroup;
|
|
|
|
|
entityActionTemplateForm: FormGroup;
|
|
|
|
|
|
|
|
|
|
triggerType = TriggerType;
|
|
|
|
|
triggerTypes: TriggerType[] = Object.values(TriggerType);
|
|
|
|
|
triggerTypeTranslationMap = TriggerTypeTranslationMap;
|
|
|
|
|
|
|
|
|
|
alarmSeverities = Object.keys(AlarmSeverity);
|
|
|
|
|
alarmSeverityEnum = AlarmSeverity;
|
|
|
|
|
alarmSeverityTranslationMap = alarmSeverityTranslations;
|
|
|
|
|
|
2023-02-01 15:58:00 +02:00
|
|
|
alarmSearchStatuses: AlarmStatus[] = Object.values(AlarmStatus);
|
2023-01-31 02:03:51 +02:00
|
|
|
alarmSearchStatusTranslationMap = alarmStatusTranslations;
|
2023-01-27 15:47:08 +02:00
|
|
|
|
2023-01-23 14:54:15 +02:00
|
|
|
entityType = EntityType;
|
2023-02-01 15:58:00 +02:00
|
|
|
entityTypes: EntityType[] = Object.values(EntityType);
|
2023-01-23 14:54:15 +02:00
|
|
|
isAdd = true;
|
|
|
|
|
|
2023-01-27 15:47:08 +02:00
|
|
|
selectedIndex = 0;
|
|
|
|
|
|
|
|
|
|
filteredDisplaySeverities: Observable<Array<string>>;
|
|
|
|
|
severitySearchText = '';
|
|
|
|
|
|
2023-02-01 02:02:27 +02:00
|
|
|
dialogTitle = 'notification.edit-rule';
|
|
|
|
|
|
2023-01-27 15:47:08 +02:00
|
|
|
severityInputChange = new Subject<string>();
|
|
|
|
|
|
2023-01-31 02:03:51 +02:00
|
|
|
private destroy$ = new Subject();
|
2023-01-23 14:54:15 +02:00
|
|
|
|
2023-02-01 02:02:27 +02:00
|
|
|
private readonly ruleNotification: NotificationRule;
|
|
|
|
|
|
2023-01-23 14:54:15 +02:00
|
|
|
constructor(protected store: Store<AppState>,
|
|
|
|
|
protected router: Router,
|
|
|
|
|
protected dialogRef: MatDialogRef<RuleNotificationDialogComponent, NotificationRule>,
|
|
|
|
|
@Inject(MAT_DIALOG_DATA) public data: RuleNotificationDialogData,
|
2023-01-27 15:47:08 +02:00
|
|
|
private breakpointObserver: BreakpointObserver,
|
2023-01-23 14:54:15 +02:00
|
|
|
private fb: FormBuilder,
|
2023-01-27 15:47:08 +02:00
|
|
|
public translate: TranslateService,
|
|
|
|
|
public truncate: TruncatePipe,
|
2023-01-23 14:54:15 +02:00
|
|
|
private notificationService: NotificationService) {
|
|
|
|
|
super(store, router, dialogRef);
|
|
|
|
|
|
|
|
|
|
if (isDefined(data.isAdd)) {
|
|
|
|
|
this.isAdd = data.isAdd;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-27 15:47:08 +02:00
|
|
|
this.stepperOrientation = this.breakpointObserver.observe(MediaBreakpoints['gt-xs'])
|
|
|
|
|
.pipe(map(({matches}) => matches ? 'horizontal' : 'vertical'));
|
|
|
|
|
|
2023-01-23 14:54:15 +02:00
|
|
|
this.ruleNotificationForm = this.fb.group({
|
|
|
|
|
name: [null, Validators.required],
|
|
|
|
|
templateId: [null, Validators.required],
|
2023-02-01 15:58:00 +02:00
|
|
|
triggerType: [TriggerType.ALARM, Validators.required],
|
2023-01-31 02:03:51 +02:00
|
|
|
recipientsConfig: this.fb.group({
|
|
|
|
|
triggerType: [],
|
|
|
|
|
}),
|
|
|
|
|
triggerConfig: this.fb.group({
|
|
|
|
|
triggerType: []
|
2023-02-01 02:02:27 +02:00
|
|
|
}),
|
|
|
|
|
additionalConfig: this.fb.group({
|
|
|
|
|
description: []
|
2023-01-23 14:54:15 +02:00
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
|
2023-01-31 02:03:51 +02:00
|
|
|
this.ruleNotificationForm.get('triggerType').valueChanges.pipe(
|
|
|
|
|
takeUntil(this.destroy$)
|
|
|
|
|
).subscribe(
|
|
|
|
|
value => {
|
|
|
|
|
this.ruleNotificationForm.get('triggerConfig').patchValue({triggerType: value}, {emitEvent: false});
|
|
|
|
|
this.ruleNotificationForm.get('recipientsConfig').patchValue({triggerType: value}, {emitEvent: false});
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2023-01-27 15:47:08 +02:00
|
|
|
this.alarmTemplateForm = this.fb.group({
|
2023-01-31 02:03:51 +02:00
|
|
|
alarmTypes: [[], Validators.required],
|
|
|
|
|
alarmSeverities: [[], Validators.required],
|
|
|
|
|
clearRule: this.fb.group({
|
|
|
|
|
alarmStatus: []
|
|
|
|
|
}),
|
|
|
|
|
escalationTable: [],
|
2023-01-27 15:47:08 +02:00
|
|
|
description: ['']
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.deviceInactivityTemplateForm = this.fb.group({
|
|
|
|
|
filterByDevice: [true],
|
2023-02-01 15:58:00 +02:00
|
|
|
devices: [null, Validators.required],
|
|
|
|
|
deviceProfiles: [{value: null, disabled: true}, Validators.required],
|
2023-01-31 02:03:51 +02:00
|
|
|
targets: [[], Validators.required],
|
2023-01-27 15:47:08 +02:00
|
|
|
description: ['']
|
|
|
|
|
});
|
|
|
|
|
|
2023-01-31 02:03:51 +02:00
|
|
|
this.deviceInactivityTemplateForm.get('filterByDevice').valueChanges.pipe(
|
|
|
|
|
takeUntil(this.destroy$)
|
2023-02-01 15:58:00 +02:00
|
|
|
).subscribe(value => {
|
|
|
|
|
if (value) {
|
|
|
|
|
this.deviceInactivityTemplateForm.get('devices').enable({emitEvent: false});
|
|
|
|
|
this.deviceInactivityTemplateForm.get('deviceProfiles').disable({emitEvent: false});
|
|
|
|
|
} else {
|
|
|
|
|
this.deviceInactivityTemplateForm.get('deviceProfiles').enable({emitEvent: false});
|
|
|
|
|
this.deviceInactivityTemplateForm.get('devices').disable({emitEvent: false});
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-01-31 02:03:51 +02:00
|
|
|
|
2023-01-27 15:47:08 +02:00
|
|
|
this.entityActionTemplateForm = this.fb.group({
|
2023-02-01 15:58:00 +02:00
|
|
|
entityType: [EntityType.DEVICE],
|
2023-01-31 02:03:51 +02:00
|
|
|
created: [false],
|
|
|
|
|
updated: [false],
|
|
|
|
|
deleted: [false],
|
|
|
|
|
targets: [[], Validators.required],
|
2023-01-27 15:47:08 +02:00
|
|
|
description: ['']
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.filteredDisplaySeverities = this.severityInputChange
|
|
|
|
|
.pipe(
|
|
|
|
|
startWith(''),
|
|
|
|
|
map((value) => value ? value : ''),
|
|
|
|
|
mergeMap(name => this.fetchSeverities(name) ),
|
|
|
|
|
share()
|
|
|
|
|
);
|
2023-02-01 02:02:27 +02:00
|
|
|
|
|
|
|
|
if (data.isAdd || data.isCopy) {
|
|
|
|
|
this.dialogTitle = 'notification.add-rule';
|
|
|
|
|
}
|
|
|
|
|
this.ruleNotification = deepClone(this.data.rule);
|
|
|
|
|
|
|
|
|
|
if (this.ruleNotification) {
|
|
|
|
|
if (this.data.isCopy) {
|
|
|
|
|
this.ruleNotification.name += ` (${this.translate.instant('action.copy')})`;
|
|
|
|
|
}
|
|
|
|
|
this.ruleNotificationForm.reset({}, {emitEvent: false});
|
|
|
|
|
this.ruleNotificationForm.patchValue(this.ruleNotification, {emitEvent: false});
|
|
|
|
|
if (this.ruleNotification.triggerType === TriggerType.ALARM) {
|
|
|
|
|
const parsedEscalationTable = [];
|
|
|
|
|
const escalationTable = this.ruleNotification.recipientsConfig.escalationTable;
|
|
|
|
|
for (const escalation in escalationTable) {
|
|
|
|
|
parsedEscalationTable.push({delayInSec: escalation, targets: escalationTable[escalation]});
|
|
|
|
|
}
|
|
|
|
|
this.alarmTemplateForm.patchValue({
|
|
|
|
|
escalationTable: parsedEscalationTable,
|
2023-02-01 15:58:00 +02:00
|
|
|
description: this.ruleNotification.additionalConfig.description,
|
|
|
|
|
...this.ruleNotification.triggerConfig
|
2023-02-01 02:02:27 +02:00
|
|
|
}, {emitEvent: false});
|
|
|
|
|
} else if (this.ruleNotification.triggerType === TriggerType.DEVICE_INACTIVITY) {
|
|
|
|
|
this.deviceInactivityTemplateForm.patchValue({
|
|
|
|
|
filterByDevice: !!this.ruleNotification.triggerConfig.devices,
|
2023-02-01 15:58:00 +02:00
|
|
|
deviceProfiles: this.ruleNotification.triggerConfig.deviceProfiles,
|
2023-02-01 02:02:27 +02:00
|
|
|
devices: this.ruleNotification.triggerConfig.devices,
|
|
|
|
|
targets: this.ruleNotification.recipientsConfig.targets,
|
|
|
|
|
description: this.ruleNotification.additionalConfig.description
|
|
|
|
|
}, {emitEvent: false});
|
2023-02-01 15:58:00 +02:00
|
|
|
this.deviceInactivityTemplateForm.get('filterByDevice').updateValueAndValidity({onlySelf: true});
|
2023-02-01 02:02:27 +02:00
|
|
|
} else {
|
|
|
|
|
this.entityActionTemplateForm.patchValue({
|
2023-02-01 15:58:00 +02:00
|
|
|
targets: this.ruleNotification.recipientsConfig.targets,
|
|
|
|
|
description: this.ruleNotification.additionalConfig.description,
|
|
|
|
|
...this.ruleNotification.triggerConfig
|
2023-02-01 02:02:27 +02:00
|
|
|
}, {emitEvent: false});
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-27 15:47:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onSeverityRemoved(severity: string): void {
|
2023-01-31 02:03:51 +02:00
|
|
|
const severities: string[] = this.alarmTemplateForm.get('alarmSeverities').value;
|
2023-01-27 15:47:08 +02:00
|
|
|
const index = severities.indexOf(severity);
|
|
|
|
|
if (index > -1) {
|
|
|
|
|
severities.splice(index, 1);
|
2023-01-31 02:03:51 +02:00
|
|
|
this.alarmTemplateForm.get('alarmSeverities').setValue(severities);
|
|
|
|
|
this.alarmTemplateForm.get('alarmSeverities').markAsDirty();
|
2023-01-27 15:47:08 +02:00
|
|
|
this.severitiesChipList.errorState = !severities.length;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onSeverityInputFocus() {
|
|
|
|
|
this.severityInputChange.next(this.severityInput.nativeElement.value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
addSeverityFromChipInput(event: MatChipInputEvent): void {
|
|
|
|
|
const value = event.value;
|
|
|
|
|
if ((value || '').trim()) {
|
|
|
|
|
const severityName = value.trim().toUpperCase();
|
|
|
|
|
const existingSeverity = this.alarmSeverities.find(severity => severity.toUpperCase() === severityName);
|
|
|
|
|
if (this.addSeverity(existingSeverity)) {
|
|
|
|
|
this.clearSeverityInput('');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private addSeverity(existingSeverity: string): boolean {
|
|
|
|
|
if (existingSeverity) {
|
2023-01-31 02:03:51 +02:00
|
|
|
const displaySeverities: string[] = this.alarmTemplateForm.get('alarmSeverities').value;
|
2023-01-27 15:47:08 +02:00
|
|
|
const index = displaySeverities.indexOf(existingSeverity);
|
|
|
|
|
if (index === -1) {
|
|
|
|
|
displaySeverities.push(existingSeverity);
|
2023-01-31 02:03:51 +02:00
|
|
|
this.alarmTemplateForm.get('alarmSeverities').setValue(displaySeverities);
|
|
|
|
|
this.alarmTemplateForm.get('alarmSeverities').markAsDirty();
|
2023-01-27 15:47:08 +02:00
|
|
|
this.severitiesChipList.errorState = false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clearSeverityInput(value: string = '') {
|
|
|
|
|
this.severityInput.nativeElement.value = value;
|
|
|
|
|
this.severityInputChange.next(null);
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
this.severityInput.nativeElement.blur();
|
|
|
|
|
this.severityInput.nativeElement.focus();
|
|
|
|
|
}, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
severitySelected(event: MatAutocompleteSelectedEvent): void {
|
|
|
|
|
this.addSeverity(event.option.value);
|
|
|
|
|
this.clearSeverityInput('');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
displaySeverityFn(severity?: string): string | undefined {
|
|
|
|
|
return severity ? this.translate.instant(this.alarmSeverityTranslationMap.get(this.alarmSeverityEnum[severity])) : undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fetchSeverities(searchText?: string): Observable<Array<string>> {
|
|
|
|
|
this.severitySearchText = searchText;
|
|
|
|
|
if (this.severitySearchText && this.severitySearchText.length) {
|
|
|
|
|
const search = this.severitySearchText.toUpperCase();
|
|
|
|
|
return of(this.alarmSeverities.filter(severity => severity.toUpperCase().includes(search)));
|
|
|
|
|
} else {
|
|
|
|
|
return of(this.alarmSeverities);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
textIsNotEmpty(text: string): boolean {
|
|
|
|
|
return (text && text.length > 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public alarmTypeList(): string[] {
|
2023-01-31 02:03:51 +02:00
|
|
|
return this.alarmTemplateForm.get('alarmTypes').value;
|
2023-01-27 15:47:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public removeAlarmType(type: string): void {
|
2023-01-31 02:03:51 +02:00
|
|
|
const types: string[] = this.alarmTemplateForm.get('alarmTypes').value;
|
2023-01-27 15:47:08 +02:00
|
|
|
const index = types.indexOf(type);
|
|
|
|
|
if (index >= 0) {
|
|
|
|
|
types.splice(index, 1);
|
2023-01-31 02:03:51 +02:00
|
|
|
this.alarmTemplateForm.get('alarmTypes').setValue(types);
|
|
|
|
|
this.alarmTemplateForm.get('alarmTypes').markAsDirty();
|
2023-01-27 15:47:08 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public addAlarmType(event: MatChipInputEvent): void {
|
2023-02-01 15:58:00 +02:00
|
|
|
const input = event.chipInput.inputElement;
|
|
|
|
|
let value = event.value || '';
|
|
|
|
|
|
|
|
|
|
if (value.trim()) {
|
|
|
|
|
value = value.trim();
|
|
|
|
|
let types: string[] = this.alarmTemplateForm.get('alarmTypes').value;
|
|
|
|
|
if (!types || types.indexOf(value) === -1) {
|
|
|
|
|
if (!types) {
|
|
|
|
|
types = [];
|
|
|
|
|
}
|
|
|
|
|
types.push(value);
|
|
|
|
|
this.alarmTemplateForm.get('alarmTypes').setValue(types);
|
|
|
|
|
this.alarmTemplateForm.get('alarmTypes').markAsDirty();
|
|
|
|
|
}
|
2023-01-27 15:47:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (input) {
|
|
|
|
|
input.value = '';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
changeStep($event: StepperSelectionEvent) {
|
|
|
|
|
this.selectedIndex = $event.selectedIndex;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
backStep() {
|
|
|
|
|
this.addNotificationRule.previous();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nextStep() {
|
|
|
|
|
if (this.selectedIndex >= this.maxStepperIndex) {
|
|
|
|
|
this.add();
|
|
|
|
|
} else {
|
|
|
|
|
this.addNotificationRule.next();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nextStepLabel(): string {
|
|
|
|
|
if (this.selectedIndex === 1 && this.selectedIndex < this.maxStepperIndex && this.alarmTemplateForm.pristine) {
|
|
|
|
|
return 'action.skip';
|
|
|
|
|
}
|
|
|
|
|
if (this.selectedIndex === 2 && this.selectedIndex < this.maxStepperIndex && this.deviceInactivityTemplateForm.pristine) {
|
|
|
|
|
return 'action.skip';
|
|
|
|
|
}
|
|
|
|
|
if (this.selectedIndex === 3 && this.selectedIndex < this.maxStepperIndex && this.entityActionTemplateForm.pristine) {
|
|
|
|
|
return 'action.skip';
|
|
|
|
|
}
|
|
|
|
|
if (this.selectedIndex !== 0 && this.selectedIndex >= this.maxStepperIndex) {
|
2023-02-01 15:58:00 +02:00
|
|
|
return (this.data.isAdd || this.data.isCopy) ? 'action.add' : 'action.save';
|
2023-01-27 15:47:08 +02:00
|
|
|
}
|
|
|
|
|
return 'action.next';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private get maxStepperIndex(): number {
|
|
|
|
|
return this.addNotificationRule?._steps?.length - 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private add(): void {
|
|
|
|
|
if (this.allValid()) {
|
2023-02-01 02:02:27 +02:00
|
|
|
let formValue = this.ruleNotificationForm.value;
|
2023-01-31 02:03:51 +02:00
|
|
|
const triggerType = this.ruleNotificationForm.get('triggerType').value;
|
|
|
|
|
if (triggerType === TriggerType.ALARM) {
|
|
|
|
|
Object.assign(formValue.triggerConfig, this.alarmTemplateForm.value);
|
|
|
|
|
const parsedEscalationTable = {};
|
|
|
|
|
this.alarmTemplateForm.get('escalationTable').value.forEach(
|
|
|
|
|
escalation => parsedEscalationTable[escalation.delayInSec] = escalation.targets
|
|
|
|
|
);
|
|
|
|
|
formValue.recipientsConfig.escalationTable = parsedEscalationTable;
|
2023-02-01 02:02:27 +02:00
|
|
|
formValue.additionalConfig.description = this.alarmTemplateForm.get('description').value;
|
2023-01-31 02:03:51 +02:00
|
|
|
delete formValue.triggerConfig.escalationTable;
|
|
|
|
|
} else if (triggerType === TriggerType.DEVICE_INACTIVITY) {
|
|
|
|
|
Object.assign(formValue.triggerConfig, this.deviceInactivityTemplateForm.value);
|
2023-02-01 02:02:27 +02:00
|
|
|
formValue.recipientsConfig.targets = this.deviceInactivityTemplateForm.get('targets').value;
|
|
|
|
|
formValue.additionalConfig.description = this.deviceInactivityTemplateForm.get('description').value;
|
2023-01-31 02:03:51 +02:00
|
|
|
delete formValue.triggerConfig.filterByDevice;
|
|
|
|
|
} else {
|
|
|
|
|
Object.assign(formValue.triggerConfig, this.entityActionTemplateForm.value);
|
2023-02-01 02:02:27 +02:00
|
|
|
formValue.recipientsConfig.targets = this.entityActionTemplateForm.get('targets').value;
|
|
|
|
|
formValue.additionalConfig.description = this.entityActionTemplateForm.get('description').value;
|
2023-01-31 02:03:51 +02:00
|
|
|
}
|
|
|
|
|
if (triggerType === TriggerType.DEVICE_INACTIVITY || triggerType === TriggerType.ENTITY_ACTION) {
|
|
|
|
|
delete formValue.triggerConfig.trigger;
|
|
|
|
|
}
|
2023-02-01 02:02:27 +02:00
|
|
|
if (this.ruleNotification) {
|
|
|
|
|
formValue = {...this.ruleNotification, ...formValue};
|
|
|
|
|
}
|
2023-01-27 15:47:08 +02:00
|
|
|
this.notificationService.saveNotificationRule(deepTrim(formValue)).subscribe(
|
|
|
|
|
(target) => this.dialogRef.close(target)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private allValid(): boolean {
|
|
|
|
|
return !this.addNotificationRule.steps.find((item, index) => {
|
|
|
|
|
if (item.stepControl.invalid) {
|
|
|
|
|
item.interacted = true;
|
|
|
|
|
this.addNotificationRule.selectedIndex = index;
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-01-23 14:54:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnDestroy() {
|
|
|
|
|
super.ngOnDestroy();
|
|
|
|
|
this.destroy$.next();
|
|
|
|
|
this.destroy$.complete();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cancel(): void {
|
|
|
|
|
this.dialogRef.close(null);
|
|
|
|
|
}
|
|
|
|
|
}
|