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 | import { Injectable, NotFoundException, Logger } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { Model } from 'mongoose'; import { CompOffPolicy, CompOffEarning, CompOffRequest, CompOffExpiry, } from '../schemas'; import { LeaveBalanceEngine } from '../balance/leave-balance.engine'; @Injectable() export class CompOffService { private readonly logger = new Logger(CompOffService.name); constructor( @InjectModel(CompOffPolicy.name) private policyModel: Model<CompOffPolicy>, @InjectModel(CompOffEarning.name) private earningModel: Model<CompOffEarning>, @InjectModel(CompOffRequest.name) private requestModel: Model<CompOffRequest>, @InjectModel(CompOffExpiry.name) private expiryModel: Model<CompOffExpiry>, private balanceEngine: LeaveBalanceEngine, ) {} async createPolicy(tenantId: string, dto: any, userId: string) { return this.policyModel.create({ ...dto, tenantId, createdBy: userId }); } async getBalance(tenantId: string, employeeId: string) { const earnings = await this.earningModel .find({ tenantId, employeeId, status: 'approved', expiresAt: { $gt: new Date() }, }) .exec(); const total = earnings.reduce((sum, e) => sum + e.earnedQuantity, 0); return { employeeId, availableCompOff: total, earnings }; } async getEarnings(tenantId: string, employeeId: string) { return this.earningModel .find({ tenantId, employeeId }) .sort({ earnedDate: -1 }) .exec(); } async requestEarning(tenantId: string, dto: any, userId: string) { const policy = await this.policyModel .findOne({ tenantId, status: 'active' }) .exec(); const expiresAt = policy ? new Date(Date.now() + policy.expiryDays * 86400000) : null; return this.earningModel.create({ ...dto, tenantId, requestedBy: userId, status: policy?.approvalRequired ? 'pending' : 'approved', expiresAt, }); } async approveEarning( tenantId: string, earningId: string, approverId: string, ) { const earning = await this.earningModel .findOne({ _id: earningId, tenantId }) .exec(); if (!earning) throw new NotFoundException('Comp-off earning not found'); earning.status = 'approved'; earning.approvedBy = approverId; earning.approvedAt = new Date(); await earning.save(); // Credit balance via ledger const policy = await this.policyModel .findOne({ tenantId, status: 'active' }) .exec(); if (policy) { const account = await this.balanceEngine.ensureAccount( tenantId, earning.employeeId, policy.creditLeaveTypeId, 'default', ); const ledger = await this.balanceEngine.writeLedger({ tenantId, employeeId: earning.employeeId, leaveTypeId: policy.creditLeaveTypeId, leavePeriodId: 'default', accountId: account._id.toString(), transactionType: 'Comp-Off Credit', quantity: earning.earnedQuantity, unit: earning.unit, effectiveDate: earning.earnedDate, sourceEntityType: 'CompOffEarning', sourceEntityId: earning._id.toString(), idempotencyKey: `comp-off-credit:${earning._id}`, expiresAt: earning.expiresAt, createdBy: approverId, }); earning.ledgerId = ledger._id.toString(); await earning.save(); } return earning; } async rejectEarning(tenantId: string, earningId: string, reason: string) { return this.earningModel.findOneAndUpdate( { _id: earningId, tenantId }, { $set: { status: 'rejected', rejectedReason: reason } }, { new: true }, ); } async expireEarnings(tenantId: string) { const expiredEarnings = await this.earningModel .find({ tenantId, status: 'approved', expiresAt: { $lt: new Date() }, }) .exec(); for (const earning of expiredEarnings) { earning.status = 'expired'; await earning.save(); const policy = await this.policyModel .findOne({ tenantId, status: 'active' }) .exec(); if (policy) { const account = await this.balanceEngine.ensureAccount( tenantId, earning.employeeId, policy.creditLeaveTypeId, 'default', ); const ledger = await this.balanceEngine.writeLedger({ tenantId, employeeId: earning.employeeId, leaveTypeId: policy.creditLeaveTypeId, leavePeriodId: 'default', accountId: account._id.toString(), transactionType: 'Comp-Off Expiry', quantity: -earning.earnedQuantity, unit: earning.unit, effectiveDate: new Date().toISOString().split('T')[0], sourceEntityType: 'CompOffEarning', sourceEntityId: earning._id.toString(), idempotencyKey: `comp-off-expiry:${earning._id}`, createdBy: 'system', }); await this.expiryModel.create({ tenantId, employeeId: earning.employeeId, earningId: earning._id.toString(), expiredQuantity: earning.earnedQuantity, expiryDate: new Date().toISOString().split('T')[0], ledgerId: ledger._id.toString(), processedAt: new Date(), }); } } return { expired: expiredEarnings.length }; } } |