Théo Le Lez aaa6e46d0c
Some checks failed
Format / formatting (push) Successful in 6s
Build / build (push) Failing after 39s
CI / build (push) Successful in 11s
fix (kinda) : refactored update of data, still trying to fix bug
from EntrepreneurApiServiceTests (code is a bit messy with prints and comments dw)
2025-04-06 19:22:18 +02:00

168 lines
7.1 KiB
Java

package enseirb.myinpulse.service;
import static enseirb.myinpulse.model.ProjectDecisionValue.ACTIVE;
import static enseirb.myinpulse.model.ProjectDecisionValue.REJECTED;
import enseirb.myinpulse.model.*;
import enseirb.myinpulse.service.database.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;
import java.util.ArrayList;
import java.util.List;
@Service
public class AdminApiService {
protected static final Logger logger = LogManager.getLogger();
private final ProjectService projectService;
private final UserService userService;
private final AdministratorService administratorService;
private final UtilsService utilsService;
private final AppointmentService appointmentService;
private final ReportService reportService;
private final SectionCellService sectionCellService;
@Autowired
AdminApiService(
ProjectService projectService,
UserService userService,
AdministratorService administratorService,
UtilsService utilsService,
AppointmentService appointmentService,
ReportService reportService,
SectionCellService sectionCellService) {
this.projectService = projectService;
this.userService = userService;
this.administratorService = administratorService;
this.utilsService = utilsService;
this.appointmentService = appointmentService;
this.reportService = reportService;
this.sectionCellService = sectionCellService;
}
// TODO: check if tests are sufficient - peer verification required
public Iterable<Project> getProjectsOfAdmin(String mail) {
return projectService.getProjectsByAdminId(
administratorService.getAdministratorById(
this.userService.getUserByEmail(mail).getIdUser()));
}
public Iterable<Appointment> getUpcomingAppointments(String mail) {
logger.info("User {} check their upcoming appointments", mail);
User user = this.userService.getUserByEmail(mail);
List<Appointment> appointments = new ArrayList<>();
if (user instanceof Administrator) {
List<Project> projects = new ArrayList<>(((Administrator) user).getListProject());
projects.forEach(
project -> {
project.getListSectionCell()
.forEach(
sectionCell -> {
appointments.addAll(
this.sectionCellService
.getAppointmentsBySectionCellId(
sectionCell
.getIdSectionCell()));
});
});
}
if (user instanceof Entrepreneur) {
Project project = ((Entrepreneur) user).getProjectParticipation();
project.getListSectionCell()
.forEach(
sectionCell -> {
appointments.addAll(
this.sectionCellService.getAppointmentsBySectionCellId(
sectionCell.getIdSectionCell()));
});
}
return appointments;
}
// TODO: check if tests are sufficient - peer verification required
public Iterable<Project> getPendingProjects() {
return this.projectService.getPendingProjects();
}
// TODO: check if tests are sufficient - peer verification required
public void validateProject(ProjectDecision decision) {
projectService.updateProject(
decision.projectId,
null,
null,
(decision.isAccepted == 1) ? ACTIVE : REJECTED,
null,
null,
this.administratorService.getAdministratorById(decision.adminId));
}
// TODO: check if tests are sufficient - peer verification required
public void addNewProject(Project project) {
project.setIdProject(null);
// We remove the ID from the request to be sure that it will be auto generated
try {
this.projectService.getProjectByName(project.getProjectName(), true);
throw new ResponseStatusException(HttpStatus.CONFLICT, "Project already exists");
} catch (ResponseStatusException e) {
if (e.getStatusCode() == HttpStatus.CONFLICT) {
throw new ResponseStatusException(HttpStatus.CONFLICT, "Project already exists");
}
}
Project newProject = projectService.addNewProject(project);
if (project.getProjectAdministrator() != null) {
newProject.getProjectAdministrator().updateListProject(newProject);
}
if (newProject.getEntrepreneurProposed() != null) {
Entrepreneur proposed = newProject.getEntrepreneurProposed();
proposed.setProjectProposed(newProject);
proposed.setProjectParticipation(newProject);
}
newProject
.getListEntrepreneurParticipation()
.forEach(
participation -> {
participation.setProjectParticipation(newProject);
});
newProject
.getListSectionCell()
.forEach(
sectionCell -> {
sectionCell.setProjectSectionCell(newProject);
});
}
public void createAppointmentReport(long appointmentId, Report report, String mail) {
long projectId =
this.appointmentService
.getAppointmentById(appointmentId)
.getAppointmentListSectionCell()
.getFirst()
.getProjectSectionCell()
.getIdProject();
if (!utilsService.isAllowedToCheckProject(mail, projectId)) {
logger.warn(
"User {} tried to add an report for appointment {} but is not allowed to.",
mail,
projectId);
throw new ResponseStatusException(
HttpStatus.UNAUTHORIZED, "You're not allowed to check this project");
}
logger.info("User {} added a report for appointment {}", mail, projectId);
Report addedReport = this.reportService.addNewReport(report);
addedReport.setAppointmentReport(this.appointmentService.getAppointmentById(appointmentId));
this.appointmentService.getAppointmentById(appointmentId).setAppointmentReport(addedReport);
}
// TODO: test
public void deleteProject(long projectId) {
this.projectService.deleteProjectById(projectId);
}
}