feat(api): add type tier normalization utility and update API
This commit is contained in:
parent
edb64926e3
commit
efe5961bea
2 changed files with 36 additions and 5 deletions
17
api.py
17
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))
|
||||
|
||||
|
||||
|
|
|
|||
24
utils/normalization.py
Normal file
24
utils/normalization.py
Normal file
|
|
@ -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
|
||||
Loading…
Reference in a new issue