|
4 | 4 |
|
5 | 5 | from typing import Annotated, Any |
6 | 6 |
|
7 | | -from fastapi import APIRouter, Depends, File, HTTPException, UploadFile, status |
| 7 | +import json |
| 8 | + |
| 9 | +from fastapi import APIRouter, Depends, File, Form, HTTPException, UploadFile, status |
8 | 10 | from sqlalchemy.exc import IntegrityError |
9 | 11 | from sqlalchemy.orm import Session |
10 | 12 |
|
11 | 13 | from core.database import get_db |
12 | 14 | from core.deps import get_current_user |
13 | 15 | from models.cardmarket_order import CardmarketOrder |
14 | 16 | from models.user import User |
| 17 | +from schemas.orders import OrderLineUpdate |
15 | 18 | from services.cardmarket_order_service import ( |
16 | 19 | get_order_detail, |
| 20 | + get_order_line_for_user, |
| 21 | + list_linkable_order_lines, |
17 | 22 | list_orders_summary, |
18 | 23 | match_order_lines, |
19 | 24 | persist_order_from_parsed, |
| 25 | + reimport_order_from_parsed, |
| 26 | + update_order_line, |
20 | 27 | ) |
21 | 28 | from services.cardmarket_pdf_parser import parse_cardmarket_pdf_bytes |
22 | 29 |
|
@@ -56,6 +63,20 @@ def list_imported_external_ids( |
56 | 63 | return [r[0] for r in rows] |
57 | 64 |
|
58 | 65 |
|
| 66 | +@router.get("/lines/linkable") |
| 67 | +def list_lines_linkable( |
| 68 | + db: Annotated[Session, Depends(get_db)], |
| 69 | + user: Annotated[User, Depends(get_current_user)], |
| 70 | + search: str | None = None, |
| 71 | +) -> list[dict[str, Any]]: |
| 72 | + """ |
| 73 | + Purchase lines that still have free link slots (manual article assignment). |
| 74 | +
|
| 75 | + Optional ``search``: space-separated tokens matched on order id, card name, set, number, etc. |
| 76 | + """ |
| 77 | + return list_linkable_order_lines(db, user.id, search=search) |
| 78 | + |
| 79 | + |
59 | 80 | @router.get("/match") |
60 | 81 | def match_lines( |
61 | 82 | db: Annotated[Session, Depends(get_db)], |
@@ -93,6 +114,94 @@ def get_order( |
93 | 114 | return row |
94 | 115 |
|
95 | 116 |
|
| 117 | +def _order_value_error_to_http(exc: ValueError) -> HTTPException: |
| 118 | + code = str(exc) |
| 119 | + if code == "purchase_line_not_found": |
| 120 | + return HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Purchase line not found.") |
| 121 | + if code == "order_not_found": |
| 122 | + return HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Order not found.") |
| 123 | + if code == "quantity_below_linked_articles": |
| 124 | + return HTTPException( |
| 125 | + status_code=status.HTTP_400_BAD_REQUEST, |
| 126 | + detail="Quantity cannot be lower than the number of linked articles on this line.", |
| 127 | + ) |
| 128 | + if code == "pdf_order_id_mismatch": |
| 129 | + return HTTPException( |
| 130 | + status_code=status.HTTP_400_BAD_REQUEST, |
| 131 | + detail="This PDF belongs to a different Cardmarket order.", |
| 132 | + ) |
| 133 | + return HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=code) |
| 134 | + |
| 135 | + |
| 136 | +@router.patch("/lines/{line_id}") |
| 137 | +def patch_order_line( |
| 138 | + line_id: int, |
| 139 | + body: OrderLineUpdate, |
| 140 | + db: Annotated[Session, Depends(get_db)], |
| 141 | + user: Annotated[User, Depends(get_current_user)], |
| 142 | +) -> dict[str, Any]: |
| 143 | + """Update one purchase line (manual correction after import).""" |
| 144 | + ln = get_order_line_for_user(db, user.id, line_id) |
| 145 | + if ln is None: |
| 146 | + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Purchase line not found.") |
| 147 | + try: |
| 148 | + update_order_line(db, user.id, line_id, body) |
| 149 | + except ValueError as exc: |
| 150 | + raise _order_value_error_to_http(exc) from exc |
| 151 | + detail = get_order_detail(db, user.id, ln.order_id) |
| 152 | + if detail is None: |
| 153 | + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Order not found.") |
| 154 | + return detail |
| 155 | + |
| 156 | + |
| 157 | +@router.post("/{order_id}/reimport") |
| 158 | +async def reimport_order_pdf( |
| 159 | + order_id: int, |
| 160 | + db: Annotated[Session, Depends(get_db)], |
| 161 | + user: Annotated[User, Depends(get_current_user)], |
| 162 | + file: Annotated[UploadFile, File()], |
| 163 | + confirm_linked_line_indexes: str | None = Form(None), |
| 164 | +) -> dict[str, Any]: |
| 165 | + """ |
| 166 | + Re-merge a Cardmarket PDF into an existing order (by line_index). |
| 167 | +
|
| 168 | + Optional form field ``confirm_linked_line_indexes``: JSON array of line indexes |
| 169 | + to overwrite even when articles are linked. |
| 170 | + """ |
| 171 | + raw = await file.read() |
| 172 | + if not raw: |
| 173 | + raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Empty file.") |
| 174 | + try: |
| 175 | + parsed = parse_cardmarket_pdf_bytes(raw) |
| 176 | + except ValueError as exc: |
| 177 | + raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(exc)) from exc |
| 178 | + |
| 179 | + confirm: set[int] = set() |
| 180 | + if confirm_linked_line_indexes and confirm_linked_line_indexes.strip(): |
| 181 | + try: |
| 182 | + parsed_indexes = json.loads(confirm_linked_line_indexes) |
| 183 | + if not isinstance(parsed_indexes, list): |
| 184 | + raise ValueError("expected JSON array") |
| 185 | + confirm = {int(x) for x in parsed_indexes} |
| 186 | + except (json.JSONDecodeError, TypeError, ValueError) as exc: |
| 187 | + raise HTTPException( |
| 188 | + status_code=status.HTTP_400_BAD_REQUEST, |
| 189 | + detail="Invalid confirm_linked_line_indexes (expected JSON array of integers).", |
| 190 | + ) from exc |
| 191 | + |
| 192 | + try: |
| 193 | + return reimport_order_from_parsed( |
| 194 | + db, |
| 195 | + user.id, |
| 196 | + order_id, |
| 197 | + parsed, |
| 198 | + file.filename, |
| 199 | + confirm_linked_line_indexes=confirm, |
| 200 | + ) |
| 201 | + except ValueError as exc: |
| 202 | + raise _order_value_error_to_http(exc) from exc |
| 203 | + |
| 204 | + |
96 | 205 | @router.post("/import", status_code=status.HTTP_201_CREATED) |
97 | 206 | async def import_order_pdf( |
98 | 207 | db: Annotated[Session, Depends(get_db)], |
|
0 commit comments