diff --git a/shop/lib/actions/companies.ts b/shop/lib/actions/companies.ts index 7aab64b..92dcc4d 100644 --- a/shop/lib/actions/companies.ts +++ b/shop/lib/actions/companies.ts @@ -286,34 +286,49 @@ export async function syncCustomersFromLicServer() { // 2. Fetch customers for each company for (const company of companies) { try { - const res = await fetch(`${cfg.base_url}/api-v1/partners/${company.id}/customers?pageSize=1000`, { - headers: { 'X-Api-Key': cfg.api_key, 'Accept': 'application/json' }, - signal: AbortSignal.timeout(10_000), - }) + // Workaround for LicServer crash on null/empty search parameter: + // We query using common vowels/umlauts in parallel and de-duplicate locally. + const searchChars = ['a', 'e', 'i', 'o', 'u', 'ä', 'ö', 'ü', 'y'] + const customerMap = new Map() - if (res.ok) { - const result = await res.json() - const customers = result.items || [] + await Promise.all( + searchChars.map(async char => { + try { + const res = await fetch(`${cfg.base_url}/api-v1/partners/${company.id}/customers?pageSize=1000&search=${encodeURIComponent(char)}`, { + headers: { 'X-Api-Key': cfg.api_key!, 'Accept': 'application/json' }, + signal: AbortSignal.timeout(10_000), + }) + if (res.ok) { + const result = await res.json() + const items = result.items || [] + for (const c of items) { + customerMap.set(c.id, c) + } + } + } catch (e: any) { + console.error(`[sync-customers] Error fetching with search=${char} for partner ${company.id}:`, e.message || e) + } + }) + ) - for (const c of customers) { - const streetCombined = [c.street, c.houseNumber, c.houseNumberAddon] - .filter(Boolean) - .map(s => s.trim()) - .join(' ') + const customers = Array.from(customerMap.values()) - allUpsertRows.push({ - id: c.id, - partner_id: company.id, - company_name: c.name || 'Unbenannt', - street: streetCombined || null, - zip: c.zipCode || null, - city: c.city || null, - email: c.email || null, - is_anonymized: false, - }) - } - } else { - console.warn(`[sync-customers] Skipping partner ${company.id} due to API status: ${res.status}`) + for (const c of customers) { + const streetCombined = [c.street, c.houseNumber, c.houseNumberAddon] + .filter(Boolean) + .map(s => s.trim()) + .join(' ') + + allUpsertRows.push({ + id: c.id, + partner_id: company.id, + company_name: c.name || 'Unbenannt', + street: streetCombined || null, + zip: c.zipCode || null, + city: c.city || null, + email: c.email || null, + is_anonymized: false, + }) } } catch (e: any) { console.error(`[sync-customers] Error fetching customers for partner ${company.id}:`, e.message || e)