Incepere creare procese separate
This commit is contained in:
@@ -27,6 +27,10 @@ app.set('backupSchemesDB', backupSchemesDB);
|
||||
app.set('ceoDB', ceoDB);
|
||||
app.set('adminDB', adminDB);
|
||||
|
||||
app.use('/heartbeat', (req, res) => {
|
||||
return res.status(200).json({message: 'Server ap and running.'});
|
||||
})
|
||||
|
||||
const checkJson = require('./middlewares/checkJson');
|
||||
const apiKeyValidation = require('./middlewares/apiKeyValidation');
|
||||
|
||||
|
||||
@@ -1 +1,9 @@
|
||||
[]
|
||||
[
|
||||
{
|
||||
"id": "4659e71f-9bb4-4902-97d8-097efa138333",
|
||||
"name": "Andrei Cerbu",
|
||||
"email": "a@c.com",
|
||||
"password": "5447045dc3cfaafdb49b365e31dad41eb88616b0b1449930e55fd3a70bd8cee4",
|
||||
"department": "Contabili"
|
||||
}
|
||||
]
|
||||
@@ -33,43 +33,6 @@ const httpStatus = {
|
||||
SERVICE_UNAVAILABLE: 503
|
||||
};
|
||||
|
||||
const httpStatusMessages = {
|
||||
// Informational
|
||||
[httpStatus.CONTINUE]: "Continue",
|
||||
[httpStatus.SWITCHING_PROTOCOLS]: "Switching Protocols",
|
||||
[httpStatus.PROCESSING]: "Processing",
|
||||
|
||||
// Success
|
||||
[httpStatus.OK]: "OK",
|
||||
[httpStatus.CREATED]: "Created",
|
||||
[httpStatus.ACCEPTED]: "Accepted",
|
||||
[httpStatus.NO_CONTENT]: "No Content",
|
||||
|
||||
// Redirection
|
||||
[httpStatus.MOVED_PERMANENTLY]: "Moved Permanently",
|
||||
[httpStatus.FOUND]: "Found",
|
||||
[httpStatus.SEE_OTHER]: "See Other",
|
||||
[httpStatus.NOT_MODIFIED]: "Not Modified",
|
||||
[httpStatus.TEMPORARY_REDIRECT]: "Temporary Redirect",
|
||||
|
||||
// Client Error
|
||||
[httpStatus.BAD_REQUEST]: "Bad Request",
|
||||
[httpStatus.UNAUTHORIZED]: "Unauthorized",
|
||||
[httpStatus.FORBIDDEN]: "Forbidden",
|
||||
[httpStatus.NOT_FOUND]: "Not Found",
|
||||
[httpStatus.METHOD_NOT_ALLOWED]: "Method Not Allowed",
|
||||
[httpStatus.CONFLICT]: "Conflict",
|
||||
[httpStatus.GONE]: "Gone",
|
||||
[httpStatus.UNSUPPORTED_MEDIA_TYPE]: "Unsupported Media Type",
|
||||
|
||||
// Server Error
|
||||
[httpStatus.INTERNAL_SERVER_ERROR]: "Internal Server Error",
|
||||
[httpStatus.NOT_IMPLEMENTED]: "Not Implemented",
|
||||
[httpStatus.SERVICE_UNAVAILABLE]: "Service Unavailable"
|
||||
};
|
||||
|
||||
const httpHeaders = {
|
||||
JSON: { 'name': 'Content-Type', 'value': 'application/json' }
|
||||
};
|
||||
|
||||
module.exports = {httpStatus, httpStatusMessages, httpHeaders};
|
||||
module.exports = {httpStatus};
|
||||
@@ -1,21 +0,0 @@
|
||||
const {httpHeaders} = require("./httpResponses");
|
||||
|
||||
const headers = {
|
||||
[httpHeaders.JSON.name]: httpHeaders.JSON.value
|
||||
}
|
||||
|
||||
function sendResponse(res, statusCode, message, data = null, headers_parsed = {}) {
|
||||
Object.entries(headers).forEach(([key, value]) => {
|
||||
res.setHeader(key, value);
|
||||
});
|
||||
|
||||
// Set headers if provided
|
||||
Object.entries(headers_parsed).forEach(([key, value]) => {
|
||||
res.setHeader(key, value);
|
||||
});
|
||||
|
||||
// Send the response with status code, message, and optional data
|
||||
res.status(statusCode).json({ message, data });
|
||||
}
|
||||
|
||||
module.exports = { sendResponse };
|
||||
@@ -1,10 +1,9 @@
|
||||
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]);
|
||||
return res.status(httpStatus.UNAUTHORIZED).json({message: 'Invalid API key'});
|
||||
}
|
||||
next();
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
const { sendResponse } = require('../helpers/responseHelper');
|
||||
const {httpStatus, httpStatusMessages} = require("../helpers/httpResponses");
|
||||
|
||||
const {httpStatus} = require("../helpers/httpResponses");
|
||||
|
||||
function checkJson(req, res, next) {
|
||||
if(req.method === 'GET'){
|
||||
@@ -7,7 +7,7 @@ function checkJson(req, res, next) {
|
||||
}
|
||||
|
||||
if (['POST', 'PUT', 'PATCH'].includes(req.method) && req.headers['content-type'] !== 'application/json') {
|
||||
return sendResponse(res, httpStatus.BAD_REQUEST, httpStatusMessages[httpStatus.BAD_REQUEST]);
|
||||
return res.status(httpStatus.BAD_REQUEST).json({message: "Body missing in action."});
|
||||
}
|
||||
|
||||
next();
|
||||
|
||||
@@ -6,6 +6,10 @@ const dirStructureModel = Joi.object({
|
||||
'string.empty': 'ID must not be empty',
|
||||
'string.guid': 'ID must be a valid GUID'
|
||||
}),
|
||||
ip: Joi.string().ip().required().messages({
|
||||
'string.ip': 'The IP address "{{#value}}" is not valid.',
|
||||
'any.required': 'IP address is required.'
|
||||
}),
|
||||
dir_config: Joi.string().required().messages({
|
||||
'any.required': 'Directory configuration is required',
|
||||
'string.empty': 'Directory configuration must not be empty'
|
||||
|
||||
@@ -5,6 +5,10 @@ const UserDepartmentPatchModel = Joi.object({
|
||||
'any.required': 'User ID is required',
|
||||
'string.empty': 'User ID must not be empty',
|
||||
'string.uuid': 'User ID must be a valid UUID'
|
||||
}),
|
||||
department: Joi.string().required().messages({
|
||||
'any.required': 'User ID is required',
|
||||
'string.empty': 'User ID must not be empty',
|
||||
})
|
||||
});
|
||||
|
||||
|
||||
@@ -16,10 +16,9 @@ const usersRegisterModel = Joi.object({
|
||||
'string.min': 'Password must be at least {#limit} characters long',
|
||||
'string.max': 'Password must be at most {#limit} characters long'
|
||||
}),
|
||||
department: Joi.string().uuid().required().messages({
|
||||
department: Joi.string().required().messages({
|
||||
'any.required': 'Department ID is required',
|
||||
'string.empty': 'Department ID must not be empty',
|
||||
'string.uuid': 'Department ID must be a valid UUID'
|
||||
'string.empty': 'Department ID must not be empty'
|
||||
})
|
||||
});
|
||||
|
||||
|
||||
@@ -48,4 +48,8 @@ router.get('/users', validateBody, (req, res) => {
|
||||
res.status(httpStatus.OK).json({ message: "Retrieving users info...", data: usersDB.readFile() }) // Corrected to readFile
|
||||
});
|
||||
|
||||
router.use((req, res) => {
|
||||
return res.status(httpStatus.NOT_FOUND).json({message: "Endpoint not found"});
|
||||
})
|
||||
|
||||
module.exports = router;
|
||||
|
||||
@@ -1,15 +1,49 @@
|
||||
const express = require('express');
|
||||
const {httpStatus} = require("../helpers/httpResponses");
|
||||
const {schemas} = require("../models/schemaMapper");
|
||||
const router = express.Router();
|
||||
|
||||
function validateBody(req, res, next) {
|
||||
let validationSchema = undefined;
|
||||
|
||||
if(req.path === '/' && req.method === 'PATCH'){
|
||||
validationSchema = schemas.dirStructureModelSchema
|
||||
}
|
||||
|
||||
if(validationSchema !== undefined){
|
||||
const {error} = validationSchema.validate(req.body);
|
||||
if(error){
|
||||
const errorMessage = error.details.map(detail => detail.message).join(', ');
|
||||
return res.status(httpStatus.BAD_REQUEST).json({ message: errorMessage });
|
||||
}
|
||||
}
|
||||
next();
|
||||
}
|
||||
|
||||
router.patch('/', validateBody, (req, res) => {
|
||||
const backupSchemesDB = req.app.get('backupSchemesDB');
|
||||
const { id, ip, backup_schema, size } = req.body;
|
||||
|
||||
let backupSchemesJson = backupSchemesDB.readFile();
|
||||
backupSchemesJson[id] = {
|
||||
ip: ip,
|
||||
backup_schema: backup_schema,
|
||||
size: size
|
||||
}
|
||||
|
||||
return res.status(httpStatus.OK).json({message: 'Backup schema updated'});
|
||||
});
|
||||
|
||||
router.get('/', validateBody, (req, res) => {
|
||||
const backupSchemesDB = req.app.get('backupSchemesDB');
|
||||
return res.status(httpStatus.OK).json({
|
||||
message: 'Backup Schemes fetched.',
|
||||
data: backupSchemesDB.readFile()
|
||||
});
|
||||
});
|
||||
|
||||
router.use((req, res) => {
|
||||
return res.status(httpStatus.NOT_FOUND).json({message: "Endpoint not found"});
|
||||
})
|
||||
|
||||
module.exports = router;
|
||||
|
||||
@@ -184,4 +184,8 @@ router.get('/get_decrypt_keys', (req, res) => {
|
||||
});
|
||||
});
|
||||
|
||||
router.use((req, res) => {
|
||||
return res.status(httpStatus.NOT_FOUND).json({message: "Endpoint not found"});
|
||||
})
|
||||
|
||||
module.exports = router;
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
const express = require('express');
|
||||
const { v4: uuidv4 } = require('uuid');
|
||||
const crypto = require('crypto');
|
||||
|
||||
const {schemas} = require('../models/schemaMapper');
|
||||
const {httpStatus} = require("../helpers/httpResponses");
|
||||
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
function validateBody(req, res, next) {
|
||||
@@ -16,6 +18,9 @@ function validateBody(req, res, next) {
|
||||
case '/login':
|
||||
validationSchema = schemas.usersLoginModelSchema;
|
||||
break;
|
||||
case '/validate_ceo_password':
|
||||
validationSchema = schemas.ceoPasswordModelSchema;
|
||||
break;
|
||||
case '/validate_email':
|
||||
validationSchema = schemas.emailVerificationSchema;
|
||||
break;
|
||||
@@ -24,16 +29,15 @@ function validateBody(req, res, next) {
|
||||
validationSchema = schemas.usersModifyModelSchema;
|
||||
}
|
||||
break;
|
||||
case 'change_department':
|
||||
case '/change_department':
|
||||
validationSchema = schemas.userDepartmentPatchSchema;
|
||||
break;
|
||||
default:
|
||||
validationSchema = undefined;
|
||||
}
|
||||
|
||||
console.log(validationSchema);
|
||||
|
||||
if(validationSchema !== undefined){
|
||||
console.log(req.body);
|
||||
const {error} = validationSchema.validate(req.body);
|
||||
if(error){
|
||||
const errorMessage = error.details.map(detail => detail.message).join(', ');
|
||||
@@ -69,14 +73,35 @@ router.post('/login', validateBody, (req, res) => {
|
||||
const { email, password } = req.body;
|
||||
const hashedPassword = crypto.createHash('sha256').update(password).digest('hex');
|
||||
|
||||
if(usersDB.readFile().length === 0){
|
||||
return res.status(httpStatus.NOT_FOUND).json({message: 'Invalid credentials.'});
|
||||
}
|
||||
|
||||
if(usersDB.findIndexByKeyValueInArray('email', email) !==
|
||||
usersDB.findIndexByKeyValueInArray('password', hashedPassword)){
|
||||
return res.status(httpStatus.NOT_FOUND).json({message: 'Invalid credentials.'});
|
||||
}
|
||||
|
||||
return res.status(httpStatus.Ok).json({message: 'Logged in.'});
|
||||
const userIndex = usersDB.findIndexByKeyValueInArray('email', email);
|
||||
|
||||
return res.status(httpStatus.OK).json({
|
||||
message: 'Logged in.',
|
||||
data: usersDB.readFile()[userIndex]
|
||||
});
|
||||
});
|
||||
|
||||
router.post('/validate_ceo_password', validateBody, (req, res) => {
|
||||
const ceoDB = req.app.get('ceoDB');
|
||||
const { password } = req.body;
|
||||
|
||||
const ceoJson = ceoDB.readFile();
|
||||
if(ceoJson.password !== password){
|
||||
return res.status(httpStatus.UNAUTHORIZED).json({message: 'Incorrect CEO password'});
|
||||
}
|
||||
|
||||
return res.status(httpStatus.OK).json({message: 'Password verified.'})
|
||||
})
|
||||
|
||||
router.post('/validate_email', validateBody, (req, res) => {
|
||||
const usersDB = req.app.get('usersDB');
|
||||
const { email } = req.body;
|
||||
@@ -159,7 +184,7 @@ router.patch('/change_department', validateBody, (req, res) => {
|
||||
usersJson[userIndex] = userInfo
|
||||
usersDB.writeFile(usersJson);
|
||||
|
||||
return res.status(httpStatus.Ok).json({message: "Department modified."});
|
||||
return res.status(httpStatus.OK).json({message: "Department modified."});
|
||||
});
|
||||
|
||||
router.get('/get_decrypt_keys', (req, res) => {
|
||||
|
||||
Reference in New Issue
Block a user