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

0% Statements 0/18
0% Branches 0/10
0% Functions 0/5
0% Lines 0/15

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                                                                                                                   
import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { AttendanceApprovalPeriod, AttendanceApprovalRecord } from '../schemas';
import { CreateApprovalPeriodDto, ActionApprovalDto } from './dto/approval.dto';
 
@Injectable()
export class ApprovalService {
  constructor(
    @InjectModel(AttendanceApprovalPeriod.name)
    private periodModel: Model<AttendanceApprovalPeriod>,
    @InjectModel(AttendanceApprovalRecord.name)
    private recordModel: Model<AttendanceApprovalRecord>,
  ) {}
 
  async createPeriod(tenantId: string, createDto: CreateApprovalPeriodDto) {
    const created = new this.periodModel({
      ...createDto,
      tenantId,
    });
    return created.save();
  }
 
  async getPeriods(tenantId: string) {
    return this.periodModel.find({ tenantId }).sort({ startDate: -1 }).exec();
  }
 
  async processApproval(
    tenantId: string,
    periodId: string,
    employeeId: string,
    actionDto: ActionApprovalDto,
    approverId: string,
  ) {
    const period = await this.periodModel
      .findOne({ _id: periodId, tenantId })
      .exec();
    if (!period) throw new NotFoundException('Approval period not found');
 
    const record = await this.recordModel.findOneAndUpdate(
      { tenantId, periodId, employeeId },
      {
        approverId,
        status: actionDto.status,
        comments: actionDto.comments,
      },
      { upsert: true, new: true },
    );
 
    // Side effect: Depending on policy, maybe lock the daily records for this period
    return record;
  }
 
  async getApprovalRecords(tenantId: string, periodId: string) {
    return this.recordModel.find({ tenantId, periodId }).exec();
  }
}