Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(properties): add new endpoints #5

Merged
merged 1 commit into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 49 additions & 13 deletions src/controllers/serverController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
Expand Down Expand Up @@ -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' });
}
Expand All @@ -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) {
Expand All @@ -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') {
Expand All @@ -207,6 +241,7 @@ async function updateProperties(req, res) {
continue;
}
}

try {
await existingProperty.save();
updatedKeys.push(cleanedKey);
Expand Down Expand Up @@ -270,13 +305,14 @@ function updateConfigValue(config, key, value) {

module.exports = {
getServerProperties,
getServerPropertyById,
getServerPropertyByKeyOrId,
updateServerProperty,
deleteServerProperty,
createCategory,
getServerInformation,
mapConfiguration,
getServerCategories,
getServerPropertiesByCategory,
updateProperties
updateProperties,
getServerPropertiesForSetup
};
72 changes: 70 additions & 2 deletions src/database/repositories/serverProperties/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -109,7 +119,6 @@ async function getProperties() {
value: '$value',
default: '$default',
data: '$data',
category: '$category._id',
isConfigured: '$isConfigured',
isArray: '$isArray'
}
Expand All @@ -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);
Expand Down Expand Up @@ -149,6 +210,10 @@ async function getCategoryById(categoryId) {
}
}

async function getCategoryByKey(key) {
return await ServerPropertiesCategory.findOne({ key: { $eq: key } });
}

module.exports = {
createServerProperty,
updateServerPropertyById,
Expand All @@ -162,5 +227,8 @@ module.exports = {
findPropertyByKeyAndCategory,
createProperty,
getCategoryById,
findPropertyByKey
findPropertyByKey,
getServerPropertyByKey,
getPropertiesForSetup,
getCategoryByKey
};
26 changes: 14 additions & 12 deletions src/routes/serverRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
9 changes: 4 additions & 5 deletions src/utils/serverPropertiesValidationSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

Expand Down
Loading