from fastapi import APIRouter, Depends, HTTPException from sqlalchemy import select, or_ from sqlalchemy.ext.asyncio import AsyncSession from app.db.base import uuid7 from app.db.models.client import Client from app.db.session import get_db from app.deps import get_tenant_id from app.clients import schemas router = APIRouter() @router.get("") async def list_clients( search: str | None = None, tenant_id: str = Depends(get_tenant_id), db: AsyncSession = Depends(get_db), ): query = select(Client).where(Client.tenant_id == tenant_id) if search: pattern = f"%{search}%" query = query.where( or_( Client.denumire.ilike(pattern), Client.nume.ilike(pattern), Client.prenume.ilike(pattern), Client.cod_fiscal.ilike(pattern), Client.telefon.ilike(pattern), Client.email.ilike(pattern), ) ) r = await db.execute(query) clients = r.scalars().all() return [ { "id": c.id, "tip_persoana": c.tip_persoana, "denumire": c.denumire, "nume": c.nume, "prenume": c.prenume, "cod_fiscal": c.cod_fiscal, "reg_com": c.reg_com, "telefon": c.telefon, "email": c.email, "adresa": c.adresa, "judet": c.judet, "oras": c.oras, "cod_postal": c.cod_postal, "tara": c.tara, "cont_iban": c.cont_iban, "banca": c.banca, "activ": c.activ, } for c in clients ] @router.post("") async def create_client( data: schemas.ClientCreate, tenant_id: str = Depends(get_tenant_id), db: AsyncSession = Depends(get_db), ): client = Client( id=data.id or uuid7(), tenant_id=tenant_id, tip_persoana=data.tip_persoana, denumire=data.denumire, nume=data.nume, prenume=data.prenume, cod_fiscal=data.cod_fiscal, reg_com=data.reg_com, telefon=data.telefon, email=data.email, adresa=data.adresa, judet=data.judet, oras=data.oras, cod_postal=data.cod_postal, tara=data.tara, cont_iban=data.cont_iban, banca=data.banca, activ=data.activ, ) db.add(client) await db.commit() return {"id": client.id} @router.get("/{client_id}") async def get_client( client_id: str, tenant_id: str = Depends(get_tenant_id), db: AsyncSession = Depends(get_db), ): r = await db.execute( select(Client).where( Client.id == client_id, Client.tenant_id == tenant_id ) ) c = r.scalar_one_or_none() if not c: raise HTTPException(status_code=404, detail="Client not found") return { "id": c.id, "tip_persoana": c.tip_persoana, "denumire": c.denumire, "nume": c.nume, "prenume": c.prenume, "cod_fiscal": c.cod_fiscal, "reg_com": c.reg_com, "telefon": c.telefon, "email": c.email, "adresa": c.adresa, "judet": c.judet, "oras": c.oras, "cod_postal": c.cod_postal, "tara": c.tara, "cont_iban": c.cont_iban, "banca": c.banca, "activ": c.activ, } @router.put("/{client_id}") async def update_client( client_id: str, data: schemas.ClientUpdate, tenant_id: str = Depends(get_tenant_id), db: AsyncSession = Depends(get_db), ): r = await db.execute( select(Client).where( Client.id == client_id, Client.tenant_id == tenant_id ) ) c = r.scalar_one_or_none() if not c: raise HTTPException(status_code=404, detail="Client not found") update_data = data.model_dump(exclude_unset=True) for key, value in update_data.items(): setattr(c, key, value) await db.commit() return {"ok": True} @router.delete("/{client_id}") async def delete_client( client_id: str, tenant_id: str = Depends(get_tenant_id), db: AsyncSession = Depends(get_db), ): r = await db.execute( select(Client).where( Client.id == client_id, Client.tenant_id == tenant_id ) ) c = r.scalar_one_or_none() if not c: raise HTTPException(status_code=404, detail="Client not found") await db.delete(c) await db.commit() return {"ok": True}