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;
}
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;
}
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");
}
}
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;
}
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"));
}
Aggregations