Skip to content

Commit

Permalink
HAI-1844 Streamline perustaja user creation, fix logging, remove redu…
Browse files Browse the repository at this point in the history
…ndant check in update validation
  • Loading branch information
pitkni committed Aug 30, 2023
1 parent 9c1a8b5 commit 125a9d0
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,9 @@ class HankeController(
featureFlags.ensureEnabled(Feature.HANKE_EDITING)

logger.info { "Updating Hanke: ${hanke.toLogString()}" }

val existingHanke = hankeService.findHankeOrThrow(hankeTunnus)
validateUpdatable(existing = existingHanke, updated = hanke, hankeTunnus)
existingHanke.verifyUserAuthorization(currentUserId(), PermissionCode.EDIT)
validateUpdatable(hanke, hankeTunnus)

val updatedHanke = hankeService.updateHanke(hanke)
logger.info { "Updated hanke ${updatedHanke.hankeTunnus}." }
Expand Down Expand Up @@ -305,14 +304,10 @@ class HankeController(
}
}

private fun validateUpdatable(existing: Hanke, updated: Hanke, hankeTunnusFromPath: String) {
val tunnusMatch =
listOf(existing.hankeTunnus, updated.hankeTunnus).all { it == hankeTunnusFromPath }

if (!tunnusMatch) {
private fun validateUpdatable(hankeUpdate: Hanke, hankeTunnusFromPath: String) {
if (hankeUpdate.hankeTunnus != hankeTunnusFromPath) {
throw HankeArgumentException(
"Hanketunnus mismatch. (Existing=${existing.hankeTunnus}, Updated=${updated.hankeTunnus}, " +
"Path=$hankeTunnusFromPath)"
"Hanketunnus mismatch. (In payload=${hankeUpdate.hankeTunnus}, In path=$hankeTunnusFromPath)"
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ open class HankeServiceImpl(
postProcessAndSaveLogging(loggingEntryHolder, savedHankeEntity, userId)

return createHankeDomainObjectFromEntity(savedHankeEntity).also {
initAccessForCreatedHanke(it, savedHankeEntity.perustaja, userId)
initAccessForCreatedHanke(it, perustaja, userId)
hankeLoggingService.logCreate(it, userId)
}
}
Expand Down Expand Up @@ -244,16 +244,10 @@ open class HankeServiceImpl(
!applicationService.isStillPending(it)
}

private fun initAccessForCreatedHanke(
hanke: Hanke,
perustaja: PerustajaEntity?,
userId: String
) {
private fun initAccessForCreatedHanke(hanke: Hanke, perustaja: Perustaja?, userId: String) {
val hankeId = hanke.id!!
val permissionAll = permissionService.setPermission(hankeId, userId, Role.KAIKKI_OIKEUDET)
perustaja?.let {
hankeKayttajaService.addHankeFounder(hankeId, it.toDomainObject(), permissionAll)
}
perustaja?.let { hankeKayttajaService.addHankeFounder(hankeId, it, permissionAll) }
hankeKayttajaService.saveNewTokensFromHanke(hanke)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class HankeKayttajaService(

@Transactional
fun addHankeFounder(hankeId: Int, founder: Perustaja, permissionEntity: PermissionEntity) {
logger.info { "Saving token for Hanke perustaja." }
logger.info { "Saving user for Hanke perustaja." }
saveUser(
HankeKayttajaEntity(
hankeId = hankeId,
Expand Down

0 comments on commit 125a9d0

Please sign in to comment.