All files / src/domains/mdm/infrastructure/schemas localization.schema.ts

0% Statements 0/80
0% Branches 0/5
100% Functions 0/0
0% Lines 0/70

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                                                                                                                                                                                                                                                                         
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document, Schema as MongooseSchema } from 'mongoose';
 
// ════════════════════════════════════════════════════════════════
// LANGUAGE
// ════════════════════════════════════════════════════════════════
 
@Schema({ collection: 'mdm_languages', timestamps: true })
export class Language extends Document {
  @Prop({ required: true, unique: true }) languageCode!: string; // ISO 639-1: en, te, hi, ta
  @Prop() iso6393Code?: string; // ISO 639-3: eng, tel, hin
  @Prop({ required: true }) languageName!: string; // English
  @Prop() nativeName?: string; // తెలుగు, हिन्दी
  @Prop({ enum: ['ltr', 'rtl'], default: 'ltr' }) textDirection!: string;
  @Prop({ type: [String], default: [] }) countryCodes!: string[];
  @Prop({ default: false }) hasRtlSupport!: boolean;
  @Prop({ default: false }) systemSupported!: boolean; // translated in the UI
  @Prop({ default: true }) active!: boolean;
}
export const LanguageSchema = SchemaFactory.createForClass(Language);
LanguageSchema.index({ languageCode: 1 }, { unique: true });
LanguageSchema.index({ systemSupported: 1, active: 1 });
 
// ════════════════════════════════════════════════════════════════
// LOCALE
// Combination of language + region for formatting preferences
// ════════════════════════════════════════════════════════════════
 
@Schema({ collection: 'mdm_locales', timestamps: true })
export class Locale extends Document {
  @Prop({ required: true, unique: true }) localeCode!: string; // en-IN, en-US, te-IN, hi-IN
  @Prop({ required: true }) localeLabel!: string; // English (India)
  @Prop({ required: true }) languageCode!: string; // en
  @Prop({ required: true }) countryCode!: string; // IN
  @Prop({ required: true }) currencyCode!: string; // INR
  @Prop({ required: true }) timezone!: string; // Asia/Kolkata
  // Number formatting
  @Prop({ default: '.' }) decimalSeparator!: string;
  @Prop({ default: ',' }) groupingSeparator!: string;
  @Prop({ default: '#,##,##0.##' }) numberPattern!: string;
  @Prop({ default: '¤#,##,##0.00' }) currencyPattern!: string;
  // Date/time formatting
  @Prop({ default: 'dd/MM/yyyy' }) shortDateFormat!: string;
  @Prop({ default: 'dd MMM yyyy' }) mediumDateFormat!: string;
  @Prop({ default: 'dd MMMM yyyy' }) longDateFormat!: string;
  @Prop({ default: 'HH:mm' }) shortTimeFormat!: string;
  @Prop({ default: 'HH:mm:ss' }) longTimeFormat!: string;
  @Prop({ enum: ['12', '24'], default: '12' }) timeHourFormat!: string;
  // Week settings
  @Prop({ enum: [0, 1, 2, 3, 4, 5, 6], default: 1 }) firstDayOfWeek!: number; // 0=Sun,1=Mon
  @Prop({ enum: ['MONDAY', 'SUNDAY', 'SATURDAY'] }) weekendStart?: string;
  // Address formatting
  @Prop() addressFormat?: string;
  @Prop({ default: false }) systemDefault!: boolean;
  @Prop({ default: true }) active!: boolean;
}
export const LocaleSchema = SchemaFactory.createForClass(Locale);
LocaleSchema.index({ localeCode: 1 }, { unique: true });
LocaleSchema.index({ languageCode: 1, countryCode: 1 });
 
// ════════════════════════════════════════════════════════════════
// DATE FORMAT TEMPLATE (tenant-level override of locale formatting)
// ════════════════════════════════════════════════════════════════
 
@Schema({ collection: 'mdm_date_formats', timestamps: true })
export class DateFormat extends Document {
  @Prop({ required: true }) formatCode!: string;
  @Prop({ required: true }) displayLabel!: string; // 'DD/MM/YYYY'
  @Prop({ required: true }) pattern!: string; // actual format string
  @Prop({
    required: true,
    enum: ['date', 'time', 'datetime', 'fiscal_year', 'month_year', 'year'],
  })
  formatType!: string;
  @Prop({ type: [String], default: [] }) localeCodes!: string[];
  @Prop({ default: false }) isDefault!: boolean;
  @Prop({ default: true }) active!: boolean;
}
export const DateFormatSchema = SchemaFactory.createForClass(DateFormat);
DateFormatSchema.index({ formatCode: 1 }, { unique: true });
DateFormatSchema.index({ formatType: 1, isDefault: 1 });
 
// ════════════════════════════════════════════════════════════════
// TRANSLATION KEY (UI/domain message catalog)
// ════════════════════════════════════════════════════════════════
 
@Schema({ collection: 'mdm_translation_keys', timestamps: true })
export class TranslationKey extends Document {
  @Prop({ required: true, unique: true }) keyPath!: string; // e.g. 'common.status.active'
  @Prop({ required: true }) defaultValue!: string; // English fallback
  @Prop({
    enum: [
      'ui',
      'domain',
      'email',
      'notification',
      'report',
      'validation',
      'api_message',
    ],
  })
  keyCategory?: string;
  @Prop() moduleKey?: string;
  @Prop({ default: true }) active!: boolean;
}
export const TranslationKeySchema =
  SchemaFactory.createForClass(TranslationKey);
TranslationKeySchema.index({ keyPath: 1 }, { unique: true });
TranslationKeySchema.index({ moduleKey: 1, keyCategory: 1 });
 
// ════════════════════════════════════════════════════════════════
// TRANSLATION VALUE (per language)
// ════════════════════════════════════════════════════════════════
 
@Schema({ collection: 'mdm_translation_values', timestamps: true })
export class TranslationValue extends Document {
  @Prop({ required: true }) keyPath!: string;
  @Prop({ required: true }) languageCode!: string;
  @Prop({ required: true }) value!: string;
  @Prop({ enum: ['human', 'machine', 'review_needed'], default: 'human' })
  quality!: string;
  @Prop({ type: MongooseSchema.Types.ObjectId })
  tenantId?: MongooseSchema.Types.ObjectId; // null = global
  @Prop({ default: true }) active!: boolean;
}
export const TranslationValueSchema =
  SchemaFactory.createForClass(TranslationValue);
TranslationValueSchema.index(
  { keyPath: 1, languageCode: 1, tenantId: 1 },
  { unique: true, sparse: true },
);
TranslationValueSchema.index({ languageCode: 1, active: 1 });