From 6eba416f726931ac20dba13992a54e4d5388fce0 Mon Sep 17 00:00:00 2001 From: Fanilo-Nantenaina Date: Tue, 30 Dec 2025 12:48:45 +0300 Subject: [PATCH] fix(api): normalize document types and fix status change validation --- api.py | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/api.py b/api.py index 7edb627..1aabde5 100644 --- a/api.py +++ b/api.py @@ -830,30 +830,38 @@ async def changer_statut_document( ): document_type_sql = None document_type_code = None + type_doc_normalized = None try: match type_doc: case 0: document_type_sql = TypeDocumentSQL.DEVIS document_type_code = TypeDocument.DEVIS + type_doc_normalized = 0 case 10 | 1: document_type_sql = TypeDocumentSQL.BON_COMMANDE document_type_code = TypeDocument.BON_COMMANDE + type_doc_normalized = 10 case 20 | 2: document_type_sql = TypeDocumentSQL.PREPARATION document_type_code = TypeDocument.PREPARATION + type_doc_normalized = 20 case 30 | 3: document_type_sql = TypeDocumentSQL.BON_LIVRAISON document_type_code = TypeDocument.BON_LIVRAISON + type_doc_normalized = 30 case 40 | 4: document_type_sql = TypeDocumentSQL.BON_RETOUR document_type_code = TypeDocument.BON_RETOUR + type_doc_normalized = 40 case 50 | 5: document_type_sql = TypeDocumentSQL.BON_AVOIR document_type_code = TypeDocument.BON_AVOIR + type_doc_normalized = 50 case 60 | 6: document_type_sql = TypeDocumentSQL.FACTURE document_type_code = TypeDocument.FACTURE + type_doc_normalized = 60 case _: raise HTTPException( 400, @@ -876,26 +884,38 @@ async def changer_statut_document( f"et ne peut plus changer de statut", ) - case 10 | 1 | 20 | 2 | 30 | 3 | 40 |4 |50 | 5 | 60 | 6: + case 10 | 1 | 20 | 2 | 30 | 3 | 40 | 4 | 50 | 5 | 60 | 6: if statut_actuel >= 2: type_names = { - 10: "la commande", 20: "la préparation", 30: "la livraison", - 40: "le retour", 50: "l'avoir", 60: "la facture" + 10: "la commande", 1: "la commande", + 20: "la préparation", 2: "la préparation", + 30: "la livraison", 3: "la livraison", + 40: "le retour", 4: "le retour", + 50: "l'avoir", 5: "l'avoir", + 60: "la facture", 6: "la facture" } raise HTTPException( 400, - f"Le document {numero} ({type_names.get(document_type_sql, 'document')}) " + f"Le document {numero} ({type_names.get(type_doc, 'document')}) " f"ne peut plus changer de statut (statut actuel ≥ 2)", ) - resultat = sage_client.changer_statut_document(numero,document_type_code, nouveau_statut) + # CORRECTION ICI : utiliser les bons paramètres dans le bon ordre + # Et convertir l'enum en int avec .value si nécessaire + document_type_int = document_type_code.value if hasattr(document_type_code, 'value') else type_doc_normalized + + resultat = sage_client.changer_statut_document( + document_type_code=document_type_int, + numero=numero, + nouveau_statut=nouveau_statut + ) logger.info(f"Statut document {numero} changé: {statut_actuel} → {nouveau_statut}") return { "success": True, "document_id": numero, - "type_document_code": document_type_code, + "type_document_code": document_type_int, "type_document_sql": str(document_type_sql), "statut_ancien": resultat.get("statut_ancien", statut_actuel), "statut_nouveau": resultat.get("statut_nouveau", nouveau_statut), @@ -907,7 +927,7 @@ async def changer_statut_document( except Exception as e: logger.error(f"Erreur changement statut document {numero}: {e}") raise HTTPException(500, str(e)) - + @app.get("/commandes/{id}", tags=["Commandes"]) async def lire_commande(id: str): try: