From 8f0c2f4302e1097401bb814913c482e444fc79ee Mon Sep 17 00:00:00 2001 From: Rishi Ghan Date: Tue, 12 Mar 2024 16:39:44 -0500 Subject: [PATCH] =?UTF-8?q?=E2=9A=99=EF=B8=8F=20getAllSettings=20is=20para?= =?UTF-8?q?meterized?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- services/settings.service.ts | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/services/settings.service.ts b/services/settings.service.ts index 7159a00..35666bf 100644 --- a/services/settings.service.ts +++ b/services/settings.service.ts @@ -28,12 +28,32 @@ export default class SettingsService extends Service { rest: "GET /getAllSettings", params: {}, async handler(ctx: Context<{ settingsKey: string }>) { - const settings = await Settings.find({}); - if (isEmpty(settings)) { + const { settingsKey } = ctx.params; + + // Initialize a projection object. Include everything by default. + let projection = settingsKey + ? { _id: 0, [settingsKey]: 1 } + : {}; + + // Find the settings with the dynamic projection + const settings = await Settings.find({}, projection); + + // Check if settings are empty + if (settings.length === 0) { return {}; } - console.log(settings[0]); - return settings[0]; + + // If settingsKey is provided, return the specific part of the settings. + // Otherwise, return the entire settings document. + if (settingsKey) { + // Check if the specific key exists in the settings document. + // Since `settings` is an array, we access the first element. + // Then, we use the settingsKey to return only that part of the document. + return settings[0][settingsKey] || {}; + } else { + // Return the entire settings document + return settings[0]; + } }, },