Search in sources :

Example 11 with User

use of com.redhat.cloud.notifications.recipients.User in project notifications-backend by RedHatInsights.

the class RbacRecipientUsersProviderTest method shouldReturnNoUsersWhenGroupNotFound.

@Test
public void shouldReturnNoUsersWhenGroupNotFound() {
    UUID nonExistentGroup = UUID.randomUUID();
    mockNotFoundGroup(nonExistentGroup);
    List<User> users = rbacRecipientUsersProvider.getGroupUsers(accountId, orgId, false, nonExistentGroup);
    assertEquals(0, users.size());
}
Also used : User(com.redhat.cloud.notifications.recipients.User) UUID(java.util.UUID) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Example 12 with User

use of com.redhat.cloud.notifications.recipients.User in project notifications-backend by RedHatInsights.

the class RbacRecipientUsersProvider method getWithPagination.

private List<User> getWithPagination(Function<Integer, Page<RbacUser>> fetcher) {
    List<User> users = new ArrayList<>();
    int page = 0;
    Page<RbacUser> rbacUsers;
    do {
        rbacUsers = fetcher.apply(page++);
        for (RbacUser rbacUser : rbacUsers.getData()) {
            User user = new User();
            user.setUsername(rbacUser.getUsername());
            user.setEmail(rbacUser.getEmail());
            user.setAdmin(rbacUser.getOrgAdmin());
            user.setActive(rbacUser.getActive());
            user.setFirstName(rbacUser.getFirstName());
            user.setLastName(rbacUser.getLastName());
            users.add(user);
        }
    } while (rbacUsers.getData().size() == rbacElementsPerPage);
    return users;
}
Also used : User(com.redhat.cloud.notifications.recipients.User) ArrayList(java.util.ArrayList)

Example 13 with User

use of com.redhat.cloud.notifications.recipients.User in project notifications-backend by RedHatInsights.

the class EmailSubscriptionTypeProcessor method processAggregateEmailsByAggregationKey.

private void processAggregateEmailsByAggregationKey(EmailAggregationKey aggregationKey, LocalDateTime startTime, LocalDateTime endTime, EmailSubscriptionType emailSubscriptionType, boolean delete) {
    final EmailTemplate emailTemplate = emailTemplateFactory.get(aggregationKey.getBundle(), aggregationKey.getApplication());
    TemplateInstance subject;
    TemplateInstance body;
    if (useTemplatesFromDb) {
        Optional<AggregationEmailTemplate> aggregationEmailTemplate = templateRepository.findAggregationEmailTemplate(aggregationKey.getBundle(), aggregationKey.getApplication(), emailSubscriptionType);
        if (aggregationEmailTemplate.isEmpty()) {
            if (delete) {
                emailAggregationRepository.purgeOldAggregation(aggregationKey, endTime);
            }
            return;
        } else {
            String subjectData = aggregationEmailTemplate.get().getSubjectTemplate().getData();
            subject = templateService.compileTemplate(subjectData, "subject");
            String bodyData = aggregationEmailTemplate.get().getBodyTemplate().getData();
            body = templateService.compileTemplate(bodyData, "body");
        }
    } else {
        if (!emailTemplate.isEmailSubscriptionSupported(emailSubscriptionType)) {
            if (delete) {
                emailAggregationRepository.purgeOldAggregation(aggregationKey, endTime);
            }
            return;
        }
        subject = emailTemplate.getTitle(null, emailSubscriptionType);
        body = emailTemplate.getBody(null, emailSubscriptionType);
    }
    if (subject == null || body == null) {
        if (delete) {
            emailAggregationRepository.purgeOldAggregation(aggregationKey, endTime);
        }
        return;
    }
    try {
        for (Map.Entry<User, Map<String, Object>> aggregation : emailAggregator.getAggregated(aggregationKey, emailSubscriptionType, startTime, endTime).entrySet()) {
            Context.ContextBuilder contextBuilder = new Context.ContextBuilder();
            aggregation.getValue().forEach(contextBuilder::withAdditionalProperty);
            Action action = new Action();
            action.setContext(contextBuilder.build());
            action.setEvents(List.of());
            action.setAccountId(aggregationKey.getAccountId());
            action.setApplication(aggregationKey.getApplication());
            action.setBundle(aggregationKey.getBundle());
            // We don't have a eventtype as this aggregates over multiple event types
            action.setEventType(null);
            action.setTimestamp(LocalDateTime.now(ZoneOffset.UTC));
            Event event = new Event();
            event.setId(UUID.randomUUID());
            event.setAction(action);
            emailSender.sendEmail(aggregation.getKey(), event, subject, body);
        }
    } finally {
        if (delete) {
            emailAggregationRepository.purgeOldAggregation(aggregationKey, endTime);
        }
    }
}
Also used : Context(com.redhat.cloud.notifications.ingress.Context) Action(com.redhat.cloud.notifications.ingress.Action) User(com.redhat.cloud.notifications.recipients.User) AggregationEmailTemplate(com.redhat.cloud.notifications.models.AggregationEmailTemplate) TemplateInstance(io.quarkus.qute.TemplateInstance) EmailTemplate(com.redhat.cloud.notifications.templates.EmailTemplate) InstantEmailTemplate(com.redhat.cloud.notifications.models.InstantEmailTemplate) AggregationEmailTemplate(com.redhat.cloud.notifications.models.AggregationEmailTemplate) Event(com.redhat.cloud.notifications.models.Event) Map(java.util.Map)

Aggregations

User (com.redhat.cloud.notifications.recipients.User)13 Action (com.redhat.cloud.notifications.ingress.Action)3 ITUserResponse (com.redhat.cloud.notifications.recipients.itservice.pojo.response.ITUserResponse)3 TemplateInstance (io.quarkus.qute.TemplateInstance)3 QuarkusTest (io.quarkus.test.junit.QuarkusTest)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 Test (org.junit.jupiter.api.Test)3 EmailAggregationRepository (com.redhat.cloud.notifications.db.repositories.EmailAggregationRepository)2 EmailSubscriptionRepository (com.redhat.cloud.notifications.db.repositories.EmailSubscriptionRepository)2 Context (com.redhat.cloud.notifications.ingress.Context)2 AggregationEmailTemplate (com.redhat.cloud.notifications.models.AggregationEmailTemplate)2 EmailAggregation (com.redhat.cloud.notifications.models.EmailAggregation)2 EmailAggregationKey (com.redhat.cloud.notifications.models.EmailAggregationKey)2 EmailSubscriptionType (com.redhat.cloud.notifications.models.EmailSubscriptionType)2 Endpoint (com.redhat.cloud.notifications.models.Endpoint)2 Event (com.redhat.cloud.notifications.models.Event)2 InstantEmailTemplate (com.redhat.cloud.notifications.models.InstantEmailTemplate)2 RecipientResolver (com.redhat.cloud.notifications.recipients.RecipientResolver)2 ITUserRequest (com.redhat.cloud.notifications.recipients.itservice.pojo.request.ITUserRequest)2