diff --git a/backend/src/main/java/core/api/controller/ScheduleController.java b/backend/src/main/java/core/api/controller/ScheduleController.java index 8d523eb..4985a28 100644 --- a/backend/src/main/java/core/api/controller/ScheduleController.java +++ b/backend/src/main/java/core/api/controller/ScheduleController.java @@ -141,7 +141,7 @@ public class ScheduleController { } } - @PostMapping("schedules/{scheduleID}/activate") + @PostMapping("/schedules/{scheduleID}/activate") public ResponseEntity activateSchedule(@PathVariable long scheduleID) { PermissionResult permissionResult = taskScheduleService.getSchedulePermissions(scheduleID, SecurityContextHolder.getContext().getAuthentication().getName()); if(permissionResult.isHasPermissions()) { @@ -156,7 +156,7 @@ public class ScheduleController { return ResponseEntity.ok(new ScheduleActivateResponse(serviceResult.getResult().getStartTime())); } - @PostMapping("schedules/{scheduleID}/stop/{finish}") + @PostMapping("/schedules/{scheduleID}/stop/{finish}") public ResponseEntity stopSchedule(@PathVariable long scheduleID, @PathVariable boolean finish) { PermissionResult permissionResult = taskScheduleService.getSchedulePermissions(scheduleID, SecurityContextHolder.getContext().getAuthentication().getName()); if(permissionResult.isHasPermissions()) { @@ -170,4 +170,23 @@ public class ScheduleController { ServiceResult serviceResult = taskScheduleService.stopSchedule(permissionResult.getResult(), finish); return ResponseEntity.ok(new TaskScheduleStopResponse(serviceResult.getResult().getActiveTime())); } + + @PostMapping("/schedules/{taskID}/forgotten") + public ResponseEntity registerForgottenSchedule(@PathVariable long taskID, @RequestBody @Valid ForgottenScheduleInfo forgottenScheduleInfo) { + PermissionResult permissionResult = taskService.getTaskPermissions(taskID, SecurityContextHolder.getContext().getAuthentication().getName()); + if(permissionResult.isHasPermissions()) { + return ResponseEntity.status(403).body(new SimpleStatusResponse("failed")); + } + + if(permissionResult.getExitCode() == ServiceExitCode.MISSING_ENTITY) { + return ResponseEntity.status(404).body(new SimpleStatusResponse("failed")); + } + + ServiceResult scheduleResult = taskScheduleService.registerForgottenSchedule(permissionResult.getResult(), forgottenScheduleInfo); + if(scheduleResult.getExitCode() == ServiceExitCode.INVALID_OPERATION) { + return ResponseEntity.status(400).body(new SimpleStatusResponse("failed")); + } else { + return ResponseEntity.ok(new TaskScheduleStopResponse(scheduleResult.getResult().getActiveTime())); + } + } } diff --git a/backend/src/main/java/core/api/models/timemanager/taskSchedule/ForgottenScheduleInfo.java b/backend/src/main/java/core/api/models/timemanager/taskSchedule/ForgottenScheduleInfo.java new file mode 100644 index 0000000..b885ee6 --- /dev/null +++ b/backend/src/main/java/core/api/models/timemanager/taskSchedule/ForgottenScheduleInfo.java @@ -0,0 +1,38 @@ +package core.api.models.timemanager.taskSchedule; + +import javax.validation.constraints.NotNull; +import java.time.LocalDate; +import java.time.LocalDateTime; + +public class ForgottenScheduleInfo { + + @NotNull + private LocalDateTime startTime; + + @NotNull + private LocalDateTime endTime; + + public ForgottenScheduleInfo(LocalDateTime startTime, LocalDateTime endTime) { + this.startTime = startTime; + this.endTime = endTime; + } + + public ForgottenScheduleInfo() { + } + + public LocalDateTime getStartTime() { + return startTime; + } + + public void setStartTime(LocalDateTime startTime) { + this.startTime = startTime; + } + + public LocalDateTime getEndTime() { + return endTime; + } + + public void setEndTime(LocalDateTime endTime) { + this.endTime = endTime; + } +} diff --git a/backend/src/main/java/core/services/TaskScheduleService.java b/backend/src/main/java/core/services/TaskScheduleService.java index 18a088f..bfec379 100644 --- a/backend/src/main/java/core/services/TaskScheduleService.java +++ b/backend/src/main/java/core/services/TaskScheduleService.java @@ -1,6 +1,7 @@ package core.services; import core.api.models.timemanager.taskSchedule.BasicScheduleFieldInfo; +import core.api.models.timemanager.taskSchedule.ForgottenScheduleInfo; import core.entities.timemanager.AbstractSchedule; import core.entities.timemanager.BasicTaskSchedule; import core.entities.timemanager.Task; @@ -122,4 +123,15 @@ public class TaskScheduleService { } return new ServiceResult<>(schedule); } + + public ServiceResult registerForgottenSchedule(Task task, ForgottenScheduleInfo forgottenScheduleInfo) { + if(task.isFinished()) { + return new ServiceResult<>(ServiceExitCode.INVALID_OPERATION); + } + + BasicTaskSchedule basicTaskSchedule = new BasicTaskSchedule(task, forgottenScheduleInfo.getStartTime(), forgottenScheduleInfo.getEndTime(), forgottenScheduleInfo.getStartTime().toLocalDate()); + scheduleRepository.save(basicTaskSchedule); + + return new ServiceResult<>(basicTaskSchedule); + } } diff --git a/backend/src/test/java/core/schedules/ScheduleServiceTest.java b/backend/src/test/java/core/schedules/ScheduleServiceTest.java index 5d95094..110a9d7 100644 --- a/backend/src/test/java/core/schedules/ScheduleServiceTest.java +++ b/backend/src/test/java/core/schedules/ScheduleServiceTest.java @@ -2,6 +2,7 @@ package core.schedules; import core.api.models.timemanager.taskSchedule.BasicScheduleFieldInfo; import core.api.models.timemanager.taskSchedule.BasicScheduleInfo; +import core.api.models.timemanager.taskSchedule.ForgottenScheduleInfo; import core.entities.timemanager.AbstractSchedule; import core.entities.timemanager.BasicTaskSchedule; import core.entities.timemanager.Task; @@ -20,6 +21,7 @@ import javax.persistence.EntityManager; import javax.transaction.Transactional; import java.time.LocalDate; +import java.time.LocalDateTime; import java.util.List; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; @@ -203,4 +205,26 @@ public class ScheduleServiceTest { assertTrue(result_3.getResult().getTask().isFinished()); assertFalse(result_3.getResult().isStartable()); } + + @Test + @SqlGroup({ + @Sql("classpath:taskgroupRepositoryTestEntries.sql"), + @Sql("classpath:taskRepositoryEntries.sql"), + @Sql("classpath:basicScheduleEntries.sql") + }) + void registerForgottenSchedule() { + //Register task schedule for task that is already finished + LocalDateTime startTime = LocalDateTime.now().minusMinutes(10L); + LocalDateTime finishTime = LocalDateTime.now(); + + ServiceResult result_1 = taskScheduleService.registerForgottenSchedule(entityManager.find(Task.class, 2L), new ForgottenScheduleInfo(startTime, finishTime)); + assertEquals(ServiceExitCode.INVALID_OPERATION, result_1.getExitCode()); + + ServiceResult result_2 = taskScheduleService.registerForgottenSchedule(entityManager.find(Task.class, 5L), new ForgottenScheduleInfo(startTime, finishTime)); + assertEquals(ServiceExitCode.OK, result_2.getExitCode()); + assertEquals(startTime, result_2.getResult().getStartTime()); + assertEquals(finishTime, result_2.getResult().getStopTime()); + assertThat(entityManager.find(BasicTaskSchedule.class, result_2.getResult().getScheduleID())).isNotNull(); + + } } diff --git a/openapi.yaml b/openapi.yaml index ed97204..bf0dad5 100644 --- a/openapi.yaml +++ b/openapi.yaml @@ -1439,7 +1439,6 @@ paths: - schedule description: get all schedules of today summary: get today's schedules - operationId: scheduleTaskNow parameters: - name: date in: path @@ -2334,19 +2333,16 @@ components: example: true ForgottenActivityRequest: required: - - mode + - startTime + - endTime additionalProperties: false properties: - mode: + startTime: type: string - description: mode of register forgotten activity - example: MANUAL - enum: - - MANUAL - - LAST - - PLANNED - minutesSpent: - type: number - description: number of minutes spent on task - example: 10 + format: date + description: time the schedule was started + endTime: + type: string + format: date + description: time the schedule was stopped \ No newline at end of file