feat(#12): Add subsections to sections, fix version deletion after update

This commit is contained in:
2025-11-19 12:11:39 +01:00
parent b919cef9c4
commit 02d9b4f97c
20 changed files with 376 additions and 199 deletions

View File

@@ -31,7 +31,7 @@ class ApplicationForm(
var name: String = "",
@OneToMany(mappedBy = "applicationForm", cascade = [CascadeType.ALL], orphanRemoval = true)
var formElementSections: MutableList<FormElementSection> = mutableListOf(),
@OneToMany(mappedBy = "applicationForm", cascade = [CascadeType.ALL], orphanRemoval = true)
@OneToMany(mappedBy = "applicationForm", cascade = [CascadeType.ALL])
var versions: MutableList<ApplicationFormVersion> = mutableListOf(),
@Column(nullable = false)
var isTemplate: Boolean,

View File

@@ -113,17 +113,17 @@ class ApplicationFormController(
@PreAuthorize(
"hasAnyRole('CHIEF_EXECUTIVE_OFFICER', 'BUSINESS_DEPARTMENT', 'IT_DEPARTMENT', 'HUMAN_RESOURCES', 'HEAD_OF_WORKS_COUNCIL', 'WORKS_COUNCIL')",
)
override fun addFormElementToSection(
override fun addFormElementToSubSection(
applicationFormId: UUID,
sectionId: UUID,
subsectionId: UUID,
position: Int,
createFormElementDto: CreateFormElementDto,
): ResponseEntity<ApplicationFormDto> =
ResponseEntity.status(201).body(
applicationFormMapper.toApplicationFormDto(
applicationFormService.addFormElementToSection(
applicationFormService.addFormElementToSubSection(
applicationFormId,
sectionId,
subsectionId,
createFormElementDto,
position,
),

View File

@@ -34,23 +34,26 @@ class ApplicationFormMapper(
status = applicationForm.status,
)
fun toApplicationForm(applicationForm: ApplicationFormDto): ApplicationForm =
ApplicationForm(
id = applicationForm.id,
name = applicationForm.name,
formElementSections =
applicationForm.formElementSections
.map {
formElementSectionMapper.toFormElementSection(it)
}.toMutableList(),
isTemplate = applicationForm.isTemplate,
organizationId = applicationForm.organizationId,
status = applicationForm.status,
createdBy = userMapper.toUser(applicationForm.createdBy),
lastModifiedBy = userMapper.toUser(applicationForm.lastModifiedBy),
createdAt = applicationForm.createdAt,
modifiedAt = applicationForm.modifiedAt,
)
fun toApplicationForm(applicationForm: ApplicationFormDto): ApplicationForm {
val form =
ApplicationForm(
id = applicationForm.id,
name = applicationForm.name,
isTemplate = applicationForm.isTemplate,
organizationId = applicationForm.organizationId,
status = applicationForm.status,
createdBy = userMapper.toUser(applicationForm.createdBy),
lastModifiedBy = userMapper.toUser(applicationForm.lastModifiedBy),
createdAt = applicationForm.createdAt,
modifiedAt = applicationForm.modifiedAt,
)
form.formElementSections =
applicationForm.formElementSections
.map {
formElementSectionMapper.toFormElementSection(it, form)
}.toMutableList()
return form
}
fun toApplicationForm(createApplicationFormDto: CreateApplicationFormDto): ApplicationForm {
val currentUser = userService.getCurrentUser()

View File

@@ -6,7 +6,6 @@ import com.betriebsratkanzlei.legalconsenthub.error.ApplicationFormNotCreatedExc
import com.betriebsratkanzlei.legalconsenthub.error.ApplicationFormNotDeletedException
import com.betriebsratkanzlei.legalconsenthub.error.ApplicationFormNotFoundException
import com.betriebsratkanzlei.legalconsenthub.error.ApplicationFormNotUpdatedException
import com.betriebsratkanzlei.legalconsenthub.error.FormElementSectionNotFoundException
import com.betriebsratkanzlei.legalconsenthub.form_element.FormElementMapper
import com.betriebsratkanzlei.legalconsenthub.notification.NotificationService
import com.betriebsratkanzlei.legalconsenthub.user.UserService
@@ -57,6 +56,8 @@ class ApplicationFormService(
fun updateApplicationForm(applicationFormDto: ApplicationFormDto): ApplicationForm {
val existingApplicationForm = getApplicationFormById(applicationFormDto.id)
val existingSnapshot = versionService.createSnapshot(existingApplicationForm)
val applicationForm = applicationFormMapper.toApplicationForm(applicationFormDto)
val updatedApplicationForm: ApplicationForm
@@ -67,7 +68,9 @@ class ApplicationFormService(
}
val currentUser = userService.getCurrentUser()
if (versionService.hasChanges(existingApplicationForm, updatedApplicationForm)) {
val newSnapshot = versionService.createSnapshot(updatedApplicationForm)
if (existingSnapshot != newSnapshot) {
versionService.createVersion(updatedApplicationForm, currentUser)
}
@@ -132,25 +135,26 @@ class ApplicationFormService(
notificationService.createNotificationForOrganization(createNotificationDto)
}
fun addFormElementToSection(
fun addFormElementToSubSection(
applicationFormId: UUID,
sectionId: UUID,
subsectionId: UUID,
createFormElementDto: CreateFormElementDto,
position: Int,
): ApplicationForm {
val applicationForm = getApplicationFormById(applicationFormId)
val section =
val subsection =
applicationForm.formElementSections
.find { it.id == sectionId }
?: throw FormElementSectionNotFoundException(sectionId)
.flatMap { it.formElementSubSections }
.find { it.id == subsectionId }
?: throw IllegalArgumentException("FormElementSubSection with id $subsectionId not found")
val newFormElement = formElementMapper.toFormElement(createFormElementDto, section)
val newFormElement = formElementMapper.toFormElement(createFormElementDto, subsection)
if (position >= 0 && position < section.formElements.size) {
section.formElements.add(position, newFormElement)
if (position >= 0 && position < subsection.formElements.size) {
subsection.formElements.add(position, newFormElement)
} else {
section.formElements.add(newFormElement)
subsection.formElements.add(newFormElement)
}
val updatedApplicationForm =

View File

@@ -6,11 +6,13 @@ import com.betriebsratkanzlei.legalconsenthub.error.ApplicationFormNotFoundExcep
import com.betriebsratkanzlei.legalconsenthub.error.ApplicationFormVersionNotFoundException
import com.betriebsratkanzlei.legalconsenthub.form_element.FormElement
import com.betriebsratkanzlei.legalconsenthub.form_element.FormElementSection
import com.betriebsratkanzlei.legalconsenthub.form_element.FormElementSubSection
import com.betriebsratkanzlei.legalconsenthub.form_element.FormOption
import com.betriebsratkanzlei.legalconsenthub.user.User
import com.betriebsratkanzlei.legalconsenthub_api.model.ApplicationFormSnapshotDto
import com.betriebsratkanzlei.legalconsenthub_api.model.FormElementSectionSnapshotDto
import com.betriebsratkanzlei.legalconsenthub_api.model.FormElementSnapshotDto
import com.betriebsratkanzlei.legalconsenthub_api.model.FormElementSubSectionSnapshotDto
import com.betriebsratkanzlei.legalconsenthub_api.model.FormOptionDto
import com.fasterxml.jackson.databind.ObjectMapper
import org.springframework.stereotype.Service
@@ -47,15 +49,6 @@ class ApplicationFormVersionService(
return versionRepository.save(version)
}
fun hasChanges(
existingForm: ApplicationForm,
newForm: ApplicationForm,
): Boolean {
val existingSnapshot = createSnapshot(existingForm)
val newSnapshot = createSnapshot(newForm)
return existingSnapshot != newSnapshot
}
fun getVersionsByApplicationFormId(applicationFormId: UUID): List<ApplicationFormVersion> =
versionRepository.findByApplicationFormIdOrderByVersionNumberDesc(applicationFormId)
@@ -99,7 +92,7 @@ class ApplicationFormVersionService(
return restoredForm
}
private fun createSnapshot(applicationForm: ApplicationForm): ApplicationFormSnapshotDto =
fun createSnapshot(applicationForm: ApplicationForm): ApplicationFormSnapshotDto =
ApplicationFormSnapshotDto(
name = applicationForm.name,
status = applicationForm.status,
@@ -110,19 +103,26 @@ class ApplicationFormVersionService(
title = section.title,
shortTitle = section.shortTitle,
description = section.description,
elements =
section.formElements.map { element ->
FormElementSnapshotDto(
title = element.title,
description = element.description,
type = element.type,
options =
element.options.map { option ->
FormOptionDto(
value = option.value,
label = option.label,
processingPurpose = option.processingPurpose,
employeeDataCategory = option.employeeDataCategory,
subsections =
section.formElementSubSections.map { subsection ->
FormElementSubSectionSnapshotDto(
title = subsection.title,
subtitle = subsection.subtitle,
elements =
subsection.formElements.map { element ->
FormElementSnapshotDto(
title = element.title,
description = element.description,
type = element.type,
options =
element.options.map { option ->
FormOptionDto(
value = option.value,
label = option.label,
processingPurpose = option.processingPurpose,
employeeDataCategory = option.employeeDataCategory,
)
},
)
},
)
@@ -143,25 +143,36 @@ class ApplicationFormVersionService(
applicationForm = applicationForm,
)
sectionSnapshot.elements.forEach { elementSnapshot ->
val element =
FormElement(
title = elementSnapshot.title,
description = elementSnapshot.description,
type = elementSnapshot.type,
sectionSnapshot.subsections.forEach { subsectionSnapshot ->
val subsection =
FormElementSubSection(
title = subsectionSnapshot.title,
subtitle = subsectionSnapshot.subtitle,
formElementSection = section,
options =
elementSnapshot.options
.map { optionDto ->
FormOption(
value = optionDto.value,
label = optionDto.label,
processingPurpose = optionDto.processingPurpose,
employeeDataCategory = optionDto.employeeDataCategory,
)
}.toMutableList(),
)
section.formElements.add(element)
subsectionSnapshot.elements.forEach { elementSnapshot ->
val element =
FormElement(
title = elementSnapshot.title,
description = elementSnapshot.description,
type = elementSnapshot.type,
formElementSubSection = subsection,
options =
elementSnapshot.options
.map { optionDto ->
FormOption(
value = optionDto.value,
label = optionDto.label,
processingPurpose = optionDto.processingPurpose,
employeeDataCategory = optionDto.employeeDataCategory,
)
}.toMutableList(),
)
subsection.formElements.add(element)
}
section.formElementSubSections.add(subsection)
}
return section

View File

@@ -24,6 +24,6 @@ class FormElement(
@Column(nullable = false)
var type: FormElementType,
@ManyToOne
@JoinColumn(name = "form_element_section_id", nullable = false)
var formElementSection: FormElementSection? = null,
@JoinColumn(name = "form_element_sub_section_id", nullable = false)
var formElementSubSection: FormElementSubSection? = null,
)

View File

@@ -1,6 +1,5 @@
package com.betriebsratkanzlei.legalconsenthub.form_element
import com.betriebsratkanzlei.legalconsenthub.error.FormElementSectionNotFoundException
import com.betriebsratkanzlei.legalconsenthub_api.model.CreateFormElementDto
import com.betriebsratkanzlei.legalconsenthub_api.model.FormElementDto
import org.springframework.stereotype.Component
@@ -8,39 +7,35 @@ import org.springframework.stereotype.Component
@Component
class FormElementMapper(
private val formOptionMapper: FormOptionMapper,
private val formElementSectionRepository: FormElementSectionRepository,
) {
fun toFormElementDto(formElement: FormElement): FormElementDto =
FormElementDto(
id = formElement.id ?: throw IllegalStateException("ApplicationForm ID must not be null!"),
id = formElement.id ?: throw IllegalStateException("FormElement ID must not be null!"),
title = formElement.title,
description = formElement.description,
options = formElement.options.map { formOptionMapper.toFormOptionDto(it) },
type = formElement.type,
formElementSectionId =
formElement.formElementSection?.id
?: throw IllegalStateException("FormElementSection ID must not be null!"),
formElementSubSectionId =
formElement.formElementSubSection?.id
?: throw IllegalStateException("FormElementSubSection ID must not be null!"),
)
fun toFormElement(formElement: FormElementDto): FormElement {
val formElementSection =
formElementSectionRepository
.findById(formElement.formElementSectionId)
.orElseThrow { FormElementSectionNotFoundException(formElement.formElementSectionId) }
return FormElement(
fun toFormElement(
formElement: FormElementDto,
formElementSubSection: FormElementSubSection,
): FormElement =
FormElement(
id = formElement.id,
title = formElement.title,
description = formElement.description,
options = formElement.options.map { formOptionMapper.toFormOption(it) }.toMutableList(),
type = formElement.type,
formElementSection = formElementSection,
formElementSubSection = formElementSubSection,
)
}
fun toFormElement(
formElement: CreateFormElementDto,
formElementSection: FormElementSection,
formElementSubSection: FormElementSubSection,
): FormElement =
FormElement(
id = null,
@@ -48,6 +43,6 @@ class FormElementMapper(
description = formElement.description,
options = formElement.options.map { formOptionMapper.toFormOption(it) }.toMutableList(),
type = formElement.type,
formElementSection = formElementSection,
formElementSubSection = formElementSubSection,
)
}

View File

@@ -22,8 +22,8 @@ class FormElementSection(
var shortTitle: String? = null,
var description: String? = null,
@OneToMany(mappedBy = "formElementSection", cascade = [CascadeType.ALL], orphanRemoval = true)
@OrderColumn(name = "form_element_order")
var formElements: MutableList<FormElement> = mutableListOf(),
@OrderColumn(name = "form_element_sub_section_order")
var formElementSubSections: MutableList<FormElementSubSection> = mutableListOf(),
@ManyToOne
@JoinColumn(name = "application_form_id", nullable = false)
var applicationForm: ApplicationForm? = null,

View File

@@ -1,16 +1,13 @@
package com.betriebsratkanzlei.legalconsenthub.form_element
import com.betriebsratkanzlei.legalconsenthub.application_form.ApplicationForm
import com.betriebsratkanzlei.legalconsenthub.application_form.ApplicationFormRepository
import com.betriebsratkanzlei.legalconsenthub.error.ApplicationFormNotFoundException
import com.betriebsratkanzlei.legalconsenthub_api.model.CreateFormElementSectionDto
import com.betriebsratkanzlei.legalconsenthub_api.model.FormElementSectionDto
import org.springframework.stereotype.Component
@Component
class FormElementSectionMapper(
private val formElementMapper: FormElementMapper,
private val applicationFormRepository: ApplicationFormRepository,
private val formElementSubSectionMapper: FormElementSubSectionMapper,
) {
fun toFormElementSectionDto(formElementSection: FormElementSection): FormElementSectionDto =
FormElementSectionDto(
@@ -18,26 +15,32 @@ class FormElementSectionMapper(
title = formElementSection.title,
description = formElementSection.description,
shortTitle = formElementSection.shortTitle,
formElements = formElementSection.formElements.map { formElementMapper.toFormElementDto(it) },
formElementSubSections =
formElementSection.formElementSubSections.map {
formElementSubSectionMapper.toFormElementSubSectionDto(it)
},
applicationFormId =
formElementSection.applicationForm?.id
?: throw IllegalStateException("ApplicationForm ID must not be null!"),
)
fun toFormElementSection(formElementSection: FormElementSectionDto): FormElementSection {
val applicationForm =
applicationFormRepository
.findById(formElementSection.applicationFormId)
.orElseThrow { ApplicationFormNotFoundException(formElementSection.applicationFormId) }
return FormElementSection(
id = formElementSection.id,
title = formElementSection.title,
description = formElementSection.description,
shortTitle = formElementSection.shortTitle,
formElements = formElementSection.formElements.map { formElementMapper.toFormElement(it) }.toMutableList(),
applicationForm = applicationForm,
)
fun toFormElementSection(
formElementSection: FormElementSectionDto,
applicationForm: ApplicationForm,
): FormElementSection {
val section =
FormElementSection(
id = formElementSection.id,
title = formElementSection.title,
description = formElementSection.description,
shortTitle = formElementSection.shortTitle,
applicationForm = applicationForm,
)
section.formElementSubSections =
formElementSection.formElementSubSections
.map { formElementSubSectionMapper.toFormElementSubSection(it, section) }
.toMutableList()
return section
}
fun toFormElementSection(
@@ -51,9 +54,9 @@ class FormElementSectionMapper(
shortTitle = createFormElementSection.shortTitle,
applicationForm = applicationForm,
)
formElementSection.formElements =
createFormElementSection.formElements
.map { formElementMapper.toFormElement(it, formElementSection) }
formElementSection.formElementSubSections =
createFormElementSection.formElementSubSections
.map { formElementSubSectionMapper.toFormElementSubSection(it, formElementSection) }
.toMutableList()
return formElementSection
}

View File

@@ -0,0 +1,28 @@
package com.betriebsratkanzlei.legalconsenthub.form_element
import jakarta.persistence.CascadeType
import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.GeneratedValue
import jakarta.persistence.Id
import jakarta.persistence.JoinColumn
import jakarta.persistence.ManyToOne
import jakarta.persistence.OneToMany
import jakarta.persistence.OrderColumn
import java.util.UUID
@Entity
class FormElementSubSection(
@Id
@GeneratedValue
var id: UUID? = null,
@Column(nullable = false)
var title: String,
var subtitle: String? = null,
@OneToMany(mappedBy = "formElementSubSection", cascade = [CascadeType.ALL], orphanRemoval = true)
@OrderColumn(name = "form_element_order")
var formElements: MutableList<FormElement> = mutableListOf(),
@ManyToOne
@JoinColumn(name = "form_element_section_id", nullable = false)
var formElementSection: FormElementSection? = null,
)

View File

@@ -0,0 +1,56 @@
package com.betriebsratkanzlei.legalconsenthub.form_element
import com.betriebsratkanzlei.legalconsenthub_api.model.CreateFormElementSubSectionDto
import com.betriebsratkanzlei.legalconsenthub_api.model.FormElementSubSectionDto
import org.springframework.stereotype.Component
@Component
class FormElementSubSectionMapper(
private val formElementMapper: FormElementMapper,
) {
fun toFormElementSubSectionDto(formElementSubSection: FormElementSubSection): FormElementSubSectionDto =
FormElementSubSectionDto(
id = formElementSubSection.id ?: throw IllegalStateException("FormElementSubSection ID must not be null!"),
title = formElementSubSection.title,
subtitle = formElementSubSection.subtitle,
formElements = formElementSubSection.formElements.map { formElementMapper.toFormElementDto(it) },
formElementSectionId =
formElementSubSection.formElementSection?.id
?: throw IllegalStateException("FormElementSection ID must not be null!"),
)
fun toFormElementSubSection(
formElementSubSection: FormElementSubSectionDto,
formElementSection: FormElementSection,
): FormElementSubSection {
val subsection =
FormElementSubSection(
id = formElementSubSection.id,
title = formElementSubSection.title,
subtitle = formElementSubSection.subtitle,
formElementSection = formElementSection,
)
subsection.formElements =
formElementSubSection.formElements
.map { formElementMapper.toFormElement(it, subsection) }
.toMutableList()
return subsection
}
fun toFormElementSubSection(
createFormElementSubSection: CreateFormElementSubSectionDto,
formElementSection: FormElementSection,
): FormElementSubSection {
val formElementSubSection =
FormElementSubSection(
title = createFormElementSubSection.title,
subtitle = createFormElementSubSection.subtitle,
formElementSection = formElementSection,
)
formElementSubSection.formElements =
createFormElementSubSection.formElements
.map { formElementMapper.toFormElement(it, formElementSubSection) }
.toMutableList()
return formElementSubSection
}
}