Просмотр исходного кода

Endpoint for add time and leave at the same time

tags/Baseline_30082024_BACKEND_UAT
Wayne 1 год назад
Родитель
Сommit
0cb16c2b10
4 измененных файлов: 108 добавлений и 2 удалений
  1. +29
    -0
      src/main/java/com/ffii/tsms/modules/timesheet/web/TimesheetsController.kt
  2. +15
    -1
      src/main/java/com/ffii/tsms/modules/timesheet/web/models/LeaveEntry.kt
  3. +15
    -1
      src/main/java/com/ffii/tsms/modules/timesheet/web/models/TimeEntry.kt
  4. +49
    -0
      src/main/java/com/ffii/tsms/modules/timesheet/web/models/TimeLeaveEntry.kt

+ 29
- 0
src/main/java/com/ffii/tsms/modules/timesheet/web/TimesheetsController.kt Просмотреть файл

@@ -43,6 +43,35 @@ class TimesheetsController(private val timesheetsService: TimesheetsService, pri
return leaveService.saveLeave(parsedEntries)
}

@PostMapping("saveTimeLeave")
fun newTimeLeave(@Valid @RequestBody recordTimeLeave: Map<String, List<TimeLeaveEntry>>): Map<String, List<TimeLeaveEntry>> {
val parsedEntries = kotlin.runCatching {
recordTimeLeave.mapKeys { (dateString) -> LocalDate.parse(dateString, DateTimeFormatter.ISO_LOCAL_DATE) }
}.getOrElse { throw BadRequestException() }

val timesheets = parsedEntries.mapValues { (_, entries) -> entries.mapNotNull { timeLeaveEntry -> timeLeaveEntry.toTimeEntry() } }
val leaves = parsedEntries.mapValues { (_, entries) -> entries.mapNotNull { timeLeaveEntry -> timeLeaveEntry.toLeaveEntry() } }

val savedTimesheets = timesheetsService.saveTimesheet(timesheets)
val savedLeaves = leaveService.saveLeave(leaves)

val newMap = mutableMapOf<String, MutableList<TimeLeaveEntry>>()
savedTimesheets.forEach { (date, entries) ->
if (!newMap.containsKey(date)) {
newMap[date] = mutableListOf()
}
newMap[date]!!.addAll(entries.map { e -> e.toTimeLeaveEntry() })
}
savedLeaves.forEach { (date, entries) ->
if (!newMap.containsKey(date)) {
newMap[date] = mutableListOf()
}
newMap[date]!!.addAll(entries.map { e -> e.toTimeLeaveEntry() })
}

return newMap
}

@GetMapping
fun getTimesheetEntry(): Map<String, List<TimeEntry>> {
return timesheetsService.getTimesheet()


+ 15
- 1
src/main/java/com/ffii/tsms/modules/timesheet/web/models/LeaveEntry.kt Просмотреть файл

@@ -5,4 +5,18 @@ data class LeaveEntry(
val leaveTypeId: Long?,
val inputHours: Double,
val remark: String?
)
) {
fun toTimeLeaveEntry(): TimeLeaveEntry {
return TimeLeaveEntry(
type = "leaveEntry",
id = this.id,
leaveTypeId = this.leaveTypeId,
inputHours = this.inputHours,
remark = this.remark,
projectId = null,
taskGroupId = null,
taskId = null,
otHours = null,
)
}
}

+ 15
- 1
src/main/java/com/ffii/tsms/modules/timesheet/web/models/TimeEntry.kt Просмотреть файл

@@ -9,4 +9,18 @@ data class TimeEntry(
val inputHours: Double?,
val otHours: Double?,
val remark: String?
)
) {
fun toTimeLeaveEntry(): TimeLeaveEntry {
return TimeLeaveEntry(
type = "timeEntry",
id = this.id,
projectId = this.projectId,
taskGroupId = this.taskGroupId,
taskId = this.taskId,
inputHours = this.inputHours,
otHours = this.otHours,
remark = this.remark,
leaveTypeId = null
)
}
}

+ 49
- 0
src/main/java/com/ffii/tsms/modules/timesheet/web/models/TimeLeaveEntry.kt Просмотреть файл

@@ -0,0 +1,49 @@
package com.ffii.tsms.modules.timesheet.web.models

import jakarta.validation.constraints.AssertTrue

data class TimeLeaveEntry(
val id: Long,
val type: String,
val projectId: Long?,
val leaveTypeId: Long?,
val taskGroupId: Long?,
val taskId: Long?,
val inputHours: Double?,
val otHours: Double?,
val remark: String?,
) {
@AssertTrue
private fun isValid(): Boolean {
return type == "timeEntry" || (type == "leaveEntry" && leaveTypeId != null)
}

fun toTimeEntry(): TimeEntry? {
if (type == "leaveEntry") {
return null
}

return TimeEntry(
id,
projectId,
taskGroupId,
taskId,
inputHours,
otHours,
remark,
)
}

fun toLeaveEntry(): LeaveEntry? {
if (type == "timeEntry" || inputHours == null) {
return null
}

return LeaveEntry(
id,
leaveTypeId,
inputHours,
remark,
)
}
}

Загрузка…
Отмена
Сохранить