Docs + email sending

This commit is contained in:
Filip Izydorczyk 2021-01-03 17:10:36 +01:00
parent 61d035e342
commit 95cc34c676
4 changed files with 137 additions and 0 deletions

View File

@ -0,0 +1,97 @@
package com.plannaplan.models;
import java.util.Iterator;
import java.util.List;
import com.plannaplan.entities.Groups;
/**
* Instance to keep data to send in mail about accepted courses results
*/
public class EmailAcceptedData {
private List<Groups> accepted;
private List<Groups> removed;
public EmailAcceptedData() {
}
/**
* creates instance of class
*
* @param accepted list of groups that user joined to
* @param removed lsit of groups that user failed to join
*/
public EmailAcceptedData(List<Groups> accepted, List<Groups> removed) {
this.accepted = accepted;
this.removed = removed;
}
/**
* get list of removed assignments
*
* @return removed assingments
*/
public List<Groups> getRemoved() {
return removed;
}
/**
* set list of removed assignments
*
* @param removed list of removed assignments
*/
public void setRemoved(List<Groups> removed) {
this.removed = removed;
}
/**
* get list of accepted assignments
*
* @return accepted assingments
*/
public List<Groups> getAccepted() {
return accepted;
}
/**
* set list of accepted assignments
*
* @param removed list of accepted assignments
*/
public void setAccepted(List<Groups> accepted) {
this.accepted = accepted;
}
/**
* it creates and return email message body
*
* @return string with emiale massage
*/
public String getEmailMessage() {
String response = "Akceptacja Twoich przedmiotów właśnie dobiegła końca.\n\n";
if (this.accepted != null && this.accepted.size() > 0) {
response += "Zatwierdzone grupy: \n";
Iterator<Groups> iterator = accepted.iterator();
while (iterator.hasNext()) {
final Groups gorup = iterator.next();
final String courseName = gorup.getCourseId() != null ? gorup.getCourseId().getName()
: "Nieznana grupa";
response += (" - " + courseName + " - " + gorup.getType() + "\n");
}
}
if (this.removed != null && this.removed.size() > 0) {
response += "Usunięte grupy: \n";
Iterator<Groups> iterator = removed.iterator();
while (iterator.hasNext()) {
final Groups gorup = iterator.next();
final String courseName = gorup.getCourseId() != null ? gorup.getCourseId().getName()
: "Nieznana grupa";
response += (" - " + courseName + " - " + gorup.getType() + "\n");
}
}
return response;
}
}

View File

@ -1,5 +1,6 @@
package com.plannaplan.services; package com.plannaplan.services;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
@ -7,6 +8,7 @@ import com.plannaplan.entities.Assignment;
import com.plannaplan.entities.Commision; import com.plannaplan.entities.Commision;
import com.plannaplan.entities.Groups; import com.plannaplan.entities.Groups;
import com.plannaplan.entities.User; import com.plannaplan.entities.User;
import com.plannaplan.models.EmailAcceptedData;
import com.plannaplan.repositories.AssignmentRepository; import com.plannaplan.repositories.AssignmentRepository;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -28,6 +30,9 @@ public class AssignmentService {
@Autowired @Autowired
private UserService userService; private UserService userService;
@Autowired
private EmailService emailService;
public AssignmentService() { public AssignmentService() {
} }
@ -65,11 +70,19 @@ public class AssignmentService {
return this.repo.findById(id); return this.repo.findById(id);
} }
/**
* this method will activate accept algorythm for all students. Algorythm is
* takeing each student in order defined by ranking and accept for him groups
* that are joinable for him
*/
public void callAcceptAlgorythm() { public void callAcceptAlgorythm() {
final List<User> students = this.userService.getStudentsSortedByRanking(); final List<User> students = this.userService.getStudentsSortedByRanking();
students.forEach(e -> { students.forEach(e -> {
final Optional<Commision> com = this.service.getNewestCommision(e); final Optional<Commision> com = this.service.getNewestCommision(e);
final List<Groups> accepted = new ArrayList<>();
final List<Groups> removed = new ArrayList<>();
if (com.isPresent()) { if (com.isPresent()) {
final List<Assignment> assignments = this.getCommisionAssignments(com.get()); final List<Assignment> assignments = this.getCommisionAssignments(com.get());
assignments.forEach(a -> { assignments.forEach(a -> {
@ -77,9 +90,13 @@ public class AssignmentService {
if (group.getCapacity() > group.getRegisteredStudents().size()) { if (group.getCapacity() > group.getRegisteredStudents().size()) {
e.claimGroup(group); e.claimGroup(group);
this.userService.save(e); this.userService.save(e);
accepted.add(group);
} else {
removed.add(group);
} }
}); });
} }
this.emailService.sendAcceptationResult(e, new EmailAcceptedData(accepted, removed));
}); });
} }
} }

View File

@ -1,5 +1,8 @@
package com.plannaplan.services; package com.plannaplan.services;
import com.plannaplan.entities.User;
import com.plannaplan.models.EmailAcceptedData;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.SimpleMailMessage;
@ -35,4 +38,20 @@ public class EmailService {
mailMessage.setText(message); mailMessage.setText(message);
emailSender.send(mailMessage); emailSender.send(mailMessage);
} }
/**
* sends email with infromation about accepted groups
*
* @param user user to send a mail
* @param data EmailAcceptedData instance containing informations about accepted
* and removed groups
*/
public void sendAcceptationResult(User user, EmailAcceptedData data) {
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setFrom(appEmail);
mailMessage.setTo(user.getEmail());
mailMessage.setSubject("[PlanNaPlan] Akceptacja przedmiotów");
mailMessage.setText(data.getEmailMessage());
emailSender.send(mailMessage);
}
} }

View File

@ -85,6 +85,10 @@ public class AssignmentServiceTest {
System.out.println("-X_-x-x-x-_X-x-x-x-X_-x-x-x-"); System.out.println("-X_-x-x-x-_X-x-x-x-X_-x-x-x-");
} }
/**
* This test will also sand a mail to users as a side effect. U can check them
* in mailcater
*/
@Test @Test
@DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD)
public void shouldNotAcceptForOnePerson() { public void shouldNotAcceptForOnePerson() {