From 3fb80e6404e33f66f4ca6db6feb38445fb0848a3 Mon Sep 17 00:00:00 2001 From: DanielS Date: Wed, 15 Jul 2026 00:07:31 +0200 Subject: [PATCH] feat(admin): add manual partner import from lic server --- shop/app/admin/companies/page.tsx | 41 +++++++++++++++++++++++------ shop/lib/actions/companies.ts | 43 +++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 8 deletions(-) diff --git a/shop/app/admin/companies/page.tsx b/shop/app/admin/companies/page.tsx index da853e0..fa24a8c 100644 --- a/shop/app/admin/companies/page.tsx +++ b/shop/app/admin/companies/page.tsx @@ -2,7 +2,7 @@ import { useState, useEffect } from 'react' import Link from 'next/link' -import { Building2, Plus, Trash2, Loader2, Pencil, Mail, MapPin, Search, Users } from 'lucide-react' +import { Building2, Plus, Trash2, Loader2, Pencil, Mail, MapPin, Search, Users, RefreshCw } from 'lucide-react' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Label } from '@/components/ui/label' @@ -14,7 +14,7 @@ import { DialogHeader, DialogTitle, } from '@/components/ui/dialog' -import { getCompanies, createCompany, updateCompany, deleteCompany } from '@/lib/actions/companies' +import { getCompanies, createCompany, updateCompany, deleteCompany, syncCompaniesFromLicServer } from '@/lib/actions/companies' export default function CompaniesPage() { const [companies, setCompanies] = useState([]) @@ -32,6 +32,20 @@ export default function CompaniesPage() { const [createError, setCreateError] = useState(null) const [deletingId, setDeletingId] = useState(null) const [searchQuery, setSearchQuery] = useState('') + const [syncing, setSyncing] = useState(false) + + const handleSync = async () => { + setSyncing(true) + try { + const res = await syncCompaniesFromLicServer() + alert(`${res.count} Partner erfolgreich vom LicServer importiert/aktualisiert.`) + loadCompanies() + } catch (err: any) { + alert(`Fehler beim Import: ${err.message || err}`) + } finally { + setSyncing(false) + } + } @@ -123,12 +137,23 @@ export default function CompaniesPage() { Verwalten Sie Firmen, deren Lieferadresse und weisen Sie Partner zu.

- +
+ + +
{/* Firmen-Tabelle */} diff --git a/shop/lib/actions/companies.ts b/shop/lib/actions/companies.ts index 3eef828..c495518 100644 --- a/shop/lib/actions/companies.ts +++ b/shop/lib/actions/companies.ts @@ -1,6 +1,7 @@ 'use server' import { createAdminClient } from '@/lib/supabase/admin' import { revalidatePath, unstable_noStore as noStore } from 'next/cache' +import { getLicServerConfig } from './licserver-config' export async function getCompanies() { noStore() @@ -219,3 +220,45 @@ export async function adminDeleteEndCustomer(id: string) { return true } +export async function syncCompaniesFromLicServer() { + const admin = createAdminClient() + const cfg = await getLicServerConfig() + + if (!cfg.base_url || !cfg.api_key) { + throw new Error('LicServer ist nicht konfiguriert.') + } + + const res = await fetch(`${cfg.base_url}/api-v1/partners?pageSize=1000`, { + headers: { 'X-Api-Key': cfg.api_key, 'Accept': 'application/json' }, + signal: AbortSignal.timeout(10_000), + }) + + if (!res.ok) { + throw new Error(`Lizenzserver antwortete mit Fehler: HTTP ${res.status}`) + } + + const result = await res.json() + const partners = result.items || [] + + if (partners.length === 0) { + return { success: true, count: 0 } + } + + const upsertRows = partners.map((p: any) => ({ + id: p.id, + name: p.name || 'Unbenannt', + email: p.email || null, + })) + + const { error } = await admin + .from('companies') + .upsert(upsertRows, { onConflict: 'id' }) + + if (error) throw error + + revalidatePath('/admin/companies') + revalidatePath('/admin/users') + + return { success: true, count: partners.length } +} +