from apps.lead.models import Lead as LeadModel from apps.cuser.models import CuserRole as CuserRoleModel, RoleType as RoleTypeModel from rest_framework import serializers from django.db.models import Count from django.db.models import Count from rest_framework import exceptions def list_leads_api() -> LeadModel: try: base_filter = { 'sys_ws__uuid': 'ca918836-ddf2-4d88-8d76-033d285062e4', } leads = LeadModel.objects.filter(**base_filter).order_by('-created_at') return leads except Exception as exp: message = f'Error Fetching List Leads: {exp}' raise exceptions.ValidationError({'error': message}) def user_lead_report_count_by_date(start_datetime, end_datetime, status: str | None = None,): base_filer = { 'cuser_role__role_type__role_name_en': 'workspace_operator', 'created_at__range': [start_datetime, end_datetime], } if status: base_filer['status'] = status return LeadModel.objects.filter(**base_filer).values('cuser_role__name_family').annotate( count=Count('cuser_role__name_family')).order_by('-count') def operators_cuser_role(): return CuserRoleModel.objects.filter(sys_ws_id=2, role_type__role_name_en='workspace_operator')