feat(fullstack): Add application form status, add submissions of forms, update DB schema
This commit is contained in:
@@ -2,12 +2,15 @@ package com.betriebsratkanzlei.legalconsenthub.application_form
|
||||
|
||||
import com.betriebsratkanzlei.legalconsenthub.form_element.FormElementSection
|
||||
import com.betriebsratkanzlei.legalconsenthub.user.User
|
||||
import com.betriebsratkanzlei.legalconsenthub_api.model.ApplicationFormStatus
|
||||
import jakarta.persistence.AttributeOverride
|
||||
import jakarta.persistence.AttributeOverrides
|
||||
import jakarta.persistence.CascadeType
|
||||
import jakarta.persistence.Column
|
||||
import jakarta.persistence.Entity
|
||||
import jakarta.persistence.EntityListeners
|
||||
import jakarta.persistence.Enumerated
|
||||
import jakarta.persistence.EnumType
|
||||
import jakarta.persistence.GeneratedValue
|
||||
import jakarta.persistence.Id
|
||||
import jakarta.persistence.OneToMany
|
||||
@@ -36,6 +39,10 @@ class ApplicationForm(
|
||||
|
||||
var organizationId: String = "",
|
||||
|
||||
@Enumerated(EnumType.STRING)
|
||||
@Column(nullable = false)
|
||||
var status: ApplicationFormStatus = ApplicationFormStatus.DRAFT,
|
||||
|
||||
@Embedded
|
||||
@AttributeOverrides(
|
||||
AttributeOverride(name = "id", column = Column(name = "created_by_id", nullable = false)),
|
||||
|
||||
@@ -77,4 +77,12 @@ class ApplicationFormController(
|
||||
applicationFormService.deleteApplicationFormByID(id)
|
||||
return ResponseEntity.noContent().build()
|
||||
}
|
||||
|
||||
override fun submitApplicationForm(id: UUID): ResponseEntity<ApplicationFormDto> {
|
||||
return ResponseEntity.ok(
|
||||
applicationFormMapper.toApplicationFormDto(
|
||||
applicationFormService.submitApplicationForm(id)
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,8 @@ class ApplicationFormMapper(private val formElementSectionMapper: FormElementSec
|
||||
createdBy = userMapper.toUserDto(applicationForm.createdBy),
|
||||
lastModifiedBy = userMapper.toUserDto(applicationForm.lastModifiedBy),
|
||||
createdAt = applicationForm.createdAt ?: LocalDateTime.now(),
|
||||
modifiedAt = applicationForm.modifiedAt ?: LocalDateTime.now()
|
||||
modifiedAt = applicationForm.modifiedAt ?: LocalDateTime.now(),
|
||||
status = applicationForm.status
|
||||
)
|
||||
}
|
||||
|
||||
@@ -33,6 +34,7 @@ class ApplicationFormMapper(private val formElementSectionMapper: FormElementSec
|
||||
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,
|
||||
@@ -50,6 +52,7 @@ class ApplicationFormMapper(private val formElementSectionMapper: FormElementSec
|
||||
name = createApplicationFormDto.name,
|
||||
isTemplate = createApplicationFormDto.isTemplate,
|
||||
organizationId = createApplicationFormDto.organizationId ?: "",
|
||||
status = createApplicationFormDto.status ?: com.betriebsratkanzlei.legalconsenthub_api.model.ApplicationFormStatus.DRAFT,
|
||||
createdBy = createdBy,
|
||||
lastModifiedBy = lastModifiedBy,
|
||||
)
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
package com.betriebsratkanzlei.legalconsenthub.application_form
|
||||
|
||||
import com.betriebsratkanzlei.legalconsenthub.error.ApplicationFormInvalidStateException
|
||||
import com.betriebsratkanzlei.legalconsenthub.error.ApplicationFormNotCreatedException
|
||||
import com.betriebsratkanzlei.legalconsenthub.error.ApplicationFormNotDeletedException
|
||||
import com.betriebsratkanzlei.legalconsenthub.error.ApplicationFormNotFoundException
|
||||
import com.betriebsratkanzlei.legalconsenthub.error.ApplicationFormNotUpdatedException
|
||||
import com.betriebsratkanzlei.legalconsenthub_api.model.ApplicationFormDto
|
||||
import com.betriebsratkanzlei.legalconsenthub_api.model.ApplicationFormStatus
|
||||
import com.betriebsratkanzlei.legalconsenthub_api.model.CreateApplicationFormDto
|
||||
import org.springframework.data.domain.Page
|
||||
import org.springframework.data.domain.PageRequest
|
||||
@@ -59,4 +61,25 @@ class ApplicationFormService(
|
||||
throw ApplicationFormNotDeletedException(e)
|
||||
}
|
||||
}
|
||||
|
||||
fun submitApplicationForm(id: UUID): ApplicationForm {
|
||||
val applicationForm = getApplicationFormById(id)
|
||||
|
||||
if (applicationForm.status != ApplicationFormStatus.DRAFT) {
|
||||
throw ApplicationFormInvalidStateException(
|
||||
applicationFormId = id,
|
||||
currentState = applicationForm.status,
|
||||
expectedState = ApplicationFormStatus.DRAFT,
|
||||
operation = "submit"
|
||||
)
|
||||
}
|
||||
|
||||
applicationForm.status = ApplicationFormStatus.SUBMITTED
|
||||
|
||||
return try {
|
||||
applicationFormRepository.save(applicationForm)
|
||||
} catch (e: Exception) {
|
||||
throw ApplicationFormNotUpdatedException(e, id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.betriebsratkanzlei.legalconsenthub.error
|
||||
|
||||
import com.betriebsratkanzlei.legalconsenthub_api.model.ApplicationFormStatus
|
||||
import java.util.UUID
|
||||
|
||||
class ApplicationFormInvalidStateException(
|
||||
val applicationFormId: UUID,
|
||||
val currentState: ApplicationFormStatus,
|
||||
val expectedState: ApplicationFormStatus,
|
||||
val operation: String
|
||||
) : RuntimeException("Cannot $operation application form with ID $applicationFormId. Current state: $currentState, expected state: $expectedState")
|
||||
@@ -31,6 +31,22 @@ class ExceptionHandler {
|
||||
)
|
||||
}
|
||||
|
||||
@ResponseBody
|
||||
@ExceptionHandler(ApplicationFormInvalidStateException::class)
|
||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||
fun handleInvalidStateError(e: ApplicationFormInvalidStateException): ResponseEntity<ProblemDetails> {
|
||||
logger.warn(e.message, e)
|
||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).contentType(MediaType.APPLICATION_PROBLEM_JSON)
|
||||
.body(
|
||||
ProblemDetails(
|
||||
title = "Invalid State",
|
||||
status = HttpStatus.BAD_REQUEST.value(),
|
||||
type = URI.create("about:blank"),
|
||||
detail = e.message ?: "Operation not allowed in current state"
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
@ResponseBody
|
||||
@ExceptionHandler(
|
||||
ApplicationFormNotCreatedException::class,
|
||||
|
||||
@@ -10,18 +10,20 @@ create table application_form
|
||||
last_modified_by_name varchar(255) not null,
|
||||
name varchar(255) not null,
|
||||
organization_id varchar(255),
|
||||
status enum ('APPROVED','DRAFT','REJECTED','SIGNED','SUBMITTED') not null,
|
||||
primary key (id)
|
||||
);
|
||||
|
||||
create table comment
|
||||
(
|
||||
created_at timestamp(6) not null,
|
||||
modified_at timestamp(6) not null,
|
||||
form_element_id uuid not null,
|
||||
id uuid not null,
|
||||
created_by_id varchar(255) not null,
|
||||
created_by_name varchar(255) not null,
|
||||
message varchar(255) not null,
|
||||
created_at timestamp(6) not null,
|
||||
modified_at timestamp(6) not null,
|
||||
application_form_id uuid not null,
|
||||
form_element_id uuid not null,
|
||||
id uuid not null,
|
||||
created_by_id varchar(255) not null,
|
||||
created_by_name varchar(255) not null,
|
||||
message varchar(255) not null,
|
||||
primary key (id)
|
||||
);
|
||||
|
||||
@@ -36,12 +38,29 @@ create table form_element_options
|
||||
|
||||
create table form_element
|
||||
(
|
||||
type tinyint not null check (type between 0 and 4),
|
||||
application_form_id uuid not null,
|
||||
id uuid not null,
|
||||
type tinyint not null check (type between 0 and 4),
|
||||
form_element_section_id uuid not null,
|
||||
id uuid not null,
|
||||
description varchar(255),
|
||||
title varchar(255),
|
||||
primary key (id)
|
||||
);
|
||||
|
||||
create table form_element_section
|
||||
(
|
||||
application_form_id uuid not null,
|
||||
id uuid not null,
|
||||
description varchar(255),
|
||||
short_title varchar(255),
|
||||
title varchar(255) not null,
|
||||
primary key (id)
|
||||
);
|
||||
|
||||
alter table if exists comment
|
||||
add constraint FKlavy9axrt26sepreg5lqtuoap
|
||||
foreign key (application_form_id)
|
||||
references application_form;
|
||||
|
||||
alter table if exists comment
|
||||
add constraint FKfg84w0i76tw9os13950272c6f
|
||||
foreign key (form_element_id)
|
||||
@@ -53,6 +72,11 @@ alter table if exists form_element_options
|
||||
references form_element;
|
||||
|
||||
alter table if exists form_element
|
||||
add constraint FKdniyq3l10lncw48tft15js5gb
|
||||
add constraint FKdpr6k93m4hqllqjsvoa4or6mp
|
||||
foreign key (form_element_section_id)
|
||||
references form_element_section;
|
||||
|
||||
alter table if exists form_element_section
|
||||
add constraint FKtn0lreovauwf2v29doo70o3qs
|
||||
foreign key (application_form_id)
|
||||
references application_form;
|
||||
|
||||
Reference in New Issue
Block a user