Skip to content

Commit

Permalink
Do not fetch templates
Browse files Browse the repository at this point in the history
  • Loading branch information
g-duval committed Dec 18, 2024
1 parent 8e4f2e6 commit d2cf6ad
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ void updateSubscription(String orgId, String username, UUID eventTypeId, Subscri
private void checkIfSubscriptionTypeIsSupportedForCurrentEventType(UUID eventTypeId, SubscriptionType subscriptionType) {
switch (subscriptionType) {
case DAILY:
templateRepository.findAggregationEmailTemplatesByEventType(eventTypeId);
templateRepository.checkIfExistAggregationEmailTemplatesByEventType(eventTypeId);
break;
case INSTANT:
if (!backendConfig.isDefaultTemplateEnabled()) {
templateRepository.findInstantEmailTemplateByEventType(eventTypeId);
templateRepository.checkIfExistInstantEmailTemplateByEventType(eventTypeId);
}
break;
case DRAWER:
templateRepository.findDrawerTemplateByEventType(eventTypeId);
templateRepository.checkIfExistDrawerTemplateByEventType(eventTypeId);
break;
default:
Log.infof("Subscription type %s not checked", subscriptionType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.redhat.cloud.notifications.models.Application;
import com.redhat.cloud.notifications.models.EventType;
import com.redhat.cloud.notifications.models.InstantEmailTemplate;
import com.redhat.cloud.notifications.models.IntegrationTemplate;
import com.redhat.cloud.notifications.models.SubscriptionType;
import com.redhat.cloud.notifications.models.Template;
import jakarta.enterprise.context.ApplicationScoped;
Expand Down Expand Up @@ -143,28 +142,36 @@ public InstantEmailTemplate findInstantEmailTemplateByEventType(UUID eventTypeId

return instantEmailTemplate;
} catch (NoResultException e) {
throw new NotFoundException("Instant email template not found");
}
}

public void checkIfExistInstantEmailTemplateByEventType(UUID eventTypeId) throws NotFoundException {
String hql = "select count(t) FROM InstantEmailTemplate t JOIN t.eventType et WHERE et.id = :eventTypeId";
Long count = entityManager.createQuery(hql, Long.class)
.setParameter("eventTypeId", eventTypeId)
.getSingleResult();
if (count == 0) {
throw new NotFoundException(String.format("Event type '%s' doesn't support instant email subscription", eventTypeId));
}
}

public AggregationEmailTemplate findAggregationEmailTemplatesByEventType(UUID eventTypeId) {
String hql = "FROM AggregationEmailTemplate t JOIN FETCH t.application a JOIN FETCH a.eventTypes ev WHERE ev.id = :eventTypeId";
try {
return entityManager.createQuery(hql, AggregationEmailTemplate.class)
.setParameter("eventTypeId", eventTypeId)
.getSingleResult();
} catch (NoResultException e) {
public void checkIfExistAggregationEmailTemplatesByEventType(UUID eventTypeId) throws NotFoundException {
String hql = "select count(t) FROM AggregationEmailTemplate t JOIN t.application a JOIN a.eventTypes ev WHERE ev.id = :eventTypeId";
Long count = entityManager.createQuery(hql, Long.class)
.setParameter("eventTypeId", eventTypeId)
.getSingleResult();
if (count == 0) {
throw new NotFoundException(String.format("Event type '%s' doesn't support daily email subscription", eventTypeId));
}
}

public IntegrationTemplate findDrawerTemplateByEventType(UUID eventTypeId) {
try {
String hql = "FROM IntegrationTemplate t WHERE t.integrationType = 'drawer' and t.eventType.id = :eventTypeId";
return entityManager.createQuery(hql, IntegrationTemplate.class)
.setParameter("eventTypeId", eventTypeId)
.getSingleResult();
} catch (NoResultException e) {
public void checkIfExistDrawerTemplateByEventType(UUID eventTypeId) throws NotFoundException {
String hql = "select count(t) FROM IntegrationTemplate t WHERE t.integrationType = 'drawer' and t.eventType.id = :eventTypeId";
Long count = entityManager.createQuery(hql, Long.class)
.setParameter("eventTypeId", eventTypeId)
.getSingleResult();
if (count == 0) {
throw new NotFoundException(String.format("Event type '%s' doesn't support Drawer subscription", eventTypeId));
}
}
Expand Down

0 comments on commit d2cf6ad

Please sign in to comment.