feat(admin): add manual partner import from lic server
All checks were successful
Staging Build / build (push) Successful in 2m48s
All checks were successful
Staging Build / build (push) Successful in 2m48s
This commit is contained in:
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
import { useState, useEffect } from 'react'
|
import { useState, useEffect } from 'react'
|
||||||
import Link from 'next/link'
|
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 { Button } from '@/components/ui/button'
|
||||||
import { Input } from '@/components/ui/input'
|
import { Input } from '@/components/ui/input'
|
||||||
import { Label } from '@/components/ui/label'
|
import { Label } from '@/components/ui/label'
|
||||||
@@ -14,7 +14,7 @@ import {
|
|||||||
DialogHeader,
|
DialogHeader,
|
||||||
DialogTitle,
|
DialogTitle,
|
||||||
} from '@/components/ui/dialog'
|
} 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() {
|
export default function CompaniesPage() {
|
||||||
const [companies, setCompanies] = useState<any[]>([])
|
const [companies, setCompanies] = useState<any[]>([])
|
||||||
@@ -32,6 +32,20 @@ export default function CompaniesPage() {
|
|||||||
const [createError, setCreateError] = useState<string | null>(null)
|
const [createError, setCreateError] = useState<string | null>(null)
|
||||||
const [deletingId, setDeletingId] = useState<string | null>(null)
|
const [deletingId, setDeletingId] = useState<string | null>(null)
|
||||||
const [searchQuery, setSearchQuery] = useState('')
|
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.
|
Verwalten Sie Firmen, deren Lieferadresse und weisen Sie Partner zu.
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<Button
|
<div className="flex items-center gap-3">
|
||||||
onClick={() => handleOpenDialog()}
|
<Button
|
||||||
className="bg-blue-600 hover:bg-blue-700 text-white font-semibold"
|
onClick={handleSync}
|
||||||
>
|
disabled={syncing}
|
||||||
<Plus className="w-4 h-4 mr-2" /> Firma anlegen
|
variant="outline"
|
||||||
</Button>
|
className="border-slate-200 dark:border-white/10 text-slate-900 dark:text-white"
|
||||||
|
>
|
||||||
|
{syncing ? <Loader2 className="w-4 h-4 mr-2 animate-spin" /> : <RefreshCw className="w-4 h-4 mr-2" />}
|
||||||
|
Partner importieren
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
onClick={() => handleOpenDialog()}
|
||||||
|
className="bg-blue-600 hover:bg-blue-700 text-white font-semibold"
|
||||||
|
>
|
||||||
|
<Plus className="w-4 h-4 mr-2" /> Firma anlegen
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Firmen-Tabelle */}
|
{/* Firmen-Tabelle */}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
'use server'
|
'use server'
|
||||||
import { createAdminClient } from '@/lib/supabase/admin'
|
import { createAdminClient } from '@/lib/supabase/admin'
|
||||||
import { revalidatePath, unstable_noStore as noStore } from 'next/cache'
|
import { revalidatePath, unstable_noStore as noStore } from 'next/cache'
|
||||||
|
import { getLicServerConfig } from './licserver-config'
|
||||||
|
|
||||||
export async function getCompanies() {
|
export async function getCompanies() {
|
||||||
noStore()
|
noStore()
|
||||||
@@ -219,3 +220,45 @@ export async function adminDeleteEndCustomer(id: string) {
|
|||||||
return true
|
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 }
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user