Restore client list pagination
This commit is contained in:
@@ -84,7 +84,7 @@ def list_clients(
|
||||
allow_cross_branch: bool = False, allow_all_clients: bool = False,
|
||||
partner_id: int | None = None, q: str = "", status: str = "", client_type: str = "",
|
||||
client_group_id: int | None = None, include_archived: bool = False,
|
||||
page: int = 1, per_page: int = 10, sort_by: str = "client_name", sort_order: str = "asc",
|
||||
page: int = 1, per_page: int = 25, sort_by: str = "client_name", sort_order: str = "asc",
|
||||
) -> dict:
|
||||
stmt = build_clients_query(
|
||||
tenant_id=tenant_id, branch_id=branch_id, allow_cross_branch=allow_cross_branch,
|
||||
@@ -92,7 +92,13 @@ def list_clients(
|
||||
client_type=client_type, client_group_id=client_group_id, include_archived=include_archived,
|
||||
)
|
||||
total = db.execute(select(func.count()).select_from(stmt.subquery())).scalar_one()
|
||||
result = db.execute(stmt.order_by(_safe_sort(sort_by, sort_order)).offset((page - 1) * per_page).limit(per_page)).all()
|
||||
per_page = min(max(int(per_page or 25), 1), 100)
|
||||
pages = max(ceil(total / per_page), 1)
|
||||
page = min(max(int(page or 1), 1), pages)
|
||||
offset = (page - 1) * per_page
|
||||
result = db.execute(
|
||||
stmt.order_by(_safe_sort(sort_by, sort_order)).offset(offset).limit(per_page)
|
||||
).all()
|
||||
rows=[]
|
||||
for client, partner_name, branch_name, tenant_name, client_group_name, client_group_code, association_type, assoc_firm_tenant_id, assoc_consultant_id, assoc_partner_user_id, assoc_created_source in result:
|
||||
row={**client.__dict__}; row.pop("_sa_instance_state",None)
|
||||
@@ -109,8 +115,7 @@ def list_clients(
|
||||
if branch_id and not allow_cross_branch: stats_stmt=stats_stmt.where(Client.branch_id==branch_id)
|
||||
if partner_id: stats_stmt=stats_stmt.where(Client.partner_id==partner_id)
|
||||
total_all,active,inactive,archived=db.execute(stats_stmt).one()
|
||||
pages=ceil(total/per_page) if per_page else 1
|
||||
return {"rows":rows,"meta":{"total":total,"page":page,"per_page":per_page,"pages":max(pages,1)},"stats":{"total":int(total_all or 0),"active":int(active or 0),"inactive":int(inactive or 0),"archived":int(archived or 0)}}
|
||||
return {"rows":rows,"meta":{"total":total,"page":page,"per_page":per_page,"pages":pages},"stats":{"total":int(total_all or 0),"active":int(active or 0),"inactive":int(inactive or 0),"archived":int(archived or 0)}}
|
||||
|
||||
|
||||
def get_client_detail_payload(db: Session, client_id: int):
|
||||
|
||||
Reference in New Issue
Block a user