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

0% Statements 0/40
0% Branches 0/30
0% Functions 0/3
0% Lines 0/34

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                                                                                                                                                                                               
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import {
  AttendanceDailyRecord,
  AttendancePunch,
  AttendanceException,
} from '../schemas';
 
@Injectable()
export class DashboardService {
  constructor(
    @InjectModel(AttendanceDailyRecord.name)
    private dailyRecordModel: Model<AttendanceDailyRecord>,
    @InjectModel(AttendancePunch.name)
    private punchModel: Model<AttendancePunch>,
    @InjectModel(AttendanceException.name)
    private exceptionModel: Model<AttendanceException>,
  ) {}
 
  async getAdminDashboardMetrics(tenantId: string, date: string) {
    const todayStr = date || new Date().toISOString().split('T')[0];
 
    const records = await this.dailyRecordModel
      .find({ tenantId, attendanceDate: todayStr })
      .exec();
 
    let presentCount = 0;
    let absentCount = 0;
    let lateCount = 0;
    const onLeaveCount = 0; // Requires leave module integration
 
    for (const rec of records) {
      if (
        rec.attendanceStatus === 'Present' ||
        rec.attendanceStatus === 'Half Day'
      ) {
        presentCount++;
      } else if (rec.attendanceStatus === 'Absent') {
        absentCount++;
      }
 
      if (rec.lateMinutes > 0) lateCount++;
    }
 
    const exceptions = await this.exceptionModel
      .countDocuments({ tenantId, attendanceDate: todayStr, status: 'open' })
      .exec();
 
    return {
      presentCount,
      absentCount,
      lateCount,
      onLeaveCount,
      openExceptionsCount: exceptions,
      totalProcessed: records.length,
    };
  }
 
  async getEmployeeDashboardMetrics(
    tenantId: string,
    employeeId: string,
    month: string,
  ) {
    // month format: YYYY-MM
    const records = await this.dailyRecordModel
      .find({
        tenantId,
        employeeId,
        attendanceDate: { $regex: `^${month}` },
      })
      .exec();
 
    let presentDays = 0;
    let absentDays = 0;
    let totalLateMinutes = 0;
    let totalOvertimeMinutes = 0;
 
    for (const rec of records) {
      if (rec.attendanceStatus === 'Present') presentDays += 1;
      if (rec.attendanceStatus === 'Half Day') presentDays += 0.5;
      if (rec.attendanceStatus === 'Absent') absentDays += 1;
 
      totalLateMinutes += rec.lateMinutes;
      totalOvertimeMinutes += rec.approvedOvertimeMinutes || 0;
    }
 
    return {
      presentDays,
      absentDays,
      totalLateMinutes,
      totalOvertimeMinutes,
    };
  }
}