fix: link-vendor opretter minimal extraction hvis ingen findes v2.2.32

This commit is contained in:
Christian 2026-03-02 13:12:01 +01:00
parent 4b2b0ea0f3
commit 4953c82b93

View File

@ -757,17 +757,36 @@ async def link_vendor_to_extraction(file_id: int, data: dict):
(file_id,))
if not extraction:
raise HTTPException(status_code=404, detail="Ingen extraction fundet for denne fil")
# No extraction exists (e.g. custom template match or not yet processed)
# Create a minimal placeholder extraction so vendor can be linked
logger.info(f"⚠️ No extraction for file {file_id} — creating minimal extraction for vendor link")
extraction_id = execute_insert(
"""INSERT INTO extractions
(file_id, vendor_matched_id, vendor_name, vendor_cvr,
document_id, document_type, document_type_detected,
currency, confidence, status)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
RETURNING extraction_id""",
(file_id, vendor_id,
vendor['name'], None,
None, 'invoice', 'invoice',
'DKK', 1.0, 'manual')
)
else:
extraction_id = extraction['extraction_id']
# Update extraction with vendor match
execute_update(
"""UPDATE extractions
SET vendor_matched_id = %s
WHERE extraction_id = %s""",
(vendor_id, extraction['extraction_id'])
"UPDATE extractions SET vendor_matched_id = %s WHERE extraction_id = %s",
(vendor_id, extraction_id)
)
logger.info(f"✅ Linked vendor {vendor['name']} (ID: {vendor_id}) to extraction for file {file_id}")
# Also update incoming_files so table shows vendor immediately
execute_update(
"UPDATE incoming_files SET detected_vendor_id = %s, status = 'processed' WHERE file_id = %s",
(vendor_id, file_id)
)
logger.info(f"✅ Linked vendor {vendor['name']} (ID: {vendor_id}) to file {file_id}")
return {
"status": "success",