Search in sources :

Example 1 with AggregationEmailTemplate

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

the class TemplateRepository method createAggregationEmailTemplate.

@Transactional
public AggregationEmailTemplate createAggregationEmailTemplate(AggregationEmailTemplate template) {
    Template subjectTemplate = findTemplate(template.getSubjectTemplateId(), SUBJECT_NOT_FOUND);
    Template bodyTemplate = findTemplate(template.getBodyTemplateId(), BODY_NOT_FOUND);
    template.setSubscriptionType(template.getSubscriptionType());
    template.setSubjectTemplate(subjectTemplate);
    template.setBodyTemplate(bodyTemplate);
    if (template.getApplicationId() != null) {
        Application app = findApplication(template.getApplicationId());
        template.setApplication(app);
    }
    entityManager.persist(template);
    // The full application isn't needed in the REST response.
    template.filterOutApplication();
    // The full templates aren't needed in the REST response.
    template.filterOutTemplates();
    return template;
}
Also used : 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 2 with AggregationEmailTemplate

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

the class TemplateRepository method findAllAggregationEmailTemplates.

public List<AggregationEmailTemplate> findAllAggregationEmailTemplates() {
    String hql = "FROM AggregationEmailTemplate t LEFT JOIN FETCH t.application";
    List<AggregationEmailTemplate> aggregationEmailTemplates = entityManager.createQuery(hql, AggregationEmailTemplate.class).getResultList();
    for (AggregationEmailTemplate aggregationEmailTemplate : aggregationEmailTemplates) {
        // The full templates aren't needed in the REST response.
        aggregationEmailTemplate.filterOutTemplates();
    }
    return aggregationEmailTemplates;
}
Also used : AggregationEmailTemplate(com.redhat.cloud.notifications.models.AggregationEmailTemplate)

Example 3 with AggregationEmailTemplate

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

the class TemplateRepository method findAggregationEmailTemplateById.

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

Example 4 with AggregationEmailTemplate

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

the class TemplateRepository method updateAggregationEmailTemplate.

@Transactional
public boolean updateAggregationEmailTemplate(UUID id, AggregationEmailTemplate template) {
    String hql = "UPDATE AggregationEmailTemplate SET application = :app, subjectTemplate = :subjectTemplate, " + "subscriptionType = :subscriptionType, bodyTemplate = :bodyTemplate WHERE id = :id";
    Application app;
    if (template.getApplicationId() == null) {
        app = null;
    } else {
        app = findApplication(template.getApplicationId());
    }
    Template subjectTemplate = findTemplate(template.getSubjectTemplateId(), SUBJECT_NOT_FOUND);
    Template bodyTemplate = findTemplate(template.getBodyTemplateId(), BODY_NOT_FOUND);
    int rowCount = entityManager.createQuery(hql).setParameter("app", app).setParameter("subscriptionType", template.getSubscriptionType()).setParameter("subjectTemplate", subjectTemplate).setParameter("bodyTemplate", bodyTemplate).setParameter("id", id).executeUpdate();
    return rowCount > 0;
}
Also used : 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 5 with AggregationEmailTemplate

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

the class TemplateResourceTest method testAllAggregationEmailTemplateEndpoints.

@Test
void testAllAggregationEmailTemplateEndpoints() {
    Header adminIdentity = TestHelpers.createTurnpikeIdentityHeader("user", adminRole);
    // First, we need a bundle and an app in the DB.
    String bundleId = createBundle(adminIdentity, "bundle-name", "Bundle", OK).get();
    String appId = createApp(adminIdentity, bundleId, "app-name", "App", null, OK).get();
    // We also need templates that will be linked with the aggregation email template tested below.
    Template subjectTemplate = buildTemplate("subject-template-name", "template-data");
    JsonObject subjectJsonTemplate = createTemplate(adminIdentity, subjectTemplate, 200).get();
    Template bodyTemplate = buildTemplate("body-template-name", "template-data");
    JsonObject bodyJsonTemplate = createTemplate(adminIdentity, bodyTemplate, 200).get();
    // Before we start, the DB shouldn't contain any aggregation email template.
    assertTrue(getAllAggregationEmailTemplates(adminIdentity).isEmpty());
    // First, we'll create, retrieve and check an aggregation email template that is NOT linked to an app.
    AggregationEmailTemplate emailTemplate = buildAggregationEmailTemplate(null, subjectJsonTemplate.getString("id"), bodyJsonTemplate.getString("id"));
    JsonObject jsonEmailTemplate = createAggregationEmailTemplate(adminIdentity, emailTemplate, 200).get();
    // Then, we'll do the same with another aggregation email template that is linked to an app.
    AggregationEmailTemplate emailTemplateWithApp = buildAggregationEmailTemplate(appId, subjectJsonTemplate.getString("id"), bodyJsonTemplate.getString("id"));
    JsonObject jsonEmailTemplateWithApp = createAggregationEmailTemplate(adminIdentity, emailTemplateWithApp, 200).get();
    // Now, we'll delete the second aggregation email template and check that it no longer exists with the following line.
    deleteAggregationEmailTemplate(adminIdentity, jsonEmailTemplateWithApp.getString("id"));
    // At this point, the DB should contain one aggregation email template: the one that wasn't linked to the app.
    JsonArray jsonEmailTemplates = getAllAggregationEmailTemplates(adminIdentity);
    assertEquals(1, jsonEmailTemplates.size());
    jsonEmailTemplates.getJsonObject(0).mapTo(AggregationEmailTemplate.class);
    assertEquals(jsonEmailTemplate.getString("id"), jsonEmailTemplates.getJsonObject(0).getString("id"));
    // Retrieving the aggregation email templates from the app ID should return an empty list.
    jsonEmailTemplates = getAggregationEmailTemplatesByApp(adminIdentity, appId);
    assertTrue(jsonEmailTemplates.isEmpty());
    // Let's update the aggregation email template and check that the new fields values are correctly persisted.
    Template newSubjectTemplate = buildTemplate("new-subject-template-name", "template-data");
    JsonObject newSubjectJsonTemplate = createTemplate(adminIdentity, newSubjectTemplate, 200).get();
    Template newBodyTemplate = buildTemplate("new-body-template-name", "template-data");
    JsonObject newBodyJsonTemplate = createTemplate(adminIdentity, newBodyTemplate, 200).get();
    emailTemplate.setApplicationId(UUID.fromString(appId));
    emailTemplate.setSubjectTemplateId(UUID.fromString(newSubjectJsonTemplate.getString("id")));
    emailTemplate.setBodyTemplateId(UUID.fromString(newBodyJsonTemplate.getString("id")));
    updateAggregationEmailTemplate(adminIdentity, jsonEmailTemplate.getString("id"), emailTemplate);
    // The aggregation email template is now linked to an app.
    // It should be returned if we retrieve all aggregation email templates from the app ID.
    jsonEmailTemplates = getAggregationEmailTemplatesByApp(adminIdentity, appId);
    assertEquals(1, jsonEmailTemplates.size());
    jsonEmailTemplates.getJsonObject(0).mapTo(AggregationEmailTemplate.class);
    assertEquals(jsonEmailTemplate.getString("id"), jsonEmailTemplates.getJsonObject(0).getString("id"));
}
Also used : JsonArray(io.vertx.core.json.JsonArray) Header(io.restassured.http.Header) JsonObject(io.vertx.core.json.JsonObject) AggregationEmailTemplate(com.redhat.cloud.notifications.models.AggregationEmailTemplate) CrudTestHelpers.deleteAggregationEmailTemplate(com.redhat.cloud.notifications.CrudTestHelpers.deleteAggregationEmailTemplate) CrudTestHelpers.createAggregationEmailTemplate(com.redhat.cloud.notifications.CrudTestHelpers.createAggregationEmailTemplate) CrudTestHelpers.updateAggregationEmailTemplate(com.redhat.cloud.notifications.CrudTestHelpers.updateAggregationEmailTemplate) CrudTestHelpers.deleteInstantEmailTemplate(com.redhat.cloud.notifications.CrudTestHelpers.deleteInstantEmailTemplate) AggregationEmailTemplate(com.redhat.cloud.notifications.models.AggregationEmailTemplate) CrudTestHelpers.deleteAggregationEmailTemplate(com.redhat.cloud.notifications.CrudTestHelpers.deleteAggregationEmailTemplate) CrudTestHelpers.updateTemplate(com.redhat.cloud.notifications.CrudTestHelpers.updateTemplate) CrudTestHelpers.updateInstantEmailTemplate(com.redhat.cloud.notifications.CrudTestHelpers.updateInstantEmailTemplate) Template(com.redhat.cloud.notifications.models.Template) InstantEmailTemplate(com.redhat.cloud.notifications.models.InstantEmailTemplate) CrudTestHelpers.deleteTemplate(com.redhat.cloud.notifications.CrudTestHelpers.deleteTemplate) CrudTestHelpers.createAggregationEmailTemplate(com.redhat.cloud.notifications.CrudTestHelpers.createAggregationEmailTemplate) CrudTestHelpers.updateAggregationEmailTemplate(com.redhat.cloud.notifications.CrudTestHelpers.updateAggregationEmailTemplate) CrudTestHelpers.createInstantEmailTemplate(com.redhat.cloud.notifications.CrudTestHelpers.createInstantEmailTemplate) CrudTestHelpers.createTemplate(com.redhat.cloud.notifications.CrudTestHelpers.createTemplate) QuarkusTest(io.quarkus.test.junit.QuarkusTest) DbIsolatedTest(com.redhat.cloud.notifications.db.DbIsolatedTest) Test(org.junit.jupiter.api.Test)

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