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 | import { Injectable, NotFoundException, ConflictException, } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { Model } from 'mongoose'; import { Designation } from './schemas/designation.schema'; import { AuditLogService } from '../../../platform/audit/audit-log.service'; import { EventBusService } from '../../../platform/events/event-bus.service'; @Injectable() export class DesignationService { constructor( @InjectModel(Designation.name) private readonly designationModel: Model<Designation>, private readonly auditLog: AuditLogService, private readonly eventBus: EventBusService, ) {} async create(tenantId: string, data: any, userId: string) { const existing = await this.designationModel .findOne({ tenantId, designationCode: data.designationCode }) .exec(); if (existing) throw new ConflictException( `Designation code ${data.designationCode} already exists.`, ); const desg = await this.designationModel.create({ ...data, tenantId, createdBy: userId, }); await this.auditLog.log({ tenantId, userId, action: 'CREATE', resource: 'Designation', resourceId: desg.id, newValues: data, moduleName: 'HR', }); await this.eventBus.publish( 'hr.designation.created.v1', { tenantId, designationId: desg.id }, tenantId, ); return desg; } async findAll(tenantId: string) { return this.designationModel .find({ tenantId, deletedAt: null }) .sort({ designationName: 1 }) .lean() .exec(); } async findOne(tenantId: string, id: string) { const desg = await this.designationModel .findOne({ tenantId, _id: id, deletedAt: null }) .lean() .exec(); if (!desg) throw new NotFoundException('Designation not found'); return desg; } async update(tenantId: string, id: string, data: any, userId: string) { const desg = await this.designationModel .findOne({ tenantId, _id: id, deletedAt: null }) .exec(); if (!desg) throw new NotFoundException('Designation not found'); const oldValues = desg.toObject(); Object.assign(desg, data, { updatedBy: userId }); await desg.save(); await this.auditLog.log({ tenantId, userId, action: 'UPDATE', resource: 'Designation', resourceId: desg.id, oldValues, newValues: data, moduleName: 'HR', }); return desg; } async delete(tenantId: string, id: string, userId: string) { const desg = await this.designationModel .findOne({ tenantId, _id: id, deletedAt: null }) .exec(); if (!desg) throw new NotFoundException('Designation not found'); desg.deletedAt = new Date(); desg.updatedBy = userId; await desg.save(); await this.auditLog.log({ tenantId, userId, action: 'DELETE', resource: 'Designation', resourceId: desg.id, moduleName: 'HR', }); return { success: true }; } } |