244
return len(result.scalars().all())
245
246
247
-async def alert_total_for_user(user: User, db: AsyncSession) -> int:
247
+async def alert_total_for_user(user: User, db: AsyncSession, customer_codes: Optional[List[str]] = None) -> int:
248
"""Get total alerts count with customer and tag filtering"""
249
from sqlalchemy import and_
250
from sqlalchemy import exists
253
filters = []
254
255
# Customer filtering
256
- accessible_customers = await customer_access_handler.get_user_accessible_customers(user, db)
256
+ accessible_customers = await customer_access_handler.resolve_effective_customers(user, customer_codes, db)
257
if "*" not in accessible_customers:
258
filters.append(Alert.customer_code.in_(accessible_customers))
259
290
return result.scalar_one()
291
292
293
-async def alerts_open_for_user(user: User, db: AsyncSession) -> int:
293
+async def alerts_open_for_user(user: User, db: AsyncSession, customer_codes: Optional[List[str]] = None) -> int:
294
"""Get open alerts count with customer and tag filtering"""
295
from sqlalchemy import and_
296
from sqlalchemy import exists
299
filters = [Alert.status == "OPEN"]
300
301
# Customer filtering
302
- accessible_customers = await customer_access_handler.get_user_accessible_customers(user, db)
302
+ accessible_customers = await customer_access_handler.resolve_effective_customers(user, customer_codes, db)
303
if "*" not in accessible_customers:
304
filters.append(Alert.customer_code.in_(accessible_customers))
305
336
return result.scalar_one()
337
338
339
-async def alerts_in_progress_for_user(user: User, db: AsyncSession) -> int:
339
+async def alerts_in_progress_for_user(user: User, db: AsyncSession, customer_codes: Optional[List[str]] = None) -> int:
340
"""Get in-progress alerts count with customer and tag filtering"""
341
from sqlalchemy import and_
342
from sqlalchemy import exists
345
filters = [Alert.status == "IN_PROGRESS"]
346
347
# Customer filtering
348
- accessible_customers = await customer_access_handler.get_user_accessible_customers(user, db)
348
+ accessible_customers = await customer_access_handler.resolve_effective_customers(user, customer_codes, db)
349
if "*" not in accessible_customers:
350
filters.append(Alert.customer_code.in_(accessible_customers))
351
382
return result.scalar_one()
383
384
385
-async def alerts_closed_for_user(user: User, db: AsyncSession) -> int:
385
+async def alerts_closed_for_user(user: User, db: AsyncSession, customer_codes: Optional[List[str]] = None) -> int:
386
"""Get closed alerts count with customer and tag filtering"""
387
from sqlalchemy import and_
388
from sqlalchemy import exists
391
filters = [Alert.status == "CLOSED"]
392
393
# Customer filtering
394
- accessible_customers = await customer_access_handler.get_user_accessible_customers(user, db)
394
+ accessible_customers = await customer_access_handler.resolve_effective_customers(user, customer_codes, db)
395
if "*" not in accessible_customers:
396
filters.append(Alert.customer_code.in_(accessible_customers))
397
2596
page: int = 1,
2597
page_size: int = 25,
2598
order: str = "desc",
2599
+ customer_codes: Optional[List[str]] = None,
2600
) -> List[AlertOut]:
2601
"""List alerts filtered by user's customer access and tag access"""
2602
from sqlalchemy import and_
2618
filters = []
2619
2620
# 1. Apply customer filtering
2620
- accessible_customers = await customer_access_handler.get_user_accessible_customers(user, session)
2621
+ accessible_customers = await customer_access_handler.resolve_effective_customers(user, customer_codes, session)
2622
if "*" not in accessible_customers:
2623
filters.append(Alert.customer_code.in_(accessible_customers))
2624
2695
return alerts_out
2696
2697
2697
-async def case_total_for_user(user: User, session: AsyncSession) -> int:
2698
+async def case_total_for_user(user: User, session: AsyncSession, customer_codes: Optional[List[str]] = None) -> int:
2699
"""Get total cases count with customer filtering"""
2700
base_query = select(func.count(Case.id))
2701
2701
- accessible_customers = await customer_access_handler.get_user_accessible_customers(user, session)
2702
+ accessible_customers = await customer_access_handler.resolve_effective_customers(user, customer_codes, session)
2703
if "*" not in accessible_customers:
2704
base_query = base_query.where(Case.customer_code.in_(accessible_customers))
2705
2707
return result.scalar_one()
2708
2709
2709
-async def cases_open_for_user(user: User, session: AsyncSession) -> int:
2710
+async def cases_open_for_user(user: User, session: AsyncSession, customer_codes: Optional[List[str]] = None) -> int:
2711
"""Get open cases count with customer filtering"""
2712
base_query = select(func.count(Case.id)).where(Case.case_status == "OPEN")
2713
2713
- accessible_customers = await customer_access_handler.get_user_accessible_customers(user, session)
2714
+ accessible_customers = await customer_access_handler.resolve_effective_customers(user, customer_codes, session)
2715
if "*" not in accessible_customers:
2716
base_query = base_query.where(Case.customer_code.in_(accessible_customers))
2717
2719
return result.scalar_one()
2720
2721
2721
-async def cases_in_progress_for_user(user: User, session: AsyncSession) -> int:
2722
+async def cases_in_progress_for_user(user: User, session: AsyncSession, customer_codes: Optional[List[str]] = None) -> int:
2723
"""Get in-progress cases count with customer filtering"""
2724
base_query = select(func.count(Case.id)).where(Case.case_status == "IN_PROGRESS")
2725
2725
- accessible_customers = await customer_access_handler.get_user_accessible_customers(user, session)
2726
+ accessible_customers = await customer_access_handler.resolve_effective_customers(user, customer_codes, session)
2727
if "*" not in accessible_customers:
2728
base_query = base_query.where(Case.customer_code.in_(accessible_customers))
2729
2731
return result.scalar_one()
2732
2733
2733
-async def cases_closed_for_user(user: User, session: AsyncSession) -> int:
2734
+async def cases_closed_for_user(user: User, session: AsyncSession, customer_codes: Optional[List[str]] = None) -> int:
2735
"""Get closed cases count with customer filtering"""
2736
base_query = select(func.count(Case.id)).where(Case.case_status == "CLOSED")
2737
2737
- accessible_customers = await customer_access_handler.get_user_accessible_customers(user, session)
2738
+ accessible_customers = await customer_access_handler.resolve_effective_customers(user, customer_codes, session)
2739
if "*" not in accessible_customers:
2740
base_query = base_query.where(Case.customer_code.in_(accessible_customers))
2741
2749
page: int = 1,
2750
page_size: int = 25,
2751
order: str = "desc",
2752
+ customer_codes: Optional[List[str]] = None,
2753
) -> List[CaseOut]:
2754
"""List cases filtered by user's customer access with pagination"""
2755
2765
selectinload(Case.comments),
2766
)
2767
2766
- # Apply customer filtering
2767
- filtered_query = await customer_access_handler.filter_query_by_customer_access(user, session, base_query, Case.customer_code)
2768
+ # Apply customer filtering (optionally narrowed to a requested subset)
2769
+ filtered_query = await customer_access_handler.filter_query_by_customer_access(
2770
+ user,
2771
+ session,
2772
+ base_query,
2773
+ Case.customer_code,
2774
+ requested_customers=customer_codes,
2775
+ )
2776
2777
# Apply ordering and pagination
2778
final_query = filtered_query.order_by(order_by).offset(offset).limit(page_size)