fix: use ceidg_api_service in arm_company.py instead of manual API call
Some checks are pending
NordaBiz Tests / Unit & Integration Tests (push) Waiting to run
NordaBiz Tests / E2E Tests (Playwright) (push) Blocked by required conditions
NordaBiz Tests / Smoke Tests (Production) (push) Blocked by required conditions
NordaBiz Tests / Send Failure Notification (push) Blocked by required conditions

The script was calling /firma?nip=X (wrong endpoint) instead of using
fetch_ceidg_by_nip() which does two-phase /firmy?nip=X then /firma/{id}.
Now uses the same service and field mapping as the admin panel button.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Maciej Pienczyn 2026-02-20 17:08:26 +01:00
parent c86d9cf368
commit 44d8ab932b

View File

@ -74,55 +74,108 @@ def arm_company(company_id, force=False):
results['registry'] = 'FAIL (KRS)'
print(" -> FAIL: Nie udało się pobrać z KRS")
else:
# Próbuj CEIDG
import requests as req
ceidg_key = os.getenv("CEIDG_API_KEY", "")
nip_clean = company.nip.replace('-', '').replace(' ', '')
ceidg_url = "https://dane.biznes.gov.pl/api/ceidg/v3/firma?nip=%s" % nip_clean
headers = {"Authorization": "Bearer %s" % ceidg_key}
resp = req.get(ceidg_url, headers=headers, timeout=10)
if resp.status_code == 200:
data = resp.json()
firmy = data.get('firmy', [])
if firmy:
firma = firmy[0]
# Zapisz surowe dane
company.ceidg_raw_data = firma
from datetime import datetime
company.ceidg_fetched_at = datetime.utcnow()
# Próbuj CEIDG — używamy tego samego serwisu co API endpoint
from ceidg_api_service import fetch_ceidg_by_nip
from datetime import datetime, date as date_type
ceidg_data = fetch_ceidg_by_nip(company.nip)
if ceidg_data:
# CEIDG identifiers & metadata
if ceidg_data.get('ceidg_id'):
company.ceidg_id = ceidg_data['ceidg_id']
if ceidg_data.get('status'):
company.ceidg_status = ceidg_data['status']
company.ceidg_raw_data = ceidg_data.get('raw')
company.ceidg_fetched_at = datetime.now()
company.data_source = 'CEIDG API'
company.last_verified_at = datetime.now()
# Mapuj podstawowe pola
if firma.get('wlasciciel'):
wl = firma['wlasciciel']
company.legal_form = 'JDG'
if not company.owner_name:
company.owner_name = '%s %s' % (wl.get('imie', ''), wl.get('nazwisko', ''))
# Owner
wlasciciel = ceidg_data.get('wlasciciel', {})
if wlasciciel.get('imie'):
company.owner_first_name = wlasciciel['imie']
if wlasciciel.get('nazwisko'):
company.owner_last_name = wlasciciel['nazwisko']
if ceidg_data.get('obywatelstwa'):
company.owner_citizenships = ceidg_data['obywatelstwa']
if firma.get('adresDzialalnosci'):
addr = firma['adresDzialalnosci']
if not company.address_street:
ulica = addr.get('ulica', '')
nr = addr.get('budynek', '')
lokal = addr.get('lokal', '')
company.address_street = ('%s %s%s' % (ulica, nr, '/%s' % lokal if lokal else '')).strip()
if not company.address_city:
company.address_city = addr.get('miasto', '')
if not company.address_zip:
company.address_zip = addr.get('kodPocztowy', '')
# Legal name
if ceidg_data.get('firma') and (not company.legal_name or company.legal_name == company.name):
company.legal_name = ceidg_data['firma']
if firma.get('email') and not company.email:
company.email = firma['email']
if firma.get('telefon') and not company.phone:
company.phone = firma['telefon']
if firma.get('adresStronyInternetowej') and not company.website:
company.website = firma['adresStronyInternetowej']
# REGON
if not company.regon:
regon = ceidg_data.get('regon') or wlasciciel.get('regon')
if regon:
company.regon = regon
db.commit()
results['registry'] = 'OK (CEIDG)'
print(" -> OK: Dane z CEIDG pobrane")
else:
results['registry'] = 'NOT FOUND'
print(" -> Nie znaleziono w CEIDG")
# Business start date
if ceidg_data.get('dataRozpoczecia'):
try:
d = ceidg_data['dataRozpoczecia']
if isinstance(d, str):
company.business_start_date = date_type.fromisoformat(d)
except (ValueError, TypeError):
pass
# Legal form
if not company.legal_form:
company.legal_form = 'JEDNOOSOBOWA DZIAŁALNOŚĆ GOSPODARCZA'
# PKD (main)
pkd_gl = ceidg_data.get('pkdGlowny', {})
if pkd_gl and pkd_gl.get('kod'):
company.pkd_code = pkd_gl['kod']
company.pkd_description = pkd_gl.get('nazwa')
# PKD (full list)
pkd_lista = ceidg_data.get('pkd', [])
if pkd_lista:
company.ceidg_pkd_list = pkd_lista
pkd_main_code = pkd_gl.get('kod', '') if pkd_gl else ''
for pkd_item in pkd_lista:
kod = pkd_item.get('kod', '')
if not kod:
continue
existing_pkd = db.query(CompanyPKD).filter(
CompanyPKD.company_id == company.id,
CompanyPKD.pkd_code == kod
).first()
if not existing_pkd:
db.add(CompanyPKD(
company_id=company.id,
pkd_code=kod,
pkd_description=pkd_item.get('nazwa', ''),
is_primary=(kod == pkd_main_code)
))
# Business address
adres = ceidg_data.get('adresDzialalnosci', {})
ulica = adres.get('ulica', '')
budynek = adres.get('budynek', '')
lokal = adres.get('lokal', '')
if ulica or budynek:
street_parts = [ulica, budynek]
if lokal:
street_parts[-1] = (budynek + '/' + lokal) if budynek else lokal
company.address_street = ' '.join(p for p in street_parts if p)
if adres.get('kod') or adres.get('kodPocztowy'):
company.address_postal = adres.get('kod') or adres.get('kodPocztowy')
if adres.get('miasto') or adres.get('miejscowosc'):
company.address_city = adres.get('miasto') or adres.get('miejscowosc')
if company.address_street and getattr(company, 'address_postal', None) and company.address_city:
company.address_full = '%s, %s %s' % (company.address_street, company.address_postal, company.address_city)
# Contact (only if empty)
if ceidg_data.get('email') and not company.email:
company.email = ceidg_data['email']
if ceidg_data.get('stronaWWW') and not company.website:
company.website = ceidg_data['stronaWWW']
if ceidg_data.get('telefon') and not company.phone:
company.phone = ceidg_data['telefon']
db.commit()
results['registry'] = 'OK (CEIDG)'
print(" -> OK: Dane z CEIDG pobrane")
else:
results['registry'] = 'NOT FOUND'
print(" -> Nie znaleziono w żadnym rejestrze")