diff --git a/src/modules/ModuleTypes.d.ts b/src/modules/ModuleTypes.d.ts index 937afc5a5..23d535b42 100644 --- a/src/modules/ModuleTypes.d.ts +++ b/src/modules/ModuleTypes.d.ts @@ -5,6 +5,7 @@ import { TablerIcon } from '@tabler/icons'; // Note: Maybe use context to keep track of the modules +// TODO: Remove this old component and the entire file export interface IModule { id: string; title: string; diff --git a/src/pages/api/configs/[slug].ts b/src/pages/api/configs/[slug].ts index 6f71ca257..e9f14d3c1 100644 --- a/src/pages/api/configs/[slug].ts +++ b/src/pages/api/configs/[slug].ts @@ -1,22 +1,61 @@ import { NextApiRequest, NextApiResponse } from 'next'; import fs from 'fs'; import path from 'path'; +import { BackendConfigType, ConfigType } from '../../../types/config'; +import { getConfig } from '../../../tools/config/getConfig'; function Put(req: NextApiRequest, res: NextApiResponse) { // Get the slug of the request const { slug } = req.query as { slug: string }; // Get the body of the request - const { body }: { body: string } = req; - if (!slug || !body) { - res.status(400).json({ + const { body: config }: { body: ConfigType } = req; + if (!slug || !config) { + return res.status(400).json({ error: 'Wrong request', }); } - // Save the body in the /data/config folder with the slug as filename + const previousConfig = getConfig(slug); + + const newConfig: BackendConfigType = { + ...config, + apps: [ + ...config.apps.map((app) => ({ + ...app, + integration: { + ...app.integration, + properties: app.integration.properties.map((property) => { + if (property.type === 'public') { + return { + field: property.field, + type: property.type, + value: property.value, + }; + } + + const previousApp = previousConfig.apps.find( + (previousApp) => previousApp.id === app.id + ); + + const previousProperty = previousApp?.integration?.properties.find( + (previousProperty) => previousProperty.field === property.field + ); + + return { + field: property.field, + type: property.type, + value: property.value !== undefined ? property.value : previousProperty?.value, + }; + }), + }, + })), + ], + }; + + // Save the body in the /data/config folder with the slug as filename fs.writeFileSync( path.join('data/configs', `${slug}.json`), - JSON.stringify(body, null, 2), + JSON.stringify(newConfig, null, 2), 'utf8' ); return res.status(200).json({