Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lisätään työaikakirjausten muutosten auditlokitus #6313

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ class RealtimeStaffAttendanceQueriesTest : PureJdbiTest(resetDbBeforeEach = true
val arrivalTime = clock.now()
val arrivalAddedAt = arrivalTime.plusMinutes(2)

val attendanceId =
val attendance =
db.transaction { tx ->
tx.upsertStaffAttendance(
attendanceId = null,
Expand Down Expand Up @@ -582,7 +582,7 @@ class RealtimeStaffAttendanceQueriesTest : PureJdbiTest(resetDbBeforeEach = true
{ it.departedAddedAt },
{ it.departedModifiedAt },
)
.containsExactly(Tuple(attendanceId, arrivalAddedAt, arrivalAddedAt, null, null))
.containsExactly(Tuple(attendance.new.id, arrivalAddedAt, arrivalAddedAt, null, null))
}

@Test
Expand All @@ -594,7 +594,7 @@ class RealtimeStaffAttendanceQueriesTest : PureJdbiTest(resetDbBeforeEach = true
val departureTime = arrivalTime.plusHours(7)
val addedAt = departureTime.plusMinutes(2)

val attendanceId =
val attendance =
db.transaction { tx ->
tx.upsertStaffAttendance(
attendanceId = null,
Expand Down Expand Up @@ -625,7 +625,7 @@ class RealtimeStaffAttendanceQueriesTest : PureJdbiTest(resetDbBeforeEach = true
{ it.departedAddedAt },
{ it.departedModifiedAt },
)
.containsExactly(Tuple(attendanceId, addedAt, addedAt, addedAt, addedAt))
.containsExactly(Tuple(attendance.new.id, addedAt, addedAt, addedAt, addedAt))
}

@Test
Expand Down
22 changes: 22 additions & 0 deletions service/src/main/kotlin/fi/espoo/evaka/Audit.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import fi.espoo.evaka.shared.Id
import fi.espoo.voltti.logging.loggers.audit
import io.github.oshai.kotlinlogging.KotlinLogging
import java.util.UUID
import kotlin.reflect.KProperty1

sealed interface AuditId {
val value: Any
Expand Down Expand Up @@ -590,3 +591,24 @@ enum class Audit(
}

private val logger = KotlinLogging.logger {}

data class AuditChange(val old: Any?, val new: Any?)

/** Returns */
fun <T> changes(
old: T,
new: T,
fields: Pair<KProperty1<T, Any?>, Map<String, KProperty1<T, Any?>>>,
): Map<String, AuditChange> {
val changes =
fields.second
.mapNotNull { (name, property) ->
val oldValue = property.get(old)
val newValue = property.get(new)
if (oldValue != newValue) name to AuditChange(old = oldValue, new = newValue)
else null
}
.toMap()
return mapOf("id" to AuditChange(old = fields.first.get(old), new = fields.first.get(new))) +
changes
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import fi.espoo.evaka.Audit
import fi.espoo.evaka.AuditId
import fi.espoo.evaka.absence.getDaycareIdByGroup
import fi.espoo.evaka.attendance.RealtimeStaffAttendanceController.OpenGroupAttendanceResponse
import fi.espoo.evaka.changes
import fi.espoo.evaka.shared.DaycareId
import fi.espoo.evaka.shared.EmployeeId
import fi.espoo.evaka.shared.GroupId
Expand Down Expand Up @@ -131,7 +132,7 @@ class MobileRealtimeStaffAttendanceController(private val ac: AccessControl) {
clock: EvakaClock,
@RequestBody body: StaffArrivalRequest,
) {
val staffAttendanceIds =
val updates =
try {
db.connect { dbc ->
dbc.transaction { tx ->
Expand Down Expand Up @@ -190,7 +191,12 @@ class MobileRealtimeStaffAttendanceController(private val ac: AccessControl) {
}
Audit.StaffAttendanceArrivalCreate.log(
targetId = AuditId(listOf(body.groupId, body.employeeId)),
objectId = AuditId(staffAttendanceIds),
objectId = AuditId(updates.mapNotNull { it.new.id }),
meta =
mapOf(
"changes" to
updates.map { changes(it.old, it.new, StaffAttendanceRealtimeAudit.fields) }
),
)
}

Expand All @@ -209,7 +215,7 @@ class MobileRealtimeStaffAttendanceController(private val ac: AccessControl) {
clock: EvakaClock,
@RequestBody body: StaffDepartureRequest,
) {
val staffAttendanceIds =
val updates =
db.connect { dbc ->
dbc.transaction { tx ->
ac.requirePermissionFor(
Expand Down Expand Up @@ -254,7 +260,12 @@ class MobileRealtimeStaffAttendanceController(private val ac: AccessControl) {
}
Audit.StaffAttendanceDepartureCreate.log(
targetId = AuditId(listOf(body.groupId, body.employeeId)),
objectId = AuditId(staffAttendanceIds),
objectId = AuditId(updates.mapNotNull { it.new.id }),
meta =
mapOf(
"changes" to
updates.map { changes(it.old, it.new, StaffAttendanceRealtimeAudit.fields) }
),
)
}

Expand Down Expand Up @@ -312,9 +323,9 @@ class MobileRealtimeStaffAttendanceController(private val ac: AccessControl) {
}

val deletedIds = existing.map { it.id }
deletedIds.forEach { tx.deleteStaffAttendance(it) }
val deleted = deletedIds.map { tx.deleteStaffAttendance(it) }

val updatedIds =
val updated =
body.rows.map { attendance ->
tx.upsertStaffAttendance(
attendanceId = null,
Expand All @@ -333,21 +344,39 @@ class MobileRealtimeStaffAttendanceController(private val ac: AccessControl) {
)
}

StaffAttendanceUpdateResponse(deleted = deletedIds, inserted = updatedIds)
updated + deleted.map { StaffAttendanceRealtimeChange(old = it) }
}
}
.also {
.also { updates ->
Audit.StaffAttendanceUpdate.log(
targetId = AuditId(body.employeeId),
objectId = AuditId(it.inserted + it.deleted),
objectId =
AuditId(
(updates.mapNotNull { it.new.id } + updates.mapNotNull { it.old.id })
.distinct()
),
meta =
mapOf(
"date" to body.date,
"inserted" to it.inserted,
"deleted" to it.deleted,
"changes" to
updates.map {
changes(it.old, it.new, StaffAttendanceRealtimeAudit.fields)
},
),
)
}
.let { updates ->
StaffAttendanceUpdateResponse(
deleted =
updates.mapNotNull {
if (it.old.id != null && it.new.id == null) it.old.id else null
},
inserted =
updates.mapNotNull {
if (it.old.id != null && it.new.id == null) null else it.new.id
},
)
}
}

data class ExternalStaffArrivalRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package fi.espoo.evaka.attendance

import fi.espoo.evaka.Audit
import fi.espoo.evaka.AuditId
import fi.espoo.evaka.changes
import fi.espoo.evaka.shared.DaycareId
import fi.espoo.evaka.shared.EmployeeId
import fi.espoo.evaka.shared.GroupId
Expand Down Expand Up @@ -205,7 +206,7 @@ class RealtimeStaffAttendanceController(private val accessControl: AccessControl
throw BadRequest("Date cannot be in the future")
}

val staffAttendanceIds =
val updates =
db.connect { dbc ->
dbc.transaction { tx ->
accessControl.requirePermissionFor(
Expand All @@ -215,12 +216,13 @@ class RealtimeStaffAttendanceController(private val accessControl: AccessControl
Action.Unit.UPDATE_STAFF_ATTENDANCES,
body.unitId,
)
tx.deleteStaffAttendancesOnDateExcept(
body.unitId,
body.employeeId,
body.date,
body.entries.mapNotNull { it.id },
)
val deleted =
tx.deleteStaffAttendancesOnDateExcept(
body.unitId,
body.employeeId,
body.date,
body.entries.mapNotNull { it.id },
)

accessControl.requirePermissionFor(
tx,
Expand All @@ -246,13 +248,21 @@ class RealtimeStaffAttendanceController(private val accessControl: AccessControl
clock.now(),
user.evakaUserId,
)
}
} + deleted.map { StaffAttendanceRealtimeChange(old = it) }
}
}
Audit.StaffAttendanceUpdate.log(
targetId = AuditId(body.unitId),
objectId = AuditId(staffAttendanceIds),
meta = mapOf("date" to body.date),
objectId =
AuditId(
(updates.mapNotNull { it.new.id } + updates.mapNotNull { it.old.id }).distinct()
),
meta =
mapOf(
"date" to body.date,
"changes" to
updates.map { changes(it.old, it.new, StaffAttendanceRealtimeAudit.fields) },
),
)
}

Expand Down
Loading