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 | import { Injectable, NotFoundException, ConflictException, BadRequestException, } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { Model } from 'mongoose'; import { LeavePolicy, LeavePolicyVersion, LeavePolicyRule, LeavePolicyAssignment, } from '../schemas'; @Injectable() export class LeavePolicyService { constructor( @InjectModel(LeavePolicy.name) private policyModel: Model<LeavePolicy>, @InjectModel(LeavePolicyVersion.name) private versionModel: Model<LeavePolicyVersion>, @InjectModel(LeavePolicyRule.name) private ruleModel: Model<LeavePolicyRule>, @InjectModel(LeavePolicyAssignment.name) private assignmentModel: Model<LeavePolicyAssignment>, ) {} async create(tenantId: string, dto: any, userId: string) { const exists = await this.policyModel .findOne({ tenantId, policyCode: dto.policyCode }) .exec(); if (exists) throw new ConflictException( `Policy code '${dto.policyCode}' already exists`, ); return this.policyModel.create({ ...dto, tenantId, createdBy: userId }); } async findAll(tenantId: string, status?: string) { const filter: any = { tenantId }; if (status) filter.status = status; return this.policyModel.find(filter).sort({ policyName: 1 }).exec(); } async findOne(tenantId: string, id: string) { const doc = await this.policyModel.findOne({ _id: id, tenantId }).exec(); if (!doc) throw new NotFoundException('Leave policy not found'); return doc; } async update(tenantId: string, id: string, dto: any, userId: string) { const doc = await this.findOne(tenantId, id); if (doc.status === 'published') throw new BadRequestException( 'Published policies are immutable. Create a new version instead.', ); Object.assign(doc, dto, { updatedBy: userId }); return doc.save(); } async publish(tenantId: string, id: string, userId: string) { const doc = await this.findOne(tenantId, id); if (doc.status === 'published') throw new BadRequestException('Policy already published'); // Create immutable version snapshot const snapshot = doc.toObject(); const newVersion = doc.currentVersion + 1; await this.versionModel.create({ tenantId, policyId: id, version: newVersion, snapshot, publishedBy: userId, publishedAt: new Date(), }); doc.status = 'published'; doc.currentVersion = newVersion; return doc.save(); } async clone(tenantId: string, id: string, userId: string) { const source = await this.findOne(tenantId, id); const { _id, ...clone } = source.toObject() as any; clone.policyCode = `${clone.policyCode}_COPY`; clone.policyName = `${clone.policyName} (Copy)`; clone.status = 'draft'; clone.currentVersion = 1; clone.createdBy = userId; return this.policyModel.create(clone); } async archive(tenantId: string, id: string) { return this.policyModel.findOneAndUpdate( { _id: id, tenantId }, { $set: { status: 'archived' } }, { new: true }, ); } /** * Resolve the effective leave policy for an employee based on priority: * 1=Employee, 2=Team, 3=Grade, 4=Designation, 5=Department, 6=Branch, 7=LegalEntity, 8=Tenant */ async resolveForEmployee( tenantId: string, employeeId: string, employeeContext: { teamId?: string; gradeId?: string; designationId?: string; departmentId?: string; branchId?: string; legalEntityId?: string; }, ) { const today = new Date().toISOString().split('T')[0]; const scopes = [ { scope: 'Employee', entityId: employeeId }, { scope: 'Team', entityId: employeeContext.teamId }, { scope: 'Grade', entityId: employeeContext.gradeId }, { scope: 'Designation', entityId: employeeContext.designationId }, { scope: 'Department', entityId: employeeContext.departmentId }, { scope: 'Branch', entityId: employeeContext.branchId }, { scope: 'LegalEntity', entityId: employeeContext.legalEntityId }, { scope: 'Tenant', entityId: null }, ]; for (const { scope, entityId } of scopes) { if (!entityId && scope !== 'Tenant') continue; const filter: any = { tenantId, scope, status: 'active' }; if (entityId) filter.scopeEntityId = entityId; const assignment = await this.assignmentModel.findOne(filter).exec(); if (!assignment) continue; const policy = await this.policyModel .findOne({ _id: assignment.policyId, tenantId, status: 'published' }) .exec(); if (policy) return { policy, assignment, resolvedScope: scope }; } return null; } // Leave Policy Rule management async setPolicyRules(tenantId: string, policyId: string, rules: any[]) { await this.ruleModel.deleteMany({ tenantId, policyId }); return this.ruleModel.insertMany( rules.map((r) => ({ ...r, tenantId, policyId })), ); } async getPolicyRules(tenantId: string, policyId: string) { return this.ruleModel.find({ tenantId, policyId }).exec(); } // Assignment management async createAssignment(tenantId: string, dto: any, userId: string) { return this.assignmentModel.create({ ...dto, tenantId, assignedBy: userId, }); } async getAssignments(tenantId: string, policyId: string) { return this.assignmentModel.find({ tenantId, policyId }).exec(); } } |