feat: wrap up translation logic

This commit is contained in:
Mohamed Marrouchi
2024-09-24 11:23:40 +01:00
parent 16e7431d83
commit ecb8d9745a
35 changed files with 291 additions and 260 deletions

View File

@@ -12,7 +12,6 @@ import { EventEmitter2 } from '@nestjs/event-emitter';
import { InjectModel } from '@nestjs/mongoose';
import { Document, Model, Query, Types } from 'mongoose';
import { config } from '@/config';
import { I18nService } from '@/i18n/services/i18n.service';
import { BaseRepository } from '@/utils/generics/base-repository';
@@ -65,8 +64,7 @@ export class SettingRepository extends BaseRepository<Setting> {
* Emits an event after a `Setting` has been updated.
*
* This method is used to synchronize global settings by emitting an event
* based on the `group` and `label` of the `Setting`. It also updates the i18n
* default language setting when the `default_lang` label is updated.
* based on the `group` and `label` of the `Setting`.
*
* @param _query The Mongoose query object used to find and update the document.
* @param setting The updated `Setting` object.
@@ -86,33 +84,5 @@ export class SettingRepository extends BaseRepository<Setting> {
'hook:settings:' + setting.group + ':' + setting.label,
setting,
);
if (setting.label === 'default_lang') {
// @todo : check if this actually updates the default lang
this.i18n.resolveLanguage(setting.value as string);
}
}
/**
* Sets default values before creating a `Setting` document.
*
* If the setting is part of the `nlp_settings` group, it sets specific values
* for `languages` and `default_lang` labels, using configuration values from the
* chatbot settings.
*
* @param setting The `Setting` document to be created.
*/
async preCreate(
setting: Document<unknown, unknown, Setting> &
Setting & { _id: Types.ObjectId },
) {
if (setting.group === 'nlp_settings') {
if (setting.label === 'languages') {
setting.value = config.chatbot.lang.available;
} else if (setting.label === 'default_lang') {
setting.value = config.chatbot.lang.default;
setting.options = config.chatbot.lang.available;
}
}
}
}

View File

@@ -98,8 +98,6 @@ export type SettingDict = { [group: string]: Setting[] };
export type Settings = {
nlp_settings: {
default_lang: string;
languages: string[];
threshold: string;
provider: string;
endpoint: string;

View File

@@ -7,8 +7,6 @@
* 3. SaaS Restriction: This software, or any derivative of it, may not be used to offer a competing product or service (SaaS) without prior written consent from Hexastack. Offering the software as a service or using it in a commercial cloud environment without express permission is strictly prohibited.
*/
import { config } from '@/config';
import { SettingCreateDto } from '../dto/setting.dto';
import { SettingType } from '../schemas/types';
@@ -67,26 +65,6 @@ export const settingModels: SettingCreateDto[] = [
type: SettingType.text,
weight: 3,
},
{
group: 'nlp_settings',
label: 'languages',
value: [],
options: [],
type: SettingType.select,
config: {
multiple: true,
allowCreate: true,
},
weight: 4,
},
{
group: 'nlp_settings',
label: 'default_lang',
value: config.chatbot.lang.default,
options: [], // NOTE : will be set onBeforeCreate from config
type: SettingType.select,
weight: 5,
},
{
group: 'nlp_settings',
label: 'threshold',
@@ -97,7 +75,7 @@ export const settingModels: SettingCreateDto[] = [
max: 1,
step: 0.01,
},
weight: 6,
weight: 4,
},
{
group: 'contact',