All files / src/domains/hr/organization work-location.service.ts

0% Statements 0/28
0% Branches 0/14
0% Functions 0/5
0% Lines 0/24

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