Search in sources :

Example 6 with AggregationEmailTemplate

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());
}
Also used : AggregationEmailTemplate(com.redhat.cloud.notifications.models.AggregationEmailTemplate)

Example 7 with AggregationEmailTemplate

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;
}
Also used : AggregationEmailTemplate(com.redhat.cloud.notifications.models.AggregationEmailTemplate) Application(com.redhat.cloud.notifications.models.Application) AggregationEmailTemplate(com.redhat.cloud.notifications.models.AggregationEmailTemplate) Template(com.redhat.cloud.notifications.models.Template) InstantEmailTemplate(com.redhat.cloud.notifications.models.InstantEmailTemplate) Transactional(javax.transaction.Transactional)

Example 8 with 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;
}
Also used : AggregationEmailTemplate(com.redhat.cloud.notifications.models.AggregationEmailTemplate)

Example 9 with AggregationEmailTemplate

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);
        }
    }
}
Also used : AggregationEmailTemplate(com.redhat.cloud.notifications.models.AggregationEmailTemplate) Application(com.redhat.cloud.notifications.models.Application) AggregationEmailTemplate(com.redhat.cloud.notifications.models.AggregationEmailTemplate) Template(com.redhat.cloud.notifications.models.Template) InstantEmailTemplate(com.redhat.cloud.notifications.models.InstantEmailTemplate)

Example 10 with AggregationEmailTemplate

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);
        }
    }
}
Also used : Context(com.redhat.cloud.notifications.ingress.Context) Action(com.redhat.cloud.notifications.ingress.Action) User(com.redhat.cloud.notifications.recipients.User) AggregationEmailTemplate(com.redhat.cloud.notifications.models.AggregationEmailTemplate) TemplateInstance(io.quarkus.qute.TemplateInstance) EmailTemplate(com.redhat.cloud.notifications.templates.EmailTemplate) InstantEmailTemplate(com.redhat.cloud.notifications.models.InstantEmailTemplate) AggregationEmailTemplate(com.redhat.cloud.notifications.models.AggregationEmailTemplate) Event(com.redhat.cloud.notifications.models.Event) Map(java.util.Map)

Aggregations

AggregationEmailTemplate (com.redhat.cloud.notifications.models.AggregationEmailTemplate)11 InstantEmailTemplate (com.redhat.cloud.notifications.models.InstantEmailTemplate)6 Template (com.redhat.cloud.notifications.models.Template)5 Application (com.redhat.cloud.notifications.models.Application)4 Transactional (javax.transaction.Transactional)3 CrudTestHelpers.createAggregationEmailTemplate (com.redhat.cloud.notifications.CrudTestHelpers.createAggregationEmailTemplate)2 CrudTestHelpers.deleteAggregationEmailTemplate (com.redhat.cloud.notifications.CrudTestHelpers.deleteAggregationEmailTemplate)2 CrudTestHelpers.updateAggregationEmailTemplate (com.redhat.cloud.notifications.CrudTestHelpers.updateAggregationEmailTemplate)2 CrudTestHelpers.createInstantEmailTemplate (com.redhat.cloud.notifications.CrudTestHelpers.createInstantEmailTemplate)1 CrudTestHelpers.createTemplate (com.redhat.cloud.notifications.CrudTestHelpers.createTemplate)1 CrudTestHelpers.deleteInstantEmailTemplate (com.redhat.cloud.notifications.CrudTestHelpers.deleteInstantEmailTemplate)1 CrudTestHelpers.deleteTemplate (com.redhat.cloud.notifications.CrudTestHelpers.deleteTemplate)1 CrudTestHelpers.updateInstantEmailTemplate (com.redhat.cloud.notifications.CrudTestHelpers.updateInstantEmailTemplate)1 CrudTestHelpers.updateTemplate (com.redhat.cloud.notifications.CrudTestHelpers.updateTemplate)1 DbIsolatedTest (com.redhat.cloud.notifications.db.DbIsolatedTest)1 Action (com.redhat.cloud.notifications.ingress.Action)1 Context (com.redhat.cloud.notifications.ingress.Context)1 Event (com.redhat.cloud.notifications.models.Event)1 User (com.redhat.cloud.notifications.recipients.User)1 EmailTemplate (com.redhat.cloud.notifications.templates.EmailTemplate)1