fix(sync): query customers by vowels to avoid crash
All checks were successful
Staging Build / build (push) Successful in 2m56s

This commit is contained in:
DanielS
2026-07-15 23:50:13 +02:00
parent 5d3eed874c
commit 13f647409f

View File

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