import { useUserStore } from '~~/stores/useUserStore' import { useTestDataApi } from '~/composables/testing/useTestDataApi' /** * TESTING-ONLY FEATURE * * One-click duplicator for the seeded demo application form "SAP S/4HANA". * * This version uses a dedicated backend endpoint to ensure reliability: * 1. Loads the seeded YAML on the backend. * 2. Creates a fresh ApplicationForm entity with new IDs. * 3. Sets current user as creator. * 4. Assigns the form to the currently selected organization. */ export function useSeededSapS4HanaDuplicator() { const { t } = useI18n() const logger = useLogger().withTag('seeded-sap-duplicator') const toast = useToast() const { canWriteApplicationForms } = usePermissions() const userStore = useUserStore() const { selectedOrganization } = storeToRefs(userStore) const isDuplicating = ref(false) const showButton = computed(() => canWriteApplicationForms.value) async function duplicateSapS4HanaForTesting() { if (isDuplicating.value) return const organizationId = selectedOrganization.value?.id if (!organizationId) { toast.add({ title: t('common.error'), description: 'Please select an organization first.', color: 'error' }) return } isDuplicating.value = true try { const { createTestDataApplicationForm } = useTestDataApi() const created = await createTestDataApplicationForm(organizationId) toast.add({ title: t('common.success'), description: 'Created a new test application form.', color: 'success' }) if (created?.id) { await navigateTo(`/application-forms/${created.id}/0`) } } catch (e: unknown) { logger.error('Failed creating test application form via backend:', e) toast.add({ title: t('common.error'), description: 'Failed to create test application form. Check backend logs.', color: 'error' }) } finally { isDuplicating.value = false } } return { showButton, isDuplicating, duplicateSapS4HanaForTesting } }