2023-01-17 18:26:25 +02:00
|
|
|
///
|
2023-02-14 11:39:29 +02:00
|
|
|
/// Copyright © 2016-2023 The Thingsboard Authors
|
2023-01-17 18:26:25 +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.
|
|
|
|
|
///
|
|
|
|
|
|
2023-02-10 18:31:05 +02:00
|
|
|
import {
|
2023-02-13 18:00:23 +02:00
|
|
|
NotificationDeliveryMethod,
|
|
|
|
|
NotificationDeliveryMethodTranslateMap,
|
2023-02-10 18:31:05 +02:00
|
|
|
NotificationRequest,
|
|
|
|
|
NotificationRequestPreview,
|
|
|
|
|
NotificationTarget,
|
|
|
|
|
NotificationType
|
|
|
|
|
} from '@shared/models/notification.models';
|
2023-01-17 18:26:25 +02:00
|
|
|
import { Component, Inject, OnDestroy, ViewChild } from '@angular/core';
|
|
|
|
|
import { Store } from '@ngrx/store';
|
|
|
|
|
import { AppState } from '@core/core.state';
|
|
|
|
|
import { Router } from '@angular/router';
|
2023-02-10 18:31:05 +02:00
|
|
|
import { MAT_DIALOG_DATA, MatDialog, MatDialogRef } from '@angular/material/dialog';
|
2023-01-17 18:26:25 +02:00
|
|
|
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
|
|
|
|
|
import { NotificationService } from '@core/http/notification.service';
|
2023-01-30 10:23:36 +02:00
|
|
|
import { deepTrim } from '@core/utils';
|
2023-02-13 18:00:23 +02:00
|
|
|
import { Observable } from 'rxjs';
|
2023-01-17 18:26:25 +02:00
|
|
|
import { EntityType } from '@shared/models/entity-type.models';
|
|
|
|
|
import { BreakpointObserver } from '@angular/cdk/layout';
|
|
|
|
|
import { MatStepper } from '@angular/material/stepper';
|
|
|
|
|
import { StepperOrientation, StepperSelectionEvent } from '@angular/cdk/stepper';
|
|
|
|
|
import { MediaBreakpoints } from '@shared/models/constants';
|
2023-01-31 18:18:55 +02:00
|
|
|
import { map, takeUntil } from 'rxjs/operators';
|
|
|
|
|
import { getCurrentTime } from '@shared/models/time/time.models';
|
2023-02-10 18:31:05 +02:00
|
|
|
import {
|
|
|
|
|
TargetNotificationDialogComponent,
|
|
|
|
|
TargetsNotificationDialogData
|
|
|
|
|
} from '@home/pages/notification-center/targets-table/target-notification-dialog.componet';
|
|
|
|
|
import { MatButton } from '@angular/material/button';
|
2023-02-13 18:00:23 +02:00
|
|
|
import { TemplateConfiguration } from '@home/pages/notification-center/template-table/template-configuration';
|
2023-01-17 18:26:25 +02:00
|
|
|
|
|
|
|
|
export interface RequestNotificationDialogData {
|
|
|
|
|
request?: NotificationRequest;
|
|
|
|
|
isAdd?: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
selector: 'tb-request-notification-dialog',
|
|
|
|
|
templateUrl: './request-notification-dialog.component.html',
|
|
|
|
|
styleUrls: ['./request-notification-dialog.component.scss']
|
|
|
|
|
})
|
|
|
|
|
export class RequestNotificationDialogComponent extends
|
2023-02-13 18:00:23 +02:00
|
|
|
TemplateConfiguration<RequestNotificationDialogComponent, NotificationRequest> implements OnDestroy {
|
2023-01-17 18:26:25 +02:00
|
|
|
|
|
|
|
|
@ViewChild('createNotification', {static: true}) createNotification: MatStepper;
|
|
|
|
|
stepperOrientation: Observable<StepperOrientation>;
|
|
|
|
|
isAdd = true;
|
|
|
|
|
entityType = EntityType;
|
2023-01-23 12:47:00 +02:00
|
|
|
notificationType = NotificationType;
|
2023-02-13 18:00:23 +02:00
|
|
|
|
2023-01-17 18:26:25 +02:00
|
|
|
notificationRequestForm: FormGroup;
|
2023-02-13 18:00:23 +02:00
|
|
|
|
|
|
|
|
notificationDeliveryMethods = Object.keys(NotificationDeliveryMethod) as NotificationDeliveryMethod[];
|
|
|
|
|
notificationDeliveryMethodTranslateMap = NotificationDeliveryMethodTranslateMap;
|
2023-01-17 18:26:25 +02:00
|
|
|
|
|
|
|
|
selectedIndex = 0;
|
|
|
|
|
preview: NotificationRequestPreview = null;
|
|
|
|
|
|
2023-01-30 10:23:36 +02:00
|
|
|
dialogTitle = 'notification.notify-again';
|
|
|
|
|
|
2023-01-17 18:26:25 +02:00
|
|
|
constructor(protected store: Store<AppState>,
|
|
|
|
|
protected router: Router,
|
|
|
|
|
protected dialogRef: MatDialogRef<RequestNotificationDialogComponent, NotificationRequest>,
|
|
|
|
|
@Inject(MAT_DIALOG_DATA) public data: RequestNotificationDialogData,
|
|
|
|
|
private breakpointObserver: BreakpointObserver,
|
2023-02-13 18:00:23 +02:00
|
|
|
protected fb: FormBuilder,
|
2023-02-10 18:31:05 +02:00
|
|
|
private notificationService: NotificationService,
|
|
|
|
|
private dialog: MatDialog) {
|
2023-02-13 18:00:23 +02:00
|
|
|
super(store, router, dialogRef, fb);
|
2023-01-17 18:26:25 +02:00
|
|
|
|
|
|
|
|
this.stepperOrientation = this.breakpointObserver.observe(MediaBreakpoints['gt-xs'])
|
|
|
|
|
.pipe(map(({matches}) => matches ? 'horizontal' : 'vertical'));
|
|
|
|
|
|
|
|
|
|
this.notificationRequestForm = this.fb.group({
|
2023-02-13 18:00:23 +02:00
|
|
|
useTemplate: [true],
|
2023-01-17 18:26:25 +02:00
|
|
|
templateId: [null, Validators.required],
|
2023-01-31 18:18:55 +02:00
|
|
|
targets: [null, Validators.required],
|
2023-02-15 17:49:28 +02:00
|
|
|
template: this.templateNotificationForm,
|
|
|
|
|
additionalConfig: this.fb.group({
|
|
|
|
|
enabled: [false],
|
|
|
|
|
timezone: [{value: '', disabled: true}, Validators.required],
|
|
|
|
|
time: [{value: 0, disabled: true}, Validators.required]
|
|
|
|
|
})
|
2023-02-13 18:00:23 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.notificationRequestForm.get('template').disable({emitEvent: false});
|
|
|
|
|
this.notificationRequestForm.get('template.name').removeValidators(Validators.required);
|
|
|
|
|
this.notificationRequestForm.get('template.name').updateValueAndValidity({emitEvent: false});
|
|
|
|
|
|
|
|
|
|
this.notificationRequestForm.get('useTemplate').valueChanges.pipe(
|
|
|
|
|
takeUntil(this.destroy$)
|
|
|
|
|
).subscribe(value => {
|
|
|
|
|
if (value) {
|
|
|
|
|
this.notificationRequestForm.get('templateId').enable({emitEvent: false});
|
|
|
|
|
this.notificationRequestForm.get('template').disable({emitEvent: false});
|
|
|
|
|
} else {
|
|
|
|
|
this.notificationRequestForm.get('templateId').disable({emitEvent: false});
|
|
|
|
|
this.notificationRequestForm.get('template').enable({emitEvent: false});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2023-02-15 17:49:28 +02:00
|
|
|
this.notificationRequestForm.get('additionalConfig.enabled').valueChanges.pipe(
|
2023-01-31 18:18:55 +02:00
|
|
|
takeUntil(this.destroy$)
|
|
|
|
|
).subscribe(value => {
|
|
|
|
|
if (value) {
|
2023-02-15 17:49:28 +02:00
|
|
|
this.notificationRequestForm.get('additionalConfig.timezone').enable({emitEvent: false});
|
|
|
|
|
this.notificationRequestForm.get('additionalConfig.time').enable({emitEvent: false});
|
2023-01-31 18:18:55 +02:00
|
|
|
} else {
|
2023-02-15 17:49:28 +02:00
|
|
|
this.notificationRequestForm.get('additionalConfig.timezone').disable({emitEvent: false});
|
|
|
|
|
this.notificationRequestForm.get('additionalConfig.time').disable({emitEvent: false});
|
2023-01-31 18:18:55 +02:00
|
|
|
}
|
2023-01-17 18:26:25 +02:00
|
|
|
});
|
2023-01-30 10:23:36 +02:00
|
|
|
|
|
|
|
|
if (data.isAdd) {
|
|
|
|
|
this.dialogTitle = 'notification.new-notification';
|
|
|
|
|
}
|
|
|
|
|
if (data.request) {
|
|
|
|
|
this.notificationRequestForm.patchValue(this.data.request, {emitEvent: false});
|
|
|
|
|
}
|
2023-01-17 18:26:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnDestroy() {
|
|
|
|
|
super.ngOnDestroy();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cancel(): void {
|
|
|
|
|
this.dialogRef.close(null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nextStepLabel(): string {
|
2023-02-14 18:16:29 +02:00
|
|
|
if (this.selectedIndex !== 0) {
|
|
|
|
|
if (this.selectedIndex >= this.maxStepperIndex) {
|
|
|
|
|
return 'action.send';
|
|
|
|
|
} else if (this.createNotification.selected.stepControl.pristine) {
|
|
|
|
|
return 'action.skip';
|
|
|
|
|
}
|
2023-01-17 18:26:25 +02:00
|
|
|
}
|
|
|
|
|
return 'action.next';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
changeStep($event: StepperSelectionEvent) {
|
|
|
|
|
this.selectedIndex = $event.selectedIndex;
|
|
|
|
|
if (this.selectedIndex === this.maxStepperIndex) {
|
|
|
|
|
this.getPreview();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
backStep() {
|
|
|
|
|
this.createNotification.previous();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nextStep() {
|
|
|
|
|
if (this.selectedIndex >= this.maxStepperIndex) {
|
|
|
|
|
this.add();
|
|
|
|
|
} else {
|
|
|
|
|
this.createNotification.next();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private get maxStepperIndex(): number {
|
|
|
|
|
return this.createNotification?._steps?.length - 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private add(): void {
|
|
|
|
|
if (this.allValid()) {
|
|
|
|
|
this.notificationService.createNotificationRequest(this.notificationFormValue).subscribe(
|
|
|
|
|
(notification) => this.dialogRef.close(notification)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private getPreview() {
|
|
|
|
|
if (this.allValid()) {
|
|
|
|
|
this.preview = null;
|
2023-01-20 18:08:30 +02:00
|
|
|
this.notificationService.getNotificationRequestPreview(this.notificationFormValue).pipe(
|
|
|
|
|
map(data => {
|
|
|
|
|
if (data.processedTemplates.PUSH?.enabled) {
|
|
|
|
|
(data.processedTemplates.PUSH as any).text = data.processedTemplates.PUSH.body;
|
|
|
|
|
}
|
|
|
|
|
return data;
|
|
|
|
|
})
|
|
|
|
|
).subscribe(
|
2023-01-17 18:26:25 +02:00
|
|
|
(preview) => this.preview = preview
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private get notificationFormValue(): NotificationRequest {
|
2023-01-31 18:18:55 +02:00
|
|
|
const formValue = deepTrim(this.notificationRequestForm.value);
|
2023-02-13 18:00:23 +02:00
|
|
|
delete formValue.useTemplate;
|
|
|
|
|
delete formValue.template;
|
2023-01-31 18:18:55 +02:00
|
|
|
let delay = 0;
|
2023-02-15 17:49:28 +02:00
|
|
|
if (formValue.additionalConfig.enabled) {
|
|
|
|
|
delay = (this.notificationRequestForm.value.additionalConfig.time.valueOf() - this.minDate().valueOf()) / 1000;
|
2023-01-31 18:18:55 +02:00
|
|
|
}
|
|
|
|
|
formValue.additionalConfig = {
|
|
|
|
|
sendingDelayInSec: delay > 0 ? delay : 0
|
|
|
|
|
};
|
2023-01-17 18:26:25 +02:00
|
|
|
return formValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private allValid(): boolean {
|
|
|
|
|
return !this.createNotification.steps.find((item, index) => {
|
|
|
|
|
if (item.stepControl?.invalid) {
|
|
|
|
|
item.interacted = true;
|
|
|
|
|
this.createNotification.selectedIndex = index;
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-01-31 18:18:55 +02:00
|
|
|
|
|
|
|
|
minDate(): Date {
|
2023-02-15 17:49:28 +02:00
|
|
|
return new Date(getCurrentTime(this.notificationRequestForm.get('additionalConfig.timezone').value).format('lll'));
|
2023-01-31 18:18:55 +02:00
|
|
|
}
|
2023-02-01 16:25:07 +02:00
|
|
|
|
|
|
|
|
maxDate(): Date {
|
|
|
|
|
const date = this.minDate();
|
|
|
|
|
date.setDate(date.getDate() + 7);
|
|
|
|
|
return date;
|
|
|
|
|
}
|
2023-02-10 18:31:05 +02:00
|
|
|
|
|
|
|
|
createTarget($event: Event, button: MatButton) {
|
|
|
|
|
if ($event) {
|
|
|
|
|
$event.stopPropagation();
|
|
|
|
|
}
|
|
|
|
|
button._elementRef.nativeElement.blur();
|
|
|
|
|
this.dialog.open<TargetNotificationDialogComponent, TargetsNotificationDialogData,
|
|
|
|
|
NotificationTarget>(TargetNotificationDialogComponent, {
|
|
|
|
|
disableClose: true,
|
|
|
|
|
panelClass: ['tb-dialog', 'tb-fullscreen-dialog'],
|
|
|
|
|
data: {}
|
|
|
|
|
}).afterClosed()
|
|
|
|
|
.subscribe((res) => {
|
|
|
|
|
if (res) {
|
|
|
|
|
let formValue: string[] = this.notificationRequestForm.get('targets').value;
|
|
|
|
|
if (!formValue) {
|
|
|
|
|
formValue = [];
|
|
|
|
|
}
|
|
|
|
|
formValue.push(res.id.id);
|
|
|
|
|
this.notificationRequestForm.get('targets').patchValue(formValue);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-01-17 18:26:25 +02:00
|
|
|
}
|