227 lines
8.1 KiB
Python
227 lines
8.1 KiB
Python
import json
|
|
from dataclasses import replace as dc_replace
|
|
from datetime import datetime
|
|
|
|
from fastapi import APIRouter, Depends, File, Form, HTTPException, UploadFile
|
|
from fastapi.encoders import jsonable_encoder
|
|
from fastapi.responses import JSONResponse
|
|
from sqlalchemy import text
|
|
from sqlmodel import Session, select
|
|
|
|
from ..csv.bank_profiles import BankProfile, detect_profile
|
|
from ..csv.normalizer import normalize_rows
|
|
from ..csv.parser import parse_csv_content
|
|
from ..database import get_session
|
|
from ..deps import get_current_user
|
|
from ..models import (
|
|
Account,
|
|
AccountType,
|
|
BalanceSnapshot,
|
|
BudgetRule,
|
|
CsvUpload,
|
|
Transaction,
|
|
TransactionType,
|
|
TransferRule,
|
|
User,
|
|
)
|
|
from ..schemas.upload import ColumnMapping
|
|
|
|
router = APIRouter(prefix="/api/upload", tags=["upload"])
|
|
|
|
MAX_FILE_SIZE = 10 * 1024 * 1024 # 10 MB
|
|
VALID_MIMES = {"text/csv", "text/plain", "application/vnd.ms-excel", "application/csv", ""}
|
|
|
|
|
|
@router.post("")
|
|
async def upload_csv(
|
|
file: UploadFile = File(...),
|
|
account_id: str = Form(..., alias="accountId"),
|
|
column_mapping: str | None = Form(default=None, alias="columnMapping"),
|
|
session: Session = Depends(get_session),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
# --- Validate file ---
|
|
raw = await file.read()
|
|
if len(raw) > MAX_FILE_SIZE:
|
|
raise HTTPException(status_code=400, detail="File too large (max 10 MB)")
|
|
if not (file.filename or "").lower().endswith(".csv"):
|
|
raise HTTPException(status_code=400, detail="File must be a .csv file")
|
|
if file.content_type and file.content_type not in VALID_MIMES:
|
|
raise HTTPException(status_code=400, detail="Invalid file type")
|
|
|
|
account = session.exec(
|
|
select(Account).where(Account.id == account_id, Account.user_id == current_user.id)
|
|
).first()
|
|
if not account:
|
|
raise HTTPException(status_code=404, detail="Account not found")
|
|
|
|
content = raw.decode("utf-8-sig") # strip BOM if present
|
|
headers, all_rows = parse_csv_content(content)
|
|
detected = detect_profile(headers)
|
|
|
|
if not detected and not column_mapping:
|
|
return JSONResponse(content={
|
|
"requiresMapping": True,
|
|
"headers": headers,
|
|
"sampleRows": all_rows[:5],
|
|
})
|
|
|
|
if detected and not column_mapping:
|
|
config = detected
|
|
else:
|
|
try:
|
|
mapping_data = json.loads(column_mapping)
|
|
except (json.JSONDecodeError, TypeError):
|
|
raise HTTPException(status_code=400, detail="Invalid columnMapping JSON")
|
|
config = ColumnMapping.model_validate(mapping_data)
|
|
|
|
# For all BANK accounts using Strategy A, positive = CREDIT (deposit)
|
|
if account.type in (AccountType.BANK, AccountType.INVESTMENT) and getattr(config, "strategy", None) == "A":
|
|
if isinstance(config, BankProfile):
|
|
config = dc_replace(config, invert_amount_sign=True)
|
|
else:
|
|
config = config.model_copy(update={"invert_amount_sign": True})
|
|
|
|
normalized = normalize_rows(all_rows, config)
|
|
|
|
# Apply transfer rules (takes priority), then budget rules
|
|
budget_rules = session.exec(
|
|
select(BudgetRule).where(BudgetRule.user_id == current_user.id).order_by(BudgetRule.created_at)
|
|
).all()
|
|
transfer_rules = session.exec(
|
|
select(TransferRule).where(TransferRule.user_id == current_user.id).order_by(TransferRule.created_at)
|
|
).all()
|
|
|
|
for row in normalized:
|
|
desc_lower = row.description.lower()
|
|
matched_transfer = any(tr.pattern.lower() in desc_lower for tr in transfer_rules)
|
|
if matched_transfer:
|
|
row.type = "TRANSFER"
|
|
row.budget_id = None
|
|
continue
|
|
for rule in budget_rules:
|
|
if rule.pattern.lower() in desc_lower:
|
|
row.budget_id = rule.budget_id
|
|
break
|
|
|
|
# Create upload record
|
|
upload = CsvUpload(
|
|
account_id=account_id,
|
|
file_name=file.filename or "upload.csv",
|
|
row_count=len(all_rows),
|
|
imported_count=0,
|
|
skipped_count=0,
|
|
status="PENDING",
|
|
)
|
|
session.add(upload)
|
|
session.commit()
|
|
session.refresh(upload)
|
|
|
|
try:
|
|
tx_objects = [
|
|
Transaction(
|
|
account_id=account_id,
|
|
upload_id=upload.id,
|
|
date=row.date,
|
|
description=row.description,
|
|
amount_cents=row.amount_cents,
|
|
type=TransactionType(row.type),
|
|
category=row.category,
|
|
budget_id=row.budget_id,
|
|
)
|
|
for row in normalized
|
|
]
|
|
for tx in tx_objects:
|
|
session.add(tx)
|
|
try:
|
|
session.flush()
|
|
except Exception:
|
|
session.rollback()
|
|
# Re-add excluding any that would violate constraints
|
|
session.add(upload)
|
|
imported_count = 0
|
|
for tx in tx_objects:
|
|
try:
|
|
session.add(tx)
|
|
session.flush()
|
|
imported_count += 1
|
|
except Exception:
|
|
session.rollback()
|
|
else:
|
|
imported_count = len(tx_objects)
|
|
|
|
session.commit()
|
|
|
|
skipped_count = len(normalized) - imported_count
|
|
|
|
# Recompute current balance from all transactions
|
|
bal_result = session.exec(
|
|
text(
|
|
'SELECT COALESCE(SUM(CASE WHEN type = \'CREDIT\' THEN "amountCents" ELSE -"amountCents" END), 0)::bigint '
|
|
'FROM "Transaction" WHERE "accountId" = :account_id AND type != \'TRANSFER\''
|
|
).bindparams(account_id=account_id)
|
|
).first()
|
|
account.current_balance_cents = int(bal_result[0]) if bal_result else 0
|
|
account.updated_at = datetime.utcnow()
|
|
session.add(account)
|
|
session.commit()
|
|
|
|
# Upsert BalanceSnapshot for each affected month
|
|
seen_months: dict[str, tuple[int, int]] = {}
|
|
for row in normalized:
|
|
key = f"{row.date.year}-{row.date.month}"
|
|
seen_months[key] = (row.date.year, row.date.month)
|
|
|
|
for year, month in seen_months.values():
|
|
end_of_month = datetime(year, month + 1, 1) if month < 12 else datetime(year + 1, 1, 1)
|
|
snap_result = session.exec(
|
|
text(
|
|
'SELECT COALESCE(SUM(CASE WHEN type = \'CREDIT\' THEN "amountCents" ELSE -"amountCents" END), 0)::bigint '
|
|
'FROM "Transaction" WHERE "accountId" = :account_id AND date < :end AND type != \'TRANSFER\''
|
|
).bindparams(account_id=account_id, end=end_of_month)
|
|
).first()
|
|
snap_balance = int(snap_result[0]) if snap_result else 0
|
|
|
|
existing = session.exec(
|
|
select(BalanceSnapshot).where(
|
|
BalanceSnapshot.account_id == account_id,
|
|
BalanceSnapshot.year == year,
|
|
BalanceSnapshot.month == month,
|
|
)
|
|
).first()
|
|
if existing:
|
|
existing.balance_cents = snap_balance
|
|
existing.computed_at = datetime.utcnow()
|
|
session.add(existing)
|
|
else:
|
|
session.add(BalanceSnapshot(
|
|
account_id=account_id,
|
|
year=year,
|
|
month=month,
|
|
balance_cents=snap_balance,
|
|
))
|
|
session.commit()
|
|
|
|
status = "FAILED" if imported_count == 0 else ("PARTIAL" if skipped_count > 0 else "SUCCESS")
|
|
upload.imported_count = imported_count
|
|
upload.skipped_count = skipped_count
|
|
upload.status = status
|
|
session.add(upload)
|
|
session.commit()
|
|
|
|
return JSONResponse(content=jsonable_encoder({
|
|
"success": True,
|
|
"detected": detected.name if detected else None,
|
|
"importedCount": imported_count,
|
|
"skippedCount": skipped_count,
|
|
"fileName": file.filename,
|
|
}))
|
|
|
|
except Exception as exc:
|
|
session.rollback()
|
|
upload.status = "FAILED"
|
|
upload.error_message = str(exc)
|
|
session.add(upload)
|
|
session.commit()
|
|
raise HTTPException(status_code=500, detail="Import failed")
|