From 0574a7874aa5550c2cc6921ca954f23a8462e7aa Mon Sep 17 00:00:00 2001 From: aXenDeveloper Date: Thu, 13 Jun 2024 17:50:36 +0200 Subject: [PATCH] feat: Add langs in config.json --- backend/src/config.ts | 4 ++++ backend/src/functions/update-object.ts | 22 ++++++++++++++++--- .../core/admin/members/edit/edit.resolver.ts | 2 +- .../core/admin/members/edit/edit.service.ts | 5 ++--- frontend/config/generate-config.ts | 12 +++++++++- frontend/config/helpers.ts | 22 ++++++++++++++++--- frontend/config/index.ts | 16 +++++++++++++- .../backend/src/decorators/user.decorator.ts | 1 + packages/backend/src/utils/context.ts | 1 + packages/frontend/src/helpers/flatten-tree.ts | 4 +--- 10 files changed, 74 insertions(+), 15 deletions(-) diff --git a/backend/src/config.ts b/backend/src/config.ts index 260a180a6..864965211 100644 --- a/backend/src/config.ts +++ b/backend/src/config.ts @@ -141,6 +141,10 @@ export interface ConfigType { }; sticky: boolean; }; + langs: { + code: string; + enabled: boolean; + }[]; rebuild_required: { langs: boolean; plugins: boolean; diff --git a/backend/src/functions/update-object.ts b/backend/src/functions/update-object.ts index 23e2878fc..5ef6e95a0 100644 --- a/backend/src/functions/update-object.ts +++ b/backend/src/functions/update-object.ts @@ -1,12 +1,28 @@ -export function updateObject(config: T, defaultData: T): T { +export function updateObject>( + config: T, + defaultData: T +): T { const updatedConfig = config; for (const key in defaultData) { - if (typeof defaultData[key] === "object" && defaultData[key] !== null) { + if (Array.isArray(defaultData[key])) { + // If the key corresponds to an array and it's not empty, don't edit + if (!config[key] || config[key].length === 0) { + updatedConfig[key] = [] as any; + } + } else if ( + typeof defaultData[key] === "object" && + defaultData[key] !== null + ) { + // Handle nested objects if (!config[key]) { updatedConfig[key] = {} as T[Extract]; } - updateObject(config[key], defaultData[key]); + updatedConfig[key] = updateObject( + (config[key] || {}) as T[Extract], + defaultData[key] + ); } else { + // Handle primitive values if (!config[key]) { updatedConfig[key] = defaultData[key]; } diff --git a/backend/src/plugins/core/admin/members/edit/edit.resolver.ts b/backend/src/plugins/core/admin/members/edit/edit.resolver.ts index 0dcf4aa01..7eb20ccf2 100644 --- a/backend/src/plugins/core/admin/members/edit/edit.resolver.ts +++ b/backend/src/plugins/core/admin/members/edit/edit.resolver.ts @@ -3,9 +3,9 @@ import { UseGuards } from "@nestjs/common"; import { EditAdminMembersService } from "./edit.service"; import { EditAdminMembersArgs } from "./dto/edit.args"; +import { EditAdminMembersObj } from "./dto/edit.obj"; import { AdminAuthGuards } from "@/utils/guards/admin-auth.guard"; -import { EditAdminMembersObj } from "./dto/edit.obj"; @Resolver() export class EditAdminMembersResolver { diff --git a/backend/src/plugins/core/admin/members/edit/edit.service.ts b/backend/src/plugins/core/admin/members/edit/edit.service.ts index d5ab37fb6..7f4a6e334 100644 --- a/backend/src/plugins/core/admin/members/edit/edit.service.ts +++ b/backend/src/plugins/core/admin/members/edit/edit.service.ts @@ -1,13 +1,12 @@ import { Injectable } from "@nestjs/common"; import { eq } from "drizzle-orm"; +import { NotFoundError, AccessDeniedError } from "@vitnode/backend"; import { EditAdminMembersArgs } from "./dto/edit.args"; +import { EditAdminMembersObj } from "./dto/edit.obj"; import { core_users } from "@/plugins/core/admin/database/schema/users"; import { DatabaseService } from "@/database/database.service"; -import { NotFoundError } from "@vitnode/backend"; -import { AccessDeniedError } from "@vitnode/backend"; -import { EditAdminMembersObj } from "./dto/edit.obj"; @Injectable() export class EditAdminMembersService { diff --git a/frontend/config/generate-config.ts b/frontend/config/generate-config.ts index 0d502790a..0b0e31670 100644 --- a/frontend/config/generate-config.ts +++ b/frontend/config/generate-config.ts @@ -9,7 +9,17 @@ import { DEFAULT_CONFIG_DATA } from "."; if (!fs.existsSync(configPath)) { fs.writeFile( configPath, - JSON.stringify(DEFAULT_CONFIG_DATA, null, 2), + JSON.stringify( + { + ...DEFAULT_CONFIG_DATA, + lang: [ + { code: "en", enabled: true }, + { code: "pl", enabled: true } + ] + }, + null, + 2 + ), "utf8", err => { if (err) throw err; diff --git a/frontend/config/helpers.ts b/frontend/config/helpers.ts index 5ef71492f..77d8d1f03 100644 --- a/frontend/config/helpers.ts +++ b/frontend/config/helpers.ts @@ -5,15 +5,31 @@ import { ConfigType } from "."; export const configPath = join(process.cwd(), "config", "config.json"); -export function updateObject(config: T, defaultData: T): T { +export function updateObject>( + config: T, + defaultData: T +): T { const updatedConfig = config; for (const key in defaultData) { - if (typeof defaultData[key] === "object" && defaultData[key] !== null) { + if (Array.isArray(defaultData[key])) { + // If the key corresponds to an array and it's not empty, don't edit + if (!config[key] || config[key].length === 0) { + updatedConfig[key] = [] as any; + } + } else if ( + typeof defaultData[key] === "object" && + defaultData[key] !== null + ) { + // Handle nested objects if (!config[key]) { updatedConfig[key] = {} as T[Extract]; } - updateObject(config[key], defaultData[key]); + updatedConfig[key] = updateObject( + (config[key] || {}) as T[Extract], + defaultData[key] + ); } else { + // Handle primitive values if (!config[key]) { updatedConfig[key] = defaultData[key]; } diff --git a/frontend/config/index.ts b/frontend/config/index.ts index 91b1d5f56..9db8e0046 100644 --- a/frontend/config/index.ts +++ b/frontend/config/index.ts @@ -5,6 +5,10 @@ export interface ConfigType { }; sticky: boolean; }; + langs: { + code: string; + enabled: boolean; + }[]; rebuild_required: { langs: boolean; plugins: boolean; @@ -43,7 +47,17 @@ export const DEFAULT_CONFIG_DATA: ConfigType = { color_primary: "hsl(220, 74%, 50%)", color_primary_foreground: "hsl(210, 40%, 98%)" } - } + }, + langs: [ + { + code: "en", + enabled: true + }, + { + code: "pl", + enabled: true + } + ] }; const ENVS = { diff --git a/packages/backend/src/decorators/user.decorator.ts b/packages/backend/src/decorators/user.decorator.ts index 5eed17a12..28c9ab5fe 100644 --- a/packages/backend/src/decorators/user.decorator.ts +++ b/packages/backend/src/decorators/user.decorator.ts @@ -1,5 +1,6 @@ import { createParamDecorator, ExecutionContext } from "@nestjs/common"; import { Field, GqlExecutionContext, Int, ObjectType } from "@nestjs/graphql"; + import { TextLanguage } from "../utils"; export const CurrentUser = createParamDecorator( diff --git a/packages/backend/src/utils/context.ts b/packages/backend/src/utils/context.ts index 96d2bdb91..cc9130aec 100644 --- a/packages/backend/src/utils/context.ts +++ b/packages/backend/src/utils/context.ts @@ -1,4 +1,5 @@ import { Request, Response } from "express"; + import { User } from "../decorators"; export interface AuthRequest extends Request { diff --git a/packages/frontend/src/helpers/flatten-tree.ts b/packages/frontend/src/helpers/flatten-tree.ts index a00f6b69f..5e9ec4596 100644 --- a/packages/frontend/src/helpers/flatten-tree.ts +++ b/packages/frontend/src/helpers/flatten-tree.ts @@ -8,7 +8,6 @@ export type WithChildren = Omit< export type FlatTree = WithChildren & { depth: number; - index: number; parentId: number | string | null; }; @@ -21,7 +20,7 @@ export function flattenTree({ depth?: number; parentId?: number | string | null; }): FlatTree[] { - return tree.reduce[]>((previousValue, currentValue, index) => { + return tree.reduce[]>((previousValue, currentValue) => { const children = currentValue.children ? flattenTree({ tree: currentValue.children, @@ -36,7 +35,6 @@ export function flattenTree({ ...currentValue, parentId: parentId, depth: depth, - index, children }, ...children