fix: Add pagination to e-conomic API to fetch ALL invoices (not just first 1000)
This commit is contained in:
parent
c05b11387d
commit
cb7e209769
@ -470,33 +470,51 @@ class EconomicService:
|
||||
logger.info(f"📋 Trying invoice endpoint: {endpoint}")
|
||||
|
||||
try:
|
||||
async with session.get(
|
||||
endpoint,
|
||||
params={"pagesize": 1000},
|
||||
headers=self._get_headers()
|
||||
) as response:
|
||||
logger.info(f"🔍 [API] Response status from {endpoint}: {response.status}")
|
||||
|
||||
if response.status == 200:
|
||||
data = await response.json()
|
||||
invoices_from_endpoint = data.get('collection', [])
|
||||
logger.info(f"✅ Successfully fetched {len(invoices_from_endpoint)} invoices from {endpoint}")
|
||||
# Pagination: Keep fetching until no more pages
|
||||
page = 0
|
||||
while True:
|
||||
async with session.get(
|
||||
endpoint,
|
||||
params={"pagesize": 1000, "skippages": page},
|
||||
headers=self._get_headers()
|
||||
) as response:
|
||||
logger.info(f"🔍 [API] Response status from {endpoint} (page {page}): {response.status}")
|
||||
|
||||
# Add new invoices (avoid duplicates by tracking invoice numbers)
|
||||
existing_invoice_numbers = set()
|
||||
for inv in all_invoices:
|
||||
inv_num = inv.get('draftInvoiceNumber') or inv.get('bookedInvoiceNumber')
|
||||
if inv_num:
|
||||
existing_invoice_numbers.add(inv_num)
|
||||
|
||||
for inv in invoices_from_endpoint:
|
||||
inv_num = inv.get('draftInvoiceNumber') or inv.get('bookedInvoiceNumber')
|
||||
if inv_num and inv_num not in existing_invoice_numbers:
|
||||
all_invoices.append(inv)
|
||||
existing_invoice_numbers.add(inv_num)
|
||||
else:
|
||||
error_text = await response.text()
|
||||
logger.warning(f"⚠️ Endpoint {endpoint} returned {response.status}: {error_text[:200]}")
|
||||
if response.status == 200:
|
||||
data = await response.json()
|
||||
invoices_from_endpoint = data.get('collection', [])
|
||||
logger.info(f"✅ Fetched {len(invoices_from_endpoint)} invoices from {endpoint} page {page}")
|
||||
|
||||
if not invoices_from_endpoint:
|
||||
# No more invoices on this page
|
||||
break
|
||||
|
||||
# Add new invoices (avoid duplicates by tracking invoice numbers)
|
||||
existing_invoice_numbers = set()
|
||||
for inv in all_invoices:
|
||||
inv_num = inv.get('draftInvoiceNumber') or inv.get('bookedInvoiceNumber')
|
||||
if inv_num:
|
||||
existing_invoice_numbers.add(inv_num)
|
||||
|
||||
new_invoices_count = 0
|
||||
for inv in invoices_from_endpoint:
|
||||
inv_num = inv.get('draftInvoiceNumber') or inv.get('bookedInvoiceNumber')
|
||||
if inv_num and inv_num not in existing_invoice_numbers:
|
||||
all_invoices.append(inv)
|
||||
existing_invoice_numbers.add(inv_num)
|
||||
new_invoices_count += 1
|
||||
|
||||
logger.info(f" Added {new_invoices_count} new unique invoices")
|
||||
|
||||
# Check if we got full page (1000) - if so, there might be more pages
|
||||
if len(invoices_from_endpoint) < 1000:
|
||||
break # Last page
|
||||
|
||||
page += 1
|
||||
else:
|
||||
error_text = await response.text()
|
||||
logger.warning(f"⚠️ Endpoint {endpoint} returned {response.status}: {error_text[:200]}")
|
||||
break
|
||||
except Exception as e:
|
||||
logger.warning(f"⚠️ Error trying endpoint {endpoint}: {e}")
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user