2023-01-12 12:38:03 +02:00
|
|
|
///
|
2025-02-25 09:39:16 +02:00
|
|
|
/// Copyright © 2016-2025 The Thingsboard Authors
|
2023-01-12 12:38:03 +02:00
|
|
|
///
|
|
|
|
|
/// 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.
|
|
|
|
|
///
|
|
|
|
|
|
2024-02-07 17:50:47 +02:00
|
|
|
import { NotificationTemplate, NotificationType } from '@shared/models/notification.models';
|
2023-01-12 18:11:37 +02:00
|
|
|
import { Component, Inject, OnDestroy, ViewChild } from '@angular/core';
|
2023-01-12 12:38:03 +02:00
|
|
|
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';
|
2024-02-07 17:50:47 +02:00
|
|
|
import { FormBuilder, FormGroup } from '@angular/forms';
|
2023-01-12 12:38:03 +02:00
|
|
|
import { NotificationService } from '@core/http/notification.service';
|
2023-02-20 11:30:26 +02:00
|
|
|
import { deepClone, isDefinedAndNotNull } from '@core/utils';
|
2023-02-13 18:00:23 +02:00
|
|
|
import { Observable } from 'rxjs';
|
|
|
|
|
import { map } from 'rxjs/operators';
|
2023-01-12 18:11:37 +02:00
|
|
|
import { StepperOrientation, StepperSelectionEvent } from '@angular/cdk/stepper';
|
|
|
|
|
import { MatStepper } from '@angular/material/stepper';
|
|
|
|
|
import { BreakpointObserver } from '@angular/cdk/layout';
|
|
|
|
|
import { MediaBreakpoints } from '@shared/models/constants';
|
2023-01-30 10:23:36 +02:00
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
2023-03-21 17:56:12 +02:00
|
|
|
import { TemplateConfiguration } from '@home/pages/notification/template/template-configuration';
|
2024-02-07 17:50:47 +02:00
|
|
|
import { getCurrentAuthUser } from '@core/auth/auth.selectors';
|
2023-03-07 12:14:49 +02:00
|
|
|
import { AuthUser } from '@shared/models/user.model';
|
|
|
|
|
import { Authority } from '@shared/models/authority.enum';
|
2023-01-12 12:38:03 +02:00
|
|
|
|
|
|
|
|
export interface TemplateNotificationDialogData {
|
|
|
|
|
template?: NotificationTemplate;
|
2023-02-10 18:31:05 +02:00
|
|
|
predefinedType?: NotificationType;
|
2023-01-12 12:38:03 +02:00
|
|
|
isAdd?: boolean;
|
|
|
|
|
isCopy?: boolean;
|
2025-07-28 17:24:37 +03:00
|
|
|
name?: string;
|
2023-01-12 12:38:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
selector: 'tb-template-notification-dialog',
|
|
|
|
|
templateUrl: './template-notification-dialog.component.html',
|
|
|
|
|
styleUrls: ['./template-notification-dialog.component.scss']
|
|
|
|
|
})
|
|
|
|
|
export class TemplateNotificationDialogComponent
|
2023-02-13 18:00:23 +02:00
|
|
|
extends TemplateConfiguration<TemplateNotificationDialogComponent, NotificationTemplate> implements OnDestroy {
|
2023-01-12 12:38:03 +02:00
|
|
|
|
2023-01-30 10:23:36 +02:00
|
|
|
@ViewChild('notificationTemplateStepper', {static: true}) notificationTemplateStepper: MatStepper;
|
2023-01-12 18:11:37 +02:00
|
|
|
|
|
|
|
|
stepperOrientation: Observable<StepperOrientation>;
|
|
|
|
|
|
2023-01-12 12:38:03 +02:00
|
|
|
dialogTitle = 'notification.edit-notification-template';
|
|
|
|
|
|
2023-03-21 15:03:30 +02:00
|
|
|
notificationTypes: NotificationType[];
|
2023-01-12 18:11:37 +02:00
|
|
|
|
|
|
|
|
selectedIndex = 0;
|
2023-02-10 18:31:05 +02:00
|
|
|
hideSelectType = false;
|
2023-01-12 18:11:37 +02:00
|
|
|
|
2024-02-07 17:50:47 +02:00
|
|
|
notificationTemplateConfigurationForm: FormGroup;
|
|
|
|
|
|
2023-01-30 10:23:36 +02:00
|
|
|
private readonly templateNotification: NotificationTemplate;
|
2024-02-07 17:50:47 +02:00
|
|
|
private authUser: AuthUser = getCurrentAuthUser(this.store);
|
2023-01-12 12:38:03 +02:00
|
|
|
|
|
|
|
|
constructor(protected store: Store<AppState>,
|
|
|
|
|
protected router: Router,
|
|
|
|
|
protected dialogRef: MatDialogRef<TemplateNotificationDialogComponent, NotificationTemplate>,
|
|
|
|
|
@Inject(MAT_DIALOG_DATA) public data: TemplateNotificationDialogData,
|
2023-01-12 18:11:37 +02:00
|
|
|
private breakpointObserver: BreakpointObserver,
|
2023-02-13 18:00:23 +02:00
|
|
|
protected fb: FormBuilder,
|
2023-01-30 10:23:36 +02:00
|
|
|
private notificationService: NotificationService,
|
|
|
|
|
private translate: TranslateService) {
|
2023-02-13 18:00:23 +02:00
|
|
|
super(store, router, dialogRef, fb);
|
2023-01-12 12:38:03 +02:00
|
|
|
|
2023-03-21 15:03:30 +02:00
|
|
|
this.notificationTypes = this.allowNotificationType();
|
|
|
|
|
|
2024-02-16 15:04:45 +02:00
|
|
|
this.stepperOrientation = this.breakpointObserver.observe(MediaBreakpoints['gt-sm'])
|
2023-01-12 18:11:37 +02:00
|
|
|
.pipe(map(({matches}) => matches ? 'horizontal' : 'vertical'));
|
|
|
|
|
|
2023-02-10 18:31:05 +02:00
|
|
|
if (isDefinedAndNotNull(this.data?.predefinedType)) {
|
|
|
|
|
this.hideSelectType = true;
|
2023-04-07 11:18:12 +03:00
|
|
|
this.templateNotificationForm.get('notificationType').setValue(this.data.predefinedType, {emitEvent: false});
|
2023-02-10 18:31:05 +02:00
|
|
|
}
|
2025-07-28 17:24:37 +03:00
|
|
|
if (isDefinedAndNotNull(this.data?.name)) {
|
|
|
|
|
this.templateNotificationForm.get('name').setValue(this.data.name, {emitEvent: false});
|
|
|
|
|
}
|
2023-02-10 18:31:05 +02:00
|
|
|
|
2023-01-30 10:23:36 +02:00
|
|
|
if (data.isAdd || data.isCopy) {
|
|
|
|
|
this.dialogTitle = 'notification.add-notification-template';
|
|
|
|
|
}
|
|
|
|
|
this.templateNotification = deepClone(this.data.template);
|
|
|
|
|
|
|
|
|
|
if (this.templateNotification) {
|
|
|
|
|
if (this.data.isCopy) {
|
|
|
|
|
this.templateNotification.name += ` (${this.translate.instant('action.copy')})`;
|
2023-04-07 11:18:12 +03:00
|
|
|
} else {
|
|
|
|
|
this.templateNotificationForm.get('notificationType').disable({emitEvent: false});
|
2023-01-30 10:23:36 +02:00
|
|
|
}
|
|
|
|
|
this.templateNotificationForm.reset({}, {emitEvent: false});
|
|
|
|
|
this.templateNotificationForm.patchValue(this.templateNotification, {emitEvent: false});
|
2024-02-07 17:50:47 +02:00
|
|
|
this.notificationTemplateConfigurationForm.patchValue({
|
|
|
|
|
deliveryMethodsTemplates: this.templateNotification.configuration.deliveryMethodsTemplates
|
|
|
|
|
}, {emitEvent: false});
|
2024-02-21 18:24:00 +02:00
|
|
|
this.deliveryConfiguration = this.templateNotificationForm.get('configuration.deliveryMethodsTemplates').value;
|
2023-01-30 10:23:36 +02:00
|
|
|
}
|
2023-01-12 12:38:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnDestroy() {
|
|
|
|
|
super.ngOnDestroy();
|
|
|
|
|
this.destroy$.next();
|
|
|
|
|
this.destroy$.complete();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cancel(): void {
|
|
|
|
|
this.dialogRef.close(null);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-12 18:11:37 +02:00
|
|
|
changeStep($event: StepperSelectionEvent) {
|
|
|
|
|
this.selectedIndex = $event.selectedIndex;
|
2024-02-07 17:50:47 +02:00
|
|
|
if ($event.previouslySelectedIndex > $event.selectedIndex) {
|
|
|
|
|
$event.previouslySelectedStep.interacted = false;
|
|
|
|
|
}
|
2023-01-12 18:11:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
backStep() {
|
2023-01-30 10:23:36 +02:00
|
|
|
this.notificationTemplateStepper.previous();
|
2023-01-12 18:11:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nextStep() {
|
|
|
|
|
if (this.selectedIndex >= this.maxStepperIndex) {
|
|
|
|
|
this.add();
|
|
|
|
|
} else {
|
2023-01-30 10:23:36 +02:00
|
|
|
this.notificationTemplateStepper.next();
|
2023-01-12 18:11:37 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nextStepLabel(): string {
|
2023-04-13 12:41:06 +03:00
|
|
|
if (this.selectedIndex >= this.maxStepperIndex && this.selectedIndex !== 0) {
|
2023-03-22 17:08:10 +02:00
|
|
|
return (this.data.isAdd || this.data.isCopy) ? 'action.add' : 'action.save';
|
2023-01-12 18:11:37 +02:00
|
|
|
}
|
|
|
|
|
return 'action.next';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private get maxStepperIndex(): number {
|
2023-01-30 10:23:36 +02:00
|
|
|
return this.notificationTemplateStepper?._steps?.length - 1;
|
2023-01-12 18:11:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private add(): void {
|
|
|
|
|
if (this.allValid()) {
|
2023-02-13 18:00:23 +02:00
|
|
|
let template = this.getNotificationTemplateValue();
|
2023-02-02 15:25:04 +02:00
|
|
|
if (this.templateNotification && !this.data.isCopy) {
|
2023-01-30 10:23:36 +02:00
|
|
|
template = {...this.templateNotification, ...template};
|
2023-01-12 18:11:37 +02:00
|
|
|
}
|
2023-02-20 11:30:26 +02:00
|
|
|
this.notificationService.saveNotificationTemplate(template).subscribe(
|
2023-01-12 18:11:37 +02:00
|
|
|
(target) => this.dialogRef.close(target)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-17 18:26:25 +02:00
|
|
|
private allValid(): boolean {
|
2023-01-30 10:23:36 +02:00
|
|
|
return !this.notificationTemplateStepper.steps.find((item, index) => {
|
2023-01-12 18:11:37 +02:00
|
|
|
if (item.stepControl.invalid) {
|
|
|
|
|
item.interacted = true;
|
2023-01-30 10:23:36 +02:00
|
|
|
this.notificationTemplateStepper.selectedIndex = index;
|
2023-01-12 18:11:37 +02:00
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-03-07 12:14:49 +02:00
|
|
|
|
|
|
|
|
private isSysAdmin(): boolean {
|
|
|
|
|
return this.authUser.authority === Authority.SYS_ADMIN;
|
|
|
|
|
}
|
2023-03-21 15:03:30 +02:00
|
|
|
|
|
|
|
|
private allowNotificationType(): NotificationType[] {
|
2023-04-10 11:21:11 +03:00
|
|
|
const sysAdminAllowNotificationTypes = new Set([
|
|
|
|
|
NotificationType.ENTITIES_LIMIT,
|
|
|
|
|
NotificationType.API_USAGE_LIMIT,
|
|
|
|
|
NotificationType.NEW_PLATFORM_VERSION,
|
2024-03-01 14:14:25 +02:00
|
|
|
NotificationType.RATE_LIMITS,
|
2025-02-06 17:53:46 +02:00
|
|
|
NotificationType.TASK_PROCESSING_FAILURE,
|
|
|
|
|
NotificationType.RESOURCES_SHORTAGE
|
2023-04-10 11:21:11 +03:00
|
|
|
]);
|
|
|
|
|
|
2023-03-21 15:03:30 +02:00
|
|
|
if (this.isSysAdmin()) {
|
2023-04-10 11:21:11 +03:00
|
|
|
return [NotificationType.GENERAL, ...sysAdminAllowNotificationTypes];
|
2023-03-21 15:03:30 +02:00
|
|
|
}
|
2023-04-10 11:21:11 +03:00
|
|
|
return Object.values(NotificationType).filter(type => !sysAdminAllowNotificationTypes.has(type));
|
2023-03-21 15:03:30 +02:00
|
|
|
}
|
2023-01-12 12:38:03 +02:00
|
|
|
}
|