All files / src/domains/mdm/application/services custom-master.service.ts

0% Statements 0/28
0% Branches 0/32
0% Functions 0/5
0% Lines 0/25

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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131                                                                                                                                                                                                                                                                     
import {
  Injectable,
  Logger,
  NotFoundException,
  BadRequestException,
  ConflictException,
} from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model, Types } from 'mongoose';
import {
  CustomMasterDefinition,
  CustomMasterRecord,
  CustomMasterLayout,
} from '../../infrastructure/schemas/custom-master.schema';
 
@Injectable()
export class CustomMasterService {
  private readonly logger = new Logger(CustomMasterService.name);
 
  constructor(
    @InjectModel(CustomMasterDefinition.name)
    private readonly defModel: Model<CustomMasterDefinition>,
    @InjectModel(CustomMasterRecord.name)
    private readonly recordModel: Model<CustomMasterRecord>,
    @InjectModel(CustomMasterLayout.name)
    private readonly layoutModel: Model<CustomMasterLayout>,
  ) {}
 
  async createDefinition(params: {
    masterKey: string;
    masterName: string;
    description?: string;
    tenantId?: string;
    masterType: string;
    fields?: any[];
    createdBy?: string;
  }): Promise<any> {
    const existing = await this.defModel
      .findOne({
        masterKey: params.masterKey,
        ...(params.tenantId
          ? { tenantId: new Types.ObjectId(params.tenantId) }
          : { tenantId: null }),
      })
      .lean()
      .exec();
 
    if (existing) {
      throw new ConflictException(
        `Custom master definition with key '${params.masterKey}' already exists`,
      );
    }
 
    const def = await this.defModel.create({
      ...params,
      tenantId: params.tenantId
        ? new Types.ObjectId(params.tenantId)
        : undefined,
      status: 'draft',
      createdBy: params.createdBy
        ? new Types.ObjectId(params.createdBy)
        : undefined,
    });
 
    return def.toObject();
  }
 
  async publishDefinition(id: string): Promise<any> {
    const def = await this.defModel.findById(id).exec();
    if (!def) throw new NotFoundException('Custom master definition not found');
    def.status = 'published';
    await def.save();
    return def.toObject();
  }
 
  async createRecord(params: {
    masterKey: string;
    recordCode: string;
    recordName: string;
    fieldValues?: any;
    tenantId?: string;
    createdBy?: string;
  }): Promise<any> {
    const def = await this.defModel
      .findOne({
        masterKey: params.masterKey,
        status: 'published',
      })
      .lean()
      .exec();
 
    if (!def) {
      throw new BadRequestException(
        `No active custom master definition for key '${params.masterKey}'`,
      );
    }
 
    const rec = await this.recordModel.create({
      masterKey: params.masterKey,
      definitionId: def._id,
      recordCode: params.recordCode,
      recordName: params.recordName,
      fieldValues: params.fieldValues || {},
      tenantId: params.tenantId
        ? new Types.ObjectId(params.tenantId)
        : undefined,
      createdBy: params.createdBy
        ? new Types.ObjectId(params.createdBy)
        : undefined,
      status: 'published',
      active: true,
    });
 
    return rec.toObject();
  }
 
  async getRecords(masterKey: string, tenantId?: string): Promise<any[]> {
    return this.recordModel
      .find({
        masterKey,
        ...(tenantId
          ? { tenantId: new Types.ObjectId(tenantId) }
          : { tenantId: null }),
        active: true,
      })
      .sort({ sortOrder: 1 })
      .lean()
      .exec();
  }
}