From efe5961bea6ae366e4e3b547d6166756eb11e7c9 Mon Sep 17 00:00:00 2001 From: Fanilo-Nantenaina Date: Mon, 29 Dec 2025 11:54:17 +0300 Subject: [PATCH] feat(api): add type tier normalization utility and update API --- api.py | 17 ++++++++++++----- utils/normalization.py | 24 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 utils/normalization.py diff --git a/api.py b/api.py index bff3fe0..6f044a9 100644 --- a/api.py +++ b/api.py @@ -31,6 +31,7 @@ from email_queue import email_queue from sage_client import sage_client from schemas import TiersDetails, TypeTiers +from utils.normalization import normaliser_type_tiers logging.basicConfig( level=logging.INFO, @@ -6087,9 +6088,9 @@ async def definir_contact_defaut(numero: str, contact_numero: int): @app.get("/tiers", response_model=List[TiersDetails], tags=["Tiers"]) async def obtenir_tiers( - type_tiers: Optional[TypeTiers] = Query( + type_tiers: Optional[str] = Query( None, - description="Filtre par type: client, fournisseur, prospect, ou all" + description="Filtre par type: 0/client, 1/fournisseur, 2/prospect, 3/all ou strings" ), query: Optional[str] = Query( None, @@ -6097,18 +6098,24 @@ async def obtenir_tiers( ) ): try: + type_normalise = normaliser_type_tiers(type_tiers) tiers = sage_client.lister_tiers( - type_tiers=type_tiers.value if type_tiers else None, + type_tiers=type_normalise, filtre=query or "" ) return [TiersDetails(**t) for t in tiers] except Exception as e: - logger.error(f" Erreur recherche tiers: {e}") + logger.error(f"Erreur recherche tiers: {e}") raise HTTPException(500, str(e)) @app.get("/tiers/{code}", response_model=TiersDetails, tags=["Tiers"]) async def lire_tiers_detail(code: str): + """ + Lecture détaillée d'un tiers par son code. + + Retourne toutes les informations du tiers ainsi que ses contacts. + """ try: tiers = sage_client.lire_tiers(code) if not tiers: @@ -6117,7 +6124,7 @@ async def lire_tiers_detail(code: str): except HTTPException: raise except Exception as e: - logger.error(f" Erreur lecture tiers {code}: {e}") + logger.error(f"Erreur lecture tiers {code}: {e}") raise HTTPException(500, str(e)) diff --git a/utils/normalization.py b/utils/normalization.py new file mode 100644 index 0000000..b7e0f07 --- /dev/null +++ b/utils/normalization.py @@ -0,0 +1,24 @@ +from typing import Optional, Union + +def normaliser_type_tiers(type_tiers: Union[str, int, None]) -> Optional[str]: + if type_tiers is None: + return None + + # Conversion int → string + mapping_int = { + 0: "client", + 1: "fournisseur", + 2: "prospect", + 3: "all" + } + + # Si c'est un int, on convertit + if isinstance(type_tiers, int): + return mapping_int.get(type_tiers, "all") + + # Si c'est une string qui ressemble à un int + if isinstance(type_tiers, str) and type_tiers.isdigit(): + return mapping_int.get(int(type_tiers), "all") + + # Sinon on retourne tel quel (string normale) + return type_tiers.lower() if isinstance(type_tiers, str) else None \ No newline at end of file