All files / src/domains/mdm/application/services product-classification.service.ts

0% Statements 0/25
0% Branches 0/26
0% Functions 0/9
0% Lines 0/21

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                                                                                                                                                                                                     
import { Injectable, Logger } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model, Types } from 'mongoose';
import {
  ProductClassification,
  Brand,
  Manufacturer,
  CommodityCode,
  ProductAttributeDefinition,
} from '../../infrastructure/schemas/product-classification.schema';
 
@Injectable()
export class ProductClassificationService {
  private readonly logger = new Logger(ProductClassificationService.name);
 
  constructor(
    @InjectModel(ProductClassification.name)
    private readonly classModel: Model<ProductClassification>,
    @InjectModel(Brand.name) private readonly brandModel: Model<Brand>,
    @InjectModel(Manufacturer.name)
    private readonly manModel: Model<Manufacturer>,
    @InjectModel(CommodityCode.name)
    private readonly commModel: Model<CommodityCode>,
    @InjectModel(ProductAttributeDefinition.name)
    private readonly attrModel: Model<ProductAttributeDefinition>,
  ) {}
 
  async listClassifications(parentId?: string): Promise<any[]> {
    return this.classModel
      .find({
        parentId: parentId ? new Types.ObjectId(parentId) : null,
        active: true,
      })
      .sort({ classificationName: 1 })
      .lean()
      .exec();
  }
 
  async bulkUpsertClassifications(classifications: any[]): Promise<number> {
    const ops = classifications.map((c) => ({
      updateOne: {
        filter: { classificationCode: c.classificationCode },
        update: { $set: c },
        upsert: true,
      },
    }));
    const result = await this.classModel.bulkWrite(ops, { ordered: false });
    return result.upsertedCount + result.modifiedCount;
  }
 
  async listBrands(tenantId?: string): Promise<any[]> {
    return this.brandModel
      .find({
        ...(tenantId
          ? { tenantId: new Types.ObjectId(tenantId) }
          : { tenantId: null }),
        active: true,
      })
      .sort({ brandName: 1 })
      .lean()
      .exec();
  }
 
  async listManufacturers(tenantId?: string): Promise<any[]> {
    return this.manModel
      .find({
        ...(tenantId
          ? { tenantId: new Types.ObjectId(tenantId) }
          : { tenantId: null }),
        active: true,
      })
      .sort({ manufacturerName: 1 })
      .lean()
      .exec();
  }
 
  async lookupCommodityCode(
    commodityCode: string,
    codeSystem: string,
  ): Promise<any> {
    return this.commModel
      .findOne({ commodityCode, codeSystem, active: true })
      .lean()
      .exec();
  }
 
  async bulkUpsertCommodities(commodities: any[]): Promise<number> {
    const ops = commodities.map((c) => ({
      updateOne: {
        filter: { commodityCode: c.commodityCode, codeSystem: c.codeSystem },
        update: { $set: c },
        upsert: true,
      },
    }));
    const result = await this.commModel.bulkWrite(ops, { ordered: false });
    return result.upsertedCount + result.modifiedCount;
  }
}