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 | import { Injectable, NotFoundException } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { Model } from 'mongoose'; import { AttendanceRegularizationRequest, AttendanceRegularizationAction, AttendanceCorrection, AttendancePunch, } from '../schemas'; import { RequestRegularizationDto, ActionRegularizationDto, } from './dto/regularization.dto'; import { AttendanceCalculationEngine } from '../engine/engine.service'; @Injectable() export class RegularizationService { constructor( @InjectModel(AttendanceRegularizationRequest.name) private requestModel: Model<AttendanceRegularizationRequest>, @InjectModel(AttendanceRegularizationAction.name) private actionModel: Model<AttendanceRegularizationAction>, @InjectModel(AttendanceCorrection.name) private correctionModel: Model<AttendanceCorrection>, @InjectModel(AttendancePunch.name) private punchModel: Model<AttendancePunch>, private engine: AttendanceCalculationEngine, ) {} async submitRequest( tenantId: string, employeeId: string, requestDto: RequestRegularizationDto, ) { const created = new this.requestModel({ ...requestDto, tenantId, employeeId, submittedAt: new Date(), }); return created.save(); } async processAction( tenantId: string, requestId: string, actionDto: ActionRegularizationDto, approverId: string, ) { const request = await this.requestModel .findOne({ _id: requestId, tenantId }) .exec(); if (!request) throw new NotFoundException('Regularization request not found'); request.status = actionDto.action; request.resolvedAt = new Date(); await request.save(); await this.actionModel.create({ tenantId, requestId, actionBy: approverId, action: actionDto.action, comments: actionDto.comments, }); // If approved, create Correction Records and invalidate old punches if (actionDto.action === 'approved') { await this.applyCorrection(tenantId, request, approverId); // Re-trigger calculation await this.engine.calculateDailyAttendance( tenantId, request.employeeId, request.attendanceDate, ); } return request; } private async applyCorrection( tenantId: string, request: AttendanceRegularizationRequest, approverId: string, ) { await this.correctionModel.create({ tenantId, employeeId: request.employeeId, attendanceDate: request.attendanceDate, regularizationRequestId: request._id, correctedCheckIn: request.requestedCheckIn, correctedCheckOut: request.requestedCheckOut, correctedBy: approverId, }); // Mark previous punches for that date as corrected=true so they are ignored by engine await this.punchModel.updateMany( { tenantId, employeeId: request.employeeId, attendanceDate: request.attendanceDate, corrected: false, }, { $set: { corrected: true, correctionReason: `Regularization ${request._id}`, }, }, ); // Insert new valid punches based on the approved request if (request.requestedCheckIn) { await this.punchModel.create({ tenantId, employeeId: request.employeeId, attendanceDate: request.attendanceDate, punchType: 'Check In', timestamp: request.requestedCheckIn, timezone: 'UTC', source: 'Admin', createdBy: approverId, metadata: { isRegularization: true, requestId: request._id }, }); } if (request.requestedCheckOut) { await this.punchModel.create({ tenantId, employeeId: request.employeeId, attendanceDate: request.attendanceDate, punchType: 'Check Out', timestamp: request.requestedCheckOut, timezone: 'UTC', source: 'Admin', createdBy: approverId, metadata: { isRegularization: true, requestId: request._id }, }); } } async getMyRequests(tenantId: string, employeeId: string) { return this.requestModel .find({ tenantId, employeeId }) .sort({ submittedAt: -1 }) .exec(); } async getPendingRequestsForApprover(tenantId: string, approverId: string) { // Basic stub. Real life handles complex hierarchies. return this.requestModel.find({ tenantId, status: 'pending' }).exec(); } } |