From 13828788860fa9e7299b50e392196834e5c73bc8 Mon Sep 17 00:00:00 2001 From: SayedAli Date: Wed, 15 May 2024 15:09:03 +0330 Subject: [PATCH] hotfix: Implement Error Handling for Tracking Time (Close #11) Key changes: - Created new exception class. - Modified exception handler class. - Modified `TimeEntryTrackingService`. --- .../OperationNotSupportedException.java | 19 +++++++++++++++++++ .../TimeEntryServiceHandlerAdvice.java | 14 +++++++++++++- .../service/TimeEntryTrackingServiceImpl.java | 11 +++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/seyed/ali/timeentryservice/exceptions/OperationNotSupportedException.java rename src/main/java/com/seyed/ali/timeentryservice/exceptions/{ => handler}/TimeEntryServiceHandlerAdvice.java (67%) diff --git a/src/main/java/com/seyed/ali/timeentryservice/exceptions/OperationNotSupportedException.java b/src/main/java/com/seyed/ali/timeentryservice/exceptions/OperationNotSupportedException.java new file mode 100644 index 0000000..d1129f5 --- /dev/null +++ b/src/main/java/com/seyed/ali/timeentryservice/exceptions/OperationNotSupportedException.java @@ -0,0 +1,19 @@ +package com.seyed.ali.timeentryservice.exceptions; + +import lombok.Getter; + +@Getter +@SuppressWarnings("unused") +public class OperationNotSupportedException extends RuntimeException { + + private String message; + + public OperationNotSupportedException() { + } + + public OperationNotSupportedException(String message) { + super(message); + this.message = message; + } + +} diff --git a/src/main/java/com/seyed/ali/timeentryservice/exceptions/TimeEntryServiceHandlerAdvice.java b/src/main/java/com/seyed/ali/timeentryservice/exceptions/handler/TimeEntryServiceHandlerAdvice.java similarity index 67% rename from src/main/java/com/seyed/ali/timeentryservice/exceptions/TimeEntryServiceHandlerAdvice.java rename to src/main/java/com/seyed/ali/timeentryservice/exceptions/handler/TimeEntryServiceHandlerAdvice.java index 3ee4207..8962dd1 100644 --- a/src/main/java/com/seyed/ali/timeentryservice/exceptions/TimeEntryServiceHandlerAdvice.java +++ b/src/main/java/com/seyed/ali/timeentryservice/exceptions/handler/TimeEntryServiceHandlerAdvice.java @@ -1,5 +1,7 @@ -package com.seyed.ali.timeentryservice.exceptions; +package com.seyed.ali.timeentryservice.exceptions.handler; +import com.seyed.ali.timeentryservice.exceptions.OperationNotSupportedException; +import com.seyed.ali.timeentryservice.exceptions.ResourceNotFoundException; import com.seyed.ali.timeentryservice.model.dto.response.Result; import org.springframework.http.ResponseEntity; import org.springframework.security.access.AccessDeniedException; @@ -32,4 +34,14 @@ public ResponseEntity handleResourceNotFoundException(ResourceNotFoundEx )); } + @ExceptionHandler({OperationNotSupportedException.class}) + public ResponseEntity handleOperationNotSupportedException(OperationNotSupportedException e) { + return ResponseEntity.status(NOT_FOUND).body(new Result( + false, + NOT_FOUND, + "This operation is not supported.", + "ServerMessage - " + e.getMessage() + )); + } + } diff --git a/src/main/java/com/seyed/ali/timeentryservice/service/TimeEntryTrackingServiceImpl.java b/src/main/java/com/seyed/ali/timeentryservice/service/TimeEntryTrackingServiceImpl.java index 951d1c3..aff0c4e 100644 --- a/src/main/java/com/seyed/ali/timeentryservice/service/TimeEntryTrackingServiceImpl.java +++ b/src/main/java/com/seyed/ali/timeentryservice/service/TimeEntryTrackingServiceImpl.java @@ -1,6 +1,7 @@ package com.seyed.ali.timeentryservice.service; import com.seyed.ali.timeentryservice.client.AuthenticationServiceClient; +import com.seyed.ali.timeentryservice.exceptions.OperationNotSupportedException; import com.seyed.ali.timeentryservice.model.domain.TimeEntry; import com.seyed.ali.timeentryservice.model.dto.TimeEntryDTO; import com.seyed.ali.timeentryservice.repository.TimeEntryRepository; @@ -46,14 +47,24 @@ public String startTrackingTimeEntry(boolean billable, BigDecimal hourlyRate) { @Transactional public TimeEntryDTO stopTrackingTimeEntry(String timeEntryId) { LocalDateTime endTime = LocalDateTime.now(); + String currentLoggedInUsersId = this.authenticationServiceClient.getCurrentLoggedInUsersId(); TimeEntry timeEntry = this.timeEntryRepository.findByUserIdAndTimeEntryId(currentLoggedInUsersId, timeEntryId); + + // Check if the time entry has any time segments and if the last segment has a start time + if (timeEntry.getTimeSegmentList().isEmpty() || timeEntry.getTimeSegmentList().getLast().getStartTime() == null) { + // Handle the case where the start time is not set + throw new OperationNotSupportedException("Cannot stop time entry as the start time is not set."); + } + this.timeEntryUtility.stopTimeEntry(timeEntry, endTime); this.timeEntryRepository.save(timeEntry); + Duration totalDuration = this.timeEntryUtility.getTotalDuration(timeEntry); String startTimeStr = this.timeParser.parseLocalDateTimeToString(timeEntry.getTimeSegmentList().getLast().getStartTime()); String endTimeStr = this.timeParser.parseLocalDateTimeToString(endTime); String durationStr = this.timeParser.parseDurationToString(totalDuration); + return new TimeEntryDTO(null, startTimeStr, endTimeStr, timeEntry.isBillable(), timeEntry.getHourlyRate().toString(), durationStr); }