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

@@ -224,10 +224,10 @@ paths:
"503": "503":
$ref: "https://api.swaggerhub.com/domains/smartbear-public/ProblemDetails/1.0.0#/components/responses/ServiceUnavailable" $ref: "https://api.swaggerhub.com/domains/smartbear-public/ProblemDetails/1.0.0#/components/responses/ServiceUnavailable"
/application-forms/{applicationFormId}/sections/{sectionId}/form-elements: /application-forms/{applicationFormId}/subsections/{subsectionId}/form-elements:
post: post:
summary: Add a new form element to a specific section summary: Add a new form element to a specific subsection
operationId: addFormElementToSection operationId: addFormElementToSubSection
tags: tags:
- application-form - application-form
parameters: parameters:
@@ -238,13 +238,13 @@ paths:
type: string type: string
format: uuid format: uuid
description: The ID of the application form description: The ID of the application form
- name: sectionId - name: subsectionId
in: path in: path
required: true required: true
schema: schema:
type: string type: string
format: uuid format: uuid
description: The ID of the form element section description: The ID of the form element subsection
- name: position - name: position
in: query in: query
required: true required: true
@@ -1215,7 +1215,7 @@ components:
required: required:
- id - id
- title - title
- formElements - formElementSubSections
- applicationFormId - applicationFormId
properties: properties:
id: id:
@@ -1227,10 +1227,10 @@ components:
type: string type: string
description: description:
type: string type: string
formElements: formElementSubSections:
type: array type: array
items: items:
$ref: "#/components/schemas/FormElementDto" $ref: "#/components/schemas/FormElementSubSectionDto"
applicationFormId: applicationFormId:
type: string type: string
format: uuid format: uuid
@@ -1239,7 +1239,7 @@ components:
type: object type: object
required: required:
- title - title
- elements - subsections
properties: properties:
title: title:
type: string type: string
@@ -1247,16 +1247,16 @@ components:
type: string type: string
description: description:
type: string type: string
elements: subsections:
type: array type: array
items: items:
$ref: "#/components/schemas/FormElementSnapshotDto" $ref: "#/components/schemas/FormElementSubSectionSnapshotDto"
CreateFormElementSectionDto: CreateFormElementSectionDto:
type: object type: object
required: required:
- title - title
- formElements - formElementSubSections
properties: properties:
id: id:
type: string type: string
@@ -1267,6 +1267,59 @@ components:
type: string type: string
description: description:
type: string type: string
formElementSubSections:
type: array
items:
$ref: "#/components/schemas/CreateFormElementSubSectionDto"
FormElementSubSectionDto:
type: object
required:
- id
- title
- formElements
- formElementSectionId
properties:
id:
type: string
format: uuid
title:
type: string
subtitle:
type: string
formElements:
type: array
items:
$ref: "#/components/schemas/FormElementDto"
formElementSectionId:
type: string
format: uuid
FormElementSubSectionSnapshotDto:
type: object
required:
- title
- elements
properties:
title:
type: string
subtitle:
type: string
elements:
type: array
items:
$ref: "#/components/schemas/FormElementSnapshotDto"
CreateFormElementSubSectionDto:
type: object
required:
- title
- formElements
properties:
title:
type: string
subtitle:
type: string
formElements: formElements:
type: array type: array
items: items:
@@ -1278,7 +1331,7 @@ components:
- id - id
- options - options
- type - type
- formElementSectionId - formElementSubSectionId
properties: properties:
id: id:
type: string type: string
@@ -1293,7 +1346,7 @@ components:
$ref: "#/components/schemas/FormOptionDto" $ref: "#/components/schemas/FormOptionDto"
type: type:
$ref: "#/components/schemas/FormElementType" $ref: "#/components/schemas/FormElementType"
formElementSectionId: formElementSubSectionId:
type: string type: string
format: uuid format: uuid

View File

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

View File

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

View File

@@ -34,23 +34,26 @@ class ApplicationFormMapper(
status = applicationForm.status, status = applicationForm.status,
) )
fun toApplicationForm(applicationForm: ApplicationFormDto): ApplicationForm = fun toApplicationForm(applicationForm: ApplicationFormDto): ApplicationForm {
ApplicationForm( val form =
id = applicationForm.id, ApplicationForm(
name = applicationForm.name, id = applicationForm.id,
formElementSections = name = applicationForm.name,
applicationForm.formElementSections isTemplate = applicationForm.isTemplate,
.map { organizationId = applicationForm.organizationId,
formElementSectionMapper.toFormElementSection(it) status = applicationForm.status,
}.toMutableList(), createdBy = userMapper.toUser(applicationForm.createdBy),
isTemplate = applicationForm.isTemplate, lastModifiedBy = userMapper.toUser(applicationForm.lastModifiedBy),
organizationId = applicationForm.organizationId, createdAt = applicationForm.createdAt,
status = applicationForm.status, modifiedAt = applicationForm.modifiedAt,
createdBy = userMapper.toUser(applicationForm.createdBy), )
lastModifiedBy = userMapper.toUser(applicationForm.lastModifiedBy), form.formElementSections =
createdAt = applicationForm.createdAt, applicationForm.formElementSections
modifiedAt = applicationForm.modifiedAt, .map {
) formElementSectionMapper.toFormElementSection(it, form)
}.toMutableList()
return form
}
fun toApplicationForm(createApplicationFormDto: CreateApplicationFormDto): ApplicationForm { fun toApplicationForm(createApplicationFormDto: CreateApplicationFormDto): ApplicationForm {
val currentUser = userService.getCurrentUser() 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.ApplicationFormNotDeletedException
import com.betriebsratkanzlei.legalconsenthub.error.ApplicationFormNotFoundException import com.betriebsratkanzlei.legalconsenthub.error.ApplicationFormNotFoundException
import com.betriebsratkanzlei.legalconsenthub.error.ApplicationFormNotUpdatedException import com.betriebsratkanzlei.legalconsenthub.error.ApplicationFormNotUpdatedException
import com.betriebsratkanzlei.legalconsenthub.error.FormElementSectionNotFoundException
import com.betriebsratkanzlei.legalconsenthub.form_element.FormElementMapper import com.betriebsratkanzlei.legalconsenthub.form_element.FormElementMapper
import com.betriebsratkanzlei.legalconsenthub.notification.NotificationService import com.betriebsratkanzlei.legalconsenthub.notification.NotificationService
import com.betriebsratkanzlei.legalconsenthub.user.UserService import com.betriebsratkanzlei.legalconsenthub.user.UserService
@@ -57,6 +56,8 @@ class ApplicationFormService(
fun updateApplicationForm(applicationFormDto: ApplicationFormDto): ApplicationForm { fun updateApplicationForm(applicationFormDto: ApplicationFormDto): ApplicationForm {
val existingApplicationForm = getApplicationFormById(applicationFormDto.id) val existingApplicationForm = getApplicationFormById(applicationFormDto.id)
val existingSnapshot = versionService.createSnapshot(existingApplicationForm)
val applicationForm = applicationFormMapper.toApplicationForm(applicationFormDto) val applicationForm = applicationFormMapper.toApplicationForm(applicationFormDto)
val updatedApplicationForm: ApplicationForm val updatedApplicationForm: ApplicationForm
@@ -67,7 +68,9 @@ class ApplicationFormService(
} }
val currentUser = userService.getCurrentUser() val currentUser = userService.getCurrentUser()
if (versionService.hasChanges(existingApplicationForm, updatedApplicationForm)) { val newSnapshot = versionService.createSnapshot(updatedApplicationForm)
if (existingSnapshot != newSnapshot) {
versionService.createVersion(updatedApplicationForm, currentUser) versionService.createVersion(updatedApplicationForm, currentUser)
} }
@@ -132,25 +135,26 @@ class ApplicationFormService(
notificationService.createNotificationForOrganization(createNotificationDto) notificationService.createNotificationForOrganization(createNotificationDto)
} }
fun addFormElementToSection( fun addFormElementToSubSection(
applicationFormId: UUID, applicationFormId: UUID,
sectionId: UUID, subsectionId: UUID,
createFormElementDto: CreateFormElementDto, createFormElementDto: CreateFormElementDto,
position: Int, position: Int,
): ApplicationForm { ): ApplicationForm {
val applicationForm = getApplicationFormById(applicationFormId) val applicationForm = getApplicationFormById(applicationFormId)
val section = val subsection =
applicationForm.formElementSections applicationForm.formElementSections
.find { it.id == sectionId } .flatMap { it.formElementSubSections }
?: throw FormElementSectionNotFoundException(sectionId) .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) { if (position >= 0 && position < subsection.formElements.size) {
section.formElements.add(position, newFormElement) subsection.formElements.add(position, newFormElement)
} else { } else {
section.formElements.add(newFormElement) subsection.formElements.add(newFormElement)
} }
val updatedApplicationForm = val updatedApplicationForm =

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -23,7 +23,7 @@
</UDropdownMenu> </UDropdownMenu>
</div> </div>
</div> </div>
<USeparator /> <USeparator v-if="index < props.modelValue.length - 1" />
</template> </template>
</template> </template>

View File

@@ -8,39 +8,49 @@
{{ currentFormElementSection.title }} {{ currentFormElementSection.title }}
</h1> </h1>
<UCard variant="subtle"> <template v-if="currentFormElementSection?.formElementSubSections">
<FormEngine <UCard
v-if="currentFormElementSection?.formElements" v-for="subsection in currentFormElementSection.formElementSubSections"
v-model="currentFormElementSection.formElements" :key="subsection.id"
:application-form-id="applicationFormId" variant="subtle"
:disabled="disabled" class="mb-6"
@add:input-form="handleAddInputForm" >
/> <div class="mb-4">
<h2 class="text-lg font-semibold text-highlighted">{{ subsection.title }}</h2>
<div class="flex gap-2 justify-between mt-4"> <p v-if="subsection.subtitle" class="text-sm text-dimmed">{{ subsection.subtitle }}</p>
<UButton leading-icon="i-lucide-arrow-left" :disabled="!stepper?.hasPrev" @click="handleNavigate('backward')">
Prev
</UButton>
<UButton
v-if="stepper?.hasNext"
trailing-icon="i-lucide-arrow-right"
:disabled="!stepper?.hasNext"
@click="handleNavigate('forward')"
>
Next
</UButton>
<div v-if="!stepper?.hasNext" class="flex flex-wrap items-center gap-1.5">
<UButton trailing-icon="i-lucide-save" :disabled="disabled" variant="outline" @click="emit('save')">
Save
</UButton>
<UButton trailing-icon="i-lucide-send-horizontal" :disabled="disabled" @click="emit('submit')">
Submit
</UButton>
</div> </div>
<FormEngine
v-model="subsection.formElements"
:application-form-id="applicationFormId"
:disabled="disabled"
@add:input-form="(position) => handleAddInputForm(position, subsection.id)"
/>
</UCard>
</template>
<div class="flex gap-2 justify-between">
<UButton leading-icon="i-lucide-arrow-left" :disabled="!stepper?.hasPrev" @click="handleNavigate('backward')">
Prev
</UButton>
<UButton
v-if="stepper?.hasNext"
trailing-icon="i-lucide-arrow-right"
:disabled="!stepper?.hasNext"
@click="handleNavigate('forward')"
>
Next
</UButton>
<div v-if="!stepper?.hasNext" class="flex flex-wrap items-center gap-1.5">
<UButton trailing-icon="i-lucide-save" :disabled="disabled" variant="outline" @click="emit('save')">
Save
</UButton>
<UButton trailing-icon="i-lucide-send-horizontal" :disabled="disabled" @click="emit('submit')">
Submit
</UButton>
</div> </div>
</UCard> </div>
</div> </div>
</template> </template>
@@ -65,16 +75,28 @@ const { stepper, activeStepperItemIndex, stepperItems, currentFormElementSection
computed(() => props.formElementSections) computed(() => props.formElementSections)
) )
const { addInputFormToApplicationForm } = useFormElementManagement(currentFormElementSection, props.applicationFormId)
onMounted(() => { onMounted(() => {
if (props.initialSectionIndex !== undefined) { if (props.initialSectionIndex !== undefined) {
activeStepperItemIndex.value = props.initialSectionIndex activeStepperItemIndex.value = props.initialSectionIndex
} }
}) })
async function handleAddInputForm(position: number) { async function handleAddInputForm(position: number, subsectionId: string) {
const updatedForm = await addInputFormToApplicationForm(position) const subsection = props.formElementSections
.flatMap((section) => section.formElementSubSections)
.find((sub) => sub.id === subsectionId)
if (!subsection) {
return
}
const { addFormElementToSubSection } = useFormElementManagement()
const updatedForm = await addFormElementToSubSection(
props.applicationFormId,
subsectionId,
subsection.formElements,
position
)
emit('add-input-form', updatedForm) emit('add-input-form', updatedForm)
} }

View File

@@ -76,25 +76,25 @@ export function useApplicationForm() {
} }
} }
async function addFormElementToSection( async function addFormElementToSubSection(
applicationFormId: string, applicationFormId: string,
sectionId: string, subsectionId: string,
createFormElementDto: CreateFormElementDto, createFormElementDto: CreateFormElementDto,
position: number position: number
): Promise<ApplicationFormDto> { ): Promise<ApplicationFormDto> {
if (!applicationFormId || !sectionId) { if (!applicationFormId || !subsectionId) {
return Promise.reject(new Error('Application form ID or section ID missing')) return Promise.reject(new Error('Application form ID or subsection ID missing'))
} }
try { try {
return await applicationFormApi.addFormElementToSection( return await applicationFormApi.addFormElementToSubSection(
applicationFormId, applicationFormId,
sectionId, subsectionId,
createFormElementDto, createFormElementDto,
position position
) )
} catch (e: unknown) { } catch (e: unknown) {
console.error(`Failed adding form element to section ${sectionId}:`, e) console.error(`Failed adding form element to subsection ${subsectionId}:`, e)
return Promise.reject(e) return Promise.reject(e)
} }
} }
@@ -106,6 +106,6 @@ export function useApplicationForm() {
updateApplicationForm, updateApplicationForm,
deleteApplicationFormById, deleteApplicationFormById,
submitApplicationForm, submitApplicationForm,
addFormElementToSection addFormElementToSubSection
} }
} }

View File

@@ -54,15 +54,15 @@ export function useApplicationFormApi() {
return applicationFormApiClient.submitApplicationForm({ id }) return applicationFormApiClient.submitApplicationForm({ id })
} }
async function addFormElementToSection( async function addFormElementToSubSection(
applicationFormId: string, applicationFormId: string,
sectionId: string, subsectionId: string,
createFormElementDto: CreateFormElementDto, createFormElementDto: CreateFormElementDto,
position: number position: number
): Promise<ApplicationFormDto> { ): Promise<ApplicationFormDto> {
return applicationFormApiClient.addFormElementToSection({ return applicationFormApiClient.addFormElementToSubSection({
applicationFormId, applicationFormId,
sectionId, subsectionId,
createFormElementDto, createFormElementDto,
position position
}) })
@@ -75,6 +75,6 @@ export function useApplicationFormApi() {
updateApplicationForm, updateApplicationForm,
deleteApplicationFormById, deleteApplicationFormById,
submitApplicationForm, submitApplicationForm,
addFormElementToSection addFormElementToSubSection
} }
} }

View File

@@ -1,18 +1,14 @@
import type { ApplicationFormDto, CreateFormElementDto, FormElementSectionDto } from '~~/.api-client' import type { ApplicationFormDto, CreateFormElementDto, FormElementDto } from '~~/.api-client'
import type { MaybeRefOrGetter } from 'vue'
export function useFormElementManagement( export function useFormElementManagement() {
currentFormElementSection: MaybeRefOrGetter<FormElementSectionDto | undefined>, const applicationForm = useApplicationForm()
applicationFormId?: string
) {
const { addFormElementToSection } = useApplicationForm()
async function addInputFormToApplicationForm(position: number): Promise<ApplicationFormDto | undefined> {
const section = toValue(currentFormElementSection)
if (!section) return
const { formElements } = section
async function addFormElementToSubSection(
applicationFormId: string | undefined,
subsectionId: string,
formElements: FormElementDto[],
position: number
): Promise<ApplicationFormDto | undefined> {
const inputFormElement: CreateFormElementDto = { const inputFormElement: CreateFormElementDto = {
title: 'Formular ergänzen', title: 'Formular ergänzen',
description: 'Bitte fügen Sie hier Ihre Ergänzungen ein.', description: 'Bitte fügen Sie hier Ihre Ergänzungen ein.',
@@ -29,7 +25,7 @@ export function useFormElementManagement(
if (applicationFormId) { if (applicationFormId) {
try { try {
return await addFormElementToSection(applicationFormId, section.id, inputFormElement, position + 1) return await applicationForm.addFormElementToSubSection(applicationFormId, subsectionId, inputFormElement, position + 1)
} catch (error) { } catch (error) {
console.error('Failed to add form element:', error) console.error('Failed to add form element:', error)
throw error throw error
@@ -42,6 +38,6 @@ export function useFormElementManagement(
} }
return { return {
addInputFormToApplicationForm addFormElementToSubSection
} }
} }

View File

@@ -83,7 +83,9 @@ const validationStatus = ref<ComplianceStatus>(ComplianceStatus.NonCritical)
const allFormElements = computed(() => { const allFormElements = computed(() => {
return ( return (
applicationForm.value?.formElementSections?.flatMap((section: FormElementSectionDto) => section.formElements) ?? [] applicationForm.value?.formElementSections?.flatMap((section: FormElementSectionDto) =>
section.formElementSubSections.flatMap((subsection) => subsection.formElements)
) ?? []
) )
}) })

View File

@@ -78,8 +78,8 @@ const validationStatus = ref<ComplianceStatus>(ComplianceStatus.NonCritical)
const allFormElements = computed(() => { const allFormElements = computed(() => {
return ( return (
applicationFormTemplate.value?.formElementSections?.flatMap( applicationFormTemplate.value?.formElementSections?.flatMap((section: FormElementSectionDto) =>
(section: FormElementSectionDto) => section.formElements section.formElementSubSections.flatMap((subsection) => subsection.formElements)
) ?? [] ) ?? []
) )
}) })

View File

@@ -72,8 +72,10 @@ export function compareApplicationForms(
function flattenFormElements(form: ApplicationFormDto): Array<{ element: FormElementDto; sectionTitle: string }> { function flattenFormElements(form: ApplicationFormDto): Array<{ element: FormElementDto; sectionTitle: string }> {
const elements: Array<{ element: FormElementDto; sectionTitle: string }> = [] const elements: Array<{ element: FormElementDto; sectionTitle: string }> = []
for (const section of form.formElementSections) { for (const section of form.formElementSections) {
for (const element of section.formElements) { for (const subsection of section.formElementSubSections) {
for (const element of subsection.formElements) {
elements.push({ element, sectionTitle: section.title }) elements.push({ element, sectionTitle: section.title })
}
} }
} }
return elements return elements
@@ -84,8 +86,10 @@ function flattenSnapshotElements(
): Array<{ element: FormElementSnapshotDto; sectionTitle: string }> { ): Array<{ element: FormElementSnapshotDto; sectionTitle: string }> {
const elements: Array<{ element: FormElementSnapshotDto; sectionTitle: string }> = [] const elements: Array<{ element: FormElementSnapshotDto; sectionTitle: string }> = []
for (const section of snapshot.sections) { for (const section of snapshot.sections) {
for (const element of section.elements) { for (const subsection of section.subsections) {
for (const element of subsection.elements) {
elements.push({ element, sectionTitle: section.title }) elements.push({ element, sectionTitle: section.title })
}
} }
} }
return elements return elements