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 @Override
public Message buildMessage(NotificationEntity notification) { public Message buildMessage(NotificationEntity notification) {
MessageInfo messageInfo = this.messageInfoBuilderService.buildMessageInfo(notification); MessageInfo messageInfo = this.messageInfoBuilderService.buildMessageInfo(notification);
NotificationProperties.Flow flow = this.flowMap.get("email").getOrDefault(notification.getType(), null); if (messageInfo == null) {
if (flow == null) return 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()); 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; String subjectTemplate = null;
ReplaceResult subjectResult = null; ReplaceResult subjectResult = null;
String bodyTemplate = null; String bodyTemplate = null;
@ -92,15 +99,15 @@ public class EmailMessageBuilder extends MessageBuilderBase implements MessageBu
FieldFormatting bodyFormatting = this.buildFieldFormatting(template.getValue().getBodyFieldOptions()); FieldFormatting bodyFormatting = this.buildFieldFormatting(template.getValue().getBodyFieldOptions());
bodyResult = this.buildTemplate(notification.getId(), bodyTemplate, messageInfo, template.getValue().getBodyFieldOptions(), bodyFormatting, cipherFields.get(notification.getType())); bodyResult = this.buildTemplate(notification.getId(), bodyTemplate, messageInfo, template.getValue().getBodyFieldOptions(), bodyFormatting, cipherFields.get(notification.getType()));
}else { }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(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(), flow.getSubjectPath(), messageInfo.getLanguage()); if (StringUtils.isNullOrEmpty(subjectTemplate)) subjectTemplate = this.lookupOrReadLocalizedFile(this.getTemplate().getTemplateCache(), options.getSubjectPath(), messageInfo.getLanguage());
FieldFormatting subjectFormatting = this.buildFieldFormatting(flow.getSubjectFieldOptions()); FieldFormatting subjectFormatting = this.buildFieldFormatting(options.getSubjectFieldOptions());
subjectResult = this.buildTemplate(notification.getId(), subjectTemplate, messageInfo, flow.getSubjectFieldOptions(), subjectFormatting, cipherFields.get(notification.getType())); 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(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(), flow.getBodyPath(), messageInfo.getLanguage()); if (StringUtils.isNullOrEmpty(bodyTemplate)) bodyTemplate = this.lookupOrReadLocalizedFile(this.getTemplate().getTemplateCache(), options.getBodyPath(), messageInfo.getLanguage());
FieldFormatting bodyFormatting = this.buildFieldFormatting(flow.getBodyFieldOptions()); FieldFormatting bodyFormatting = this.buildFieldFormatting(options.getBodyFieldOptions());
bodyResult = this.buildTemplate(notification.getId(), bodyTemplate, messageInfo, flow.getBodyFieldOptions(), bodyFormatting, cipherFields.get(notification.getType())); bodyResult = this.buildTemplate(notification.getId(), bodyTemplate, messageInfo, options.getBodyFieldOptions(), bodyFormatting, cipherFields.get(notification.getType()));
} }
if (bodyResult != null && subjectResult != null) { 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.cache.NotificationTemplateCache;
import gr.cite.notification.common.StringUtils; import gr.cite.notification.common.StringUtils;
import gr.cite.notification.common.enums.InAppNotificationPriority; 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.NotificationContactType;
import gr.cite.notification.common.enums.NotificationTemplateChannel;
import gr.cite.notification.common.types.notification.FieldInfo; import gr.cite.notification.common.types.notification.FieldInfo;
import gr.cite.notification.config.notification.NotificationConfig; import gr.cite.notification.config.notification.NotificationConfig;
import gr.cite.notification.config.notification.NotificationProperties; import gr.cite.notification.config.notification.NotificationProperties;
import gr.cite.notification.data.NotificationEntity; import gr.cite.notification.data.NotificationEntity;
import gr.cite.notification.errorcode.ErrorThesaurusProperties; 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.formatting.FormattingService;
import gr.cite.notification.service.message.common.MessageBuilderBase; import gr.cite.notification.service.message.common.MessageBuilderBase;
import gr.cite.notification.service.message.infobuilder.MessageInfoBuilderService; import gr.cite.notification.service.message.infobuilder.MessageInfoBuilderService;
import gr.cite.notification.service.message.model.InAppMessage; import gr.cite.notification.service.message.model.InAppMessage;
import gr.cite.notification.service.message.model.Message; import gr.cite.notification.service.message.model.Message;
import gr.cite.notification.service.message.model.MessageInfo; 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.CipherService;
import gr.cite.tools.cipher.config.CipherProfileProperties; import gr.cite.tools.cipher.config.CipherProfileProperties;
import gr.cite.tools.data.query.QueryFactory;
import gr.cite.tools.logging.LoggerService; import gr.cite.tools.logging.LoggerService;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -36,6 +42,8 @@ public class InAppMessageBuilder extends MessageBuilderBase implements MessageBu
private final MessageInfoBuilderService messageInfoBuilderService; private final MessageInfoBuilderService messageInfoBuilderService;
private final Map<String, Map<UUID, NotificationProperties.Flow>> flowMap; private final Map<String, Map<UUID, NotificationProperties.Flow>> flowMap;
private final NotificationProperties properties; private final NotificationProperties properties;
private final NotificationTemplateService notificationTemplateService;
private final QueryFactory queryFactory;
@Autowired @Autowired
public InAppMessageBuilder(MessageInfoBuilderService messageInfoBuilderService, public InAppMessageBuilder(MessageInfoBuilderService messageInfoBuilderService,
@ -45,54 +53,79 @@ public class InAppMessageBuilder extends MessageBuilderBase implements MessageBu
CipherService cipherService, CipherService cipherService,
CipherProfileProperties cipherProfileProperties, CipherProfileProperties cipherProfileProperties,
FormattingService formattingService, FormattingService formattingService,
NotificationTemplateCache cache) { NotificationTemplateCache cache, NotificationTemplateService notificationTemplateService, QueryFactory queryFactory) {
super(logger, errors, cipherService, cipherProfileProperties, formattingService, cache); super(logger, errors, cipherService, cipherProfileProperties, formattingService, cache);
this.messageInfoBuilderService = messageInfoBuilderService; this.messageInfoBuilderService = messageInfoBuilderService;
this.flowMap = flowMap; this.flowMap = flowMap;
this.properties = properties; this.properties = properties;
this.notificationTemplateService = notificationTemplateService;
this.queryFactory = queryFactory;
} }
@Override @Override
public Message buildMessage(NotificationEntity notification) { public Message buildMessage(NotificationEntity notification) {
MessageInfo messageInfo = this.messageInfoBuilderService.buildMessageInfo(notification); MessageInfo messageInfo = this.messageInfoBuilderService.buildMessageInfo(notification);
if (messageInfo == null) if (messageInfo == null) {
{
logger.error("Could not retrieve message info for notification " + notification.getId()); logger.error("Could not retrieve message info for notification " + notification.getId());
return null; return null;
} }
NotificationProperties.Flow options = this.flowMap.get("in-app").getOrDefault(notification.getType(), 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()); logger.error("Could not retrieve flow options for notification " + notification.getId() + " of type " + notification.getType());
return null; return null;
} }
MessageBuilderBase.FieldCiphering ciphering = options.getCipherFields() == null || options.getCipherFields().isEmpty() ? new MessageBuilderBase.FieldCiphering() : new MessageBuilderBase.FieldCiphering(options.getCipherFields()); MessageBuilderBase.FieldCiphering ciphering = options.getCipherFields() == null || options.getCipherFields().isEmpty() ? new MessageBuilderBase.FieldCiphering() : new MessageBuilderBase.FieldCiphering(options.getCipherFields());
String subjectTemplate = null; String subjectTemplate = null;
ReplaceResult subjectResult = null;
String bodyTemplate = null;
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(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()); if (StringUtils.isNullOrEmpty(subjectTemplate)) subjectTemplate = this.lookupOrReadLocalizedFile(this.getTemplate().getTemplateCache(), options.getSubjectPath(), messageInfo.getLanguage());
FieldFormatting subjectFormatting = this.buildFieldFormatting(options.getSubjectFieldOptions()); FieldFormatting subjectFormatting = this.buildFieldFormatting(options.getSubjectFieldOptions());
ReplaceResult subjectResult = this.buildTemplate(notification.getId(), subjectTemplate, messageInfo, options.getSubjectFieldOptions(), subjectFormatting, ciphering); subjectResult = this.buildTemplate(notification.getId(), subjectTemplate, messageInfo, options.getSubjectFieldOptions(), subjectFormatting, ciphering);
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(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()); if (StringUtils.isNullOrEmpty(bodyTemplate)) bodyTemplate = this.lookupOrReadLocalizedFile(this.getTemplate().getTemplateCache(), options.getBodyPath(), messageInfo.getLanguage());
FieldFormatting bodyFormatting = this.buildFieldFormatting(options.getBodyFieldOptions()); FieldFormatting bodyFormatting = this.buildFieldFormatting(options.getBodyFieldOptions());
ReplaceResult bodyResult = this.buildTemplate(notification.getId(), bodyTemplate, messageInfo, options.getBodyFieldOptions(), bodyFormatting, ciphering); bodyResult = this.buildTemplate(notification.getId(), bodyTemplate, messageInfo, options.getBodyFieldOptions(), bodyFormatting, ciphering);
}
String priorityString = ""; 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; InAppNotificationPriority inAppNotificationPriority = InAppNotificationPriority.NORMAL;
if (!StringUtils.isNullOrEmpty(priorityString)) { if (!StringUtils.isNullOrEmpty(priorityString)) {
inAppNotificationPriority = InAppNotificationPriority.valueOf(priorityString.toLowerCase()); inAppNotificationPriority = InAppNotificationPriority.valueOf(priorityString.toLowerCase());
} }
List<FieldInfo> extraData = null; List<FieldInfo> extraData = null;
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()); 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); 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 { Language } from "../language/language";
import { NotificationDataType } from "@app/core/common/enum/notification-data-type"; import { NotificationDataType } from "@app/core/common/enum/notification-data-type";
import { EmailOverrideMode } from "@app/core/common/enum/email-override-mode"; import { EmailOverrideMode } from "@app/core/common/enum/email-override-mode";
import { NotificationType } from "@app/core/common/enum/notification-type";
export interface NotificationTemplate extends BaseEntity{ export interface NotificationTemplate extends BaseEntity{
channel: NotificationTemplateChannel; channel: NotificationTemplateChannel;
notificationType: Guid; notificationType: NotificationType;
kind: NotificationTemplateKind; kind: NotificationTemplateKind;
language: Language; language: Language;
value: NotificationTemplateValue; value: NotificationTemplateValue;
@ -46,6 +47,7 @@ export interface NotificationFieldInfo {
export interface NotificationTemplatePersist extends BaseEntityPersist{ export interface NotificationTemplatePersist extends BaseEntityPersist{
channel: NotificationTemplateChannel; channel: NotificationTemplateChannel;
notificationType: NotificationType;
kind: NotificationTemplateKind; kind: NotificationTemplateKind;
languageId: Guid; languageId: Guid;
value: NotificationTemplateValuePersist; value: NotificationTemplateValuePersist;

View File

@ -33,7 +33,7 @@
<div class="col-4"> <div class="col-4">
<mat-form-field class="w-100"> <mat-form-field class="w-100">
<mat-label>{{'NOTIFICATION-SERVICE.NOTIFICATION-TEMPLATE-EDITOR.FIELDS.NOTIFICATION-TYPE' | translate}}</mat-label> <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"> <mat-option *ngFor="let type of notificationTypeEnum" [value]="type">
{{enumUtils.toNotificationTypeString(type)}} {{enumUtils.toNotificationTypeString(type)}}
</mat-option> </mat-option>

View File

@ -124,7 +124,6 @@ export class NotificationTemplateEditorComponent extends BaseEditor<Notification
this.editorModel = data ? new NotificationTemplateEditorModel().fromModel(data) : new NotificationTemplateEditorModel(); this.editorModel = data ? new NotificationTemplateEditorModel().fromModel(data) : new NotificationTemplateEditorModel();
if(data){ if(data){
this.notificationType = data.notificationType.toString();
if(data.value && data.value.subjectFieldOptions){ if(data.value && data.value.subjectFieldOptions){
this.subjectFieldOptionsEnabled = true; this.subjectFieldOptionsEnabled = true;
this.subjectMandatoryFields = data.value.subjectFieldOptions.mandatory; this.subjectMandatoryFields = data.value.subjectFieldOptions.mandatory;
@ -224,9 +223,6 @@ export class NotificationTemplateEditorComponent extends BaseEditor<Notification
this.formService.validateAllFormFields(this.formGroup); this.formService.validateAllFormFields(this.formGroup);
} }
selectedNotificationTypeChanged(type: NotificationType){
this.formGroup.get('notificationType').patchValue(Guid.parse(type));
}
selectedLangChanged(code: string){ 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 { NotificationDataType } from '@app/core/common/enum/notification-data-type';
import { EmailOverrideMode } from '@app/core/common/enum/email-override-mode'; import { EmailOverrideMode } from '@app/core/common/enum/email-override-mode';
import { BaseEditorModel } from '@common/base/base-form-editor-model'; 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 { export class NotificationTemplateEditorModel extends BaseEditorModel implements NotificationTemplatePersist {
channel: NotificationTemplateChannel; channel: NotificationTemplateChannel;
notificationType: Guid; notificationType: NotificationType;
kind: NotificationTemplateKind; kind: NotificationTemplateKind;
languageId: Guid; languageId: Guid;
value: NotificationTemplateValueEditorModel = new NotificationTemplateValueEditorModel(); value: NotificationTemplateValueEditorModel = new NotificationTemplateValueEditorModel();