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 } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { Model, Types } from 'mongoose'; import { Language, Locale, DateFormat, TranslationKey, TranslationValue, } from '../../infrastructure/schemas/localization.schema'; @Injectable() export class LocalizationService { private readonly logger = new Logger(LocalizationService.name); constructor( @InjectModel(Language.name) private readonly languageModel: Model<Language>, @InjectModel(Locale.name) private readonly localeModel: Model<Locale>, @InjectModel(DateFormat.name) private readonly dateFormatModel: Model<DateFormat>, @InjectModel(TranslationKey.name) private readonly keyModel: Model<TranslationKey>, @InjectModel(TranslationValue.name) private readonly valueModel: Model<TranslationValue>, ) {} async listLanguages(): Promise<any[]> { return this.languageModel .find({ active: true }) .sort({ languageName: 1 }) .lean() .exec(); } async bulkUpsertLanguages(languages: any[]): Promise<number> { const ops = languages.map((lang) => ({ updateOne: { filter: { languageCode: lang.languageCode }, update: { $set: lang }, upsert: true, }, })); const result = await this.languageModel.bulkWrite(ops, { ordered: false }); return result.upsertedCount + result.modifiedCount; } async listLocales(): Promise<any[]> { return this.localeModel .find({ active: true }) .sort({ localeCode: 1 }) .lean() .exec(); } async getLocale(localeCode: string): Promise<any> { const doc = await this.localeModel .findOne({ localeCode, active: true }) .lean() .exec(); if (!doc) throw new NotFoundException(`Locale '${localeCode}' not found`); return doc; } async bulkUpsertLocales(locales: any[]): Promise<number> { const ops = locales.map((loc) => ({ updateOne: { filter: { localeCode: loc.localeCode }, update: { $set: loc }, upsert: true, }, })); const result = await this.localeModel.bulkWrite(ops, { ordered: false }); return result.upsertedCount + result.modifiedCount; } async listDateFormats(): Promise<any[]> { return this.dateFormatModel .find({ active: true }) .sort({ formatCode: 1 }) .lean() .exec(); } async bulkUpsertDateFormats(formats: any[]): Promise<number> { const ops = formats.map((fmt) => ({ updateOne: { filter: { formatCode: fmt.formatCode }, update: { $set: fmt }, upsert: true, }, })); const result = await this.dateFormatModel.bulkWrite(ops, { ordered: false, }); return result.upsertedCount + result.modifiedCount; } async translate( keyPath: string, languageCode: string, tenantId?: string, ): Promise<string> { if (tenantId) { const tenantVal = await this.valueModel .findOne({ keyPath, languageCode, tenantId: new Types.ObjectId(tenantId), active: true, }) .lean() .exec(); if (tenantVal) return tenantVal.value; } const globalVal = await this.valueModel .findOne({ keyPath, languageCode, tenantId: null, active: true, }) .lean() .exec(); if (globalVal) return globalVal.value; const fallbackKey = await this.keyModel.findOne({ keyPath }).lean().exec(); return fallbackKey?.defaultValue || keyPath; } } |