2727 VintedBatchStartBody ,
2828)
2929from services import article_service
30+ from services .cardmarket_order_service import assign_article_order_line
3031from services .combined_marketplace_service import CombinedMarketplaceService
3132from services .ebay_background_service import EbayBackgroundService
3233from services .user_settings_service import ebay_listing_config_complete , get_or_create_user_settings
@@ -53,6 +54,43 @@ def _parse_decimal_required(value: str) -> Decimal:
5354 return Decimal (str (value ).strip ())
5455
5556
57+ def _parse_optional_order_line_id (raw : str | None ) -> int | None :
58+ """Parse optional multipart ``order_line_id`` field."""
59+ if raw is None or not str (raw ).strip ():
60+ return None
61+ try :
62+ return int (str (raw ).strip ())
63+ except ValueError as exc :
64+ raise HTTPException (
65+ status_code = status .HTTP_400_BAD_REQUEST ,
66+ detail = "Invalid order_line_id (expected integer)." ,
67+ ) from exc
68+
69+
70+ def _apply_order_line_assignment (
71+ db : Session ,
72+ user : User ,
73+ article : Article ,
74+ order_line_id : int | None ,
75+ ) -> None :
76+ """Attach purchase line or translate validation errors."""
77+ try :
78+ assign_article_order_line (db , user .id , article , order_line_id )
79+ except ValueError as exc :
80+ code = str (exc )
81+ if code == "purchase_line_not_found" :
82+ raise HTTPException (
83+ status_code = status .HTTP_400_BAD_REQUEST ,
84+ detail = "Ligne d'achat introuvable ou non autorisée." ,
85+ ) from exc
86+ if code == "purchase_line_full" :
87+ raise HTTPException (
88+ status_code = status .HTTP_400_BAD_REQUEST ,
89+ detail = "Plus de quantité disponible sur cette ligne d'achat." ,
90+ ) from exc
91+ raise
92+
93+
5694def _form_bool (value : str | None ) -> bool :
5795 if value is None or str (value ).strip () == "" :
5896 return False
@@ -466,6 +504,7 @@ async def create_article(
466504 graded_grader_value_id : str | None = Form (None ),
467505 graded_grade_value_id : str | None = Form (None ),
468506 graded_cert_number : str | None = Form (None ),
507+ order_line_id : str | None = Form (None ),
469508 images : list [UploadFile ] | None = File (None ),
470509) -> dict [str , Any ]:
471510 settings = get_settings ()
@@ -519,6 +558,7 @@ async def create_article(
519558 sold_at = sold_at_dt if sold_flag else None ,
520559 )
521560 _validate_graded_article_or_raise (article )
561+ _apply_order_line_assignment (db , user , article , _parse_optional_order_line_id (order_line_id ))
522562
523563 if _form_bool (wardrobe_vinted_listed ):
524564 article .published_on_vinted = True
@@ -644,6 +684,9 @@ def update_article(
644684 raise HTTPException (status_code = 404 , detail = "Article not found" )
645685 article_service .update_article_from_body (article , body )
646686 _validate_graded_article_or_raise (article )
687+ unset = body .model_dump (exclude_unset = True )
688+ if "order_line_id" in unset :
689+ _apply_order_line_assignment (db , user , article , body .order_line_id )
647690 db .commit ()
648691 db .refresh (article )
649692 return article_service .article_to_dict (article )
0 commit comments