-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: restrict vacations on selected year (#131)
* feat: add chargeYear to request object * feat: refactor tests and refactor create vacation method. Added new exception branch * feat: create new remaining vacation service with shared calculation logic to validation, create and update vacation * feat: refactor calendar calculations to RemainingVacationService and refactor tests * feat: add NoMoreDaysLeftInYearException to Vacation controller * feat: check vacation on update * feat: Refactor test * feat: Include new test cases for NoMoreDaysLeftInYearException exception and refactor * feat: Post and Put method will return object response instead of lists Refactor tests * feat: Fixed test in VacationControllerIT * feat: Remove validations from VacationService * feat: Add tests to RemainingVacationService * refactor: remove todo comment
- Loading branch information
1 parent
5097642
commit 606f8dc
Showing
23 changed files
with
680 additions
and
595 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
...acle/src/main/kotlin/com/autentia/tnt/binnacle/exception/NoMoreDaysLeftInYearException.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.autentia.tnt.binnacle.exception | ||
|
||
class NoMoreDaysLeftInYearException(message: String) : BinnacleException(message) { | ||
constructor() : this("There are no more days left in selected year") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
...t-binnacle/src/main/kotlin/com/autentia/tnt/binnacle/services/RemainingVacationService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.autentia.tnt.binnacle.services | ||
|
||
import com.autentia.tnt.binnacle.converters.VacationConverter | ||
import com.autentia.tnt.binnacle.core.domain.Calendar | ||
import com.autentia.tnt.binnacle.core.domain.CalendarFactory | ||
import com.autentia.tnt.binnacle.core.domain.DateInterval | ||
import com.autentia.tnt.binnacle.core.domain.RequestVacation | ||
import com.autentia.tnt.binnacle.core.domain.Vacation | ||
import com.autentia.tnt.binnacle.entities.User | ||
import com.autentia.tnt.binnacle.repositories.VacationRepository | ||
import jakarta.inject.Singleton | ||
import java.time.LocalDate | ||
import java.time.Month | ||
|
||
@Singleton | ||
internal class RemainingVacationService( | ||
private val vacationRepository: VacationRepository, | ||
private val myVacationsDetailService: MyVacationsDetailService, | ||
private val vacationConverter: VacationConverter, | ||
private val calendarFactory: CalendarFactory, | ||
) { | ||
|
||
private fun getVacationsWithWorkableDays(vacations: List<com.autentia.tnt.binnacle.entities.Vacation>): List<Vacation> { | ||
return if (vacations.isEmpty()) { | ||
emptyList() | ||
} else { | ||
val start: LocalDate? = vacations.minOfOrNull(com.autentia.tnt.binnacle.entities.Vacation::startDate) | ||
val end: LocalDate? = vacations.maxOfOrNull(com.autentia.tnt.binnacle.entities.Vacation::endDate) | ||
val dateInterval = DateInterval.of(start!!, end!!) | ||
val calendar = calendarFactory.create(dateInterval) | ||
getVacationsWithWorkableDays(calendar, vacations) | ||
} | ||
} | ||
|
||
private fun getVacationsWithWorkableDays(calendar: Calendar, vacations: List<com.autentia.tnt.binnacle.entities.Vacation>): List<Vacation> = | ||
vacations.map { | ||
val days = calendar.getWorkableDays(DateInterval.of(it.startDate, it.endDate)) | ||
vacationConverter.toVacationDomain(it, days) | ||
} | ||
|
||
private fun getVacationsByChargeYear( | ||
chargeYear: LocalDate | ||
): List<Vacation> { | ||
val vacations = vacationRepository.findByChargeYear(chargeYear) | ||
return getVacationsWithWorkableDays(vacations) | ||
} | ||
|
||
fun getRemainingVacations(chargeYear: Int, user: User) : Int { | ||
val vacations = getVacationsByChargeYear(LocalDate.of(chargeYear, 1, 1)) | ||
return myVacationsDetailService | ||
.getRemainingVacations(chargeYear, vacations, user) | ||
} | ||
|
||
fun getRequestedVacationsSelectedYear( | ||
requestVacation: RequestVacation | ||
): List<LocalDate> { | ||
val currentYear = requestVacation.chargeYear | ||
val lastYear = currentYear - 1 | ||
val nextYear = currentYear + 1 | ||
|
||
val lastYearFirstDay = LocalDate.of(lastYear, Month.JANUARY, 1) | ||
val nextYearLastDay = LocalDate.of(nextYear, Month.DECEMBER, 31) | ||
|
||
val calendar = calendarFactory.create(DateInterval.of(lastYearFirstDay, nextYearLastDay)) | ||
return calendar.getWorkableDays(DateInterval.of(requestVacation.startDate, requestVacation.endDate)) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.