1-
1+ from django . db import connection
22from django .db .models import Count
33import django_filters .rest_framework as df_filters
4- from django .db .models import BooleanField , Case , Value , When
54from drf_spectacular .utils import extend_schema
6- from rest_framework import exceptions , parsers , status , viewsets , filters
75from rest_framework .decorators import action
86from rest_framework .response import Response
7+ from rest_framework .views import APIView
98from rest_framework .permissions import IsAuthenticated
9+ from django .db .models import BooleanField , Case , Value , When
10+ from rest_framework import exceptions , parsers , status , viewsets , filters
11+
1012
1113from apps .accounts .permissions import Moderator
1214from apps .jobs .constants import response , values
1315from apps .userprofile .models import UserProfile
16+ from apps .applicants .models import Applicants
1417from apps .jobs .models import Company , ContactMessage , Job
1518from apps .accounts .permissions import IsEmployer
16- from apps .jobs .serializers import CompanySerializer , ContactUsSerializer , JobSerializer , JobsCountByCategoriesSerializer
19+ from apps .jobs .serializers import CompanySerializer , ContactUsSerializer , JobSerializer , JobsCountByCategoriesSerializer , CompanyStatsResponseSerializer
1720from apps .jobs .utils .validators import validationClass
1821from apps .utils .responses import InternalServerError
1922from apps .utils .pagination import DefaultPagination
@@ -403,3 +406,40 @@ def list(self, request, *args, **kwargs):
403406 return super ().list (request , * args , ** kwargs )
404407 else :
405408 return super ().list (request , * args , ** kwargs )
409+
410+
411+ class CompanyStats (APIView ):
412+
413+ permission_classes = [IsAuthenticated , IsEmployer ]
414+
415+ @extend_schema (tags = ["company" ], responses = {200 : CompanyStatsResponseSerializer })
416+ def get (self , request ):
417+ """Get company stats"""
418+ try :
419+ raw_query = """
420+ SELECT
421+ COUNT(DISTINCT(tj.job_id)) AS job_count,
422+ COUNT(ta.id) AS applications_count,
423+ COUNT(CASE WHEN ta.status != 'applied' THEN 1 ELSE NULL END) AS reviewed_count,
424+ COUNT(CASE WHEN ta.status = 'shortlisted' THEN 1 ELSE NULL END) AS shortlisted_count
425+ FROM tbl_job tj
426+ JOIN tbl_applicants ta ON tj.job_id = ta.job_id
427+ WHERE tj.employer_id = %s
428+ """
429+
430+ # Execute the raw SQL query
431+ with connection .cursor () as cursor :
432+ cursor .execute (raw_query , [str (request .user .id ).replace ("-" , "" )])
433+ result = cursor .fetchone ()
434+
435+ job_count , applications_count , reviewed_count , shortlisted_count = result
436+ company_stats = CompanyStatsResponseSerializer ({
437+ "job_count" : job_count ,
438+ "applications_count" : applications_count ,
439+ "reviewed_count" : reviewed_count ,
440+ "shortlisted_count" : shortlisted_count
441+ })
442+
443+ return Response (company_stats .data )
444+ except Exception as e :
445+ raise InternalServerError ()
0 commit comments