|
| 1 | +@file:Suppress("unused", "UNUSED_VARIABLE") |
| 2 | + |
| 3 | +package codes.jakob.tstse.example.idiomatic.result |
| 4 | + |
| 5 | +import codes.jakob.tstse.example.common.* |
| 6 | +import codes.jakob.tstse.example.idiomatic.scopefunctions.DeveloperRepository |
| 7 | +import mu.KLogger |
| 8 | +import mu.KotlinLogging |
| 9 | +import java.time.Clock |
| 10 | +import java.time.LocalDate |
| 11 | +import java.time.Month |
| 12 | +import java.time.Year |
| 13 | + |
| 14 | +class PaycheckService( |
| 15 | + private val humanResourcesClient: HumanResourcesClient, |
| 16 | + private val paycheckRepository: PaycheckRepository, |
| 17 | + private val developerRepository: DeveloperRepository, |
| 18 | + private val clock: Clock, |
| 19 | +) { |
| 20 | + /** |
| 21 | + * Returns an ordered list of [Paycheck]; sorted lexicographically by the [Name] of the [Developer]. |
| 22 | + * Additionally, each paycheck, successfully generated or not, is persisted into the database. |
| 23 | + */ |
| 24 | + fun generateAndPersistPaychecks(developerType: DeveloperType, year: Year, month: Month): List<Paycheck> { |
| 25 | + val today: LocalDate = LocalDate.now(clock) |
| 26 | + val period: LocalDateRange = determinePeriod(year, month) |
| 27 | + |
| 28 | + return developerRepository.findDevelopersByType(developerType) |
| 29 | + .filterNot { paycheckRepository.exists(it, period.range) } |
| 30 | + .mapNotNull { dev: Developer -> |
| 31 | + val paycheckBuilder = Paycheck.Builder().apply { |
| 32 | + this.date = today |
| 33 | + this.period = period |
| 34 | + this.developer = dev |
| 35 | + } |
| 36 | + Result.success(paycheckBuilder) |
| 37 | + .mapCatching { paycheck -> |
| 38 | + paycheck.apply { |
| 39 | + hourlyRate = humanResourcesClient.getHourlyRate(dev) |
| 40 | + hoursWorked = humanResourcesClient.getHoursWorked(dev, period.range) |
| 41 | + } |
| 42 | + } |
| 43 | + .mapCatching { paycheck -> |
| 44 | + paycheck.build() |
| 45 | + } |
| 46 | + .fold( |
| 47 | + { paycheck -> |
| 48 | + paycheckRepository.save(paycheck) |
| 49 | + logger.info { "Generated and persisted paycheck for '$dev' in period '$period'" } |
| 50 | + return@fold paycheck |
| 51 | + }, |
| 52 | + { exception -> |
| 53 | + logger.error(exception) { "Generation of paycheck for '$dev' in period '$period' failed; persisting this" } |
| 54 | + paycheckRepository.saveFailed(dev, period.range) |
| 55 | + return@fold null |
| 56 | + } |
| 57 | + ) |
| 58 | + } |
| 59 | + .sortedBy { it.developer.name } |
| 60 | + .also { logger.info { "Generated and persisted paychecks for ${it.count()} developers" } } |
| 61 | + |
| 62 | +// val (successfulPaychecks: List<Paycheck>, failedPaychecks: List<Developer>) = |
| 63 | +// developerRepository.findDevelopersByType(developerType) |
| 64 | +// .asSequence() |
| 65 | +// .filterNot { paycheckRepository.exists(it, period.range) } |
| 66 | +// .map { dev -> |
| 67 | +// dev to Paycheck.Builder().apply { |
| 68 | +// this.date = today |
| 69 | +// this.period = period |
| 70 | +// this.developer = dev |
| 71 | +// } |
| 72 | +// } |
| 73 | +// .map { (dev, paycheckBuilder) -> |
| 74 | +// try { |
| 75 | +// dev to paycheckBuilder.apply { |
| 76 | +// hourlyRate = humanResourcesClient.getHourlyRate(dev) |
| 77 | +// hoursWorked = humanResourcesClient.getHoursWorked(dev, period.range) |
| 78 | +// } |
| 79 | +// } catch (exception: HumanResourcesClient.HumanResourcesException) { |
| 80 | +// logger.error(exception) { |
| 81 | +// "Generation of ${Paycheck::class.simpleName} for '$dev' in period '$period' failed" |
| 82 | +// } |
| 83 | +// dev to null |
| 84 | +// } |
| 85 | +// } |
| 86 | +// .map { (dev, paycheckBuilder) -> |
| 87 | +// try { |
| 88 | +// dev to paycheckBuilder?.build() |
| 89 | +// } catch (exception: RuntimeException) { |
| 90 | +// logger.error(exception) { |
| 91 | +// "Generation of ${Paycheck::class.simpleName} for '$dev' in period '$period' failed" |
| 92 | +// } |
| 93 | +// dev to null |
| 94 | +// } |
| 95 | +// } |
| 96 | +// .partition { (dev, paycheck) -> |
| 97 | +// paycheck != null |
| 98 | +// } |
| 99 | +// .let { (success, failure) -> |
| 100 | +// success.map { it.second!! } to failure.map { it.first } |
| 101 | +// } |
| 102 | +// |
| 103 | +// successfulPaychecks.forEach { paycheck -> |
| 104 | +// paycheckRepository.save(paycheck) |
| 105 | +// .also { logger.info { "Generated and persisted paycheck for '${paycheck.developer}' in period '$period'" } } |
| 106 | +// } |
| 107 | +// failedPaychecks.forEach { dev -> |
| 108 | +// paycheckRepository.saveFailed(dev, period.range) |
| 109 | +// } |
| 110 | +// |
| 111 | +// return successfulPaychecks |
| 112 | +// .sortedBy { it.developer.name } |
| 113 | +// .also { logger.info { "Generated and persisted paychecks for ${it.count()} developers" } } |
| 114 | + } |
| 115 | + |
| 116 | + private fun determinePeriod(year: Year, month: Month): LocalDateRange { |
| 117 | + val firstDayInPeriod: LocalDate = LocalDate.of(year.value, month, 1) |
| 118 | + val lastDayInPeriod: LocalDate = firstDayInPeriod.withDayOfMonth(firstDayInPeriod.lengthOfMonth()) |
| 119 | + return LocalDateRange(firstDayInPeriod.rangeTo(lastDayInPeriod)) |
| 120 | + } |
| 121 | + |
| 122 | + companion object { |
| 123 | + private val logger: KLogger = KotlinLogging.logger {} |
| 124 | + } |
| 125 | +} |
0 commit comments