From 8856e85872fe628f0e65cff4140d9f00d04c16ff Mon Sep 17 00:00:00 2001 From: Tero Laakso Date: Fri, 27 Dec 2024 15:40:18 +0200 Subject: [PATCH] Add out of office delete endpoint --- .../outofoffice/OutOfOfficeIntegrationTest.kt | 10 ++++++++ .../src/main/kotlin/fi/espoo/evaka/Audit.kt | 1 + .../fi/espoo/evaka/outofoffice/OutOfOffice.kt | 12 +++++++++ .../outofoffice/OutOfOfficeController.kt | 25 +++++++++++++++++++ 4 files changed, 48 insertions(+) diff --git a/service/src/integrationTest/kotlin/fi/espoo/evaka/outofoffice/OutOfOfficeIntegrationTest.kt b/service/src/integrationTest/kotlin/fi/espoo/evaka/outofoffice/OutOfOfficeIntegrationTest.kt index d6db360149..41f9052a9c 100644 --- a/service/src/integrationTest/kotlin/fi/espoo/evaka/outofoffice/OutOfOfficeIntegrationTest.kt +++ b/service/src/integrationTest/kotlin/fi/espoo/evaka/outofoffice/OutOfOfficeIntegrationTest.kt @@ -76,5 +76,15 @@ class OutOfOfficeIntegrationTest : FullApplicationTest(resetDbBeforeEach = true) val updatedPeriods = outOfOfficeController.getOutOfOfficePeriods(dbInstance(), employee, clock) assertEquals(listOf(updatedPeriod.period), updatedPeriods.map { it.period }) + + outOfOfficeController.deleteOutOfOfficePeriod( + dbInstance(), + employee, + clock, + updatedPeriod.id!!, + ) + val deletedPeriods = + outOfOfficeController.getOutOfOfficePeriods(dbInstance(), employee, clock) + assertEquals(0, deletedPeriods.size) } } diff --git a/service/src/main/kotlin/fi/espoo/evaka/Audit.kt b/service/src/main/kotlin/fi/espoo/evaka/Audit.kt index 9f8316d44d..0e341451f9 100755 --- a/service/src/main/kotlin/fi/espoo/evaka/Audit.kt +++ b/service/src/main/kotlin/fi/espoo/evaka/Audit.kt @@ -391,6 +391,7 @@ enum class Audit( OtherAssistanceMeasureDelete, OutOfOfficeRead, OutOfOfficeUpdate, + OutOfOfficeDelete, PairingInit(securityEvent = true), PairingChallenge(securityEvent = true), PairingResponse(securityEvent = true, securityLevel = "high"), diff --git a/service/src/main/kotlin/fi/espoo/evaka/outofoffice/OutOfOffice.kt b/service/src/main/kotlin/fi/espoo/evaka/outofoffice/OutOfOffice.kt index 4b1aa03cae..23e64b00d0 100644 --- a/service/src/main/kotlin/fi/espoo/evaka/outofoffice/OutOfOffice.kt +++ b/service/src/main/kotlin/fi/espoo/evaka/outofoffice/OutOfOffice.kt @@ -56,3 +56,15 @@ WHERE id = ${bind(period.id)} .updateExactlyOne() } } + +fun Database.Transaction.deleteOutOfOfficePeriod(id: OutOfOfficeId) { + createUpdate { + sql( + """ +DELETE FROM out_of_office +WHERE id = ${bind(id)} +""" + ) + } + .updateExactlyOne() +} diff --git a/service/src/main/kotlin/fi/espoo/evaka/outofoffice/OutOfOfficeController.kt b/service/src/main/kotlin/fi/espoo/evaka/outofoffice/OutOfOfficeController.kt index 32cb5b3de5..d75de2c629 100644 --- a/service/src/main/kotlin/fi/espoo/evaka/outofoffice/OutOfOfficeController.kt +++ b/service/src/main/kotlin/fi/espoo/evaka/outofoffice/OutOfOfficeController.kt @@ -1,13 +1,16 @@ package fi.espoo.evaka.outofoffice import fi.espoo.evaka.Audit +import fi.espoo.evaka.shared.OutOfOfficeId import fi.espoo.evaka.shared.auth.AuthenticatedUser import fi.espoo.evaka.shared.db.Database import fi.espoo.evaka.shared.domain.BadRequest import fi.espoo.evaka.shared.domain.EvakaClock import fi.espoo.evaka.shared.security.AccessControl import fi.espoo.evaka.shared.security.Action +import org.springframework.web.bind.annotation.DeleteMapping import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.PathVariable import org.springframework.web.bind.annotation.PostMapping import org.springframework.web.bind.annotation.RestController @@ -62,4 +65,26 @@ class OutOfOfficeController(private val accessControl: AccessControl) { } .also { Audit.OutOfOfficeUpdate.log(targetId = fi.espoo.evaka.AuditId(user.id)) } } + + @DeleteMapping("/employee/out-of-office/{id}") + fun deleteOutOfOfficePeriod( + db: Database, + user: AuthenticatedUser.Employee, + clock: EvakaClock, + @PathVariable id: OutOfOfficeId, + ) { + return db.connect { dbc -> + dbc.transaction { + accessControl.requirePermissionFor( + it, + user, + clock, + Action.Employee.UPDATE_OUT_OF_OFFICE, + user.id, + ) + it.deleteOutOfOfficePeriod(id = id) + } + } + .also { Audit.OutOfOfficeDelete.log(targetId = fi.espoo.evaka.AuditId(user.id)) } + } }