use of com.redhat.cloud.notifications.models.AggregationEmailTemplate in project notifications-backend by RedHatInsights.
the class EmailTemplateMigrationServiceTest method findAndCompileAggregationEmailTemplate.
private void findAndCompileAggregationEmailTemplate(String bundleName, String appName, EmailSubscriptionType subscriptionType) {
AggregationEmailTemplate emailTemplate = templateRepository.findAggregationEmailTemplate(bundleName, appName, subscriptionType).get();
templateService.compileTemplate(emailTemplate.getSubjectTemplate().getData(), emailTemplate.getSubjectTemplate().getName());
templateService.compileTemplate(emailTemplate.getBodyTemplate().getData(), emailTemplate.getBodyTemplate().getName());
}
use of com.redhat.cloud.notifications.models.AggregationEmailTemplate in project notifications-backend by RedHatInsights.
the class ResourceHelpers method createAggregationEmailTemplate.
@Transactional
public AggregationEmailTemplate createAggregationEmailTemplate(UUID appId, UUID subjectTemplateId, UUID bodyTemplateId, boolean enabled) {
AggregationEmailTemplate aggregationEmailTemplate = new AggregationEmailTemplate();
aggregationEmailTemplate.setApplication(entityManager.find(Application.class, appId));
aggregationEmailTemplate.setApplicationId(appId);
aggregationEmailTemplate.setSubjectTemplate(entityManager.find(Template.class, subjectTemplateId));
aggregationEmailTemplate.setSubjectTemplateId(subjectTemplateId);
aggregationEmailTemplate.setBodyTemplate(entityManager.find(Template.class, bodyTemplateId));
aggregationEmailTemplate.setBodyTemplateId(bodyTemplateId);
aggregationEmailTemplate.setSubscriptionType(DAILY);
entityManager.persist(aggregationEmailTemplate);
return aggregationEmailTemplate;
}
use of com.redhat.cloud.notifications.models.AggregationEmailTemplate in project notifications-backend by RedHatInsights.
the class TemplateRepository method findAggregationEmailTemplatesByApplication.
public List<AggregationEmailTemplate> findAggregationEmailTemplatesByApplication(UUID appId) {
String hql = "FROM AggregationEmailTemplate t JOIN FETCH t.application a WHERE a.id = :appId";
List<AggregationEmailTemplate> aggregationEmailTemplates = entityManager.createQuery(hql, AggregationEmailTemplate.class).setParameter("appId", appId).getResultList();
for (AggregationEmailTemplate aggregationEmailTemplate : aggregationEmailTemplates) {
// The full application isn't needed in the REST response.
aggregationEmailTemplate.filterOutApplication();
// The full templates aren't needed in the REST response.
aggregationEmailTemplate.filterOutTemplates();
}
return aggregationEmailTemplates;
}
use of com.redhat.cloud.notifications.models.AggregationEmailTemplate in project notifications-backend by RedHatInsights.
the class EmailTemplateMigrationService method createDailyEmailTemplate.
/*
* Creates an aggregation email template and the underlying templates only if:
* - the application exists in the DB
* - the aggregation email template does not already exist in the DB
* Existing aggregation email templates are never updated by this migration service.
*/
private void createDailyEmailTemplate(List<String> warnings, String bundleName, String appName, String subjectTemplateName, String subjectTemplateExtension, String subjectTemplateDescription, String bodyTemplateName, String bodyTemplateExtension, String bodyTemplateDescription) {
Optional<Application> app = findApplication(warnings, bundleName, appName);
if (app.isPresent()) {
if (aggregationEmailTemplateExists(app.get())) {
warnings.add(String.format("Aggregation email template found in DB for application: %s/%s", bundleName, appName));
} else {
Template subjectTemplate = getOrCreateTemplate(warnings, subjectTemplateName, subjectTemplateExtension, subjectTemplateDescription);
Template bodyTemplate = getOrCreateTemplate(warnings, bodyTemplateName, bodyTemplateExtension, bodyTemplateDescription);
LOGGER.infof("Creating daily email template for application: %s/%s", bundleName, appName);
AggregationEmailTemplate emailTemplate = new AggregationEmailTemplate();
emailTemplate.setApplication(app.get());
emailTemplate.setSubscriptionType(DAILY);
emailTemplate.setSubjectTemplate(subjectTemplate);
emailTemplate.setSubjectTemplateId(subjectTemplate.getId());
emailTemplate.setBodyTemplate(bodyTemplate);
emailTemplate.setBodyTemplateId(bodyTemplate.getId());
entityManager.persist(emailTemplate);
}
}
}
use of com.redhat.cloud.notifications.models.AggregationEmailTemplate in project notifications-backend by RedHatInsights.
the class EmailSubscriptionTypeProcessor method processAggregateEmailsByAggregationKey.
private void processAggregateEmailsByAggregationKey(EmailAggregationKey aggregationKey, LocalDateTime startTime, LocalDateTime endTime, EmailSubscriptionType emailSubscriptionType, boolean delete) {
final EmailTemplate emailTemplate = emailTemplateFactory.get(aggregationKey.getBundle(), aggregationKey.getApplication());
TemplateInstance subject;
TemplateInstance body;
if (useTemplatesFromDb) {
Optional<AggregationEmailTemplate> aggregationEmailTemplate = templateRepository.findAggregationEmailTemplate(aggregationKey.getBundle(), aggregationKey.getApplication(), emailSubscriptionType);
if (aggregationEmailTemplate.isEmpty()) {
if (delete) {
emailAggregationRepository.purgeOldAggregation(aggregationKey, endTime);
}
return;
} else {
String subjectData = aggregationEmailTemplate.get().getSubjectTemplate().getData();
subject = templateService.compileTemplate(subjectData, "subject");
String bodyData = aggregationEmailTemplate.get().getBodyTemplate().getData();
body = templateService.compileTemplate(bodyData, "body");
}
} else {
if (!emailTemplate.isEmailSubscriptionSupported(emailSubscriptionType)) {
if (delete) {
emailAggregationRepository.purgeOldAggregation(aggregationKey, endTime);
}
return;
}
subject = emailTemplate.getTitle(null, emailSubscriptionType);
body = emailTemplate.getBody(null, emailSubscriptionType);
}
if (subject == null || body == null) {
if (delete) {
emailAggregationRepository.purgeOldAggregation(aggregationKey, endTime);
}
return;
}
try {
for (Map.Entry<User, Map<String, Object>> aggregation : emailAggregator.getAggregated(aggregationKey, emailSubscriptionType, startTime, endTime).entrySet()) {
Context.ContextBuilder contextBuilder = new Context.ContextBuilder();
aggregation.getValue().forEach(contextBuilder::withAdditionalProperty);
Action action = new Action();
action.setContext(contextBuilder.build());
action.setEvents(List.of());
action.setAccountId(aggregationKey.getAccountId());
action.setApplication(aggregationKey.getApplication());
action.setBundle(aggregationKey.getBundle());
// We don't have a eventtype as this aggregates over multiple event types
action.setEventType(null);
action.setTimestamp(LocalDateTime.now(ZoneOffset.UTC));
Event event = new Event();
event.setId(UUID.randomUUID());
event.setAction(action);
emailSender.sendEmail(aggregation.getKey(), event, subject, body);
}
} finally {
if (delete) {
emailAggregationRepository.purgeOldAggregation(aggregationKey, endTime);
}
}
}
Aggregations