2023-12-08 10:25:07 +01:00
|
|
|
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;
|
2024-01-12 17:32:27 +01:00
|
|
|
import gr.cite.notification.common.enums.IsActive;
|
2023-12-08 10:25:07 +01:00
|
|
|
import gr.cite.notification.common.enums.NotificationContactType;
|
2024-01-12 17:32:27 +01:00
|
|
|
import gr.cite.notification.common.enums.NotificationTemplateChannel;
|
2023-12-08 10:25:07 +01:00
|
|
|
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;
|
2024-01-12 17:32:27 +01:00
|
|
|
import gr.cite.notification.model.NotificationTemplate;
|
2023-12-08 10:25:07 +01:00
|
|
|
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;
|
2024-01-12 17:32:27 +01:00
|
|
|
import gr.cite.notification.service.notificationtemplate.NotificationTemplateService;
|
2023-12-08 10:25:07 +01:00
|
|
|
import gr.cite.tools.cipher.CipherService;
|
|
|
|
import gr.cite.tools.cipher.config.CipherProfileProperties;
|
2024-01-12 17:32:27 +01:00
|
|
|
import gr.cite.tools.data.query.QueryFactory;
|
2023-12-08 10:25:07 +01:00
|
|
|
import gr.cite.tools.logging.LoggerService;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import org.springframework.web.context.annotation.RequestScope;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.UUID;
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
@Component
|
|
|
|
@RequestScope
|
|
|
|
public class InAppMessageBuilder extends MessageBuilderBase implements MessageBuilder{
|
|
|
|
private final static LoggerService logger = new LoggerService(LoggerFactory.getLogger(InAppMessageBuilder.class));
|
|
|
|
private final MessageInfoBuilderService messageInfoBuilderService;
|
|
|
|
private final Map<String, Map<UUID, NotificationProperties.Flow>> flowMap;
|
|
|
|
private final NotificationProperties properties;
|
2024-01-12 17:32:27 +01:00
|
|
|
private final NotificationTemplateService notificationTemplateService;
|
|
|
|
private final QueryFactory queryFactory;
|
2023-12-08 10:25:07 +01:00
|
|
|
|
|
|
|
@Autowired
|
|
|
|
public InAppMessageBuilder(MessageInfoBuilderService messageInfoBuilderService,
|
|
|
|
@Qualifier(NotificationConfig.BeanQualifier.FLOW_MAP)
|
|
|
|
Map<String, Map<UUID, NotificationProperties.Flow>> flowMap,
|
|
|
|
ErrorThesaurusProperties errors, NotificationProperties properties,
|
|
|
|
CipherService cipherService,
|
|
|
|
CipherProfileProperties cipherProfileProperties,
|
|
|
|
FormattingService formattingService,
|
2024-01-12 17:32:27 +01:00
|
|
|
NotificationTemplateCache cache, NotificationTemplateService notificationTemplateService, QueryFactory queryFactory) {
|
2023-12-08 10:25:07 +01:00
|
|
|
super(logger, errors, cipherService, cipherProfileProperties, formattingService, cache);
|
|
|
|
this.messageInfoBuilderService = messageInfoBuilderService;
|
|
|
|
this.flowMap = flowMap;
|
|
|
|
this.properties = properties;
|
2024-01-12 17:32:27 +01:00
|
|
|
this.notificationTemplateService = notificationTemplateService;
|
|
|
|
this.queryFactory = queryFactory;
|
2023-12-08 10:25:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Message buildMessage(NotificationEntity notification) {
|
|
|
|
MessageInfo messageInfo = this.messageInfoBuilderService.buildMessageInfo(notification);
|
2024-01-12 17:32:27 +01:00
|
|
|
if (messageInfo == null) {
|
2023-12-08 10:25:07 +01:00
|
|
|
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);
|
2024-04-15 17:36:55 +02:00
|
|
|
NotificationTemplate template = notificationTemplateService.lookupOverriddenTemplates(notification.getType(), NotificationTemplateChannel.InApp, messageInfo.getLanguage());
|
2023-12-08 10:25:07 +01:00
|
|
|
|
2024-01-12 17:32:27 +01:00
|
|
|
if (options == null && template == null) {
|
2023-12-08 10:25:07 +01:00
|
|
|
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;
|
2024-01-12 17:32:27 +01:00
|
|
|
ReplaceResult subjectResult = null;
|
2023-12-08 10:25:07 +01:00
|
|
|
String bodyTemplate = null;
|
2024-01-12 17:32:27 +01:00
|
|
|
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);
|
|
|
|
}
|
2023-12-08 10:25:07 +01:00
|
|
|
|
|
|
|
String priorityString = "";
|
2024-01-12 17:32:27 +01:00
|
|
|
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();
|
|
|
|
}
|
2023-12-08 10:25:07 +01:00
|
|
|
|
|
|
|
InAppNotificationPriority inAppNotificationPriority = InAppNotificationPriority.NORMAL;
|
2024-01-12 17:32:27 +01:00
|
|
|
|
2023-12-08 10:25:07 +01:00
|
|
|
if (!StringUtils.isNullOrEmpty(priorityString)) {
|
|
|
|
inAppNotificationPriority = InAppNotificationPriority.valueOf(priorityString.toLowerCase());
|
|
|
|
}
|
|
|
|
|
|
|
|
List<FieldInfo> extraData = null;
|
2024-01-12 17:32:27 +01:00
|
|
|
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());
|
|
|
|
}
|
2023-12-08 10:25:07 +01:00
|
|
|
|
|
|
|
return new InAppMessage(subjectResult.getText(), bodyResult.getText(), notification.getType(), inAppNotificationPriority, extraData);
|
|
|
|
}
|
|
|
|
|
|
|
|
private NotificationProperties.Template getTemplate() {
|
|
|
|
return this.properties.getMessage().get("in-app");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public NotificationContactType supports() {
|
|
|
|
return NotificationContactType.IN_APP;
|
|
|
|
}
|
|
|
|
}
|