From d46255805399b4d854b92ffcd9473d2e7477c5a3 Mon Sep 17 00:00:00 2001 From: Heshan Andrews <45477334+Gravewalker666@users.noreply.github.com> Date: Tue, 10 Aug 2021 17:23:45 +0530 Subject: [PATCH] Remove duplicate mentee profiles when sending emails (#202) --- .../sefglobal/scholarx/util/ProgramUtil.java | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/sefglobal/scholarx/util/ProgramUtil.java b/src/main/java/org/sefglobal/scholarx/util/ProgramUtil.java index 00d5f8f9..7ef64adb 100644 --- a/src/main/java/org/sefglobal/scholarx/util/ProgramUtil.java +++ b/src/main/java/org/sefglobal/scholarx/util/ProgramUtil.java @@ -1,6 +1,5 @@ package org.sefglobal.scholarx.util; -import org.apache.commons.lang.StringUtils; import org.sefglobal.scholarx.model.Mentee; import org.sefglobal.scholarx.model.Mentor; import org.sefglobal.scholarx.model.Program; @@ -9,11 +8,11 @@ import org.sefglobal.scholarx.service.EmailService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import org.springframework.util.StringUtils; import javax.mail.MessagingException; import java.io.IOException; -import java.util.List; -import java.util.Optional; +import java.util.*; @Component public class ProgramUtil { @@ -64,7 +63,7 @@ public void sendMenteeApplicationEmails(long id, Optional program) thro public void sendMenteeSelectionEmails(long id, Optional program) throws IOException, MessagingException { List approvedMentors = mentorRepository.findAllByProgramIdAndState(id, EnrolmentState.APPROVED); - List mentees = menteeRepository.findAllByProgramId(id); + List mentees = getMenteesWithoutDuplicatesByProgramId(id); // Notify mentors for (Mentor mentor : approvedMentors) { @@ -104,7 +103,7 @@ public void sendOnGoingEmails(long id, Optional program) throws IOExcep } public void sendMentorConfirmationEmails(long id, Optional program) throws IOException, MessagingException { - List mentees = menteeRepository.findAllByProgramId(id); + List mentees = getMenteesWithoutDuplicatesByProgramId(id); String message = "You can check your mentor by visiting the dashboard"; @@ -112,4 +111,19 @@ public void sendMentorConfirmationEmails(long id, Optional program) thr emailService.sendEmail(mentee.getProfile().getEmail(), program.get().getTitle(), message, true); } } + + /** + * Removes mentee duplicates + */ + private List getMenteesWithoutDuplicatesByProgramId(long id) { + List output = new ArrayList<>(); + List idList = new ArrayList<>(); + for (Mentee mentee: menteeRepository.findAllByProgramId(id)) { + if (!idList.contains(mentee.getProfile().getId())) { + idList.add(mentee.getProfile().getId()); + output.add(mentee); + } + } + return output; + } }