2023-02-13 18:00:23 +02:00
|
|
|
///
|
2023-02-14 11:39:29 +02:00
|
|
|
/// Copyright © 2016-2023 The Thingsboard Authors
|
2023-02-13 18:00:23 +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.
|
|
|
|
|
///
|
|
|
|
|
|
|
|
|
|
import { FormBuilder, FormGroup, ValidationErrors, Validators } from '@angular/forms';
|
|
|
|
|
import {
|
2023-02-24 11:29:36 +02:00
|
|
|
ActionButtonLinkType,
|
|
|
|
|
ActionButtonLinkTypeTranslateMap,
|
2023-02-13 18:00:23 +02:00
|
|
|
NotificationDeliveryMethod,
|
|
|
|
|
NotificationDeliveryMethodTranslateMap,
|
|
|
|
|
NotificationTemplate,
|
2023-02-14 18:16:29 +02:00
|
|
|
NotificationTemplateTypeTranslateMap,
|
2023-02-13 18:00:23 +02:00
|
|
|
NotificationType
|
|
|
|
|
} from '@shared/models/notification.models';
|
|
|
|
|
import { takeUntil } from 'rxjs/operators';
|
|
|
|
|
import { Subject } from 'rxjs';
|
|
|
|
|
import { Directive, OnDestroy } from '@angular/core';
|
|
|
|
|
import { DialogComponent } from '@shared/components/dialog.component';
|
|
|
|
|
import { Store } from '@ngrx/store';
|
|
|
|
|
import { AppState } from '@core/core.state';
|
|
|
|
|
import { Router } from '@angular/router';
|
|
|
|
|
import { MatDialogRef } from '@angular/material/dialog';
|
2023-02-20 11:30:26 +02:00
|
|
|
import { deepClone, deepTrim } from '@core/utils';
|
2023-02-13 18:00:23 +02:00
|
|
|
|
|
|
|
|
@Directive()
|
|
|
|
|
// tslint:disable-next-line:directive-class-suffix
|
|
|
|
|
export abstract class TemplateConfiguration<T, R = any> extends DialogComponent<T, R> implements OnDestroy{
|
|
|
|
|
|
|
|
|
|
templateNotificationForm: FormGroup;
|
2023-03-15 13:28:31 +02:00
|
|
|
webTemplateForm: FormGroup;
|
2023-02-13 18:00:23 +02:00
|
|
|
emailTemplateForm: FormGroup;
|
|
|
|
|
smsTemplateForm: FormGroup;
|
|
|
|
|
slackTemplateForm: FormGroup;
|
|
|
|
|
|
|
|
|
|
notificationDeliveryMethods = Object.keys(NotificationDeliveryMethod) as NotificationDeliveryMethod[];
|
|
|
|
|
notificationDeliveryMethodTranslateMap = NotificationDeliveryMethodTranslateMap;
|
2023-02-14 18:16:29 +02:00
|
|
|
notificationTemplateTypeTranslateMap = NotificationTemplateTypeTranslateMap;
|
2023-02-13 18:00:23 +02:00
|
|
|
|
2023-02-24 11:29:36 +02:00
|
|
|
actionButtonLinkType = ActionButtonLinkType;
|
|
|
|
|
actionButtonLinkTypes = Object.keys(ActionButtonLinkType) as ActionButtonLinkType[];
|
|
|
|
|
actionButtonLinkTypeTranslateMap = ActionButtonLinkTypeTranslateMap;
|
|
|
|
|
|
2023-02-13 18:00:23 +02:00
|
|
|
tinyMceOptions: Record<string, any> = {
|
|
|
|
|
base_url: '/assets/tinymce',
|
|
|
|
|
suffix: '.min',
|
|
|
|
|
plugins: ['link table image imagetools code fullscreen'],
|
|
|
|
|
menubar: 'edit insert tools view format table',
|
|
|
|
|
toolbar: 'fontselect fontsizeselect | formatselect | bold italic strikethrough forecolor backcolor ' +
|
|
|
|
|
'| link | table | image | alignleft aligncenter alignright alignjustify ' +
|
|
|
|
|
'| numlist bullist outdent indent | removeformat | code | fullscreen',
|
|
|
|
|
height: 400,
|
|
|
|
|
autofocus: false,
|
|
|
|
|
branding: false
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
protected readonly destroy$ = new Subject<void>();
|
|
|
|
|
|
|
|
|
|
protected deliveryMethodFormsMap: Map<NotificationDeliveryMethod, FormGroup>;
|
|
|
|
|
|
|
|
|
|
protected constructor(protected store: Store<AppState>,
|
|
|
|
|
protected router: Router,
|
|
|
|
|
protected dialogRef: MatDialogRef<T, R>,
|
|
|
|
|
protected fb: FormBuilder) {
|
|
|
|
|
super(store, router, dialogRef);
|
|
|
|
|
|
|
|
|
|
this.templateNotificationForm = this.fb.group({
|
|
|
|
|
name: ['', Validators.required],
|
|
|
|
|
notificationType: [NotificationType.GENERAL],
|
|
|
|
|
configuration: this.fb.group({
|
|
|
|
|
deliveryMethodsTemplates: this.fb.group({}, {validators: this.atLeastOne()})
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.notificationDeliveryMethods.forEach(method => {
|
|
|
|
|
(this.templateNotificationForm.get('configuration.deliveryMethodsTemplates') as FormGroup)
|
2023-03-15 13:28:31 +02:00
|
|
|
.addControl(method, this.fb.group({enabled: method === NotificationDeliveryMethod.WEB}), {emitEvent: false});
|
2023-02-13 18:00:23 +02:00
|
|
|
});
|
|
|
|
|
|
2023-03-15 13:28:31 +02:00
|
|
|
this.webTemplateForm = this.fb.group({
|
2023-03-22 17:08:10 +02:00
|
|
|
subject: ['', Validators.required],
|
|
|
|
|
body: ['', Validators.required],
|
2023-02-13 18:00:23 +02:00
|
|
|
additionalConfig: this.fb.group({
|
|
|
|
|
icon: this.fb.group({
|
|
|
|
|
enabled: [false],
|
2023-03-09 17:10:09 +02:00
|
|
|
icon: [{value: 'notifications', disabled: true}, Validators.required],
|
2023-02-13 18:00:23 +02:00
|
|
|
color: ['#757575']
|
|
|
|
|
}),
|
|
|
|
|
actionButtonConfig: this.fb.group({
|
|
|
|
|
enabled: [false],
|
|
|
|
|
text: [{value: '', disabled: true}, Validators.required],
|
2023-02-24 11:29:36 +02:00
|
|
|
linkType: [ActionButtonLinkType.LINK],
|
|
|
|
|
link: [{value: '', disabled: true}, Validators.required],
|
|
|
|
|
dashboardId: [{value: null, disabled: true}, Validators.required],
|
2023-03-10 18:23:25 +02:00
|
|
|
dashboardState: [{value: null, disabled: true}],
|
|
|
|
|
setEntityIdInState: [{value: true, disabled: true}],
|
2023-02-13 18:00:23 +02:00
|
|
|
}),
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
|
2023-03-15 13:28:31 +02:00
|
|
|
this.webTemplateForm.get('additionalConfig.icon.enabled').valueChanges.pipe(
|
2023-02-13 18:00:23 +02:00
|
|
|
takeUntil(this.destroy$)
|
|
|
|
|
).subscribe((value) => {
|
|
|
|
|
if (value) {
|
2023-03-15 13:28:31 +02:00
|
|
|
this.webTemplateForm.get('additionalConfig.icon.icon').enable({emitEvent: false});
|
2023-02-13 18:00:23 +02:00
|
|
|
} else {
|
2023-03-15 13:28:31 +02:00
|
|
|
this.webTemplateForm.get('additionalConfig.icon.icon').disable({emitEvent: false});
|
2023-02-13 18:00:23 +02:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2023-03-15 13:28:31 +02:00
|
|
|
this.webTemplateForm.get('additionalConfig.actionButtonConfig.enabled').valueChanges.pipe(
|
2023-02-13 18:00:23 +02:00
|
|
|
takeUntil(this.destroy$)
|
|
|
|
|
).subscribe((value) => {
|
|
|
|
|
if (value) {
|
2023-03-15 13:28:31 +02:00
|
|
|
this.webTemplateForm.get('additionalConfig.actionButtonConfig').enable({emitEvent: false});
|
|
|
|
|
this.webTemplateForm.get('additionalConfig.actionButtonConfig.linkType').updateValueAndValidity({onlySelf: true});
|
2023-02-24 11:29:36 +02:00
|
|
|
} else {
|
2023-03-15 13:28:31 +02:00
|
|
|
this.webTemplateForm.get('additionalConfig.actionButtonConfig').disable({emitEvent: false});
|
|
|
|
|
this.webTemplateForm.get('additionalConfig.actionButtonConfig.enabled').enable({emitEvent: false});
|
2023-02-24 11:29:36 +02:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2023-03-15 13:28:31 +02:00
|
|
|
this.webTemplateForm.get('additionalConfig.actionButtonConfig.linkType').valueChanges.pipe(
|
2023-02-24 11:29:36 +02:00
|
|
|
takeUntil(this.destroy$)
|
|
|
|
|
).subscribe((value) => {
|
2023-03-15 13:28:31 +02:00
|
|
|
const isEnabled = this.webTemplateForm.get('additionalConfig.actionButtonConfig.enabled').value;
|
2023-03-07 12:14:49 +02:00
|
|
|
if (isEnabled) {
|
|
|
|
|
if (value === ActionButtonLinkType.LINK) {
|
2023-03-15 13:28:31 +02:00
|
|
|
this.webTemplateForm.get('additionalConfig.actionButtonConfig.link').enable({emitEvent: false});
|
|
|
|
|
this.webTemplateForm.get('additionalConfig.actionButtonConfig.dashboardId').disable({emitEvent: false});
|
|
|
|
|
this.webTemplateForm.get('additionalConfig.actionButtonConfig.dashboardState').disable({emitEvent: false});
|
|
|
|
|
this.webTemplateForm.get('additionalConfig.actionButtonConfig.setEntityIdInState').disable({emitEvent: false});
|
2023-03-07 12:14:49 +02:00
|
|
|
} else {
|
2023-03-15 13:28:31 +02:00
|
|
|
this.webTemplateForm.get('additionalConfig.actionButtonConfig.link').disable({emitEvent: false});
|
|
|
|
|
this.webTemplateForm.get('additionalConfig.actionButtonConfig.dashboardId').enable({emitEvent: false});
|
|
|
|
|
this.webTemplateForm.get('additionalConfig.actionButtonConfig.dashboardState').enable({emitEvent: false});
|
|
|
|
|
this.webTemplateForm.get('additionalConfig.actionButtonConfig.setEntityIdInState').enable({emitEvent: false});
|
2023-03-07 12:14:49 +02:00
|
|
|
}
|
2023-02-13 18:00:23 +02:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.emailTemplateForm = this.fb.group({
|
2023-03-22 17:08:10 +02:00
|
|
|
subject: ['', Validators.required],
|
|
|
|
|
body: ['', Validators.required]
|
2023-02-13 18:00:23 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.smsTemplateForm = this.fb.group({
|
2023-04-12 17:48:43 +03:00
|
|
|
body: ['', [Validators.required, Validators.maxLength(320)]]
|
2023-02-13 18:00:23 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.slackTemplateForm = this.fb.group({
|
2023-03-22 17:08:10 +02:00
|
|
|
body: ['', Validators.required]
|
2023-02-13 18:00:23 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.deliveryMethodFormsMap = new Map<NotificationDeliveryMethod, FormGroup>([
|
2023-03-15 13:28:31 +02:00
|
|
|
[NotificationDeliveryMethod.WEB, this.webTemplateForm],
|
2023-02-13 18:00:23 +02:00
|
|
|
[NotificationDeliveryMethod.EMAIL, this.emailTemplateForm],
|
|
|
|
|
[NotificationDeliveryMethod.SMS, this.smsTemplateForm],
|
|
|
|
|
[NotificationDeliveryMethod.SLACK, this.slackTemplateForm]
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnDestroy() {
|
|
|
|
|
this.destroy$.next();
|
|
|
|
|
this.destroy$.complete();
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-24 11:29:36 +02:00
|
|
|
atLeastOne() {
|
2023-02-13 18:00:23 +02:00
|
|
|
return (group: FormGroup): ValidationErrors | null => {
|
|
|
|
|
let hasAtLeastOne = true;
|
|
|
|
|
if (group?.controls) {
|
|
|
|
|
const controlsFormValue: FormGroup[] = Object.entries(group.controls).map(method => method[1]) as any;
|
|
|
|
|
hasAtLeastOne = controlsFormValue.some(value => value.controls.enabled.value);
|
|
|
|
|
}
|
|
|
|
|
return hasAtLeastOne ? null : {atLeastOne: true};
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected getNotificationTemplateValue(): NotificationTemplate {
|
2023-02-20 11:30:26 +02:00
|
|
|
const template: NotificationTemplate = deepClone(this.templateNotificationForm.value);
|
2023-02-13 18:00:23 +02:00
|
|
|
this.notificationDeliveryMethods.forEach(method => {
|
2023-05-02 17:56:29 +03:00
|
|
|
if (template.configuration.deliveryMethodsTemplates[method]?.enabled) {
|
2023-02-13 18:00:23 +02:00
|
|
|
Object.assign(template.configuration.deliveryMethodsTemplates[method], this.deliveryMethodFormsMap.get(method).value, {method});
|
|
|
|
|
} else {
|
|
|
|
|
delete template.configuration.deliveryMethodsTemplates[method];
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-02-20 11:30:26 +02:00
|
|
|
return deepTrim(template);
|
2023-02-13 18:00:23 +02:00
|
|
|
}
|
|
|
|
|
}
|