UC done v1.0

This commit is contained in:
andrei-mihnea-cerbu
2024-03-27 17:15:57 +02:00
parent 5f902e1a28
commit d1d238287a
56 changed files with 4964 additions and 81 deletions
+12
View File
@@ -0,0 +1,12 @@
const { sendResponse } = require('../helpers/responseHelper');
const {httpStatus, httpStatusMessages} = require('../helpers/httpResponses')
function apiKeyValidation(req, res, next) {
const apiKey = req.headers['x-api-key'];
if (apiKey !== req.app.locals.apiKey) {
return sendResponse(res, httpStatus.UNAUTHORIZED, httpStatusMessages[httpStatus.UNAUTHORIZED]);
}
next();
}
module.exports = apiKeyValidation;
+16
View File
@@ -0,0 +1,16 @@
const { sendResponse } = require('../helpers/responseHelper');
const {httpStatus, httpStatusMessages} = require("../helpers/httpResponses");
function checkJson(req, res, next) {
if(req.method === 'GET'){
next()
}
if (['POST', 'PUT', 'PATCH'].includes(req.method) && req.headers['content-type'] !== 'application/json') {
return sendResponse(res, httpStatus.BAD_REQUEST, httpStatusMessages[httpStatus.BAD_REQUEST]);
}
next();
}
module.exports = checkJson;
+31
View File
@@ -0,0 +1,31 @@
const path = require("path");
const fs = require("fs");
function SetAdminValuesMiddleware(req, res, next){
if(!req.path.startsWith('/admin')){
next();
return res;
}
try{
const configFilePath = path.join(__dirname, '..', 'db', 'config.json');
const config = JSON.parse(fs.readFileSync(configFilePath));
res.locals.adminKey = config.adminKey === undefined ? " " : config.adminKey;
res.locals.ceoID = config.ceoID === undefined ? " " : config.ceoID;
next();
fs.writeFileSync(configFilePath, JSON.stringify(
{
adminKey: res.locals.adminKey,
ceoID: res.locals.ceoID
}, null, 2));
return res;
} catch(error){
console.log(error.message);
return res;
}
}
module.exports = SetAdminValuesMiddleware;