@@ -43,6 +43,35 @@ class TimesheetsController(private val timesheetsService: TimesheetsService, pri | |||||
return leaveService.saveLeave(parsedEntries) | 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 | @GetMapping | ||||
fun getTimesheetEntry(): Map<String, List<TimeEntry>> { | fun getTimesheetEntry(): Map<String, List<TimeEntry>> { | ||||
return timesheetsService.getTimesheet() | return timesheetsService.getTimesheet() | ||||
@@ -5,4 +5,18 @@ data class LeaveEntry( | |||||
val leaveTypeId: Long?, | val leaveTypeId: Long?, | ||||
val inputHours: Double, | val inputHours: Double, | ||||
val remark: String? | 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, | |||||
) | |||||
} | |||||
} |
@@ -9,4 +9,18 @@ data class TimeEntry( | |||||
val inputHours: Double?, | val inputHours: Double?, | ||||
val otHours: Double?, | val otHours: Double?, | ||||
val remark: String? | 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 | |||||
) | |||||
} | |||||
} |
@@ -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, | |||||
) | |||||
} | |||||
} |