2023-12-19 18:08:17 +01:00
|
|
|
import { Component, OnInit } from '@angular/core';
|
|
|
|
import { FormArray, UntypedFormGroup } from '@angular/forms';
|
|
|
|
import { MatDialog } from '@angular/material/dialog';
|
|
|
|
import { AuthService } from '@app/core/services/auth/auth.service';
|
|
|
|
import { FormService } from '@common/forms/form-service';
|
|
|
|
import { ConfirmationDialogComponent } from '@common/modules/confirmation-dialog/confirmation-dialog.component';
|
|
|
|
import { HttpErrorHandlingService } from '@common/modules/errors/error-handling/http-error-handling.service';
|
|
|
|
import { SnackBarNotificationLevel, UiNotificationService } from '@app/core/services/notification/ui-notification-service';
|
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
|
|
|
import { NotificationTemplateKind } from '@app/core/common/enum/notification-template-kind';
|
|
|
|
import { AppPermission } from '@app/core/common/enum/permission.enum';
|
|
|
|
import { NotificationTemplate, NotificationTemplatePersist } from '@app/core/model/notification-template/notification-template';
|
|
|
|
import { NotificationTemplateService } from '@app/core/services/notification-template/notification-template.service';
|
|
|
|
import { NotificationFieldInfoEditorModel, NotificationFieldOptionsEditorModel, NotificationTemplateEditorModel } from './notification-template-editor.model';
|
|
|
|
import { map, takeUntil } from 'rxjs/operators';
|
|
|
|
import { BaseEditor } from '@common/base/base-editor';
|
|
|
|
import { ActivatedRoute, Router } from '@angular/router';
|
|
|
|
import { FilterService } from '@common/modules/text-filter/filter-service';
|
|
|
|
import { DatePipe } from '@angular/common';
|
|
|
|
import { QueryParamsService } from '@app/core/services/utilities/query-params.service';
|
|
|
|
import { EnumUtils } from '@app/core/services/utilities/enum-utils.service';
|
|
|
|
import { MatomoService } from '@app/core/services/matomo/matomo-service';
|
|
|
|
import { NotificationTemplateEditorService } from './notification-template-editor.service';
|
|
|
|
import { Guid } from '@common/types/guid';
|
|
|
|
import { NotificationTemplateEditorResolver } from './notification-template-editor.resolver';
|
|
|
|
import { IsActive } from '@app/core/common/enum/is-active.enum';
|
|
|
|
import { LoggingService } from '@app/core/services/logging/logging-service';
|
|
|
|
import { LanguageHttpService } from '@app/core/services/language/language.http.service';
|
|
|
|
import { Language } from '@app/core/model/language/language';
|
|
|
|
import { nameof } from 'ts-simple-nameof';
|
|
|
|
import { NotificationTemplateChannel } from '@app/core/common/enum/notification-template-channel';
|
|
|
|
import { MatChipEditedEvent, MatChipInputEvent } from '@angular/material/chips';
|
|
|
|
import { MatCheckboxChange } from '@angular/material/checkbox';
|
|
|
|
import { NotificationDataType } from '@app/core/common/enum/notification-data-type';
|
|
|
|
import { EmailOverrideMode } from '@app/core/common/enum/email-override-mode';
|
2024-01-04 14:17:56 +01:00
|
|
|
import { NotificationType } from '@app/core/common/enum/notification-type';
|
2024-03-21 08:46:18 +01:00
|
|
|
import { ConfigurationService } from '@app/core/services/configuration/configuration.service';
|
|
|
|
import { LockService } from '@app/core/services/lock/lock.service';
|
2023-12-19 18:08:17 +01:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-notification-template-editor',
|
|
|
|
templateUrl: './notification-template-editor.component.html',
|
|
|
|
styleUrls: ['./notification-template-editor.component.scss'],
|
|
|
|
providers: [NotificationTemplateEditorService]
|
|
|
|
})
|
|
|
|
export class NotificationTemplateEditorComponent extends BaseEditor<NotificationTemplateEditorModel, NotificationTemplate> implements OnInit {
|
|
|
|
|
|
|
|
isNew = true;
|
|
|
|
isDeleted = false;
|
|
|
|
formGroup: UntypedFormGroup = null;
|
2024-01-22 10:35:40 +01:00
|
|
|
languages: Language[] = [];
|
2023-12-19 18:08:17 +01:00
|
|
|
subjectMandatoryFields: string[] = [];
|
|
|
|
bodyMandatoryFields: string[] = [];
|
|
|
|
subjectFieldOptionsEnabled: Boolean = false;
|
|
|
|
bodyFieldOptionsEnabled: Boolean = false;
|
|
|
|
ccValues: string[] = [];
|
|
|
|
bccValues: string[] = [];
|
|
|
|
extraDataKeys: string[] = [];
|
2024-01-03 15:46:57 +01:00
|
|
|
subjectFormatting: { [key: string]: string } = {};
|
|
|
|
bodyFormatting: { [key: string]: string } = {};
|
2024-01-04 14:17:56 +01:00
|
|
|
notificationType: string;
|
|
|
|
public notificationTypeEnum = this.enumUtils.getEnumValues(NotificationType);
|
2023-12-19 18:08:17 +01:00
|
|
|
public notificationTemplateKindEnum = this.enumUtils.getEnumValues(NotificationTemplateKind);
|
|
|
|
public notificationTemplateChannelEnum = this.enumUtils.getEnumValues(NotificationTemplateChannel);
|
|
|
|
public notificationDataTypeEnum = this.enumUtils.getEnumValues(NotificationDataType);
|
|
|
|
public emailOverrideModeEnum = this.enumUtils.getEnumValues(EmailOverrideMode);
|
|
|
|
|
|
|
|
protected get canDelete(): boolean {
|
|
|
|
return !this.isDeleted && !this.isNew && this.hasPermission(this.authService.permissionEnum.DeleteNotificationTemplate);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected get canSave(): boolean {
|
|
|
|
return !this.isDeleted && this.hasPermission(this.authService.permissionEnum.EditNotificationTemplate);
|
|
|
|
}
|
|
|
|
|
|
|
|
private hasPermission(permission: AppPermission): boolean {
|
|
|
|
return this.authService.hasPermission(permission) || this.editorModel?.permissions?.includes(permission);
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
// BaseFormEditor injected dependencies
|
|
|
|
protected dialog: MatDialog,
|
|
|
|
protected language: TranslateService,
|
|
|
|
protected formService: FormService,
|
|
|
|
protected router: Router,
|
|
|
|
protected uiNotificationService: UiNotificationService,
|
|
|
|
protected httpErrorHandlingService: HttpErrorHandlingService,
|
|
|
|
protected filterService: FilterService,
|
|
|
|
protected datePipe: DatePipe,
|
|
|
|
protected route: ActivatedRoute,
|
|
|
|
protected queryParamsService: QueryParamsService,
|
2024-03-21 08:46:18 +01:00
|
|
|
protected lockService: LockService,
|
|
|
|
protected authService: AuthService,
|
|
|
|
protected configurationService: ConfigurationService,
|
2023-12-19 18:08:17 +01:00
|
|
|
// Rest dependencies. Inject any other needed deps here:
|
|
|
|
public enumUtils: EnumUtils,
|
|
|
|
private languageHttpService: LanguageHttpService,
|
|
|
|
private notificationTemplateService: NotificationTemplateService,
|
|
|
|
private notificationTemplateEditorService: NotificationTemplateEditorService,
|
|
|
|
private logger: LoggingService,
|
|
|
|
private matomoService: MatomoService
|
|
|
|
) {
|
2024-03-21 08:46:18 +01:00
|
|
|
super(dialog, language, formService, router, uiNotificationService, httpErrorHandlingService, filterService, datePipe, route, queryParamsService, lockService, authService, configurationService);
|
2023-12-19 18:08:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit(): void {
|
|
|
|
this.matomoService.trackPageView('Admin: Notification Tempplates');
|
|
|
|
super.ngOnInit();
|
2024-01-22 10:35:40 +01:00
|
|
|
this.languageHttpService.query(this.languageHttpService.buildAutocompleteLookup())
|
2023-12-19 18:08:17 +01:00
|
|
|
.pipe(takeUntil(this._destroyed))
|
|
|
|
.subscribe(
|
2024-01-22 10:35:40 +01:00
|
|
|
data => this.languages = data.items,
|
2023-12-19 18:08:17 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
getItem(itemId: Guid, successFunction: (item: NotificationTemplate) => void) {
|
|
|
|
this.notificationTemplateService.getSingle(itemId, NotificationTemplateEditorResolver.lookupFields())
|
|
|
|
.pipe(map(data => data as NotificationTemplate), takeUntil(this._destroyed))
|
|
|
|
.subscribe(
|
|
|
|
data => successFunction(data),
|
|
|
|
error => this.onCallbackError(error)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
prepareForm(data: NotificationTemplate) {
|
|
|
|
try {
|
|
|
|
this.editorModel = data ? new NotificationTemplateEditorModel().fromModel(data) : new NotificationTemplateEditorModel();
|
|
|
|
|
|
|
|
if(data){
|
|
|
|
if(data.value && data.value.subjectFieldOptions){
|
|
|
|
this.subjectFieldOptionsEnabled = true;
|
2023-12-20 17:02:55 +01:00
|
|
|
this.subjectMandatoryFields = data.value.subjectFieldOptions.mandatory;
|
2023-12-19 18:08:17 +01:00
|
|
|
}
|
2023-12-20 17:02:55 +01:00
|
|
|
if(data.value && data.value.bodyFieldOptions){
|
|
|
|
this.bodyFieldOptionsEnabled = true;
|
|
|
|
this.bodyMandatoryFields = data.value.bodyFieldOptions.mandatory;
|
|
|
|
}
|
2024-01-03 15:46:57 +01:00
|
|
|
if(data.value && data.value.subjectFieldOptions && data.value.subjectFieldOptions.formatting){
|
|
|
|
this.subjectFormatting = data.value.subjectFieldOptions.formatting;
|
|
|
|
}
|
|
|
|
if(data.value && data.value.bodyFieldOptions && data.value.bodyFieldOptions.formatting){
|
|
|
|
this.bodyFormatting = data.value.bodyFieldOptions.formatting;
|
|
|
|
}
|
2023-12-20 17:02:55 +01:00
|
|
|
this.ccValues = this.editorModel.value.cc;
|
|
|
|
this.bccValues = this.editorModel.value.bcc;
|
|
|
|
this.extraDataKeys = this.editorModel.value.extraDataKeys;
|
2023-12-19 18:08:17 +01:00
|
|
|
}
|
|
|
|
this.isDeleted = data ? data.isActive === IsActive.Inactive : false;
|
|
|
|
this.buildForm();
|
|
|
|
} catch (error) {
|
2023-12-20 17:02:55 +01:00
|
|
|
this.logger.error('Could not parse Notification Template item: ' + data + error);
|
2023-12-19 18:08:17 +01:00
|
|
|
this.uiNotificationService.snackBarNotification(this.language.instant('COMMONS.ERRORS.DEFAULT'), SnackBarNotificationLevel.Error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
buildForm() {
|
|
|
|
this.formGroup = this.editorModel.buildForm(null, this.isDeleted || !this.authService.hasPermission(AppPermission.EditNotificationTemplate));
|
|
|
|
this.notificationTemplateEditorService.setValidationErrorModel(this.editorModel.validationErrorModel);
|
2024-01-22 10:35:40 +01:00
|
|
|
NotificationTemplateEditorModel.reApplyValueValidators({
|
|
|
|
formGroup: this.formGroup,
|
|
|
|
validationErrorModel: this.editorModel.validationErrorModel
|
|
|
|
}
|
|
|
|
)
|
2023-12-19 18:08:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
refreshData(): void {
|
|
|
|
this.getItem(this.editorModel.id, (data: NotificationTemplate) => this.prepareForm(data));
|
|
|
|
}
|
|
|
|
|
|
|
|
refreshOnNavigateToData(id?: Guid): void {
|
|
|
|
this.formGroup.markAsPristine();
|
|
|
|
let route = [];
|
|
|
|
|
|
|
|
if (id === null) {
|
|
|
|
route.push('../..');
|
|
|
|
} else if (this.isNew) {
|
|
|
|
route.push('../' + id);
|
|
|
|
} else {
|
|
|
|
route.push('..');
|
|
|
|
}
|
|
|
|
|
|
|
|
this.router.navigate(route, { queryParams: { 'lookup': this.queryParamsService.serializeLookup(this.lookupParams), 'lv': ++this.lv }, replaceUrl: true, relativeTo: this.route });
|
|
|
|
}
|
|
|
|
|
|
|
|
persistEntity(onSuccess?: (response) => void): void {
|
|
|
|
const formData = this.formService.getValue(this.formGroup.value) as NotificationTemplatePersist;
|
|
|
|
|
|
|
|
this.notificationTemplateService.persist(formData)
|
|
|
|
.pipe(takeUntil(this._destroyed)).subscribe(
|
|
|
|
complete => onSuccess ? onSuccess(complete) : this.onCallbackSuccess(complete),
|
|
|
|
error => this.onCallbackError(error)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
formSubmit(): void {
|
2024-03-12 13:07:47 +01:00
|
|
|
this.formService.removeAllBackEndErrors(this.formGroup);
|
2023-12-19 18:08:17 +01:00
|
|
|
this.formService.touchAllFormFields(this.formGroup);
|
2024-03-12 13:07:47 +01:00
|
|
|
if (!this.isFormValid()) {
|
|
|
|
return;
|
|
|
|
}
|
2023-12-19 18:08:17 +01:00
|
|
|
|
|
|
|
this.persistEntity();
|
|
|
|
}
|
|
|
|
|
|
|
|
public delete() {
|
|
|
|
const value = this.formGroup.value;
|
|
|
|
if (value.id) {
|
|
|
|
const dialogRef = this.dialog.open(ConfirmationDialogComponent, {
|
|
|
|
maxWidth: '300px',
|
|
|
|
data: {
|
|
|
|
message: this.language.instant('GENERAL.CONFIRMATION-DIALOG.DELETE-ITEM'),
|
|
|
|
confirmButton: this.language.instant('GENERAL.CONFIRMATION-DIALOG.ACTIONS.CONFIRM'),
|
|
|
|
cancelButton: this.language.instant('GENERAL.CONFIRMATION-DIALOG.ACTIONS.CANCEL')
|
|
|
|
}
|
|
|
|
});
|
|
|
|
dialogRef.afterClosed().pipe(takeUntil(this._destroyed)).subscribe(result => {
|
|
|
|
if (result) {
|
|
|
|
this.notificationTemplateService.delete(value.id).pipe(takeUntil(this._destroyed))
|
|
|
|
.subscribe(
|
2024-04-05 17:03:26 +02:00
|
|
|
complete => this.onCallbackDeleteSuccess(),
|
2023-12-19 18:08:17 +01:00
|
|
|
error => this.onCallbackError(error)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
clearErrorModel() {
|
|
|
|
this.editorModel.validationErrorModel.clear();
|
|
|
|
this.formService.validateAllFormFields(this.formGroup);
|
|
|
|
}
|
|
|
|
|
2024-01-04 14:17:56 +01:00
|
|
|
|
2023-12-19 18:08:17 +01:00
|
|
|
subjectFieldOptionsSelectionChanged(matCheckBox: MatCheckboxChange){
|
|
|
|
if(matCheckBox.checked == true){
|
|
|
|
this.subjectFieldOptionsEnabled = true;
|
|
|
|
}else{
|
|
|
|
this.subjectFieldOptionsEnabled = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bodyFieldOptionsSelectionChanged(matCheckBox: MatCheckboxChange){
|
|
|
|
if(matCheckBox.checked == true){
|
|
|
|
this.bodyFieldOptionsEnabled = true;
|
|
|
|
}else{
|
|
|
|
this.bodyFieldOptionsEnabled = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// chip lists
|
|
|
|
|
|
|
|
addChipListValues(type: string, event: MatChipInputEvent){
|
|
|
|
if(type == "cc"){
|
|
|
|
this.ccValues = this.add(event, this.ccValues);
|
|
|
|
}else if(type == "bcc"){
|
|
|
|
this.bccValues = this.add(event, this.bccValues);
|
|
|
|
}else if(type == "extraDataKeys"){
|
|
|
|
this.extraDataKeys = this.add(event, this.extraDataKeys);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
editChipListValues(type: string, event: MatChipEditedEvent, field: string){
|
|
|
|
if(type == "cc"){
|
|
|
|
this.ccValues = this.edit(field, this.ccValues, event);
|
|
|
|
}else if(type == "bcc"){
|
|
|
|
this.bccValues = this.edit(field, this.bccValues, event);
|
|
|
|
}else if(type == "extraDataKeys"){
|
|
|
|
this.extraDataKeys = this.edit(field, this.extraDataKeys, event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
removeChipListValues(type: string, field: string){
|
|
|
|
if(type == "cc"){
|
|
|
|
this.ccValues = this.remove(field, this.ccValues);
|
|
|
|
}else if(type == "bcc"){
|
|
|
|
this.bccValues = this.remove(field, this.bccValues);
|
|
|
|
}else if(type == "extraDataKeys"){
|
|
|
|
this.extraDataKeys = this.remove(field, this.extraDataKeys);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
add(event: MatChipInputEvent, values: string[]) {
|
|
|
|
const value = (event.value || '').trim();
|
|
|
|
|
|
|
|
if (value) values.push(value)
|
|
|
|
event.chipInput!.clear();
|
|
|
|
return values;
|
2024-04-05 17:03:26 +02:00
|
|
|
|
2023-12-19 18:08:17 +01:00
|
|
|
}
|
2024-04-05 17:03:26 +02:00
|
|
|
|
2023-12-19 18:08:17 +01:00
|
|
|
remove(field: string, values: string[]) {
|
|
|
|
const index = values.indexOf(field);
|
2024-04-05 17:03:26 +02:00
|
|
|
|
2023-12-19 18:08:17 +01:00
|
|
|
if (index >= 0) values.splice(index, 1);
|
|
|
|
return values;
|
|
|
|
}
|
2024-04-05 17:03:26 +02:00
|
|
|
|
2023-12-19 18:08:17 +01:00
|
|
|
edit(field: string, values: string[], event: MatChipEditedEvent) {
|
|
|
|
const value = event.value.trim();
|
2024-04-05 17:03:26 +02:00
|
|
|
|
2023-12-19 18:08:17 +01:00
|
|
|
if (!value) {
|
|
|
|
values = this.remove(field, values);
|
|
|
|
return values;
|
|
|
|
}
|
2024-04-05 17:03:26 +02:00
|
|
|
|
2023-12-19 18:08:17 +01:00
|
|
|
const index = values.indexOf(field);
|
|
|
|
if (index >= 0) values[index] = value
|
|
|
|
|
|
|
|
return values;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|