Skip to content

Commit

Permalink
Merge pull request #23 from sayedxali/hotfix/error-handling-time-trac…
Browse files Browse the repository at this point in the history
…king

hotfix: Implement Error Handling for Tracking Time (Close #11)
  • Loading branch information
seyedali-dev committed May 15, 2024
2 parents 61ed5e7 + 1382878 commit 21e165e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -32,4 +34,14 @@ public ResponseEntity<Result> handleResourceNotFoundException(ResourceNotFoundEx
));
}

@ExceptionHandler({OperationNotSupportedException.class})
public ResponseEntity<Result> handleOperationNotSupportedException(OperationNotSupportedException e) {
return ResponseEntity.status(NOT_FOUND).body(new Result(
false,
NOT_FOUND,
"This operation is not supported.",
"ServerMessage - " + e.getMessage()
));
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit 21e165e

Please sign in to comment.