diff --git a/app/composables/useMachineCreatePage.ts b/app/composables/useMachineCreatePage.ts index cd63389..89af5af 100644 --- a/app/composables/useMachineCreatePage.ts +++ b/app/composables/useMachineCreatePage.ts @@ -34,7 +34,7 @@ export function useMachineCreatePage() { // Composable calls // --------------------------------------------------------------------------- - const { createMachine, createMachineFromType, reconfigureSkeleton } = useMachines() + const { createMachine, createMachineFromType, reconfigureSkeleton, addMissingCustomFields, deleteMachine } = useMachines() const { sites, loadSites } = useSites() const { machineTypes, loadMachineTypes, loading: machineTypesLoading } = useMachineTypesApi() const { composants, loadComposants, loading: composantsLoading } = useComposants() @@ -340,17 +340,24 @@ export function useMachineCreatePage() { : await createMachineFromType(baseMachineData as any, type) if (result.success) { - if (hasRequirements && result.data?.id) { - const skeletonResult: any = await reconfigureSkeleton(result.data.id, { + const machineId = result.data?.id + if (hasRequirements && machineId) { + const skeletonResult: any = await reconfigureSkeleton(machineId, { componentLinks, pieceLinks, productLinks, } as any) if (!skeletonResult.success) { - toast.showError(skeletonResult.error || 'Impossible d\'enregistrer les pièces/composants') + // Rollback: delete the orphaned machine + await deleteMachine(machineId).catch(() => {}) + toast.showError(skeletonResult.error || 'Impossible d\'enregistrer les pièces/composants. La machine n\'a pas été créée.') return } } + // Initialize custom fields for the machine type + if (machineId) { + await addMissingCustomFields(machineId, { showToast: false }).catch(() => {}) + } newMachine.name = '' newMachine.siteId = '' newMachine.typeMachineId = '' @@ -386,9 +393,9 @@ export function useMachineCreatePage() { await Promise.all([ loadSites(), loadMachineTypes(), - loadComposants(), - loadPieces(), - loadProducts(), + loadComposants({ itemsPerPage: 200, force: true }), + loadPieces({ itemsPerPage: 200, force: true }), + loadProducts({ itemsPerPage: 200, force: true }), ]) })