Skip to content

Commit

Permalink
HAI-1844 Change hanke perustaja back to nullable, use validator to va…
Browse files Browse the repository at this point in the history
…lidate create request
  • Loading branch information
pitkni committed Aug 22, 2023
1 parent 4aae699 commit b2ca01b
Show file tree
Hide file tree
Showing 12 changed files with 154 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ open class HankeServiceImpl(

val userId = currentUserId()

val entity = HankeEntity(perustaja = hanke.perustaja.toEntity())
val entity = HankeEntity()
val loggingEntryHolder = prepareLogging(entity)

// Create a new hanketunnus for it and save it:
Expand Down Expand Up @@ -158,13 +158,6 @@ open class HankeServiceImpl(
}
}

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

/**
* Create application when no existing hanke. Autogenerates hanke and applies application to it.
*/
Expand Down Expand Up @@ -242,6 +235,15 @@ open class HankeServiceImpl(
hankeLoggingService.logDelete(hanke, userId)
}

private fun initAccessForCreatedHanke(hanke: Hanke, userId: String) {
val hankeId = hanke.id!!
val permissionAll = permissionService.setPermission(hankeId, userId, Role.KAIKKI_OIKEUDET)
val perustaja =
hanke.perustaja ?: throw HankeArgumentException("Missing perustaja information")
hankeKayttajaService.addHankeFounder(hankeId, perustaja, permissionAll)
hankeKayttajaService.saveNewTokensFromHanke(hanke)
}

private fun anyHakemusProcessingInAllu(hakemukset: List<Application>): Boolean =
hakemukset.any {
logger.info { "Hakemus ${it.id} has alluStatus ${it.alluStatus}" }
Expand Down Expand Up @@ -342,7 +344,7 @@ open class HankeServiceImpl(
if (hankeEntity.modifiedAt != null) ZonedDateTime.of(hankeEntity.modifiedAt, TZ_UTC)
else null,
hankeEntity.status,
hankeEntity.perustaja.toDomainObject(),
hankeEntity.perustaja?.toDomainObject(),
hankeEntity.generated,
)

Expand Down Expand Up @@ -569,6 +571,7 @@ open class HankeServiceImpl(
hanke.nimi?.let { entity.nimi = hanke.nimi }
hanke.kuvaus?.let { entity.kuvaus = hanke.kuvaus }

hanke.perustaja?.let { entity.perustaja = it.toEntity() }
entity.generated = hanke.generated
hanke.vaihe?.let { entity.vaihe = hanke.vaihe }
hanke.suunnitteluVaihe?.let { entity.suunnitteluVaihe = hanke.suunnitteluVaihe }
Expand Down Expand Up @@ -960,11 +963,11 @@ open class HankeServiceImpl(
)
)

private fun generatePerustajaFrom(cableReport: CableReportApplicationData): Perustaja =
with(cableReport.findOrderer()) {
if (this == null || fullName().isNullOrBlank() || email.isNullOrBlank()) {
throw HankeArgumentException("Invalid orderer $this for Hanke perustaja")
}
Perustaja(fullName()!!, email)
}
private fun generatePerustajaFrom(cableReport: CableReportApplicationData): Perustaja {
val orderer =
cableReport.findOrderer()
?: throw HankeArgumentException("Orderer not found for Hanke perustaja")

return orderer.toHankePerustaja()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class HankeEntity(
var createdAt: LocalDateTime? = null,
var modifiedByUserId: String? = null,
var modifiedAt: LocalDateTime? = null,
@Embedded var perustaja: PerustajaEntity,
@Embedded var perustaja: PerustajaEntity? = null,
var generated: Boolean = false,
// NOTE: using IDENTITY (i.e. db does auto-increments, Hibernate reads the result back)
// can be a performance problem if there is a need to do bulk inserts.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import jakarta.persistence.Embeddable

@Embeddable
data class PerustajaEntity(
@Column(name = "perustajanimi") var nimi: String,
@Column(name = "perustajanimi") var nimi: String?,
@Column(name = "perustajaemail") var email: String
)

fun PerustajaEntity.toDomainObject(): Perustaja = Perustaja(nimi, email)
) {
fun toDomainObject(): Perustaja = Perustaja(nimi, email)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import com.fasterxml.jackson.annotation.JsonIgnore
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.annotation.JsonView
import fi.hel.haitaton.hanke.ChangeLogView
import fi.hel.haitaton.hanke.HankeArgumentException
import fi.hel.haitaton.hanke.allu.Contact as AlluContact
import fi.hel.haitaton.hanke.allu.Customer as AlluCustomer
import fi.hel.haitaton.hanke.allu.CustomerType
import fi.hel.haitaton.hanke.allu.CustomerWithContacts as AlluCustomerWithContacts
import fi.hel.haitaton.hanke.allu.PostalAddress as AlluPostalAddress
import fi.hel.haitaton.hanke.allu.StreetAddress as AlluStreetAddress
import fi.hel.haitaton.hanke.domain.BusinessId
import fi.hel.haitaton.hanke.domain.Perustaja

@JsonIgnoreProperties(ignoreUnknown = true)
data class CustomerWithContacts(val customer: Customer, val contacts: List<Contact>) {
Expand Down Expand Up @@ -44,6 +46,19 @@ data class Contact(
}
return names.filter { !it.isNullOrBlank() }.joinToString(" ")
}

/**
* It is possible to create a cable report without an existing Hanke. In these cases an
* application contact (orderer) is used as Hanke perustaja.
*/
fun toHankePerustaja(): Perustaja {
val name = fullName()
if (name.isNullOrBlank() || email.isNullOrBlank()) {
throw HankeArgumentException("Invalid orderer $this for Hanke perustaja")
}

return Perustaja(name, email)
}
}

@JsonIgnoreProperties(ignoreUnknown = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ data class Hanke(
var status: HankeStatus? = HankeStatus.DRAFT,
//
@JsonView(ChangeLogView::class)
@field:Schema(description = "Hanke founder contact information")
var perustaja: Perustaja,
@field:Schema(description = "Hanke founder contact information", required = true)
var perustaja: Perustaja? = null,
//
@JsonView(ChangeLogView::class)
@field:Schema(description = "Indicates whether the Hanke data is generated, set by the service")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package fi.hel.haitaton.hanke.domain

import fi.hel.haitaton.hanke.PerustajaEntity
import fi.hel.haitaton.hanke.permissions.UserContact
import io.swagger.v3.oas.annotations.media.Schema

@Schema(description = "Founder information")
data class Perustaja(
@field:Schema(description = "Name") val nimi: String,
@field:Schema(description = "Name") val nimi: String?,
@field:Schema(description = "Email address") val email: String
) {
fun toEntity(): PerustajaEntity = PerustajaEntity(nimi, email)

fun toUserContact(): UserContact = UserContact(nimi, email)
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class HankeKayttajaService(
saveUser(
HankeKayttajaEntity(
hankeId = hankeId,
nimi = founder.nimi,
nimi = founder.nimi!!,
sahkoposti = founder.email,
permission = permissionEntity,
kayttajaTunniste = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,13 @@ class HankeValidator : ConstraintValidator<ValidHanke, Hanke> {
var ok = true

with(hanke.perustaja) {
if (nimi.isBlank()) {
if (this == null) {
context.addViolation("perustaja")
ok = false
return false
}

if (nimi.isNullOrBlank()) {
context.addViolation("perustaja.nimi")
ok = false
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,3 @@ databaseChangeLog:
file: db/changelog/changesets/041-drop-column-organisaatioid-from-yhteystiedot.yml
- include:
file: db/changelog/changesets/042-add-indices-to-hanke-kayttaja.yml
- include:
file: db/changelog/changesets/043-change-hanke-perustaja-non-nullable.yaml
Loading

0 comments on commit b2ca01b

Please sign in to comment.