All files / src/domains/procurement/inspections/services inspection.service.ts

0% Statements 0/19
0% Branches 0/12
0% Functions 0/3
0% Lines 0/17

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                                                                                                 
import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { QualityInspection } from '../schemas/inspection.schema';
import { EventBusService } from '../../../../platform/events/event-bus.service';
import { AuditLogService } from '../../../../platform/audit/audit-log.service';
 
@Injectable()
export class InspectionService {
  constructor(
    @InjectModel(QualityInspection.name)
    private readonly inspectionModel: Model<QualityInspection>,
    private readonly eventBus: EventBusService,
    private readonly auditLog: AuditLogService,
  ) {}
 
  async createInspection(tenantId: string, data: any, userId: string): Promise<QualityInspection> {
    const count = await this.inspectionModel.countDocuments({ tenantId }).exec();
    const inspectionNumber = `QA-${new Date().getFullYear()}-${(count + 1).toString().padStart(5, '0')}`;
 
    const qa = await this.inspectionModel.create({
      ...data,
      tenantId,
      inspectionNumber,
    });
 
    await this.eventBus.publish('procurement.inspection.completed.v1', {
      inspectionId: (qa as any)._id.toString(),
      tenantId,
      decision: qa.decision,
    }, tenantId);
 
    await this.auditLog.log({
      tenantId,
      userId,
      action: 'COMPLETE_INSPECTION',
      resource: 'QualityInspection',
      resourceId: (qa as any)._id.toString(),
      newValues: { inspectionNumber, decision: qa.decision },
    });
 
    return qa;
  }
 
  async getInspections(tenantId: string): Promise<QualityInspection[]> {
    return this.inspectionModel.find({ tenantId }).exec();
  }
}