feat(#5): Add title-body control element that can be added dynamically, refactored sectionIndex/create

This commit is contained in:
2025-11-02 10:32:46 +01:00
parent 4d371be2e3
commit 736cd17789
12 changed files with 407 additions and 88 deletions

View File

@@ -3,6 +3,7 @@ package com.betriebsratkanzlei.legalconsenthub.application_form
import com.betriebsratkanzlei.legalconsenthub_api.api.ApplicationFormApi
import com.betriebsratkanzlei.legalconsenthub_api.model.ApplicationFormDto
import com.betriebsratkanzlei.legalconsenthub_api.model.CreateApplicationFormDto
import com.betriebsratkanzlei.legalconsenthub_api.model.CreateFormElementDto
import com.betriebsratkanzlei.legalconsenthub_api.model.PagedApplicationFormDto
import org.springframework.core.io.ByteArrayResource
import org.springframework.core.io.Resource
@@ -108,4 +109,24 @@ class ApplicationFormController(
applicationFormService.submitApplicationForm(id),
),
)
@PreAuthorize(
"hasAnyRole('CHIEF_EXECUTIVE_OFFICER', 'BUSINESS_DEPARTMENT', 'IT_DEPARTMENT', 'HUMAN_RESOURCES', 'HEAD_OF_WORKS_COUNCIL', 'WORKS_COUNCIL')",
)
override fun addFormElementToSection(
applicationFormId: UUID,
sectionId: UUID,
position: Int,
createFormElementDto: CreateFormElementDto,
): ResponseEntity<ApplicationFormDto> =
ResponseEntity.status(201).body(
applicationFormMapper.toApplicationFormDto(
applicationFormService.addFormElementToSection(
applicationFormId,
sectionId,
createFormElementDto,
position,
),
),
)
}

View File

@@ -5,10 +5,13 @@ 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_api.model.ApplicationFormDto
import com.betriebsratkanzlei.legalconsenthub_api.model.ApplicationFormStatus
import com.betriebsratkanzlei.legalconsenthub_api.model.CreateApplicationFormDto
import com.betriebsratkanzlei.legalconsenthub_api.model.CreateFormElementDto
import com.betriebsratkanzlei.legalconsenthub_api.model.CreateNotificationDto
import com.betriebsratkanzlei.legalconsenthub_api.model.NotificationType
import org.springframework.data.domain.Page
@@ -20,6 +23,7 @@ import java.util.UUID
class ApplicationFormService(
private val applicationFormRepository: ApplicationFormRepository,
private val applicationFormMapper: ApplicationFormMapper,
private val formElementMapper: FormElementMapper,
private val notificationService: NotificationService,
) {
fun createApplicationForm(createApplicationFormDto: CreateApplicationFormDto): ApplicationForm {
@@ -45,7 +49,6 @@ class ApplicationFormService(
}
fun updateApplicationForm(applicationFormDto: ApplicationFormDto): ApplicationForm {
// TODO find statt mappen?
val applicationForm = applicationFormMapper.toApplicationForm(applicationFormDto)
val updatedApplicationForm: ApplicationForm
@@ -112,4 +115,35 @@ class ApplicationFormService(
notificationService.createNotificationForOrganization(createNotificationDto)
}
fun addFormElementToSection(
applicationFormId: UUID,
sectionId: UUID,
createFormElementDto: CreateFormElementDto,
position: Int,
): ApplicationForm {
val applicationForm = getApplicationFormById(applicationFormId)
val section =
applicationForm.formElementSections
.find { it.id == sectionId }
?: throw FormElementSectionNotFoundException(sectionId)
val newFormElement = formElementMapper.toFormElement(createFormElementDto, section)
if (position >= 0 && position < section.formElements.size) {
section.formElements.add(position, newFormElement)
} else {
section.formElements.add(newFormElement)
}
val updatedApplicationForm =
try {
applicationFormRepository.save(applicationForm)
} catch (e: Exception) {
throw ApplicationFormNotUpdatedException(e, applicationFormId)
}
return updatedApplicationForm
}
}