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 | import { Injectable, NotFoundException, ConflictException, } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { Model } from 'mongoose'; import { CostCenter } from './schemas/cost-center.schema'; import { AuditLogService } from '../../../platform/audit/audit-log.service'; @Injectable() export class CostCenterService { constructor( @InjectModel(CostCenter.name) private readonly costCenterModel: Model<CostCenter>, private readonly auditLog: AuditLogService, ) {} async create(tenantId: string, data: any, userId: string) { const existing = await this.costCenterModel .findOne({ tenantId, costCenterCode: data.costCenterCode }) .exec(); if (existing) throw new ConflictException( `Cost center code ${data.costCenterCode} already exists.`, ); const cc = await this.costCenterModel.create({ ...data, tenantId, createdBy: userId, }); await this.auditLog.log({ tenantId, userId, action: 'CREATE', resource: 'CostCenter', resourceId: cc.id, newValues: data, moduleName: 'HR', }); return cc; } async findAll(tenantId: string) { return this.costCenterModel .find({ tenantId, deletedAt: null }) .sort({ name: 1 }) .lean() .exec(); } async findOne(tenantId: string, id: string) { const cc = await this.costCenterModel .findOne({ tenantId, _id: id, deletedAt: null }) .lean() .exec(); if (!cc) throw new NotFoundException('Cost center not found'); return cc; } async update(tenantId: string, id: string, data: any, userId: string) { const cc = await this.costCenterModel .findOne({ tenantId, _id: id, deletedAt: null }) .exec(); if (!cc) throw new NotFoundException('Cost center not found'); Object.assign(cc, data, { updatedBy: userId }); await cc.save(); await this.auditLog.log({ tenantId, userId, action: 'UPDATE', resource: 'CostCenter', resourceId: cc.id, newValues: data, moduleName: 'HR', }); return cc; } } |