Skip to content
This repository was archived by the owner on Aug 19, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions src/main/java/com/shiftsl/backend/Service/ShiftService.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,15 @@ public List<Shift> getAvailableShifts() {
logger.info("Getting available shifts");
return shiftRepo.findAvailableShifts();
} catch (Exception e) {
logger.error("Unable to retrieve available shifts.");
logger.warn("Unable to retrieve available shifts.");
throw new ShiftRetrievalException("Error occurred while trying to retrieve available shifts from database");
}
}

// Doctor claims a shift from the shift pool
@Transactional
public void claimShift(Long doctorId, Long shiftId) {
String lockErrorMessage = null;
try {
logger.info("Claiming shift " + shiftId);
Shift shift = getShiftWithLock(shiftId);
Expand All @@ -92,18 +93,30 @@ public void claimShift(Long doctorId, Long shiftId) {
shiftRepo.save(shift);
} catch (LockTimeoutException | PessimisticLockException e) {
logger.error("Too many threads are trying to claim shift.");
throw new ShiftClaimFailedException("System is experiencing high load. Please try again later."+ e);
lockErrorMessage = "System is experiencing high load. Please try again later. " + e.getMessage();
throw new ShiftClaimFailedException(lockErrorMessage);
} catch (Exception e) {
logger.error("Error occurred while trying to store shift {} for doctor {} in database", shiftId, doctorId);
throw new ShiftClaimFailedException(String.format("Unable to claim shift %d for doctor %d", shiftId, doctorId));

String fullMessage = String.format(
"Unable to claim shiftId: %d for doctorId: %d", shiftId, doctorId
);

if (lockErrorMessage != null) {
fullMessage += ". " + lockErrorMessage;
} else {
fullMessage += ". " + e.getMessage();
}

throw new ShiftClaimFailedException(fullMessage);
}
}

@Transactional
public Shift getShiftByID(Long shiftID){
logger.info("Retrieving Shift '{}' from database", shiftID);
return shiftRepo.findById(shiftID).orElseThrow(() -> {
logger.error("Unable to find shift with ID " + shiftID);
logger.warn("Unable to find shift with ID {}", shiftID);
return new ShiftNotFoundException("Shift ID - (" + shiftID + ") not found.");
});
}
Expand All @@ -112,7 +125,7 @@ public Shift getShiftByID(Long shiftID){
public Shift getShiftWithLock(Long shiftID){
logger.info("Retrieving Shift {} with pessimistic lock", shiftID);
return shiftRepo.findShiftWithLock(shiftID).orElseThrow(() -> {
logger.error("Unable to find shift with locking implemented for ID " + shiftID);
logger.warn("Unable to find shift with locking implemented for ID {}", shiftID);
return new ShiftNotFoundException("Shift ID - (" + shiftID + ") not found.");
});
}
Expand All @@ -123,7 +136,7 @@ public List<Shift> getShiftsForDoctor(Long doctorId) {
userService.getUserById(doctorId); //check whether the doctor exists or else throws UserNotFoundException
List<Shift> shifts = shiftRepo.findByDoctors_Id(doctorId);
if (shifts.isEmpty()) {
logger.info("No shift found for doctor " + doctorId);
logger.warn("No shift found for doctor {}", doctorId);
throw new ShiftsNotFoundException("No shifts found for doctor with ID " + doctorId);
}
return shifts;
Expand Down Expand Up @@ -185,7 +198,7 @@ public List<Shift> getRoster(int month) {

return shiftRepo.findByStartTimeBetween(startDateTime, endDateTime);
} catch (Exception e) {
logger.error("Error occurred while trying to retrieve roster for month " + month);
logger.error("Error occurred while trying to retrieve roster for month {}", month);
throw new ShiftsNotFoundException("Unable to retrieve shifts for the given month from database");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public class LeaveServiceTest {
@Mock
private LeaveDTO testLeaveDTO;

// private ArgumentCaptor<Leave> captor = ArgumentCaptor.forClass(Leave.class);
private ArgumentCaptor<Leave> captor;

private static final Long ID = 123456L;
Expand Down Expand Up @@ -130,7 +129,7 @@ void requestLeaveTest() {
when(shiftService.getShiftByID(ID)).thenReturn(testShift);
when(leaveRepo.save(any(Leave.class))).thenReturn(testLeave);

Leave result = underTest.requestLeave(testLeaveDTO);
underTest.requestLeave(testLeaveDTO);

verify(leaveRepo).save(captor.capture());
verify(userService).getUserById(ID);
Expand Down Expand Up @@ -163,7 +162,6 @@ void getLeaveTest() {

@Test
void rejectTest() {
// ArgumentCaptor<Leave> captor = ArgumentCaptor.forClass(Leave.class);
when(leaveRepo.findById(ID)).thenReturn(Optional.of(testLeave));
when(leaveRepo.save(any(Leave.class))).thenReturn(testLeave);
String result = underTest.reject(ID);
Expand All @@ -188,7 +186,6 @@ void rejectExceptionTest() {
@Order(2)
void approveTest() {
testShift = Mockito.mock(Shift.class);
// ArgumentCaptor<Leave> captor = ArgumentCaptor.forClass(Leave.class);
when(shiftService.getShiftWithLock(2L)).thenReturn(testShift);
when(testShift.getDoctors()).thenReturn(testDoctors);
when(shiftService.updateShiftByID(testShift)).thenReturn(testShift);
Expand Down