use of com.redhat.cloud.notifications.recipients.RecipientSettings in project notifications-backend by RedHatInsights.
the class EmailSubscriptionTypeProcessor method sendEmail.
private List<NotificationHistory> sendEmail(Event event, Set<Endpoint> endpoints, EmailTemplate emailTemplate) {
EmailSubscriptionType emailSubscriptionType = EmailSubscriptionType.INSTANT;
processedEmailCount.increment();
Action action = event.getAction();
TemplateInstance subject;
TemplateInstance body;
if (useTemplatesFromDb) {
Optional<InstantEmailTemplate> instantEmailTemplate = templateRepository.findInstantEmailTemplate(event.getEventType().getId());
if (instantEmailTemplate.isEmpty()) {
return Collections.emptyList();
} else {
String subjectData = instantEmailTemplate.get().getSubjectTemplate().getData();
subject = templateService.compileTemplate(subjectData, "subject");
String bodyData = instantEmailTemplate.get().getBodyTemplate().getData();
body = templateService.compileTemplate(bodyData, "body");
}
} else {
if (!emailTemplate.isSupported(action.getEventType(), emailSubscriptionType)) {
return Collections.emptyList();
}
subject = emailTemplate.getTitle(action.getEventType(), emailSubscriptionType);
body = emailTemplate.getBody(action.getEventType(), emailSubscriptionType);
}
if (subject == null || body == null) {
return Collections.emptyList();
}
Set<RecipientSettings> requests = Stream.concat(endpoints.stream().map(EndpointRecipientSettings::new), ActionRecipientSettings.fromAction(action).stream()).collect(Collectors.toSet());
Set<String> subscribers = Set.copyOf(emailSubscriptionRepository.getEmailSubscribersUserId(action.getAccountId(), action.getBundle(), action.getApplication(), emailSubscriptionType));
LOG.info("sending email for event: " + event);
return recipientResolver.recipientUsers(action.getAccountId(), action.getOrgId(), requests, subscribers).stream().map(user -> emailSender.sendEmail(user, event, subject, body)).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList());
}
Aggregations