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}")
|
logger.info(f"📋 Trying invoice endpoint: {endpoint}")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
async with session.get(
|
# Pagination: Keep fetching until no more pages
|
||||||
endpoint,
|
page = 0
|
||||||
params={"pagesize": 1000},
|
while True:
|
||||||
headers=self._get_headers()
|
async with session.get(
|
||||||
) as response:
|
endpoint,
|
||||||
logger.info(f"🔍 [API] Response status from {endpoint}: {response.status}")
|
params={"pagesize": 1000, "skippages": page},
|
||||||
|
headers=self._get_headers()
|
||||||
if response.status == 200:
|
) as response:
|
||||||
data = await response.json()
|
logger.info(f"🔍 [API] Response status from {endpoint} (page {page}): {response.status}")
|
||||||
invoices_from_endpoint = data.get('collection', [])
|
|
||||||
logger.info(f"✅ Successfully fetched {len(invoices_from_endpoint)} invoices from {endpoint}")
|
|
||||||
|
|
||||||
# Add new invoices (avoid duplicates by tracking invoice numbers)
|
if response.status == 200:
|
||||||
existing_invoice_numbers = set()
|
data = await response.json()
|
||||||
for inv in all_invoices:
|
invoices_from_endpoint = data.get('collection', [])
|
||||||
inv_num = inv.get('draftInvoiceNumber') or inv.get('bookedInvoiceNumber')
|
logger.info(f"✅ Fetched {len(invoices_from_endpoint)} invoices from {endpoint} page {page}")
|
||||||
if inv_num:
|
|
||||||
existing_invoice_numbers.add(inv_num)
|
if not invoices_from_endpoint:
|
||||||
|
# No more invoices on this page
|
||||||
for inv in invoices_from_endpoint:
|
break
|
||||||
inv_num = inv.get('draftInvoiceNumber') or inv.get('bookedInvoiceNumber')
|
|
||||||
if inv_num and inv_num not in existing_invoice_numbers:
|
# Add new invoices (avoid duplicates by tracking invoice numbers)
|
||||||
all_invoices.append(inv)
|
existing_invoice_numbers = set()
|
||||||
existing_invoice_numbers.add(inv_num)
|
for inv in all_invoices:
|
||||||
else:
|
inv_num = inv.get('draftInvoiceNumber') or inv.get('bookedInvoiceNumber')
|
||||||
error_text = await response.text()
|
if inv_num:
|
||||||
logger.warning(f"⚠️ Endpoint {endpoint} returned {response.status}: {error_text[:200]}")
|
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:
|
except Exception as e:
|
||||||
logger.warning(f"⚠️ Error trying endpoint {endpoint}: {e}")
|
logger.warning(f"⚠️ Error trying endpoint {endpoint}: {e}")
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user