155 lines
4.9 KiB
TypeScript
155 lines
4.9 KiB
TypeScript
import type { ApplicationFormDto, PagedApplicationFormDto, FormElementDto } from '~~/.api-client'
|
|
import { useApplicationFormApi } from './useApplicationFormApi'
|
|
import { useLogger } from '../useLogger'
|
|
|
|
export function useApplicationForm() {
|
|
const applicationFormApi = useApplicationFormApi()
|
|
const logger = useLogger().withTag('applicationForm')
|
|
|
|
/**
|
|
* Extract all file IDs from FILE_UPLOAD form elements
|
|
*/
|
|
function extractFileIdsFromForm(applicationFormDto: ApplicationFormDto): string[] {
|
|
const fileIds: string[] = []
|
|
|
|
applicationFormDto.formElementSections?.forEach((section) => {
|
|
section.formElementSubSections?.forEach((subsection) => {
|
|
subsection.formElements?.forEach((element) => {
|
|
if (element.type === 'FILE_UPLOAD') {
|
|
element.options?.forEach((option) => {
|
|
try {
|
|
const metadata = JSON.parse(option.value)
|
|
if (metadata.fileId) {
|
|
fileIds.push(metadata.fileId)
|
|
}
|
|
} catch {
|
|
// Ignore parsing errors
|
|
}
|
|
})
|
|
}
|
|
})
|
|
})
|
|
})
|
|
|
|
return fileIds
|
|
}
|
|
|
|
/**
|
|
* Creates an application form with atomic file association.
|
|
*
|
|
* File IDs are included in the DTO and associated atomically on the backend.
|
|
* If file association fails, the entire operation rolls back (form is not created).
|
|
*/
|
|
async function createApplicationForm(applicationFormDto: ApplicationFormDto): Promise<ApplicationFormDto> {
|
|
try {
|
|
// Extract all temporary file IDs and include them in the DTO for atomic association
|
|
const fileIds = extractFileIdsFromForm(applicationFormDto)
|
|
|
|
// Single atomic API call - backend handles form creation and file association transactionally
|
|
const createdForm = await applicationFormApi.createApplicationForm({
|
|
...applicationFormDto,
|
|
fileIds: fileIds.length > 0 ? fileIds : undefined
|
|
})
|
|
|
|
if (fileIds.length > 0) {
|
|
logger.debug(`Created form ${createdForm.id} with ${fileIds.length} files atomically associated`)
|
|
}
|
|
|
|
return createdForm
|
|
} catch (e: unknown) {
|
|
logger.error('Failed creating application form:', e)
|
|
return Promise.reject(e)
|
|
}
|
|
}
|
|
|
|
async function getAllApplicationForms(organizationId: string): Promise<PagedApplicationFormDto> {
|
|
try {
|
|
return await applicationFormApi.getAllApplicationForms(organizationId)
|
|
} catch (e: unknown) {
|
|
logger.error('Failed retrieving application forms:', e)
|
|
return Promise.reject(e)
|
|
}
|
|
}
|
|
|
|
async function getApplicationFormById(id: string): Promise<ApplicationFormDto> {
|
|
try {
|
|
return await applicationFormApi.getApplicationFormById(id)
|
|
} catch (e: unknown) {
|
|
logger.error(`Failed retrieving application form with ID ${id}:`, e)
|
|
return Promise.reject(e)
|
|
}
|
|
}
|
|
|
|
async function updateApplicationForm(
|
|
id?: string,
|
|
applicationFormDto?: ApplicationFormDto
|
|
): Promise<ApplicationFormDto> {
|
|
if (!id || !applicationFormDto) {
|
|
return Promise.reject(new Error('ID or application form DTO missing'))
|
|
}
|
|
|
|
logger.debug('Updating application form with ID:', id, applicationFormDto)
|
|
try {
|
|
return await applicationFormApi.updateApplicationForm(id, applicationFormDto)
|
|
} catch (e: unknown) {
|
|
logger.error(`Failed updating application form with ID ${id}:`, e)
|
|
return Promise.reject(e)
|
|
}
|
|
}
|
|
|
|
async function deleteApplicationFormById(id: string): Promise<void> {
|
|
try {
|
|
return await applicationFormApi.deleteApplicationFormById(id)
|
|
} catch (e: unknown) {
|
|
logger.error(`Failed deleting application form with ID ${id}:`, e)
|
|
return Promise.reject(e)
|
|
}
|
|
}
|
|
|
|
async function submitApplicationForm(id: string): Promise<ApplicationFormDto> {
|
|
if (!id) {
|
|
return Promise.reject(new Error('ID missing'))
|
|
}
|
|
|
|
try {
|
|
return await applicationFormApi.submitApplicationForm(id)
|
|
} catch (e: unknown) {
|
|
logger.error(`Failed submitting application form with ID ${id}:`, e)
|
|
return Promise.reject(e)
|
|
}
|
|
}
|
|
|
|
async function addFormElementToSubSection(
|
|
applicationFormId: string,
|
|
subsectionId: string,
|
|
formElementDto: FormElementDto,
|
|
position: number
|
|
): Promise<ApplicationFormDto> {
|
|
if (!applicationFormId || !subsectionId) {
|
|
return Promise.reject(new Error('Application form ID or subsection ID missing'))
|
|
}
|
|
|
|
try {
|
|
return await applicationFormApi.addFormElementToSubSection(
|
|
applicationFormId,
|
|
subsectionId,
|
|
formElementDto,
|
|
position
|
|
)
|
|
} catch (e: unknown) {
|
|
logger.error(`Failed adding form element to subsection ${subsectionId}:`, e)
|
|
return Promise.reject(e)
|
|
}
|
|
}
|
|
|
|
return {
|
|
createApplicationForm,
|
|
getAllApplicationForms,
|
|
getApplicationFormById,
|
|
updateApplicationForm,
|
|
deleteApplicationFormById,
|
|
submitApplicationForm,
|
|
addFormElementToSubSection
|
|
}
|
|
}
|