add in app notification fallback from db

This commit is contained in:
amentis 2024-01-12 18:32:27 +02:00
parent c7a3b58019
commit 0d65c38668
6 changed files with 74 additions and 35 deletions

View File

@ -71,10 +71,17 @@ public class EmailMessageBuilder extends MessageBuilderBase implements MessageBu
@Override
public Message buildMessage(NotificationEntity notification) {
MessageInfo messageInfo = this.messageInfoBuilderService.buildMessageInfo(notification);
NotificationProperties.Flow flow = this.flowMap.get("email").getOrDefault(notification.getType(), null);
if (flow == null) return null;
if (messageInfo == null) {
logger.error("Could not retrieve message info for notification " + notification.getId());
return null;
}
NotificationProperties.Flow options = this.flowMap.get("email").getOrDefault(notification.getType(), null);
NotificationTemplate template = notificationTemplateService.lookupOverriddenTemplates(notification.getType(), NotificationTemplateChannel.Email, this.queryFactory.query(LanguageQuery.class).codes(messageInfo.getLanguage()).isActive(IsActive.Active).first().getId());
if (options == null && template == null) {
logger.error("Could not retrieve flow options for notification " + notification.getId() + " of type " + notification.getType());
return null;
}
String subjectTemplate = null;
ReplaceResult subjectResult = null;
String bodyTemplate = null;
@ -92,15 +99,15 @@ public class EmailMessageBuilder extends MessageBuilderBase implements MessageBu
FieldFormatting bodyFormatting = this.buildFieldFormatting(template.getValue().getBodyFieldOptions());
bodyResult = this.buildTemplate(notification.getId(), bodyTemplate, messageInfo, template.getValue().getBodyFieldOptions(), bodyFormatting, cipherFields.get(notification.getType()));
}else {
if(!StringUtils.isNullOrEmpty(flow.getSubjectKey())) subjectTemplate = messageInfo.getFields().stream().filter(fieldInfo -> fieldInfo.getKey().equals(flow.getSubjectKey())).findFirst().orElse(new FieldInfo()).getValue();
if (StringUtils.isNullOrEmpty(subjectTemplate)) subjectTemplate = this.lookupOrReadLocalizedFile(this.getTemplate().getTemplateCache(), flow.getSubjectPath(), messageInfo.getLanguage());
FieldFormatting subjectFormatting = this.buildFieldFormatting(flow.getSubjectFieldOptions());
subjectResult = this.buildTemplate(notification.getId(), subjectTemplate, messageInfo, flow.getSubjectFieldOptions(), subjectFormatting, cipherFields.get(notification.getType()));
if(!StringUtils.isNullOrEmpty(options.getSubjectKey())) subjectTemplate = messageInfo.getFields().stream().filter(fieldInfo -> fieldInfo.getKey().equals(options.getSubjectKey())).findFirst().orElse(new FieldInfo()).getValue();
if (StringUtils.isNullOrEmpty(subjectTemplate)) subjectTemplate = this.lookupOrReadLocalizedFile(this.getTemplate().getTemplateCache(), options.getSubjectPath(), messageInfo.getLanguage());
FieldFormatting subjectFormatting = this.buildFieldFormatting(options.getSubjectFieldOptions());
subjectResult = this.buildTemplate(notification.getId(), subjectTemplate, messageInfo, options.getSubjectFieldOptions(), subjectFormatting, cipherFields.get(notification.getType()));
if(!StringUtils.isNullOrEmpty(flow.getBodyKey())) bodyTemplate = messageInfo.getFields().stream().filter(fieldInfo -> fieldInfo.getKey().equals(flow.getBodyKey())).findFirst().orElse(new FieldInfo()).getValue();
if (StringUtils.isNullOrEmpty(bodyTemplate)) bodyTemplate = this.lookupOrReadLocalizedFile(this.getTemplate().getTemplateCache(), flow.getBodyPath(), messageInfo.getLanguage());
FieldFormatting bodyFormatting = this.buildFieldFormatting(flow.getBodyFieldOptions());
bodyResult = this.buildTemplate(notification.getId(), bodyTemplate, messageInfo, flow.getBodyFieldOptions(), bodyFormatting, cipherFields.get(notification.getType()));
if(!StringUtils.isNullOrEmpty(options.getBodyKey())) bodyTemplate = messageInfo.getFields().stream().filter(fieldInfo -> fieldInfo.getKey().equals(options.getBodyKey())).findFirst().orElse(new FieldInfo()).getValue();
if (StringUtils.isNullOrEmpty(bodyTemplate)) bodyTemplate = this.lookupOrReadLocalizedFile(this.getTemplate().getTemplateCache(), options.getBodyPath(), messageInfo.getLanguage());
FieldFormatting bodyFormatting = this.buildFieldFormatting(options.getBodyFieldOptions());
bodyResult = this.buildTemplate(notification.getId(), bodyTemplate, messageInfo, options.getBodyFieldOptions(), bodyFormatting, cipherFields.get(notification.getType()));
}
if (bodyResult != null && subjectResult != null) {

View File

@ -3,20 +3,26 @@ package gr.cite.notification.service.message.builder;
import gr.cite.notification.cache.NotificationTemplateCache;
import gr.cite.notification.common.StringUtils;
import gr.cite.notification.common.enums.InAppNotificationPriority;
import gr.cite.notification.common.enums.IsActive;
import gr.cite.notification.common.enums.NotificationContactType;
import gr.cite.notification.common.enums.NotificationTemplateChannel;
import gr.cite.notification.common.types.notification.FieldInfo;
import gr.cite.notification.config.notification.NotificationConfig;
import gr.cite.notification.config.notification.NotificationProperties;
import gr.cite.notification.data.NotificationEntity;
import gr.cite.notification.errorcode.ErrorThesaurusProperties;
import gr.cite.notification.model.NotificationTemplate;
import gr.cite.notification.query.LanguageQuery;
import gr.cite.notification.service.formatting.FormattingService;
import gr.cite.notification.service.message.common.MessageBuilderBase;
import gr.cite.notification.service.message.infobuilder.MessageInfoBuilderService;
import gr.cite.notification.service.message.model.InAppMessage;
import gr.cite.notification.service.message.model.Message;
import gr.cite.notification.service.message.model.MessageInfo;
import gr.cite.notification.service.notificationtemplate.NotificationTemplateService;
import gr.cite.tools.cipher.CipherService;
import gr.cite.tools.cipher.config.CipherProfileProperties;
import gr.cite.tools.data.query.QueryFactory;
import gr.cite.tools.logging.LoggerService;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@ -36,6 +42,8 @@ public class InAppMessageBuilder extends MessageBuilderBase implements MessageBu
private final MessageInfoBuilderService messageInfoBuilderService;
private final Map<String, Map<UUID, NotificationProperties.Flow>> flowMap;
private final NotificationProperties properties;
private final NotificationTemplateService notificationTemplateService;
private final QueryFactory queryFactory;
@Autowired
public InAppMessageBuilder(MessageInfoBuilderService messageInfoBuilderService,
@ -45,54 +53,79 @@ public class InAppMessageBuilder extends MessageBuilderBase implements MessageBu
CipherService cipherService,
CipherProfileProperties cipherProfileProperties,
FormattingService formattingService,
NotificationTemplateCache cache) {
NotificationTemplateCache cache, NotificationTemplateService notificationTemplateService, QueryFactory queryFactory) {
super(logger, errors, cipherService, cipherProfileProperties, formattingService, cache);
this.messageInfoBuilderService = messageInfoBuilderService;
this.flowMap = flowMap;
this.properties = properties;
this.notificationTemplateService = notificationTemplateService;
this.queryFactory = queryFactory;
}
@Override
public Message buildMessage(NotificationEntity notification) {
MessageInfo messageInfo = this.messageInfoBuilderService.buildMessageInfo(notification);
if (messageInfo == null)
{
if (messageInfo == null) {
logger.error("Could not retrieve message info for notification " + notification.getId());
return null;
}
NotificationProperties.Flow options = this.flowMap.get("in-app").getOrDefault(notification.getType(), null);
NotificationTemplate template = notificationTemplateService.lookupOverriddenTemplates(notification.getType(), NotificationTemplateChannel.Email, this.queryFactory.query(LanguageQuery.class).codes(messageInfo.getLanguage()).isActive(IsActive.Active).first().getId());
if (options == null)
{
if (options == null && template == null) {
logger.error("Could not retrieve flow options for notification " + notification.getId() + " of type " + notification.getType());
return null;
}
MessageBuilderBase.FieldCiphering ciphering = options.getCipherFields() == null || options.getCipherFields().isEmpty() ? new MessageBuilderBase.FieldCiphering() : new MessageBuilderBase.FieldCiphering(options.getCipherFields());
String subjectTemplate = null;
if (!StringUtils.isNullOrEmpty(options.getSubjectKey())) subjectTemplate = messageInfo.getFields().stream().filter(x -> x.getKey().equals(options.getSubjectKey())).findFirst().orElse(new FieldInfo()).getValue();
if (StringUtils.isNullOrEmpty(subjectTemplate)) subjectTemplate = this.lookupOrReadLocalizedFile(this.getTemplate().getTemplateCache(), options.getSubjectPath(), messageInfo.getLanguage());
FieldFormatting subjectFormatting = this.buildFieldFormatting(options.getSubjectFieldOptions());
ReplaceResult subjectResult = this.buildTemplate(notification.getId(), subjectTemplate, messageInfo, options.getSubjectFieldOptions(), subjectFormatting, ciphering);
ReplaceResult subjectResult = null;
String bodyTemplate = null;
if (!StringUtils.isNullOrEmpty(options.getBodyKey())) bodyTemplate = messageInfo.getFields().stream().filter(x -> x.getKey().equals(options.getBodyKey())).findFirst().orElse(new FieldInfo()).getValue();
if (StringUtils.isNullOrEmpty(bodyTemplate)) bodyTemplate = this.lookupOrReadLocalizedFile(this.getTemplate().getTemplateCache(), options.getBodyPath(), messageInfo.getLanguage());
FieldFormatting bodyFormatting = this.buildFieldFormatting(options.getBodyFieldOptions());
ReplaceResult bodyResult = this.buildTemplate(notification.getId(), bodyTemplate, messageInfo, options.getBodyFieldOptions(), bodyFormatting, ciphering);
ReplaceResult bodyResult = null;
// fallback
if ( template != null && template.getValue() != null){
if (!StringUtils.isNullOrEmpty(template.getValue().getSubjectKey())) subjectTemplate = messageInfo.getFields().stream().filter(fieldInfo -> fieldInfo.getKey().equals(template.getValue().getSubjectKey())).findFirst().orElse(new FieldInfo()).getValue();
if (StringUtils.isNullOrEmpty(subjectTemplate)) subjectTemplate = template.getValue().getSubjectText();
FieldFormatting subjectFormatting = this.buildFieldFormatting(template.getValue().getSubjectFieldOptions());
subjectResult = this.buildTemplate(notification.getId(), subjectTemplate, messageInfo, template.getValue().getSubjectFieldOptions(), subjectFormatting, ciphering);
if(!StringUtils.isNullOrEmpty(template.getValue().getBodyKey())) bodyTemplate = messageInfo.getFields().stream().filter(fieldInfo -> fieldInfo.getKey().equals(template.getValue().getBodyKey())).findFirst().orElse(new FieldInfo()).getValue();
if (StringUtils.isNullOrEmpty(bodyTemplate)) bodyTemplate = template.getValue().getBodyText();
FieldFormatting bodyFormatting = this.buildFieldFormatting(template.getValue().getBodyFieldOptions());
bodyResult = this.buildTemplate(notification.getId(), bodyTemplate, messageInfo, template.getValue().getBodyFieldOptions(), bodyFormatting, ciphering);
}else{
if (!StringUtils.isNullOrEmpty(options.getSubjectKey())) subjectTemplate = messageInfo.getFields().stream().filter(x -> x.getKey().equals(options.getSubjectKey())).findFirst().orElse(new FieldInfo()).getValue();
if (StringUtils.isNullOrEmpty(subjectTemplate)) subjectTemplate = this.lookupOrReadLocalizedFile(this.getTemplate().getTemplateCache(), options.getSubjectPath(), messageInfo.getLanguage());
FieldFormatting subjectFormatting = this.buildFieldFormatting(options.getSubjectFieldOptions());
subjectResult = this.buildTemplate(notification.getId(), subjectTemplate, messageInfo, options.getSubjectFieldOptions(), subjectFormatting, ciphering);
if (!StringUtils.isNullOrEmpty(options.getBodyKey())) bodyTemplate = messageInfo.getFields().stream().filter(x -> x.getKey().equals(options.getBodyKey())).findFirst().orElse(new FieldInfo()).getValue();
if (StringUtils.isNullOrEmpty(bodyTemplate)) bodyTemplate = this.lookupOrReadLocalizedFile(this.getTemplate().getTemplateCache(), options.getBodyPath(), messageInfo.getLanguage());
FieldFormatting bodyFormatting = this.buildFieldFormatting(options.getBodyFieldOptions());
bodyResult = this.buildTemplate(notification.getId(), bodyTemplate, messageInfo, options.getBodyFieldOptions(), bodyFormatting, ciphering);
}
String priorityString = "";
if ( template != null && template.getValue() != null){
if (!StringUtils.isNullOrEmpty(template.getValue().getPriorityKey())) priorityString = messageInfo.getFields().stream().filter(x -> x.getKey().equals(template.getValue().getPriorityKey())).findFirst().orElse(new FieldInfo()).getValue();
}else {
if (!StringUtils.isNullOrEmpty(options.getPriorityKey())) priorityString = messageInfo.getFields().stream().filter(x -> x.getKey().equals(options.getPriorityKey())).findFirst().orElse(new FieldInfo()).getValue();
}
if (!StringUtils.isNullOrEmpty(options.getPriorityKey())) priorityString = messageInfo.getFields().stream().filter(x -> x.getKey().equals(options.getPriorityKey())).findFirst().orElse(new FieldInfo()).getValue();
InAppNotificationPriority inAppNotificationPriority = InAppNotificationPriority.NORMAL;
if (!StringUtils.isNullOrEmpty(priorityString)) {
inAppNotificationPriority = InAppNotificationPriority.valueOf(priorityString.toLowerCase());
}
List<FieldInfo> extraData = null;
if (options.getExtraDataKeys() != null && !options.getExtraDataKeys().isEmpty()) extraData = messageInfo.getFields().stream().filter(x -> options.getExtraDataKeys().contains(x.getKey())).collect(Collectors.toList());
if (template != null && template.getValue() != null){
if (template.getValue().getExtraDataKeys() != null && !template.getValue().getExtraDataKeys().isEmpty()) extraData = messageInfo.getFields().stream().filter(x -> template.getValue().getExtraDataKeys().contains(x.getKey())).collect(Collectors.toList());
}else {
if (options.getExtraDataKeys() != null && !options.getExtraDataKeys().isEmpty()) extraData = messageInfo.getFields().stream().filter(x -> options.getExtraDataKeys().contains(x.getKey())).collect(Collectors.toList());
}
return new InAppMessage(subjectResult.getText(), bodyResult.getText(), notification.getType(), inAppNotificationPriority, extraData);
}

View File

@ -5,10 +5,11 @@ import { Guid } from "@common/types/guid";
import { Language } from "../language/language";
import { NotificationDataType } from "@app/core/common/enum/notification-data-type";
import { EmailOverrideMode } from "@app/core/common/enum/email-override-mode";
import { NotificationType } from "@app/core/common/enum/notification-type";
export interface NotificationTemplate extends BaseEntity{
channel: NotificationTemplateChannel;
notificationType: Guid;
notificationType: NotificationType;
kind: NotificationTemplateKind;
language: Language;
value: NotificationTemplateValue;
@ -46,6 +47,7 @@ export interface NotificationFieldInfo {
export interface NotificationTemplatePersist extends BaseEntityPersist{
channel: NotificationTemplateChannel;
notificationType: NotificationType;
kind: NotificationTemplateKind;
languageId: Guid;
value: NotificationTemplateValuePersist;

View File

@ -33,7 +33,7 @@
<div class="col-4">
<mat-form-field class="w-100">
<mat-label>{{'NOTIFICATION-SERVICE.NOTIFICATION-TEMPLATE-EDITOR.FIELDS.NOTIFICATION-TYPE' | translate}}</mat-label>
<mat-select [value] = "notificationType" (selectionChange)="selectedNotificationTypeChanged($event.value)" name="notificationType" required>
<mat-select [value] = "notificationType" [formControl]="formGroup.get('notificationType')" name="notificationType" required>
<mat-option *ngFor="let type of notificationTypeEnum" [value]="type">
{{enumUtils.toNotificationTypeString(type)}}
</mat-option>

View File

@ -124,7 +124,6 @@ export class NotificationTemplateEditorComponent extends BaseEditor<Notification
this.editorModel = data ? new NotificationTemplateEditorModel().fromModel(data) : new NotificationTemplateEditorModel();
if(data){
this.notificationType = data.notificationType.toString();
if(data.value && data.value.subjectFieldOptions){
this.subjectFieldOptionsEnabled = true;
this.subjectMandatoryFields = data.value.subjectFieldOptions.mandatory;
@ -224,9 +223,6 @@ export class NotificationTemplateEditorComponent extends BaseEditor<Notification
this.formService.validateAllFormFields(this.formGroup);
}
selectedNotificationTypeChanged(type: NotificationType){
this.formGroup.get('notificationType').patchValue(Guid.parse(type));
}
selectedLangChanged(code: string){

View File

@ -9,10 +9,11 @@ import { NotificationTemplateKind } from '@app/core/common/enum/notification-tem
import { NotificationDataType } from '@app/core/common/enum/notification-data-type';
import { EmailOverrideMode } from '@app/core/common/enum/email-override-mode';
import { BaseEditorModel } from '@common/base/base-form-editor-model';
import { NotificationType } from '@app/core/common/enum/notification-type';
export class NotificationTemplateEditorModel extends BaseEditorModel implements NotificationTemplatePersist {
channel: NotificationTemplateChannel;
notificationType: Guid;
notificationType: NotificationType;
kind: NotificationTemplateKind;
languageId: Guid;
value: NotificationTemplateValueEditorModel = new NotificationTemplateValueEditorModel();