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 | import { Injectable, NotFoundException, BadRequestException, } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { Model } from 'mongoose'; import { LeaveDonationPolicy, LeaveDonationRequest, LeaveDonationTransaction, } from '../schemas'; import { LeaveBalanceEngine } from '../balance/leave-balance.engine'; import { FeatureFlagService } from '../../../../platform/feature-flags/feature-flag.service'; @Injectable() export class LeaveDonationService { constructor( @InjectModel(LeaveDonationPolicy.name) private policyModel: Model<LeaveDonationPolicy>, @InjectModel(LeaveDonationRequest.name) private requestModel: Model<LeaveDonationRequest>, @InjectModel(LeaveDonationTransaction.name) private txnModel: Model<LeaveDonationTransaction>, private balanceEngine: LeaveBalanceEngine, private featureFlagService: FeatureFlagService, ) {} async createPolicy(tenantId: string, dto: any) { return this.policyModel.create({ ...dto, tenantId }); } async submitDonation(tenantId: string, dto: any, userId: string) { const isEnabled = await this.featureFlagService.evaluate( 'leave-donation-enabled', { tenantId, userId, userEmail: '', }, ); if (!isEnabled) { throw new BadRequestException( 'Leave donation feature is currently disabled', ); } const policy = await this.policyModel .findOne({ _id: dto.policyId, tenantId }) .exec(); if (!policy) throw new NotFoundException('Leave donation policy not found'); const donorAvailable = await this.balanceEngine.computeAvailableBalance( tenantId, dto.donorEmployeeId, dto.leaveTypeId, dto.leavePeriodId, ); if (dto.donationQuantity > policy.maxDonationDays) { throw new BadRequestException( `Maximum donation limit is ${policy.maxDonationDays} days`, ); } if ( donorAvailable - dto.donationQuantity < policy.minDonorRemainingBalance ) { throw new BadRequestException( `Must retain at least ${policy.minDonorRemainingBalance} days of balance`, ); } return this.requestModel.create({ ...dto, tenantId, status: policy.approvalRequired ? 'pending' : 'approved', expiresAt: policy.expiryDays ? new Date(Date.now() + policy.expiryDays * 86400000) : null, }); } async approveDonation(tenantId: string, requestId: string, userId: string) { const request = await this.requestModel .findOne({ _id: requestId, tenantId }) .exec(); if (!request) throw new NotFoundException('Donation request not found'); if (request.status !== 'pending') throw new BadRequestException('Request is not in pending status'); request.status = 'approved'; request.approvedBy = userId; request.approvedAt = new Date(); await request.save(); // Deduct from donor const donorAccount = await this.balanceEngine.ensureAccount( tenantId, request.donorEmployeeId, request.leaveTypeId, request.leavePeriodId, ); const donorLedger = await this.balanceEngine.writeLedger({ tenantId, employeeId: request.donorEmployeeId, leaveTypeId: request.leaveTypeId, leavePeriodId: request.leavePeriodId, accountId: donorAccount._id.toString(), transactionType: 'Donation Debit', quantity: -request.donationQuantity, unit: request.unit, effectiveDate: new Date().toISOString().split('T')[0], sourceEntityType: 'LeaveDonationRequest', sourceEntityId: request._id.toString(), idempotencyKey: `donation-donor:${request._id}`, createdBy: userId, }); let recipientLedgerId: string | null = null; // Credit to recipient if direct transfer if (request.recipientEmployeeId) { const recipientAccount = await this.balanceEngine.ensureAccount( tenantId, request.recipientEmployeeId, request.leaveTypeId, request.leavePeriodId, ); const recipientLedger = await this.balanceEngine.writeLedger({ tenantId, employeeId: request.recipientEmployeeId, leaveTypeId: request.leaveTypeId, leavePeriodId: request.leavePeriodId, accountId: recipientAccount._id.toString(), transactionType: 'Donation Credit', quantity: request.donationQuantity, unit: request.unit, effectiveDate: new Date().toISOString().split('T')[0], sourceEntityType: 'LeaveDonationRequest', sourceEntityId: request._id.toString(), idempotencyKey: `donation-recipient:${request._id}`, createdBy: userId, }); recipientLedgerId = recipientLedger._id.toString(); } await this.txnModel.create({ tenantId, requestId: request._id.toString(), donorEmployeeId: request.donorEmployeeId, recipientEmployeeId: request.recipientEmployeeId, leaveTypeId: request.leaveTypeId, quantity: request.donationQuantity, unit: request.unit, donorLedgerId: donorLedger._id.toString(), recipientLedgerId, }); return request; } async rejectDonation(tenantId: string, requestId: string, reason: string) { return this.requestModel.findOneAndUpdate( { _id: requestId, tenantId }, { $set: { status: 'rejected', rejectionReason: reason } }, { new: true }, ); } } |