fix(sync): query customers by vowels to avoid crash
All checks were successful
Staging Build / build (push) Successful in 2m56s
All checks were successful
Staging Build / build (push) Successful in 2m56s
This commit is contained in:
@@ -286,14 +286,32 @@ 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.
|
||||||
|
const searchChars = ['a', 'e', 'i', 'o', 'u', 'ä', 'ö', 'ü', 'y']
|
||||||
|
const customerMap = new Map<string, any>()
|
||||||
|
|
||||||
|
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),
|
signal: AbortSignal.timeout(10_000),
|
||||||
})
|
})
|
||||||
|
|
||||||
if (res.ok) {
|
if (res.ok) {
|
||||||
const result = await res.json()
|
const result = await res.json()
|
||||||
const customers = result.items || []
|
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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
|
const customers = Array.from(customerMap.values())
|
||||||
|
|
||||||
for (const c of customers) {
|
for (const c of customers) {
|
||||||
const streetCombined = [c.street, c.houseNumber, c.houseNumberAddon]
|
const streetCombined = [c.street, c.houseNumber, c.houseNumberAddon]
|
||||||
@@ -312,9 +330,6 @@ export async function syncCustomersFromLicServer() {
|
|||||||
is_anonymized: false,
|
is_anonymized: false,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
console.warn(`[sync-customers] Skipping partner ${company.id} due to API status: ${res.status}`)
|
|
||||||
}
|
|
||||||
} 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)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user