diff --git a/src/controllers/serverController.js b/src/controllers/serverController.js index c8a308b..1547476 100644 --- a/src/controllers/serverController.js +++ b/src/controllers/serverController.js @@ -88,12 +88,37 @@ async function getServerProperties(req, res) { } } -async function getServerPropertyById(req, res) { +async function getServerPropertiesForSetup(req, res) { try { - const serverProperty = await databaseOperations.getServerPropertyById(req.params.id); + const serverProperties = await databaseOperations.getPropertiesForSetup(); + const result = {}; + serverProperties.forEach((category) => { + result[category.category] = category.default; + }); + + res.json(result); + } catch (error) { + res.status(500).json({ error: 'Could not fetch server properties' }); + } +} + +async function getServerPropertyByKeyOrId(req, res) { + try { + const keyOrId = req.params.value; + let serverProperty; + + if (keyOrId.match(/^[0-9a-fA-F]{24}$/)) { + serverProperty = await databaseOperations.getServerPropertyById(keyOrId); + } + + if (!serverProperty) { + serverProperty = await databaseOperations.findPropertyByKey(keyOrId); + } + if (!serverProperty) { return res.status(404).json({ error: 'Server property not found' }); } + res.json(serverProperty); } catch (error) { res.status(500).json({ error: 'Could not fetch server property' }); @@ -146,15 +171,21 @@ async function getServerCategories(req, res) { async function getServerPropertiesByCategory(req, res) { try { - const categoryId = req.params.categoryId; - const category = await databaseOperations.getCategoryById(categoryId); + const keyOrId = req.params.value; + let category; + if (keyOrId.match(/^[0-9a-fA-F]{24}$/)) { + category = await databaseOperations.getCategoryById(keyOrId); + } + + if (!category) { + category = await databaseOperations.getCategoryByKey(keyOrId); + } if (!category) { return res.status(404).json({ error: 'Category not found' }); } - const properties = await databaseOperations.getPropertiesByCategory(categoryId); - res.json(properties); + res.json(category); } catch (error) { res.status(500).json({ error: 'Could not fetch server properties' }); } @@ -174,11 +205,13 @@ async function updateProperties(req, res) { let updatedConfig = currentConfig; const originalProperties = {}; - for (const item of data) { - if (item.key && item.value !== undefined && item.value !== null) { - const cleanedKey = item.key; - const cleanedValue = item.value; - const existingProperty = await databaseOperations.findPropertyByKey(cleanedKey) + for (const key in data) { + const value = data[key]; + + if (key && value !== undefined && value !== null) { + const cleanedKey = key; + const cleanedValue = value; + const existingProperty = await databaseOperations.findPropertyByKey(cleanedKey); if (existingProperty) { if (existingProperty.value === cleanedValue) { @@ -188,6 +221,7 @@ async function updateProperties(req, res) { updatedConfig = updateConfigValue(updatedConfig, cleanedKey, cleanedValue); originalProperties[cleanedKey] = existingProperty.value; + if (existingProperty.type === 'string') { existingProperty.value = cleanedValue.toString(); } else if (existingProperty.type === 'bool') { @@ -207,6 +241,7 @@ async function updateProperties(req, res) { continue; } } + try { await existingProperty.save(); updatedKeys.push(cleanedKey); @@ -270,7 +305,7 @@ function updateConfigValue(config, key, value) { module.exports = { getServerProperties, - getServerPropertyById, + getServerPropertyByKeyOrId, updateServerProperty, deleteServerProperty, createCategory, @@ -278,5 +313,6 @@ module.exports = { mapConfiguration, getServerCategories, getServerPropertiesByCategory, - updateProperties + updateProperties, + getServerPropertiesForSetup }; \ No newline at end of file diff --git a/src/database/repositories/serverProperties/index.js b/src/database/repositories/serverProperties/index.js index 2c3a626..b20d1a6 100644 --- a/src/database/repositories/serverProperties/index.js +++ b/src/database/repositories/serverProperties/index.js @@ -54,6 +54,16 @@ async function getServerPropertyById(id) { } } +async function getServerPropertyByKey(key) { + try { + const serverProperty = await ServerProperties.findOne({ key }).populate('category'); + return serverProperty; + } catch (error) { + throw error; + } +} + + async function createCategory(data) { try { const newCategory = await ServerPropertiesCategory.create(data); @@ -109,7 +119,6 @@ async function getProperties() { value: '$value', default: '$default', data: '$data', - category: '$category._id', isConfigured: '$isConfigured', isArray: '$isArray' } @@ -122,6 +131,58 @@ async function getProperties() { category: '$_id', default: 1 } + }, + { + $sort: { + category: 1 + } + } + ]; + + const properties = await ServerProperties.aggregate(pipeline); + return properties; + } catch (error) { + throw error; + } +} + +async function getPropertiesForSetup() { + try { + const pipeline = [ + { + $lookup: { + from: 'serverpropertiescategories', + localField: 'category', + foreignField: '_id', + as: 'category' + } + }, + { + $unwind: '$category' + }, + { + $group: { + _id: '$category.key', + default: { + $push: { + _id: '$_id', + key: '$key', + isConfigured: '$isConfigured', + } + } + } + }, + { + $project: { + _id: 0, + category: '$_id', + default: 1 + } + }, + { + $sort: { + category: 1 + } } ]; const properties = await ServerProperties.aggregate(pipeline); @@ -149,6 +210,10 @@ async function getCategoryById(categoryId) { } } +async function getCategoryByKey(key) { + return await ServerPropertiesCategory.findOne({ key: { $eq: key } }); +} + module.exports = { createServerProperty, updateServerPropertyById, @@ -162,5 +227,8 @@ module.exports = { findPropertyByKeyAndCategory, createProperty, getCategoryById, - findPropertyByKey + findPropertyByKey, + getServerPropertyByKey, + getPropertiesForSetup, + getCategoryByKey }; diff --git a/src/routes/serverRouter.js b/src/routes/serverRouter.js index 65d3cf8..97dd971 100644 --- a/src/routes/serverRouter.js +++ b/src/routes/serverRouter.js @@ -2,30 +2,32 @@ let express = require('express'); let router = express.Router(); let { updateProperties, getServerProperties, - getServerPropertyById, + getServerPropertyByKeyOrId, updateServerProperty, deleteServerProperty, createCategory, getServerInformation, mapConfiguration, getServerCategories, - getServerPropertiesByCategory } + getServerPropertiesByCategory, + getServerPropertiesForSetup } = require('../controllers/serverController') let verifyToken = require('../middlewares/verifyToken') let verifyPermissions = require('../middlewares/verifyPermissions') let { serverPropertiesValidation, serverPropertiesKeyValueValidation } = require('../middlewares/serverPropertiesValidation') const checkUniqueKeyServerPropertiesCategory = require('../middlewares/checkUniqueKeyServerPropertiesCategory') -router.get('/', verifyToken, verifyPermissions(), getServerInformation); // Properties -router.get('/properties/category/:categoryId', getServerPropertiesByCategory); -router.get('/properties', getServerProperties); -router.get('/properties/:id', getServerPropertyById); -router.post('/properties/map', mapConfiguration); -router.patch('/properties/:id', serverPropertiesValidation, updateServerProperty); -router.put('/properties/', serverPropertiesKeyValueValidation, updateProperties); -router.delete('/properties/:id', deleteServerProperty); +router.get('/', verifyToken, verifyPermissions(), getServerInformation); +router.get('/properties/category/:value', verifyToken, verifyPermissions(), getServerPropertiesByCategory); +router.get('/properties', verifyToken, verifyPermissions(), getServerProperties); +router.get('/properties/:value', verifyToken, verifyPermissions(), getServerPropertyByKeyOrId); +router.get('/properties/setup/keys', verifyToken, verifyPermissions(), getServerPropertiesForSetup); +router.post('/properties/map', verifyToken, verifyPermissions(), mapConfiguration); +router.patch('/properties/:id', verifyToken, verifyPermissions(), serverPropertiesValidation, updateServerProperty); +router.put('/properties/', verifyToken, verifyPermissions(), serverPropertiesKeyValueValidation, updateProperties); +router.delete('/properties/:id', verifyToken, verifyPermissions(), deleteServerProperty); // Categories -router.get('/categories', getServerCategories); -router.post('/categories', checkUniqueKeyServerPropertiesCategory, createCategory); +router.get('/categories', verifyToken, verifyPermissions(), getServerCategories); +router.post('/categories', verifyToken, verifyPermissions(), checkUniqueKeyServerPropertiesCategory, createCategory); module.exports = router; diff --git a/src/utils/serverPropertiesValidationSchema.js b/src/utils/serverPropertiesValidationSchema.js index 4e86754..ba2505f 100644 --- a/src/utils/serverPropertiesValidationSchema.js +++ b/src/utils/serverPropertiesValidationSchema.js @@ -14,12 +14,11 @@ const serverPropertyValidation = (body) => { const serverPropertyKeyValueValidation = (body) => { - const schema = Joi.array().items( - Joi.object({ - key: Joi.string().required(), - value: Joi.any().required(), - }).unknown(false).options({ abortEarly: false }) + const schema = Joi.object().pattern( + Joi.string().required(), + Joi.any().required() ); + return schema.validate(body); };