Search in sources :

Example 1 with InstantEmailTemplate

use of com.redhat.cloud.notifications.models.InstantEmailTemplate 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());
}
Also used : Arrays(java.util.Arrays) Endpoint(com.redhat.cloud.notifications.models.Endpoint) BaseTransformer(com.redhat.cloud.notifications.transformers.BaseTransformer) NotificationHistory(com.redhat.cloud.notifications.models.NotificationHistory) RecipientSettings(com.redhat.cloud.notifications.recipients.RecipientSettings) EmailTemplateFactory(com.redhat.cloud.notifications.templates.EmailTemplateFactory) EmailTemplate(com.redhat.cloud.notifications.templates.EmailTemplate) Map(java.util.Map) InstantEmailTemplate(com.redhat.cloud.notifications.models.InstantEmailTemplate) Event(com.redhat.cloud.notifications.models.Event) JsonObject(io.vertx.core.json.JsonObject) User(com.redhat.cloud.notifications.recipients.User) ZoneOffset(java.time.ZoneOffset) Context(com.redhat.cloud.notifications.ingress.Context) Counter(io.micrometer.core.instrument.Counter) TemplateInstance(io.quarkus.qute.TemplateInstance) ActionRecipientSettings(com.redhat.cloud.notifications.recipients.request.ActionRecipientSettings) Set(java.util.Set) UUID(java.util.UUID) EmailAggregation(com.redhat.cloud.notifications.models.EmailAggregation) Collectors(java.util.stream.Collectors) List(java.util.List) Stream(java.util.stream.Stream) StatelessSessionFactory(com.redhat.cloud.notifications.db.StatelessSessionFactory) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) ApplicationScoped(javax.enterprise.context.ApplicationScoped) AggregationCommand(com.redhat.cloud.notifications.models.AggregationCommand) Incoming(org.eclipse.microprofile.reactive.messaging.Incoming) AggregationEmailTemplate(com.redhat.cloud.notifications.models.AggregationEmailTemplate) Logger(org.jboss.logging.Logger) LocalDateTime(java.time.LocalDateTime) USE_TEMPLATES_FROM_DB_KEY(com.redhat.cloud.notifications.templates.TemplateService.USE_TEMPLATES_FROM_DB_KEY) TemplateService(com.redhat.cloud.notifications.templates.TemplateService) Inject(javax.inject.Inject) EmailAggregationRepository(com.redhat.cloud.notifications.db.repositories.EmailAggregationRepository) EmailSubscriptionRepository(com.redhat.cloud.notifications.db.repositories.EmailSubscriptionRepository) EmailSubscriptionType(com.redhat.cloud.notifications.models.EmailSubscriptionType) Blocking(io.smallrye.reactive.messaging.annotations.Blocking) Acknowledgment(org.eclipse.microprofile.reactive.messaging.Acknowledgment) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) EmailAggregationKey(com.redhat.cloud.notifications.models.EmailAggregationKey) EndpointTypeProcessor(com.redhat.cloud.notifications.processors.EndpointTypeProcessor) MeterRegistry(io.micrometer.core.instrument.MeterRegistry) TemplateRepository(com.redhat.cloud.notifications.db.repositories.TemplateRepository) RecipientResolver(com.redhat.cloud.notifications.recipients.RecipientResolver) ConfigProperty(org.eclipse.microprofile.config.inject.ConfigProperty) Action(com.redhat.cloud.notifications.ingress.Action) Collections(java.util.Collections) EndpointRecipientSettings(com.redhat.cloud.notifications.recipients.request.EndpointRecipientSettings) Action(com.redhat.cloud.notifications.ingress.Action) EndpointRecipientSettings(com.redhat.cloud.notifications.recipients.request.EndpointRecipientSettings) RecipientSettings(com.redhat.cloud.notifications.recipients.RecipientSettings) ActionRecipientSettings(com.redhat.cloud.notifications.recipients.request.ActionRecipientSettings) EndpointRecipientSettings(com.redhat.cloud.notifications.recipients.request.EndpointRecipientSettings) EmailSubscriptionType(com.redhat.cloud.notifications.models.EmailSubscriptionType) Optional(java.util.Optional) InstantEmailTemplate(com.redhat.cloud.notifications.models.InstantEmailTemplate) TemplateInstance(io.quarkus.qute.TemplateInstance)

Example 2 with InstantEmailTemplate

use of com.redhat.cloud.notifications.models.InstantEmailTemplate in project notifications-backend by RedHatInsights.

the class TemplateRepository method findAllInstantEmailTemplates.

public List<InstantEmailTemplate> findAllInstantEmailTemplates(UUID applicationId) {
    String hql = "FROM InstantEmailTemplate t LEFT JOIN FETCH t.eventType";
    if (applicationId != null) {
        hql += " WHERE t.eventType.application.id = :applicationId";
    }
    TypedQuery<InstantEmailTemplate> query = entityManager.createQuery(hql, InstantEmailTemplate.class);
    if (applicationId != null) {
        query.setParameter("applicationId", applicationId);
    }
    List<InstantEmailTemplate> instantEmailTemplates = query.getResultList();
    for (InstantEmailTemplate instantEmailTemplate : instantEmailTemplates) {
        if (instantEmailTemplate.getEventType() != null) {
            // We need the event types in the REST response, but not their parent application.
            instantEmailTemplate.getEventType().filterOutApplication();
        }
        // The full templates aren't needed in the REST response.
        instantEmailTemplate.filterOutTemplates();
    }
    return instantEmailTemplates;
}
Also used : InstantEmailTemplate(com.redhat.cloud.notifications.models.InstantEmailTemplate)

Example 3 with InstantEmailTemplate

use of com.redhat.cloud.notifications.models.InstantEmailTemplate in project notifications-backend by RedHatInsights.

the class TemplateRepository method updateInstantEmailTemplate.

@Transactional
public boolean updateInstantEmailTemplate(UUID id, InstantEmailTemplate template) {
    String hql = "UPDATE InstantEmailTemplate SET eventType = :eventType, subjectTemplate = :subjectTemplate, " + "bodyTemplate = :bodyTemplate WHERE id = :id";
    EventType eventType;
    if (template.getEventTypeId() == null) {
        eventType = null;
    } else {
        eventType = findEventType(template.getEventTypeId());
    }
    Template subjectTemplate = findTemplate(template.getSubjectTemplateId(), SUBJECT_NOT_FOUND);
    Template bodyTemplate = findTemplate(template.getBodyTemplateId(), BODY_NOT_FOUND);
    int rowCount = entityManager.createQuery(hql).setParameter("eventType", eventType).setParameter("subjectTemplate", subjectTemplate).setParameter("bodyTemplate", bodyTemplate).setParameter("id", id).executeUpdate();
    return rowCount > 0;
}
Also used : EventType(com.redhat.cloud.notifications.models.EventType) 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 4 with InstantEmailTemplate

use of com.redhat.cloud.notifications.models.InstantEmailTemplate in project notifications-backend by RedHatInsights.

the class TemplateRepository method createInstantEmailTemplate.

@Transactional
public InstantEmailTemplate createInstantEmailTemplate(InstantEmailTemplate template) {
    Template subjectTemplate = findTemplate(template.getSubjectTemplateId(), SUBJECT_NOT_FOUND);
    Template bodyTemplate = findTemplate(template.getBodyTemplateId(), BODY_NOT_FOUND);
    template.setSubjectTemplate(subjectTemplate);
    template.setBodyTemplate(bodyTemplate);
    if (template.getEventTypeId() != null) {
        EventType eventType = findEventType(template.getEventTypeId());
        template.setEventType(eventType);
    }
    entityManager.persist(template);
    // The full event type isn't needed in the REST response.
    template.filterOutEventType();
    // The full templates aren't needed in the REST response.
    template.filterOutTemplates();
    return template;
}
Also used : EventType(com.redhat.cloud.notifications.models.EventType) 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 5 with InstantEmailTemplate

use of com.redhat.cloud.notifications.models.InstantEmailTemplate in project notifications-backend by RedHatInsights.

the class TemplateRepository method findInstantEmailTemplateById.

public InstantEmailTemplate findInstantEmailTemplateById(UUID id) {
    String hql = "FROM InstantEmailTemplate t JOIN FETCH t.subjectTemplate JOIN FETCH t.bodyTemplate " + "LEFT JOIN FETCH t.eventType WHERE t.id = :id";
    try {
        InstantEmailTemplate template = entityManager.createQuery(hql, InstantEmailTemplate.class).setParameter("id", id).getSingleResult();
        // The full event type isn't needed in the REST response.
        template.filterOutEventType();
        return template;
    } catch (NoResultException e) {
        throw new NotFoundException("Instant email template not found");
    }
}
Also used : NotFoundException(javax.ws.rs.NotFoundException) NoResultException(javax.persistence.NoResultException) InstantEmailTemplate(com.redhat.cloud.notifications.models.InstantEmailTemplate)

Aggregations

InstantEmailTemplate (com.redhat.cloud.notifications.models.InstantEmailTemplate)11 AggregationEmailTemplate (com.redhat.cloud.notifications.models.AggregationEmailTemplate)6 Template (com.redhat.cloud.notifications.models.Template)5 EventType (com.redhat.cloud.notifications.models.EventType)4 Transactional (javax.transaction.Transactional)3 CrudTestHelpers.createInstantEmailTemplate (com.redhat.cloud.notifications.CrudTestHelpers.createInstantEmailTemplate)2 CrudTestHelpers.deleteInstantEmailTemplate (com.redhat.cloud.notifications.CrudTestHelpers.deleteInstantEmailTemplate)2 CrudTestHelpers.updateInstantEmailTemplate (com.redhat.cloud.notifications.CrudTestHelpers.updateInstantEmailTemplate)2 JsonObject (io.vertx.core.json.JsonObject)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 CrudTestHelpers.createAggregationEmailTemplate (com.redhat.cloud.notifications.CrudTestHelpers.createAggregationEmailTemplate)1 CrudTestHelpers.createTemplate (com.redhat.cloud.notifications.CrudTestHelpers.createTemplate)1 CrudTestHelpers.deleteAggregationEmailTemplate (com.redhat.cloud.notifications.CrudTestHelpers.deleteAggregationEmailTemplate)1 CrudTestHelpers.deleteTemplate (com.redhat.cloud.notifications.CrudTestHelpers.deleteTemplate)1 CrudTestHelpers.updateAggregationEmailTemplate (com.redhat.cloud.notifications.CrudTestHelpers.updateAggregationEmailTemplate)1 CrudTestHelpers.updateTemplate (com.redhat.cloud.notifications.CrudTestHelpers.updateTemplate)1 DbIsolatedTest (com.redhat.cloud.notifications.db.DbIsolatedTest)1 StatelessSessionFactory (com.redhat.cloud.notifications.db.StatelessSessionFactory)1 EmailAggregationRepository (com.redhat.cloud.notifications.db.repositories.EmailAggregationRepository)1