All files / src/domains/hr/dashboard dashboard.service.ts

0% Statements 0/27
0% Branches 0/11
0% Functions 0/10
0% Lines 0/25

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180                                                                                                                                                                                                                                                                                                                                                                       
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { Employee } from '../employee/schemas/employee.schema';
import { EmployeeEmploymentHistory } from '../employee/schemas/employee-history.schema';
import { Cron, CronExpression } from '@nestjs/schedule';
 
@Injectable()
export class HrDashboardService {
  constructor(
    @InjectModel(Employee.name) private readonly employeeModel: Model<Employee>,
    @InjectModel(EmployeeEmploymentHistory.name)
    private readonly empHistModel: Model<EmployeeEmploymentHistory>,
  ) {}
 
  async getSummary(tenantId: string) {
    const [
      total,
      active,
      probation,
      notice,
      suspended,
      draft,
      invited,
      onboarding,
    ] = await Promise.all([
      this.employeeModel.countDocuments({ tenantId, deletedAt: null }),
      this.employeeModel.countDocuments({
        tenantId,
        employmentStatus: 'active',
      }),
      this.employeeModel.countDocuments({
        tenantId,
        employmentStatus: 'probation',
      }),
      this.employeeModel.countDocuments({
        tenantId,
        employmentStatus: 'notice_period',
      }),
      this.employeeModel.countDocuments({
        tenantId,
        employmentStatus: 'suspended',
      }),
      this.employeeModel.countDocuments({
        tenantId,
        employmentStatus: 'draft',
      }),
      this.employeeModel.countDocuments({
        tenantId,
        employmentStatus: 'invited',
      }),
      this.employeeModel.countDocuments({
        tenantId,
        employmentStatus: 'onboarding',
      }),
    ]);
 
    return {
      total,
      active,
      probation,
      notice,
      suspended,
      draft,
      invited,
      onboarding,
    };
  }
 
  async getHeadcountByBranch(tenantId: string) {
    return this.employeeModel.aggregate([
      {
        $match: {
          tenantId,
          deletedAt: null,
          employmentStatus: { $in: ['active', 'probation', 'confirmed'] },
        },
      },
      { $group: { _id: '$branchId', count: { $sum: 1 } } },
      { $sort: { count: -1 } },
    ]);
  }
 
  async getHeadcountByDepartment(tenantId: string) {
    return this.employeeModel.aggregate([
      {
        $match: {
          tenantId,
          deletedAt: null,
          employmentStatus: { $in: ['active', 'probation', 'confirmed'] },
        },
      },
      { $group: { _id: '$departmentId', count: { $sum: 1 } } },
      { $sort: { count: -1 } },
    ]);
  }
 
  async getEmploymentTypeDistribution(tenantId: string) {
    return this.employeeModel.aggregate([
      {
        $match: {
          tenantId,
          deletedAt: null,
          employmentStatus: { $in: ['active', 'probation', 'confirmed'] },
        },
      },
      { $group: { _id: '$employmentType', count: { $sum: 1 } } },
    ]);
  }
 
  async getNewJoiners(tenantId: string, days = 30) {
    const since = new Date();
    since.setDate(since.getDate() - days);
    return this.employeeModel
      .find({ tenantId, joiningDate: { $gte: since }, deletedAt: null })
      .select(
        'firstName lastName displayName employeeCode designationId branchId joiningDate',
      )
      .sort({ joiningDate: -1 })
      .limit(50)
      .lean()
      .exec();
  }
 
  async getUpcomingConfirmations(tenantId: string, days = 30) {
    const now = new Date();
    const until = new Date();
    until.setDate(until.getDate() + days);
    return this.employeeModel
      .find({
        tenantId,
        employmentStatus: 'probation',
        probationEndDate: { $gte: now, $lte: until },
        deletedAt: null,
      })
      .select(
        'firstName lastName displayName employeeCode designationId probationEndDate',
      )
      .sort({ probationEndDate: 1 })
      .limit(50)
      .lean()
      .exec();
  }
 
  async getRecentActivity(tenantId: string) {
    return this.empHistModel
      .find({ tenantId })
      .sort({ createdAt: -1 })
      .limit(20)
      .lean()
      .exec();
  }
 
  async getStatusDistribution(tenantId: string) {
    return this.employeeModel.aggregate([
      { $match: { tenantId, deletedAt: null } },
      { $group: { _id: '$employmentStatus', count: { $sum: 1 } } },
      { $sort: { count: -1 } },
    ]);
  }
 
  async getHeadcountTrend(tenantId: string, months = 12) {
    const since = new Date();
    since.setMonth(since.getMonth() - months);
    return this.employeeModel.aggregate([
      { $match: { tenantId, joiningDate: { $gte: since }, deletedAt: null } },
      {
        $group: {
          _id: {
            year: { $year: '$joiningDate' },
            month: { $month: '$joiningDate' },
          },
          joiners: { $sum: 1 },
        },
      },
      { $sort: { '_id.year': 1, '_id.month': 1 } },
    ]);
  }
}