Skip to content

Commit

Permalink
HAI-2749 WIP: no tests or docs
Browse files Browse the repository at this point in the history
  • Loading branch information
corvidian committed Oct 21, 2024
1 parent 4a8f392 commit 90e7eed
Show file tree
Hide file tree
Showing 9 changed files with 423 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import fi.hel.haitaton.hanke.factory.PaatosFactory
import fi.hel.haitaton.hanke.factory.PaperDecisionReceiverFactory
import fi.hel.haitaton.hanke.factory.TaydennysFactory
import fi.hel.haitaton.hanke.factory.TaydennyspyyntoFactory
import fi.hel.haitaton.hanke.geometria.GeometriatDao
import fi.hel.haitaton.hanke.getResourceAsBytes
import fi.hel.haitaton.hanke.hankeError
import fi.hel.haitaton.hanke.logging.DisclosureLogService
Expand Down Expand Up @@ -767,7 +768,8 @@ class HakemusControllerITest(@Autowired override val mockMvc: MockMvc) : Control
authorizer.authorizeHakemusId(id, PermissionCode.EDIT_APPLICATIONS.name)
} returns true
every { hakemusService.updateHakemus(id, request, USERNAME) } throws
HakemusGeometryException("Invalid geometry")
HakemusGeometryException(
HakemusFactory.create(), GeometriatDao.InvalidDetail("", ""))

put(url, request)
.andExpect(status().isBadRequest)
Expand Down Expand Up @@ -831,7 +833,8 @@ class HakemusControllerITest(@Autowired override val mockMvc: MockMvc) : Control
authorizer.authorizeHakemusId(id, PermissionCode.EDIT_APPLICATIONS.name)
} returns true
every { hakemusService.updateHakemus(id, request, USERNAME) } throws
InvalidHiddenRegistryKey(HakemusFactory.create(id = id), "Reason for error")
InvalidHiddenRegistryKey(
"Reason for error", CustomerType.COMPANY, CustomerType.PERSON)

put(url, request)
.andExpect(status().isBadRequest)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1343,9 +1343,10 @@ class HakemusServiceITest(

failure.all {
hasClass(InvalidHiddenRegistryKey::class)
messageContains("id=${hakemus.id}")
messageContains("RegistryKeyHidden used in an incompatible way")
messageContains("New customer type doesn't match the old")
messageContains("New=PERSON")
messageContains("Old=COMPANY")
}
}

Expand Down Expand Up @@ -1411,9 +1412,10 @@ class HakemusServiceITest(

failure.all {
hasClass(InvalidHiddenRegistryKey::class)
messageContains("id=${hakemus.id}")
messageContains("RegistryKeyHidden used in an incompatible way")
messageContains("New invoicing customer type doesn't match the old")
messageContains("New=PERSON")
messageContains("Old=COMPANY")
}
}

Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ sealed interface HakemusUpdateRequest {
fun hasChanges(hakemusData: HakemusData): Boolean

/**
* Converts this update request to an [HakemusEntityData] object using the given [hakemusEntity]
* as a basis.
* Converts this update request to an [HakemusEntityData] object using the given
* [hakemusEntityData] as a basis.
*/
fun toEntityData(hakemusEntity: HakemusEntity): HakemusEntityData
fun toEntityData(hakemusEntityData: HakemusEntityData): HakemusEntityData

fun customersByRole(): Map<ApplicationContactType, CustomerWithContactsRequest?>
}
Expand Down Expand Up @@ -106,8 +106,8 @@ data class JohtoselvityshakemusUpdateRequest(
representativeWithContacts.hasChanges(hakemusData.propertyDeveloperWithContacts)
}

override fun toEntityData(hakemusEntity: HakemusEntity) =
(hakemusEntity.hakemusEntityData as JohtoselvityshakemusEntityData).copy(
override fun toEntityData(hakemusEntityData: HakemusEntityData) =
(hakemusEntityData as JohtoselvityshakemusEntityData).copy(
name = this.name,
postalAddress =
PostalAddress(StreetAddress(this.postalAddress?.streetAddress?.streetName), "", ""),
Expand Down Expand Up @@ -210,8 +210,8 @@ data class KaivuilmoitusUpdateRequest(
additionalInfo != hakemusData.additionalInfo
}

override fun toEntityData(hakemusEntity: HakemusEntity) =
(hakemusEntity.hakemusEntityData as KaivuilmoitusEntityData).copy(
override fun toEntityData(hakemusEntityData: HakemusEntityData) =
(hakemusEntityData as KaivuilmoitusEntityData).copy(
name = this.name,
workDescription = this.workDescription,
constructionWork = this.constructionWork,
Expand All @@ -225,7 +225,7 @@ data class KaivuilmoitusUpdateRequest(
startTime = this.startTime,
endTime = this.endTime,
areas = this.areas,
invoicingCustomer = this.invoicingCustomer.toCustomer(hakemusEntity),
invoicingCustomer = this.invoicingCustomer.toCustomer(hakemusEntityData),
customerReference = this.invoicingCustomer?.customerReference,
additionalInfo = this.additionalInfo,
)
Expand Down Expand Up @@ -349,15 +349,15 @@ fun InvoicingPostalAddressRequest?.hasChanges(postalAddress: PostalAddress?): Bo
city != postalAddress.city
}

fun InvoicingCustomerRequest?.toCustomer(hakemus: HakemusEntity): InvoicingCustomer? {
fun InvoicingCustomerRequest?.toCustomer(hakemusEntityData: HakemusEntityData): InvoicingCustomer? {
return this?.let {
val baseData = (hakemus.hakemusEntityData as KaivuilmoitusEntityData).invoicingCustomer
val baseData = (hakemusEntityData as KaivuilmoitusEntityData).invoicingCustomer
if (baseData != null && type != baseData.type && registryKeyHidden) {
// If new invoicing customer type doesn't match the old one, the type of registry key
// will be wrong, but it will be retained if the key is hidden.
// Validation only checks the new type.
throw InvalidHiddenRegistryKey(
hakemus, "New invoicing customer type doesn't match the old.")
"New invoicing customer type doesn't match the old.", type, baseData.type)
}

InvoicingCustomer(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package fi.hel.haitaton.hanke.taydennys

import fi.hel.haitaton.hanke.HankeError
import fi.hel.haitaton.hanke.currentUserId
import fi.hel.haitaton.hanke.hakemus.HakemusUpdateRequest
import fi.hel.haitaton.hanke.hakemus.ValidHakemusUpdateRequest
import fi.hel.haitaton.hanke.logging.DisclosureLogService
import io.swagger.v3.oas.annotations.Hidden
import io.swagger.v3.oas.annotations.Operation
Expand All @@ -10,13 +12,16 @@ import io.swagger.v3.oas.annotations.media.Schema
import io.swagger.v3.oas.annotations.responses.ApiResponse
import io.swagger.v3.oas.annotations.responses.ApiResponses
import io.swagger.v3.oas.annotations.security.SecurityRequirement
import java.util.UUID
import mu.KotlinLogging
import org.springframework.http.HttpStatus
import org.springframework.security.access.prepost.PreAuthorize
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.ResponseStatus
import org.springframework.web.bind.annotation.RestController

Expand Down Expand Up @@ -62,6 +67,14 @@ class TaydennysController(
return response
}

@RequestMapping("/taydennykset/{id}")
@PreAuthorize("@taydennysAuthorizer.authorize(#id, 'EDIT_APPLICATIONS')")
fun update(
@PathVariable id: UUID,
@ValidHakemusUpdateRequest @RequestBody request: HakemusUpdateRequest
): TaydennysResponse =
taydennysService.updateTaydennys(id, request, currentUserId()).toResponse()

@ExceptionHandler(NoTaydennyspyyntoException::class)
@ResponseStatus(HttpStatus.CONFLICT)
@Hidden
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package fi.hel.haitaton.hanke.taydennys

import fi.hel.haitaton.hanke.hakemus.ApplicationContactType
import fi.hel.haitaton.hanke.hakemus.ApplicationType
import fi.hel.haitaton.hanke.hakemus.HakemusEntityData
import fi.hel.haitaton.hanke.hakemus.Hakemusyhteystieto
import fi.hel.haitaton.hanke.hakemus.JohtoselvityshakemusEntityData
import fi.hel.haitaton.hanke.hakemus.KaivuilmoitusEntityData
import fi.hel.haitaton.hanke.permissions.HankekayttajaEntity
import io.hypersistence.utils.hibernate.type.json.JsonType
import jakarta.persistence.CascadeType
import jakarta.persistence.Column
Expand All @@ -25,7 +27,7 @@ import org.hibernate.annotations.Type
@Entity
@Table(name = "taydennys")
class TaydennysEntity(
@Id var id: UUID = UUID.randomUUID(),
@Id override var id: UUID = UUID.randomUUID(),
@OneToOne(fetch = FetchType.LAZY, optional = false)
@OnDelete(action = OnDeleteAction.CASCADE)
@JoinColumn(name = "taydennyspyynto_id", nullable = false)
Expand All @@ -43,7 +45,7 @@ class TaydennysEntity(
@BatchSize(size = 100)
var yhteystiedot: MutableMap<ApplicationContactType, TaydennysyhteystietoEntity> =
mutableMapOf(),
) {
) : TaydennysIdentifier {
fun toDomain(): Taydennys {
val yhteystiedot: Map<ApplicationContactType, Hakemusyhteystieto> =
yhteystiedot.mapValues { it.value.toDomain() }
Expand All @@ -60,4 +62,19 @@ class TaydennysEntity(
hakemusData = applicationData,
)
}

override fun taydennyspyyntoId(): UUID = taydennyspyynto.id

override fun taydennyspyyntoAlluId(): Int = taydennyspyynto.alluId

override fun hakemusId(): Long = taydennyspyynto.applicationId

override fun hakemustyyppi(): ApplicationType = hakemusData.applicationType

/** Returns all distinct contact users for this täydennys. */
fun allContactUsers(): List<HankekayttajaEntity> =
yhteystiedot.values
.flatMap { it.yhteyshenkilot }
.map { it.hankekayttaja }
.distinctBy { it.id }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package fi.hel.haitaton.hanke.taydennys

import fi.hel.haitaton.hanke.domain.HasId
import fi.hel.haitaton.hanke.hakemus.ApplicationType
import java.util.UUID

interface TaydennysIdentifier : HasId<UUID> {
override val id: UUID

fun taydennyspyyntoId(): UUID

fun taydennyspyyntoAlluId(): Int

fun hakemusId(): Long

fun hakemustyyppi(): ApplicationType

fun logString() =
"Täydennys: (" +
listOf(
"id=$id",
"täydennyspyyntö=${taydennyspyyntoId()}",
"täydennyspyyntöAlluId=${taydennyspyyntoAlluId()}",
"hakemusId=${hakemusId()}",
"hakemustyyppi=${hakemustyyppi()}")
.joinToString() +
")"
}
Loading

0 comments on commit 90e7eed

Please sign in to comment.