59 lines
2.0 KiB
Python
59 lines
2.0 KiB
Python
"""Parse raw OCR / multimodal extraction results into TransactionRecord instances."""
|
|
from datetime import datetime
|
|
from uuid import UUID
|
|
|
|
from app.models.transaction import TransactionRecord, Direction
|
|
from app.models.evidence_image import SourceApp
|
|
|
|
|
|
def parse_extracted_fields(
|
|
raw: dict | list,
|
|
case_id: UUID,
|
|
image_id: UUID,
|
|
source_app: SourceApp,
|
|
) -> list[TransactionRecord]:
|
|
"""Convert raw extraction dict(s) into TransactionRecord ORM objects."""
|
|
items = raw if isinstance(raw, list) else [raw]
|
|
records: list[TransactionRecord] = []
|
|
|
|
for idx, item in enumerate(items):
|
|
if not item or not item.get("amount"):
|
|
continue
|
|
|
|
raw_trade_time = item.get("trade_time")
|
|
try:
|
|
if isinstance(raw_trade_time, datetime):
|
|
trade_time = raw_trade_time
|
|
elif isinstance(raw_trade_time, str):
|
|
trade_time = datetime.fromisoformat(raw_trade_time)
|
|
else:
|
|
raise TypeError("trade_time is not str/datetime")
|
|
except (ValueError, KeyError, TypeError):
|
|
trade_time = datetime.now()
|
|
|
|
direction_str = item.get("direction", "out")
|
|
direction = Direction.in_ if direction_str == "in" else Direction.out
|
|
try:
|
|
amount = float(item.get("amount", 0))
|
|
confidence = float(item.get("confidence", 0.5))
|
|
except Exception:
|
|
raise
|
|
|
|
record = TransactionRecord(
|
|
case_id=case_id,
|
|
evidence_image_id=image_id,
|
|
source_app=source_app,
|
|
trade_time=trade_time,
|
|
amount=amount,
|
|
direction=direction,
|
|
counterparty_name=str(item.get("counterparty_name", "")),
|
|
counterparty_account=str(item.get("counterparty_account", "")),
|
|
self_account_tail_no=str(item.get("self_account_tail_no", "")),
|
|
order_no=str(item.get("order_no", "")),
|
|
remark=str(item.get("remark", "")),
|
|
confidence=confidence,
|
|
)
|
|
records.append(record)
|
|
|
|
return records
|