feat(fullstack): Add file upload
This commit is contained in:
@@ -6,9 +6,56 @@ 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 {
|
||||
return await applicationFormApi.createApplicationForm(applicationFormDto)
|
||||
// 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)
|
||||
|
||||
Reference in New Issue
Block a user