All files / src/domains/manufacturing/services mps-capacity.service.ts

0% Statements 0/22
0% Branches 0/22
0% Functions 0/4
0% Lines 0/19

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                                                                                                           
import { Injectable, BadRequestException } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model, Types } from 'mongoose';
import { MasterProductionSchedule, WorkCenter, MachineResource } from '../schemas';
 
@Injectable()
export class MpsCapacityService {
  constructor(
    @InjectModel(MasterProductionSchedule.name) private readonly mpsModel: Model<MasterProductionSchedule>,
    @InjectModel(WorkCenter.name) private readonly workCenterModel: Model<WorkCenter>,
    @InjectModel(MachineResource.name) private readonly machineModel: Model<MachineResource>
  ) {}
 
  async createMpsLine(tenantId: string, payload: any): Promise<MasterProductionSchedule> {
    const mps = new this.mpsModel({
      tenantId,
      mpsCode: payload.mpsCode,
      itemCode: payload.itemCode,
      scheduleDate: new Date(payload.scheduleDate),
      quantity: payload.quantity,
      status: 'Active',
    });
    return mps.save();
  }
 
  async getMpsLines(tenantId: string): Promise<MasterProductionSchedule[]> {
    return this.mpsModel.find({ tenantId }).exec();
  }
 
  async calculateWorkCenterLoad(tenantId: string, workCenterId: string, date: Date): Promise<{
    availableCapacityHours: number;
    requiredCapacityHours: number;
    utilizationPercent: number;
  }> {
    const wc = await this.workCenterModel.findOne({ _id: workCenterId, tenantId }).exec();
    if (!wc) throw new BadRequestException('Work center not found');
 
    // Basic shift capacity check
    const efficiency = wc.efficiency || 1.0;
    const utilization = wc.utilization || 1.0;
    const machinesCount = wc.numberOfMachines || 1;
    const baseDailyCapacity = wc.dailyCapacityHours || 8;
 
    const availableHours = baseDailyCapacity * machinesCount * efficiency * utilization;
 
    // Return dummy load check (can be populated by active operations schedules in execution checks)
    return {
      availableCapacityHours: availableHours,
      requiredCapacityHours: 0, // execution logs will populate actual loads
      utilizationPercent: 0,
    };
  }
}