feat: implemented date filtration and a utils service to prevent code ducplication
Some checks failed
Format / formatting (push) Failing after 7s
CI / build (push) Successful in 11s

This commit is contained in:
Pierre Tellier 2025-02-28 11:45:55 +01:00
parent b5c03798fc
commit 80b2d087e4
9 changed files with 99 additions and 85 deletions

View File

@ -1,8 +0,0 @@
package enseirb.myinpulse.model;
public class DelAppointment {
int validated;
int[] akserId;
int[] destId;
String date; // TODO: date type ?
}

View File

@ -1,7 +0,0 @@
package enseirb.myinpulse.model;
public class DelProject {
int projectId;
String projectName;
String projectDescription;
}

View File

@ -1,6 +0,0 @@
package enseirb.myinpulse.model;
public class DelReport {
int projectId;
String reportContent;
}

View File

@ -1,7 +0,0 @@
package enseirb.myinpulse.model;
// TODO: is this redundant with the Section class from the database ?
// TODO: In the one hand it represent the same data, and on the other it should be much lighter.
// TODO: btw why does a LC section have an administrator ?
public class LCSection {}

View File

@ -6,8 +6,13 @@ import enseirb.myinpulse.model.SectionCell;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource; import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import java.time.LocalDateTime;
@RepositoryRestResource @RepositoryRestResource
public interface SectionCellRepository extends JpaRepository<SectionCell, Long> { public interface SectionCellRepository extends JpaRepository<SectionCell, Long> {
Iterable<SectionCell> findByProjectSectionCellAndSectionId(Project project, long sectionId); Iterable<SectionCell> findByProjectSectionCellAndSectionId(Project project, long sectionId);
Iterable<SectionCell> findByProjectSectionCellAndSectionIdAndModificationDateBefore(
Project project, long sectionId, LocalDateTime date);
} }

View File

@ -1,13 +1,9 @@
package enseirb.myinpulse.service; package enseirb.myinpulse.service;
import enseirb.myinpulse.model.Entrepreneur;
import enseirb.myinpulse.model.Project; import enseirb.myinpulse.model.Project;
import enseirb.myinpulse.model.SectionCell; import enseirb.myinpulse.model.SectionCell;
import enseirb.myinpulse.model.User;
import enseirb.myinpulse.service.database.EntrepreneurService;
import enseirb.myinpulse.service.database.ProjectService; import enseirb.myinpulse.service.database.ProjectService;
import enseirb.myinpulse.service.database.SectionCellService; import enseirb.myinpulse.service.database.SectionCellService;
import enseirb.myinpulse.service.database.UserService;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
@ -23,28 +19,19 @@ public class EntrepreneurApiService {
private final SectionCellService sectionCellService; private final SectionCellService sectionCellService;
private final ProjectService projectService; private final ProjectService projectService;
private final UserService userService; private final UtilsService utilsService;
private final EntrepreneurService entrepreneurService;
@Autowired @Autowired
EntrepreneurApiService( EntrepreneurApiService(
SectionCellService sectionCellService, SectionCellService sectionCellService,
ProjectService projectService, ProjectService projectService,
UserService userService, UtilsService utilsService) {
EntrepreneurService entrepreneurService) {
this.sectionCellService = sectionCellService; this.sectionCellService = sectionCellService;
this.projectService = projectService; this.projectService = projectService;
this.userService = userService; this.utilsService = utilsService;
this.entrepreneurService = entrepreneurService;
} }
// Create utils file ?
Boolean isAllowedToCheckProject(String mail, long projectId) {
User user = this.userService.getUserByEmail(mail);
Entrepreneur entrepreneur = this.entrepreneurService.getEntrepreneurById(user.getIdUser());
Project project = this.projectService.getProjectById(projectId);
return entrepreneur.getProjectParticipation() == project;
}
public void editSectionCell(Long sectionCellId, SectionCell sectionCell, String mail) { public void editSectionCell(Long sectionCellId, SectionCell sectionCell, String mail) {
SectionCell editSectionCell = sectionCellService.getSectionCellById(sectionCellId); SectionCell editSectionCell = sectionCellService.getSectionCellById(sectionCellId);
@ -53,7 +40,7 @@ public class EntrepreneurApiService {
throw new ResponseStatusException( throw new ResponseStatusException(
HttpStatus.NOT_FOUND, "Cette cellule de section n'existe pas"); HttpStatus.NOT_FOUND, "Cette cellule de section n'existe pas");
} }
if (!isAllowedToCheckProject(mail, this.sectionCellService.getProjectId(sectionCellId))) { if (!utilsService.isAllowedToCheckProject(mail, this.sectionCellService.getProjectId(sectionCellId))) {
logger.warn( logger.warn(
"User {} tried to edit section cells {} of the project {} but is not allowed to.", "User {} tried to edit section cells {} of the project {} but is not allowed to.",
mail, mail,
@ -81,7 +68,7 @@ public class EntrepreneurApiService {
throw new ResponseStatusException( throw new ResponseStatusException(
HttpStatus.NOT_FOUND, "Cette cellule de section n'existe pas"); HttpStatus.NOT_FOUND, "Cette cellule de section n'existe pas");
} }
if (!isAllowedToCheckProject(mail, this.sectionCellService.getProjectId(sectionCellId))) { if (!utilsService.isAllowedToCheckProject(mail, this.sectionCellService.getProjectId(sectionCellId))) {
logger.warn( logger.warn(
"User {} tried to remove section cells {} of the project {} but is not allowed to.", "User {} tried to remove section cells {} of the project {} but is not allowed to.",
mail, mail,
@ -104,7 +91,7 @@ public class EntrepreneurApiService {
throw new ResponseStatusException( throw new ResponseStatusException(
HttpStatus.BAD_REQUEST, "La cellule de section fournie est vide"); HttpStatus.BAD_REQUEST, "La cellule de section fournie est vide");
} }
if (!isAllowedToCheckProject( if (!utilsService.isAllowedToCheckProject(
mail, this.sectionCellService.getProjectId(sectionCell.getIdSectionCell()))) { mail, this.sectionCellService.getProjectId(sectionCell.getIdSectionCell()))) {
logger.warn( logger.warn(
"User {} tried to add a section cell to the project {} but is not allowed to.", "User {} tried to add a section cell to the project {} but is not allowed to.",

View File

@ -10,6 +10,8 @@ import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException; import org.springframework.web.server.ResponseStatusException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -18,57 +20,32 @@ public class SharedApiService {
protected static final Logger logger = LogManager.getLogger(); protected static final Logger logger = LogManager.getLogger();
private final AdministratorService administratorService;
private final UserService userService;
private final ProjectService projectService; private final ProjectService projectService;
private final EntrepreneurService entrepreneurService; private final EntrepreneurService entrepreneurService;
private final SectionCellService sectionCellService; private final SectionCellService sectionCellService;
private final AppointmentService appointmentService; private final AppointmentService appointmentService;
private final UtilsService utilsService;
@Autowired @Autowired
SharedApiService( SharedApiService(
AdministratorService administratorService,
UserService userService,
ProjectService projectService, ProjectService projectService,
EntrepreneurService entrepreneurService, EntrepreneurService entrepreneurService,
SectionCellService sectionCellService, SectionCellService sectionCellService,
AppointmentService appointmentService) { AppointmentService appointmentService,
this.administratorService = administratorService; UtilsService utilsService) {
this.userService = userService;
this.projectService = projectService; this.projectService = projectService;
this.entrepreneurService = entrepreneurService; this.entrepreneurService = entrepreneurService;
this.sectionCellService = sectionCellService; this.sectionCellService = sectionCellService;
this.appointmentService = appointmentService; this.appointmentService = appointmentService;
} this.utilsService = utilsService;
// TODO: test
Boolean isAnAdmin(String mail) {
try {
long userId = this.userService.getUserByEmail(mail).getIdUser();
Administrator a = this.administratorService.getAdministratorById(userId);
return true;
} catch (ResponseStatusException e) {
logger.info(e);
return false;
}
}
// TODO: test
Boolean isAllowedToCheckProject(String mail, long projectId) {
if (isAnAdmin(mail)) {
return true;
}
User user = this.userService.getUserByEmail(mail);
Entrepreneur entrepreneur = this.entrepreneurService.getEntrepreneurById(user.getIdUser());
Project project = this.projectService.getProjectById(projectId);
return entrepreneur.getProjectParticipation() == project;
} }
// TODO filter this with date // TODO filter this with date
public Iterable<SectionCell> getSectionCells( public Iterable<SectionCell> getSectionCells(
long projectId, long sectionId, String date, String mail) { long projectId, long sectionId, String date, String mail) {
if (!isAllowedToCheckProject(mail, projectId)) { if (!utilsService.isAllowedToCheckProject(mail, projectId)) {
logger.warn( logger.warn(
"User {} tried to check section cells of the project {} but is not allowed to.", "User {} tried to check section cells of the project {} but is not allowed to.",
mail, mail,
@ -76,13 +53,18 @@ public class SharedApiService {
throw new ResponseStatusException( throw new ResponseStatusException(
HttpStatus.UNAUTHORIZED, "You're not allowed to check this project"); HttpStatus.UNAUTHORIZED, "You're not allowed to check this project");
} }
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime dateTime = LocalDateTime.parse(date, formatter);
Project project = this.projectService.getProjectById(projectId); Project project = this.projectService.getProjectById(projectId);
return this.sectionCellService.getSectionCellsByProject(project, sectionId); return this.sectionCellService.getSectionCellsByProjectAndSectionIdBeforeDate(
project, sectionId, dateTime);
} }
// TODO: test // TODO: test
public Iterable<Entrepreneur> getEntrepreneursByProjectId(long projectId, String mail) { public Iterable<Entrepreneur> getEntrepreneursByProjectId(long projectId, String mail) {
if (!isAllowedToCheckProject(mail, projectId)) { if (!utilsService.isAllowedToCheckProject(mail, projectId)) {
logger.warn( logger.warn(
"User {} tried to check the member of the project {} but is not allowed to.", "User {} tried to check the member of the project {} but is not allowed to.",
mail, mail,
@ -94,9 +76,9 @@ public class SharedApiService {
return this.entrepreneurService.GetEntrepreneurByProject(project); return this.entrepreneurService.GetEntrepreneurByProject(project);
} }
// TODO: test, protect via email // TODO: test
public Administrator getAdminByProjectId(long projectId, String mail) { public Administrator getAdminByProjectId(long projectId, String mail) {
if (!isAllowedToCheckProject(mail, projectId)) { if (!utilsService.isAllowedToCheckProject(mail, projectId)) {
logger.warn( logger.warn(
"User {} tried to check the admin of the project {} but is not allowed to.", "User {} tried to check the admin of the project {} but is not allowed to.",
mail, mail,
@ -110,7 +92,7 @@ public class SharedApiService {
// TODO // TODO
public Iterable<Appointment> getAppointmentsByProjectId(long projectId, String mail) { public Iterable<Appointment> getAppointmentsByProjectId(long projectId, String mail) {
if (!isAllowedToCheckProject(mail, projectId)) { if (!utilsService.isAllowedToCheckProject(mail, projectId)) {
logger.warn( logger.warn(
"User {} tried to check the appointments related to the project {} but is not allowed to.", "User {} tried to check the appointments related to the project {} but is not allowed to.",
mail, mail,
@ -145,7 +127,7 @@ public class SharedApiService {
.getFirst() .getFirst()
.getProjectSectionCell() .getProjectSectionCell()
.getIdProject(); .getIdProject();
if (!isAllowedToCheckProject(mail, projectId)) { if (!utilsService.isAllowedToCheckProject(mail, projectId)) {
logger.warn( logger.warn(
"User {} tried to generate the PDF report {} related to the appointment {} but is not allowed to.", "User {} tried to generate the PDF report {} related to the appointment {} but is not allowed to.",
mail, mail,

View File

@ -0,0 +1,62 @@
package enseirb.myinpulse.service;
import enseirb.myinpulse.model.Administrator;
import enseirb.myinpulse.model.Entrepreneur;
import enseirb.myinpulse.model.Project;
import enseirb.myinpulse.model.User;
import enseirb.myinpulse.service.database.AdministratorService;
import enseirb.myinpulse.service.database.EntrepreneurService;
import enseirb.myinpulse.service.database.ProjectService;
import enseirb.myinpulse.service.database.UserService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;
@Service
public class UtilsService {
protected static final Logger logger = LogManager.getLogger();
private final UserService userService;
private final ProjectService projectService;
private final EntrepreneurService entrepreneurService;
private final AdministratorService administratorService;
@Autowired
UtilsService(
ProjectService projectService,
UserService userService,
EntrepreneurService entrepreneurService,
AdministratorService administratorService) {
this.userService = userService;
this.projectService = projectService;
this.entrepreneurService = entrepreneurService;
this.administratorService = administratorService;
}
// TODO: test?
public Boolean isAllowedToCheckProject(String mail, long projectId) {
if (isAnAdmin(mail)) {
return true;
}
User user = this.userService.getUserByEmail(mail);
Entrepreneur entrepreneur = this.entrepreneurService.getEntrepreneurById(user.getIdUser());
Project project = this.projectService.getProjectById(projectId);
return entrepreneur.getProjectParticipation() == project;
}
// TODO: test
Boolean isAnAdmin(String mail) {
try {
long userId = this.userService.getUserByEmail(mail).getIdUser();
Administrator a = this.administratorService.getAdministratorById(userId);
return true;
} catch (ResponseStatusException e) {
logger.info(e);
return false;
}
}
}

View File

@ -84,4 +84,10 @@ public class SectionCellService {
SectionCell sectionCell = getSectionCellById(sectionCellId); SectionCell sectionCell = getSectionCellById(sectionCellId);
return sectionCell.getAppointmentSectionCell(); return sectionCell.getAppointmentSectionCell();
} }
public Iterable<SectionCell> getSectionCellsByProjectAndSectionIdBeforeDate(
Project project, long sectionId, LocalDateTime date) {
return sectionCellRepository.findByProjectSectionCellAndSectionIdAndModificationDateBefore(
project, sectionId, date);
}
} }