KI-AGENT: Ergänze Mandantenexport und Import
This commit is contained in:
@@ -48,6 +48,13 @@ export type AdminOverview = {
|
||||
unassignedProfiles: AdminUserProfile[]
|
||||
}
|
||||
|
||||
export type TenantImportResult = {
|
||||
success: boolean
|
||||
tenantId: number
|
||||
tables: { table: string; rows: number }[]
|
||||
files: { restored: number; skipped: number }
|
||||
}
|
||||
|
||||
export const useAdmin = () => {
|
||||
const { $api } = useNuxtApp()
|
||||
|
||||
@@ -110,6 +117,19 @@ export const useAdmin = () => {
|
||||
})
|
||||
}
|
||||
|
||||
const exportTenant = async (id: number): Promise<Blob> => {
|
||||
return await $api(`/api/admin/tenants/${id}/export`, {
|
||||
responseType: "blob",
|
||||
})
|
||||
}
|
||||
|
||||
const importTenant = async (body: Record<string, any>): Promise<TenantImportResult> => {
|
||||
return await $api("/api/admin/tenant-imports", {
|
||||
method: "POST",
|
||||
body,
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
getOverview,
|
||||
createUser,
|
||||
@@ -119,5 +139,7 @@ export const useAdmin = () => {
|
||||
createTenant,
|
||||
invitePortalUser,
|
||||
updateTenant,
|
||||
exportTenant,
|
||||
importTenant,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,9 +10,19 @@ const admin = useAdmin()
|
||||
const tenantId = Number(route.params.id)
|
||||
const loading = ref(true)
|
||||
const saving = ref(false)
|
||||
const exportingTenant = ref(false)
|
||||
const importingTenant = ref(false)
|
||||
const creatingUser = ref(false)
|
||||
const createUserModalOpen = ref(false)
|
||||
const createdUserPassword = ref("")
|
||||
const importFileInput = ref<HTMLInputElement | null>(null)
|
||||
const lastImportResult = ref<null | {
|
||||
tenantId: number
|
||||
tableCount: number
|
||||
rowCount: number
|
||||
restoredFiles: number
|
||||
skippedFiles: number
|
||||
}>(null)
|
||||
|
||||
const tenantForm = ref<AdminTenant | null>(null)
|
||||
const assignedUsers = ref<AdminUser[]>([])
|
||||
@@ -79,6 +89,88 @@ const saveTenant = async () => {
|
||||
}
|
||||
}
|
||||
|
||||
const downloadTenantExport = async () => {
|
||||
if (!tenantForm.value || exportingTenant.value) return
|
||||
|
||||
exportingTenant.value = true
|
||||
|
||||
try {
|
||||
const blob = await admin.exportTenant(tenantForm.value.id)
|
||||
const objectUrl = URL.createObjectURL(blob)
|
||||
const link = document.createElement("a")
|
||||
const safeName = (tenantForm.value.short || tenantForm.value.name || tenantForm.value.id)
|
||||
.toString()
|
||||
.trim()
|
||||
.replace(/[^a-z0-9_-]+/gi, "-")
|
||||
.replace(/^-+|-+$/g, "")
|
||||
.toLowerCase()
|
||||
|
||||
link.href = objectUrl
|
||||
link.download = `fedeo-tenant-${safeName || tenantForm.value.id}-${new Date().toISOString().slice(0, 10)}.json`
|
||||
document.body.appendChild(link)
|
||||
link.click()
|
||||
link.remove()
|
||||
URL.revokeObjectURL(objectUrl)
|
||||
|
||||
toast.add({ title: "Mandantenexport erstellt", color: "green" })
|
||||
} catch (err: any) {
|
||||
console.error("[administration/tenants/export]", err)
|
||||
toast.add({
|
||||
title: "Mandant konnte nicht exportiert werden",
|
||||
description: err?.data?.error || err?.message || "Unbekannter Fehler",
|
||||
color: "red",
|
||||
})
|
||||
} finally {
|
||||
exportingTenant.value = false
|
||||
}
|
||||
}
|
||||
|
||||
const openImportFileDialog = () => {
|
||||
importFileInput.value?.click()
|
||||
}
|
||||
|
||||
const importTenantExport = async (event: Event) => {
|
||||
const input = event.target as HTMLInputElement
|
||||
const file = input.files?.[0]
|
||||
if (!file || importingTenant.value) return
|
||||
|
||||
importingTenant.value = true
|
||||
lastImportResult.value = null
|
||||
|
||||
try {
|
||||
const rawContent = await file.text()
|
||||
const exportData = JSON.parse(rawContent)
|
||||
const result = await admin.importTenant(exportData)
|
||||
const rowCount = (result.tables || []).reduce((sum, table) => sum + table.rows, 0)
|
||||
|
||||
lastImportResult.value = {
|
||||
tenantId: result.tenantId,
|
||||
tableCount: result.tables?.length || 0,
|
||||
rowCount,
|
||||
restoredFiles: result.files?.restored || 0,
|
||||
skippedFiles: result.files?.skipped || 0,
|
||||
}
|
||||
|
||||
await fetchTenant()
|
||||
|
||||
toast.add({
|
||||
title: "Mandantenimport abgeschlossen",
|
||||
description: `${rowCount} Datensätze und ${lastImportResult.value.restoredFiles} Dateien verarbeitet.`,
|
||||
color: "green",
|
||||
})
|
||||
} catch (err: any) {
|
||||
console.error("[administration/tenants/import]", err)
|
||||
toast.add({
|
||||
title: "Mandant konnte nicht importiert werden",
|
||||
description: err?.data?.error || err?.message || "Unbekannter Fehler",
|
||||
color: "red",
|
||||
})
|
||||
} finally {
|
||||
importingTenant.value = false
|
||||
input.value = ""
|
||||
}
|
||||
}
|
||||
|
||||
const createTenantUser = async () => {
|
||||
if (!tenantForm.value || creatingUser.value) return
|
||||
|
||||
@@ -186,6 +278,59 @@ onMounted(async () => {
|
||||
</UForm>
|
||||
</UCard>
|
||||
|
||||
<UCard v-if="!loading && tenantForm" class="mt-3">
|
||||
<USeparator label="Backup und Umzug" />
|
||||
|
||||
<div class="grid grid-cols-1 md:grid-cols-2 gap-4 mt-4">
|
||||
<div class="border border-gray-200 dark:border-gray-800 rounded-lg p-4">
|
||||
<div class="font-medium">Full Export</div>
|
||||
<p class="text-sm text-gray-500 mt-1">
|
||||
Erstellt eine portable JSON-Datei mit Mandantendaten, Benutzerzuordnungen, Rollen, Profilen und Dateiinhalten.
|
||||
</p>
|
||||
<UButton
|
||||
class="mt-4"
|
||||
icon="i-heroicons-arrow-down-tray"
|
||||
:loading="exportingTenant"
|
||||
@click="downloadTenantExport"
|
||||
>
|
||||
Export herunterladen
|
||||
</UButton>
|
||||
</div>
|
||||
|
||||
<div class="border border-gray-200 dark:border-gray-800 rounded-lg p-4">
|
||||
<div class="font-medium">Import</div>
|
||||
<p class="text-sm text-gray-500 mt-1">
|
||||
Spielt einen FEDEO-Mandantenexport auf diesem Server ein. Bestehende Datensätze mit gleicher ID werden übersprungen.
|
||||
</p>
|
||||
<input
|
||||
ref="importFileInput"
|
||||
type="file"
|
||||
accept="application/json,.json"
|
||||
class="hidden"
|
||||
@change="importTenantExport"
|
||||
>
|
||||
<UButton
|
||||
class="mt-4"
|
||||
icon="i-heroicons-arrow-up-tray"
|
||||
color="warning"
|
||||
:loading="importingTenant"
|
||||
@click="openImportFileDialog"
|
||||
>
|
||||
Export importieren
|
||||
</UButton>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<UAlert
|
||||
v-if="lastImportResult"
|
||||
class="mt-4"
|
||||
title="Letzter Import"
|
||||
:description="`Tenant ${lastImportResult.tenantId}: ${lastImportResult.rowCount} Datensätze aus ${lastImportResult.tableCount} Tabellen verarbeitet, ${lastImportResult.restoredFiles} Dateien wiederhergestellt, ${lastImportResult.skippedFiles} Dateien übersprungen.`"
|
||||
color="green"
|
||||
variant="soft"
|
||||
/>
|
||||
</UCard>
|
||||
|
||||
<UCard v-if="!loading && tenantForm" class="mt-3">
|
||||
<USeparator label="Zugeordnete Benutzer" />
|
||||
|
||||
|
||||
@@ -8,7 +8,9 @@ const admin = useAdmin()
|
||||
|
||||
const loading = ref(true)
|
||||
const creatingTenant = ref(false)
|
||||
const importingTenant = ref(false)
|
||||
const createTenantModalOpen = ref(false)
|
||||
const importFileInput = ref<HTMLInputElement | null>(null)
|
||||
const tenants = ref<AdminTenant[]>([])
|
||||
const searchString = ref("")
|
||||
|
||||
@@ -89,6 +91,46 @@ const createTenant = async () => {
|
||||
}
|
||||
}
|
||||
|
||||
const openImportFileDialog = () => {
|
||||
importFileInput.value?.click()
|
||||
}
|
||||
|
||||
const importTenantExport = async (event: Event) => {
|
||||
const input = event.target as HTMLInputElement
|
||||
const file = input.files?.[0]
|
||||
if (!file || importingTenant.value) return
|
||||
|
||||
importingTenant.value = true
|
||||
|
||||
try {
|
||||
const exportData = JSON.parse(await file.text())
|
||||
const result = await admin.importTenant(exportData)
|
||||
const rowCount = (result.tables || []).reduce((sum, table) => sum + table.rows, 0)
|
||||
|
||||
await fetchTenants()
|
||||
|
||||
toast.add({
|
||||
title: "Mandantenimport abgeschlossen",
|
||||
description: `Tenant ${result.tenantId}: ${rowCount} Datensätze und ${result.files?.restored || 0} Dateien verarbeitet.`,
|
||||
color: "green",
|
||||
})
|
||||
|
||||
if (result.tenantId) {
|
||||
await router.push(`/administration/tenants/${result.tenantId}`)
|
||||
}
|
||||
} catch (err: any) {
|
||||
console.error("[administration/tenants/import]", err)
|
||||
toast.add({
|
||||
title: "Mandant konnte nicht importiert werden",
|
||||
description: err?.data?.error || err?.message || "Unbekannter Fehler",
|
||||
color: "red",
|
||||
})
|
||||
} finally {
|
||||
importingTenant.value = false
|
||||
input.value = ""
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(async () => {
|
||||
if (!auth.user?.is_admin) {
|
||||
await router.push("/")
|
||||
@@ -111,6 +153,22 @@ onMounted(async () => {
|
||||
<UButton icon="i-heroicons-plus" @click="createTenantModalOpen = true">
|
||||
Tenant
|
||||
</UButton>
|
||||
<input
|
||||
ref="importFileInput"
|
||||
type="file"
|
||||
accept="application/json,.json"
|
||||
class="hidden"
|
||||
@change="importTenantExport"
|
||||
>
|
||||
<UButton
|
||||
icon="i-heroicons-arrow-up-tray"
|
||||
color="warning"
|
||||
variant="soft"
|
||||
:loading="importingTenant"
|
||||
@click="openImportFileDialog"
|
||||
>
|
||||
Import
|
||||
</UButton>
|
||||
</template>
|
||||
</UDashboardNavbar>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user