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 | import { Injectable, NotFoundException, BadRequestException, ConflictException, } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { Model } from 'mongoose'; import { Branch } from './schemas/branch.schema'; import { AuditLogService } from '../../../platform/audit/audit-log.service'; import { EventBusService } from '../../../platform/events/event-bus.service'; @Injectable() export class BranchService { constructor( @InjectModel(Branch.name) private readonly branchModel: Model<Branch>, private readonly auditLog: AuditLogService, private readonly eventBus: EventBusService, ) {} async create(tenantId: string, data: any, userId: string) { const existing = await this.branchModel .findOne({ tenantId, branchCode: data.branchCode }) .exec(); if (existing) throw new ConflictException( `Branch code ${data.branchCode} already exists.`, ); if (data.parentBranchId) { const parent = await this.branchModel .findOne({ tenantId, _id: data.parentBranchId }) .exec(); if (!parent) throw new BadRequestException( 'Parent branch not found in this tenant.', ); } const branch = await this.branchModel.create({ ...data, tenantId, createdBy: userId, }); await this.auditLog.log({ tenantId, userId, action: 'CREATE', resource: 'Branch', resourceId: branch.id, newValues: data, moduleName: 'HR', }); await this.eventBus.publish( 'hr.branch.created.v1', { tenantId, branchId: branch.id }, tenantId, ); return branch; } async findAll(tenantId: string, filters?: any) { const query: any = { tenantId, deletedAt: null }; if (filters?.status) query.status = filters.status; return this.branchModel.find(query).sort({ branchName: 1 }).lean().exec(); } async findOne(tenantId: string, id: string) { const branch = await this.branchModel .findOne({ tenantId, _id: id, deletedAt: null }) .lean() .exec(); if (!branch) throw new NotFoundException('Branch not found'); return branch; } async update(tenantId: string, id: string, data: any, userId: string) { const branch = await this.branchModel .findOne({ tenantId, _id: id, deletedAt: null }) .exec(); if (!branch) throw new NotFoundException('Branch not found'); if (data.branchCode && data.branchCode !== branch.branchCode) { const dup = await this.branchModel .findOne({ tenantId, branchCode: data.branchCode }) .exec(); if (dup) throw new ConflictException( `Branch code ${data.branchCode} already exists.`, ); } const oldValues = branch.toObject(); Object.assign(branch, data, { updatedBy: userId }); await branch.save(); await this.auditLog.log({ tenantId, userId, action: 'UPDATE', resource: 'Branch', resourceId: branch.id, oldValues, newValues: data, moduleName: 'HR', }); await this.eventBus.publish( 'hr.branch.updated.v1', { tenantId, branchId: branch.id }, tenantId, ); return branch; } async delete( tenantId: string, id: string, userId: string, employeeModel?: Model<any>, ) { const branch = await this.branchModel .findOne({ tenantId, _id: id, deletedAt: null }) .exec(); if (!branch) throw new NotFoundException('Branch not found'); if (employeeModel) { const activeCount = await employeeModel.countDocuments({ tenantId, branchId: id, employmentStatus: { $nin: ['terminated', 'archived'] }, }); if (activeCount > 0) throw new BadRequestException( 'Cannot delete branch with active employees.', ); } branch.deletedAt = new Date(); branch.updatedBy = userId; await branch.save(); await this.auditLog.log({ tenantId, userId, action: 'DELETE', resource: 'Branch', resourceId: branch.id, moduleName: 'HR', }); return { success: true }; } } |