Untitled

 avatar
unknown
plain_text
17 days ago
6.3 kB
1
Indexable
package com.trendmicro.serapis.admin.apiservice.api.emailnotification;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import com.forescout.soarcore.model.EmailNotificationData;
import com.forescout.soarcore.model.EmailNotificationDetails;
import com.forescout.soarcore.model.EmailNotificationRecipients;
import com.forescout.soarcore.model.EmailNotificationResponse;
import com.forescout.soarcore.model.SoarEmailRequest;
import com.trendmicro.serapis.apiservice.api.inappnotifications.model.NotificationAttributes;
import com.trendmicro.serapis.apiservice.database.UserRepositoryService;
import com.trendmicro.serapis.apiservice.database.entities.UserDao;
import com.trendmicro.serapis.apiservice.util.NotificationPublisher;
import com.trendmicro.serapis.apiservice.util.UuidUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.trendmicro.serapis.apiservice.api.accounts.model.Account;
import com.trendmicro.serapis.apiservice.api.exceptions.EntityNotFoundException;
import com.trendmicro.serapis.apiservice.database.AccountRepositoryService;
import com.trendmicro.serapis.apiservice.database.InAppNotificationRepositoryService;

@Service
public class SoarEmailNotificationAdminApiService {

    private static final Logger log = LoggerFactory.getLogger(SoarEmailNotificationAdminApiService.class);

    private final InAppNotificationRepositoryService inAppNotificationRepositoryService;
    private final AccountRepositoryService accountRepositoryService;
    private final NotificationPublisher notificationPublisher;
    private final UserRepositoryService userRepositoryService;

    public SoarEmailNotificationAdminApiService(InAppNotificationRepositoryService inAppNotificationRepositoryService,
                                                AccountRepositoryService accountRepositoryService,
                                                NotificationPublisher notificationPublisher,
                                                UserRepositoryService userRepositoryService) throws EntityNotFoundException {
        this.inAppNotificationRepositoryService = inAppNotificationRepositoryService;
        this.accountRepositoryService = accountRepositoryService;
        this.notificationPublisher = notificationPublisher;
        this.userRepositoryService = userRepositoryService;
    }

    public EmailNotificationResponse sendEmailNotification(String accountId, SoarEmailRequest soarEmailRequest) throws EntityNotFoundException {

        List<Account> accounts = getAccountDetails(soarEmailRequest.recipients());
        Account account = accountRepositoryService.read(accountId);
        return sendEmailNotificationPrivileged(accountId, soarEmailRequest, accounts, account);
    }

    private EmailNotificationResponse sendEmailNotificationPrivileged(String accountId, SoarEmailRequest soarEmailRequest, List<Account> accounts, Account account) throws EntityNotFoundException {
        Set<UserDao> setOfUsers = getUserDaosPrivileged(accounts);
        Set<EmailNotificationRecipients> recipients = getRecipients(setOfUsers);
        EmailNotificationDetails emailNotificationDetails = new EmailNotificationDetails(soarEmailRequest.emailSubject(), soarEmailRequest.emailBody());
        notifyEmailNotificationsToRecipients(recipients,emailNotificationDetails,account.getName());

        EmailNotificationResponse response = new EmailNotificationResponse();
        response.setAccountId(accountId);

        response.setSubject(emailNotificationDetails.subject());
        response.setBody(emailNotificationDetails.body());

        log.info("Email notification sent successfully to {} recipients for account {}", recipients.size(), account.getId());
        return response;
    }

    private List<Account> getAccountDetails(List<String> accountIds) throws EntityNotFoundException {

        List<Account> accounts = new ArrayList<>();
        for (String accountId : accountIds) {
            Account account = accountRepositoryService.read(accountId);
            accounts.add(account);
        }

        return accounts;
    }
    public void validateRecipients(List<String> accountIds) throws EntityNotFoundException {

        for (String accountId : accountIds) {
            Account account = accountRepositoryService.read(accountId);
            if (account == null) {
                throw new EntityNotFoundException("Account ID not found: " + accountId);
            }
        }
    }

    private Set<UserDao> getUserDaosPrivileged(List<Account> accounts) {
        Set<Set<String>> userIds = accounts.stream().map(acc -> acc.getUserIds()).collect(Collectors.toSet());
        Set<List<UserDao>> users = userIds.stream().map(u -> {
            try {
                return userRepositoryService.readUserDaosByIds(u);
            } catch (EntityNotFoundException e) {
                throw new RuntimeException(e);
            }
        }).collect(Collectors.toSet());
        Set<UserDao> setOfUsers = new HashSet<>();
        for (List<UserDao> userList : users) {
            setOfUsers.addAll(userList);
        }
        return setOfUsers;
    }

    private Set<EmailNotificationRecipients> getRecipients(Set<UserDao> setOfUsers) {
        return setOfUsers.stream().map(usr -> new EmailNotificationRecipients(UuidUtils.toStringNoDashes(usr.getId()), usr.getEmail())).collect(Collectors.toSet());
    }

    private void notifyEmailNotificationsToRecipients(Set<EmailNotificationRecipients> recipients, EmailNotificationDetails notificationDetails, String accountName) {
        EmailNotificationData notificationData = new EmailNotificationData(notificationDetails, recipients);
        NotificationAttributes attributes = new NotificationAttributes(false, true);
        if (!recipients.isEmpty()) {
            notificationPublisher
                    .notifyEmailNotificationsToRecipients(notificationData, attributes);
        } else {
            log.warn(
                    "There are no users to notify while attempting to notify the email notifications to the users. Account Name- {}",
                    accountName);
        }
    }
}
Leave a Comment