use of com.redhat.cloud.notifications.models.EndpointType.EMAIL_SUBSCRIPTION in project notifications-backend by RedHatInsights.
the class EndpointRepository method getOrCreateDefaultEmailSubscription.
/**
* The purpose of this method is to find or create an EMAIL_SUBSCRIPTION endpoint with empty properties. This
* endpoint is used to aggregate and store in the DB the email actions outcome, which will be used later by the
* event log. The recipients of the current email action have already been resolved before this step, possibly from
* multiple endpoints and recipients settings. The properties created below have no impact on the resolution of the
* action recipients.
*/
public Endpoint getOrCreateDefaultEmailSubscription(String accountId) {
String query = "FROM Endpoint WHERE accountId = :accountId AND compositeType.type = :endpointType";
List<Endpoint> emailEndpoints = statelessSessionFactory.getCurrentSession().createQuery(query, Endpoint.class).setParameter("accountId", accountId).setParameter("endpointType", EMAIL_SUBSCRIPTION).getResultList();
loadProperties(emailEndpoints);
EmailSubscriptionProperties properties = new EmailSubscriptionProperties();
Optional<Endpoint> endpointOptional = emailEndpoints.stream().filter(endpoint -> properties.hasSameProperties(endpoint.getProperties(EmailSubscriptionProperties.class))).findFirst();
if (endpointOptional.isPresent()) {
return endpointOptional.get();
}
Endpoint endpoint = new Endpoint();
endpoint.setProperties(properties);
endpoint.setAccountId(accountId);
endpoint.setEnabled(true);
endpoint.setDescription("System email endpoint");
endpoint.setName("Email endpoint");
endpoint.setType(EMAIL_SUBSCRIPTION);
endpoint.prePersist();
properties.setEndpoint(endpoint);
statelessSessionFactory.getCurrentSession().insert(endpoint);
statelessSessionFactory.getCurrentSession().insert(endpoint.getProperties());
return endpoint;
}
Aggregations