feat: implemented date filtration and a utils service to prevent code ducplication
This commit is contained in:
@ -10,6 +10,8 @@ import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@ -18,57 +20,32 @@ public class SharedApiService {
|
||||
|
||||
protected static final Logger logger = LogManager.getLogger();
|
||||
|
||||
private final AdministratorService administratorService;
|
||||
private final UserService userService;
|
||||
private final ProjectService projectService;
|
||||
private final EntrepreneurService entrepreneurService;
|
||||
private final SectionCellService sectionCellService;
|
||||
private final AppointmentService appointmentService;
|
||||
|
||||
private final UtilsService utilsService;
|
||||
|
||||
@Autowired
|
||||
SharedApiService(
|
||||
AdministratorService administratorService,
|
||||
UserService userService,
|
||||
ProjectService projectService,
|
||||
EntrepreneurService entrepreneurService,
|
||||
SectionCellService sectionCellService,
|
||||
AppointmentService appointmentService) {
|
||||
this.administratorService = administratorService;
|
||||
this.userService = userService;
|
||||
AppointmentService appointmentService,
|
||||
UtilsService utilsService) {
|
||||
this.projectService = projectService;
|
||||
this.entrepreneurService = entrepreneurService;
|
||||
this.sectionCellService = sectionCellService;
|
||||
this.appointmentService = appointmentService;
|
||||
}
|
||||
|
||||
// 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;
|
||||
this.utilsService = utilsService;
|
||||
}
|
||||
|
||||
// TODO filter this with date
|
||||
public Iterable<SectionCell> getSectionCells(
|
||||
long projectId, long sectionId, String date, String mail) {
|
||||
|
||||
if (!isAllowedToCheckProject(mail, projectId)) {
|
||||
if (!utilsService.isAllowedToCheckProject(mail, projectId)) {
|
||||
logger.warn(
|
||||
"User {} tried to check section cells of the project {} but is not allowed to.",
|
||||
mail,
|
||||
@ -76,13 +53,18 @@ public class SharedApiService {
|
||||
throw new ResponseStatusException(
|
||||
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);
|
||||
return this.sectionCellService.getSectionCellsByProject(project, sectionId);
|
||||
return this.sectionCellService.getSectionCellsByProjectAndSectionIdBeforeDate(
|
||||
project, sectionId, dateTime);
|
||||
}
|
||||
|
||||
// TODO: test
|
||||
public Iterable<Entrepreneur> getEntrepreneursByProjectId(long projectId, String mail) {
|
||||
if (!isAllowedToCheckProject(mail, projectId)) {
|
||||
if (!utilsService.isAllowedToCheckProject(mail, projectId)) {
|
||||
logger.warn(
|
||||
"User {} tried to check the member of the project {} but is not allowed to.",
|
||||
mail,
|
||||
@ -94,9 +76,9 @@ public class SharedApiService {
|
||||
return this.entrepreneurService.GetEntrepreneurByProject(project);
|
||||
}
|
||||
|
||||
// TODO: test, protect via email
|
||||
// TODO: test
|
||||
public Administrator getAdminByProjectId(long projectId, String mail) {
|
||||
if (!isAllowedToCheckProject(mail, projectId)) {
|
||||
if (!utilsService.isAllowedToCheckProject(mail, projectId)) {
|
||||
logger.warn(
|
||||
"User {} tried to check the admin of the project {} but is not allowed to.",
|
||||
mail,
|
||||
@ -110,7 +92,7 @@ public class SharedApiService {
|
||||
|
||||
// TODO
|
||||
public Iterable<Appointment> getAppointmentsByProjectId(long projectId, String mail) {
|
||||
if (!isAllowedToCheckProject(mail, projectId)) {
|
||||
if (!utilsService.isAllowedToCheckProject(mail, projectId)) {
|
||||
logger.warn(
|
||||
"User {} tried to check the appointments related to the project {} but is not allowed to.",
|
||||
mail,
|
||||
@ -145,7 +127,7 @@ public class SharedApiService {
|
||||
.getFirst()
|
||||
.getProjectSectionCell()
|
||||
.getIdProject();
|
||||
if (!isAllowedToCheckProject(mail, projectId)) {
|
||||
if (!utilsService.isAllowedToCheckProject(mail, projectId)) {
|
||||
logger.warn(
|
||||
"User {} tried to generate the PDF report {} related to the appointment {} but is not allowed to.",
|
||||
mail,
|
||||
|
Reference in New Issue
Block a user