All files / src/domains/mdm/infrastructure/schemas product-classification.schema.ts

0% Statements 0/123
0% Branches 0/35
100% Functions 0/0
0% Lines 0/105

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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214                                                                                                                                                                                                                                                                                                                                                                                                                                           
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document, Schema as MongooseSchema } from 'mongoose';
 
// ════════════════════════════════════════════════════════════════
// INDUSTRY
// ════════════════════════════════════════════════════════════════
 
@Schema({ collection: 'mdm_industries', timestamps: true })
export class Industry extends Document {
  @Prop({ required: true, unique: true }) industryCode!: string; // IT_SERVICES, MANUFACTURING, HEALTHCARE
  @Prop({ required: true }) industryName!: string;
  @Prop() description?: string;
  @Prop({ type: MongooseSchema.Types.ObjectId })
  parentIndustryId?: MongooseSchema.Types.ObjectId;
  @Prop() depth!: number; // 0 = sector, 1 = industry, 2 = sub-industry
  @Prop() nicCode?: string; // National Industry Classification
  @Prop() naicsCode?: string; // North American Industry Classification
  @Prop() isicCode?: string; // International Standard Industrial Classification
  @Prop({ default: true }) active!: boolean;
  @Prop({ default: false }) systemManaged!: boolean;
}
export const IndustrySchema = SchemaFactory.createForClass(Industry);
IndustrySchema.index({ industryCode: 1 }, { unique: true });
IndustrySchema.index({ parentIndustryId: 1, active: 1 });
IndustrySchema.index({ depth: 1, active: 1 });
 
// ════════════════════════════════════════════════════════════════
// BUSINESS TYPE
// ════════════════════════════════════════════════════════════════
 
@Schema({ collection: 'mdm_business_types', timestamps: true })
export class BusinessType extends Document {
  @Prop({ required: true }) businessTypeCode!: string; // PRIVATE_LTD, LLP, SOLE_PROP, PARTNERSHIP
  @Prop({ required: true }) businessTypeName!: string;
  @Prop() description?: string;
  @Prop({ required: true }) countryCode!: string;
  @Prop({ type: MongooseSchema.Types.Mixed }) regulatoryAttributes?: any; // MCA form numbers, etc.
  @Prop({ default: false }) requiresRegistration!: boolean;
  @Prop({ type: [String], default: [] }) applicableTaxTypes!: string[];
  @Prop({ default: true }) active!: boolean;
}
export const BusinessTypeSchema = SchemaFactory.createForClass(BusinessType);
BusinessTypeSchema.index(
  { businessTypeCode: 1, countryCode: 1 },
  { unique: true },
);
BusinessTypeSchema.index({ countryCode: 1, active: 1 });
 
// ════════════════════════════════════════════════════════════════
// LEGAL ENTITY TYPE
// ════════════════════════════════════════════════════════════════
 
@Schema({ collection: 'mdm_legal_entity_types', timestamps: true })
export class LegalEntityType extends Document {
  @Prop({ required: true }) entityTypeCode!: string;
  @Prop({ required: true }) entityTypeName!: string;
  @Prop({ required: true }) countryCode!: string;
  @Prop() description?: string;
  @Prop() abbreviation?: string; // Pvt. Ltd., LLP, Inc.
  @Prop({ type: MongooseSchema.Types.Mixed }) complianceAttributes?: any;
  @Prop({ default: true }) active!: boolean;
}
export const LegalEntityTypeSchema =
  SchemaFactory.createForClass(LegalEntityType);
LegalEntityTypeSchema.index(
  { entityTypeCode: 1, countryCode: 1 },
  { unique: true },
);
LegalEntityTypeSchema.index({ countryCode: 1, active: 1 });
 
// ════════════════════════════════════════════════════════════════
// MARKET SEGMENT
// ════════════════════════════════════════════════════════════════
 
@Schema({ collection: 'mdm_market_segments', timestamps: true })
export class MarketSegment extends Document {
  @Prop({ required: true, unique: true }) segmentCode!: string; // SME, ENTERPRISE, STARTUP, GOVT
  @Prop({ required: true }) segmentName!: string;
  @Prop() description?: string;
  @Prop({ type: MongooseSchema.Types.ObjectId })
  parentSegmentId?: MongooseSchema.Types.ObjectId;
  @Prop({ enum: ['B2B', 'B2C', 'B2G', 'B2B2C', 'C2C'] }) channelType?: string;
  @Prop({ default: true }) active!: boolean;
}
export const MarketSegmentSchema = SchemaFactory.createForClass(MarketSegment);
MarketSegmentSchema.index({ segmentCode: 1 }, { unique: true });
 
// ════════════════════════════════════════════════════════════════
// PRODUCT CLASSIFICATION (for procurement/inventory future use)
// ════════════════════════════════════════════════════════════════
 
@Schema({ collection: 'mdm_product_classifications', timestamps: true })
export class ProductClassification extends Document {
  @Prop({ required: true }) classificationCode!: string;
  @Prop({ required: true }) classificationName!: string;
  @Prop({ required: true, enum: ['product', 'service', 'both'] })
  applicableTo!: string;
  @Prop({ type: MongooseSchema.Types.ObjectId })
  parentId?: MongooseSchema.Types.ObjectId;
  @Prop({ default: 0 }) depth!: number;
  @Prop() hsnCode?: string; // India Harmonized System Nomenclature
  @Prop() sacCode?: string; // India Services Accounting Code
  @Prop() unspscCode?: string; // UN Standard Products & Services Code
  @Prop({ default: true }) active!: boolean;
}
export const ProductClassificationSchema = SchemaFactory.createForClass(
  ProductClassification,
);
ProductClassificationSchema.index({ classificationCode: 1 }, { unique: true });
ProductClassificationSchema.index({ parentId: 1, applicableTo: 1 });
ProductClassificationSchema.index({ hsnCode: 1 });
ProductClassificationSchema.index({ sacCode: 1 });
 
// ════════════════════════════════════════════════════════════════
// BRAND
// ════════════════════════════════════════════════════════════════
 
@Schema({ collection: 'mdm_brands', timestamps: true })
export class Brand extends Document {
  @Prop({ required: true }) brandCode!: string;
  @Prop({ required: true }) brandName!: string;
  @Prop() description?: string;
  @Prop() website?: string;
  @Prop() logoUrl?: string;
  @Prop({ type: MongooseSchema.Types.ObjectId })
  tenantId?: MongooseSchema.Types.ObjectId;
  @Prop({ default: true }) active!: boolean;
}
export const BrandSchema = SchemaFactory.createForClass(Brand);
BrandSchema.index(
  { brandCode: 1, tenantId: 1 },
  { unique: true, sparse: true },
);
 
// ════════════════════════════════════════════════════════════════
// MANUFACTURER
// ════════════════════════════════════════════════════════════════
 
@Schema({ collection: 'mdm_manufacturers', timestamps: true })
export class Manufacturer extends Document {
  @Prop({ required: true }) manufacturerCode!: string;
  @Prop({ required: true }) manufacturerName!: string;
  @Prop() countryCode?: string;
  @Prop() website?: string;
  @Prop({ type: MongooseSchema.Types.ObjectId })
  tenantId?: MongooseSchema.Types.ObjectId;
  @Prop({ default: true }) active!: boolean;
}
export const ManufacturerSchema = SchemaFactory.createForClass(Manufacturer);
ManufacturerSchema.index(
  { manufacturerCode: 1, tenantId: 1 },
  { unique: true, sparse: true },
);
 
// ════════════════════════════════════════════════════════════════
// COMMODITY CODE
// ════════════════════════════════════════════════════════════════
 
@Schema({ collection: 'mdm_commodity_codes', timestamps: true })
export class CommodityCode extends Document {
  @Prop({ required: true }) commodityCode!: string;
  @Prop({ required: true }) commodityName!: string;
  @Prop({ required: true, enum: ['hs', 'unspsc', 'nigp', 'naccs', 'custom'] })
  codeSystem!: string;
  @Prop() description?: string;
  @Prop({ type: MongooseSchema.Types.ObjectId })
  parentId?: MongooseSchema.Types.ObjectId;
  @Prop({ default: 0 }) depth!: number;
  @Prop({ default: true }) active!: boolean;
}
export const CommodityCodeSchema = SchemaFactory.createForClass(CommodityCode);
CommodityCodeSchema.index(
  { commodityCode: 1, codeSystem: 1 },
  { unique: true },
);
CommodityCodeSchema.index({ codeSystem: 1, parentId: 1 });
 
// ════════════════════════════════════════════════════════════════
// PRODUCT ATTRIBUTE DEFINITION
// ════════════════════════════════════════════════════════════════
 
@Schema({ collection: 'mdm_product_attribute_defs', timestamps: true })
export class ProductAttributeDefinition extends Document {
  @Prop({ required: true }) attributeCode!: string;
  @Prop({ required: true }) attributeName!: string;
  @Prop({
    required: true,
    enum: [
      'text',
      'number',
      'decimal',
      'boolean',
      'date',
      'select',
      'multiselect',
      'unit_value',
    ],
  })
  dataType!: string;
  @Prop({ type: [String], default: [] }) classificationCodes!: string[];
  @Prop({ default: false }) required!: boolean;
  @Prop({ type: MongooseSchema.Types.Mixed }) validation?: any;
  @Prop({ type: MongooseSchema.Types.ObjectId })
  tenantId?: MongooseSchema.Types.ObjectId;
  @Prop({ default: true }) active!: boolean;
}
export const ProductAttributeDefinitionSchema = SchemaFactory.createForClass(
  ProductAttributeDefinition,
);
ProductAttributeDefinitionSchema.index(
  { attributeCode: 1, tenantId: 1 },
  { unique: true, sparse: true },
);