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
13
api.py
13
api.py
|
|
@ -31,6 +31,7 @@ from email_queue import email_queue
|
||||||
from sage_client import sage_client
|
from sage_client import sage_client
|
||||||
|
|
||||||
from schemas import TiersDetails, TypeTiers
|
from schemas import TiersDetails, TypeTiers
|
||||||
|
from utils.normalization import normaliser_type_tiers
|
||||||
|
|
||||||
logging.basicConfig(
|
logging.basicConfig(
|
||||||
level=logging.INFO,
|
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"])
|
@app.get("/tiers", response_model=List[TiersDetails], tags=["Tiers"])
|
||||||
async def obtenir_tiers(
|
async def obtenir_tiers(
|
||||||
type_tiers: Optional[TypeTiers] = Query(
|
type_tiers: Optional[str] = Query(
|
||||||
None,
|
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(
|
query: Optional[str] = Query(
|
||||||
None,
|
None,
|
||||||
|
|
@ -6097,8 +6098,9 @@ async def obtenir_tiers(
|
||||||
)
|
)
|
||||||
):
|
):
|
||||||
try:
|
try:
|
||||||
|
type_normalise = normaliser_type_tiers(type_tiers)
|
||||||
tiers = sage_client.lister_tiers(
|
tiers = sage_client.lister_tiers(
|
||||||
type_tiers=type_tiers.value if type_tiers else None,
|
type_tiers=type_normalise,
|
||||||
filtre=query or ""
|
filtre=query or ""
|
||||||
)
|
)
|
||||||
return [TiersDetails(**t) for t in tiers]
|
return [TiersDetails(**t) for t in tiers]
|
||||||
|
|
@ -6109,6 +6111,11 @@ async def obtenir_tiers(
|
||||||
|
|
||||||
@app.get("/tiers/{code}", response_model=TiersDetails, tags=["Tiers"])
|
@app.get("/tiers/{code}", response_model=TiersDetails, tags=["Tiers"])
|
||||||
async def lire_tiers_detail(code: str):
|
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:
|
try:
|
||||||
tiers = sage_client.lire_tiers(code)
|
tiers = sage_client.lire_tiers(code)
|
||||||
if not tiers:
|
if not tiers:
|
||||||
|
|
|
||||||
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