API CEO + USER v1.0
This commit is contained in:
+157
-10
@@ -1,40 +1,187 @@
|
||||
const express = require('express');
|
||||
const { v4: uuidv4 } = require('uuid');
|
||||
|
||||
const {schemas} = require("../models/schemaMapper");
|
||||
const {httpStatus} = require("../helpers/httpResponses");
|
||||
const crypto = require("crypto");
|
||||
const {departmentsDB} = require("../db/jsonDatabaseManager");
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
function validateBody(req, res, next) {
|
||||
if(req.path !== '/login'){
|
||||
const ceoDB = req.app.get('ceoDB');
|
||||
const ceoPassword = req.headers['ceo_password'];
|
||||
const {password} = ceoDB.readFile()
|
||||
|
||||
if(ceoPassword !== password){
|
||||
return res.status(httpStatus.UNAUTHORIZED).json({message: 'Invalid CEO password.'});
|
||||
}
|
||||
}
|
||||
|
||||
let validationSchema = undefined;
|
||||
|
||||
switch(req.path){
|
||||
case '/set_security_levels':
|
||||
validationSchema = schemas.securityLevelsModelSchema;
|
||||
break;
|
||||
case '/login':
|
||||
validationSchema = schemas.usersLoginModelSchema;
|
||||
break;
|
||||
case '/departments':
|
||||
validationSchema = schemas.departmentRegisterModelSchema;
|
||||
break;
|
||||
case '/':
|
||||
if(req.method === 'PUT'){
|
||||
validationSchema = schemas.ceoModifyModelSchema;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
validationSchema = undefined;
|
||||
}
|
||||
|
||||
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.post('/login', validateBody, (req, res) => {
|
||||
const ceoDB = req.app.get('ceoDB');
|
||||
const { email, password } = req.body;
|
||||
|
||||
if(ceoDB.findIndexByKeyValueInArray('email', email) !== ceoDB.findIndexByKeyValueInArray('password', password)){
|
||||
return res.status(httpStatus.UNAUTHORIZED).json({message: 'Invalid credentials.'});
|
||||
}
|
||||
|
||||
return res.status(httpStatus.Ok).json({message: 'Logged in.'});
|
||||
});
|
||||
|
||||
router.get('/set_security_levels', validateBody, (req, res) => {
|
||||
router.post('/set_security_levels', validateBody, (req, res) => {
|
||||
const departmentsDB = req.app.get('departmentsDB');
|
||||
const departmentsJson = departmentsDB.readFile();
|
||||
|
||||
const levelsJson = req.body;
|
||||
console.log(levelsJson);
|
||||
|
||||
const newJson = {};
|
||||
|
||||
Object.entries(levelsJson).forEach(([levelKey, departmentName]) => {
|
||||
let departmentId = -1;
|
||||
for (let deptKey in departmentsJson) {
|
||||
if (departmentsJson.hasOwnProperty(deptKey)) {
|
||||
if (departmentsJson[deptKey].name === departmentName) {
|
||||
departmentId = deptKey;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (departmentId !== -1) {
|
||||
newJson[levelKey] = {
|
||||
name: departmentName,
|
||||
key: departmentsJson[departmentId].key
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
departmentsDB.writeFile(newJson);
|
||||
return res.status(httpStatus.OK).json({message: 'Departments levels updated.'});
|
||||
});
|
||||
|
||||
router.get('/users/delete/:id', validateBody, (req, res) => {
|
||||
router.delete('/users/:id', validateBody, (req, res) => {
|
||||
const usersDB = req.app.get('usersDB');
|
||||
const { id } = req.params;
|
||||
|
||||
const userIndex = usersDB.findIndexByKeyValueInArray('id', id);
|
||||
if(userIndex === -1){
|
||||
return res.status(httpStatus.NOT_FOUND).json({message: 'User not found in system.'});
|
||||
}
|
||||
|
||||
let usersJson = usersDB.readFile;
|
||||
usersJson.splice(userIndex, 1);
|
||||
usersDB.writeFile(usersJson);
|
||||
|
||||
return res.status(httpStatus.OK).json({ message: 'User deleted successfully.' });
|
||||
});
|
||||
|
||||
router.post('/departments', validateBody, (req, res) => {
|
||||
const departmentsDB = req.app.get('departmentsDB');
|
||||
const { name } = req.body;
|
||||
// Generate key with crypto of 32Hex
|
||||
|
||||
let jsonDepartments = departmentsDB.readFile();
|
||||
const nameExists = Object.values(jsonDepartments).some(department => department.name === name);
|
||||
if(nameExists){
|
||||
return res.status(httpStatus.CONFLICT).json({message: 'Department already in system.'})
|
||||
}
|
||||
|
||||
const isObjectEmpty = !Object.keys(jsonDepartments).length;
|
||||
const highestKey = isObjectEmpty ? 0 : Math.max(...Object.keys(jsonDepartments).map(Number));
|
||||
const nextKey = highestKey + 1;
|
||||
|
||||
jsonDepartments[nextKey] = {
|
||||
name: name,
|
||||
key: crypto.randomBytes(32).toString('hex')
|
||||
};
|
||||
departmentsDB.writeFile(jsonDepartments);
|
||||
|
||||
return res.status(httpStatus.CREATED).json({message: 'Department successfully created'});
|
||||
});
|
||||
|
||||
router.get('/departments/:id', validateBody, (req, res) => {
|
||||
const { id } = req.params;
|
||||
router.delete('/departments/:name', validateBody, (req, res) => {
|
||||
const departmentsDB = req.app.get('departmentsDB');
|
||||
let departmentsJson = departmentsDB.readFile();
|
||||
let foundName = false;
|
||||
const { name } = req.params;
|
||||
|
||||
console.log(name);
|
||||
|
||||
for (let key in departmentsJson) {
|
||||
if (departmentsJson.hasOwnProperty(key)) {
|
||||
const department = departmentsJson[key];
|
||||
if (department.name === name) {
|
||||
delete departmentsJson[key];
|
||||
foundName = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(foundName !== true){
|
||||
return res.status(httpStatus.NOT_FOUND).json({ message: 'Department not found.' });
|
||||
}
|
||||
|
||||
departmentsDB.writeFile(departmentsJson);
|
||||
return res.status(httpStatus.OK).json({ message: 'Department deleted successfully.' });
|
||||
});
|
||||
|
||||
router.put('/', validateBody, (req, res) => {
|
||||
const { email } = req.body;
|
||||
});
|
||||
const ceoDB = req.app.get('ceoDB');
|
||||
const { name, email } = req.body;
|
||||
const {id} = ceoDB.readFile()
|
||||
|
||||
router.get('/', validateBody, (req, res) => {
|
||||
// Send CEO info
|
||||
const newCeo = {
|
||||
id: id,
|
||||
name: name,
|
||||
email: email,
|
||||
password: crypto.randomBytes(16).toString('hex')
|
||||
}
|
||||
|
||||
ceoDB.writeFile(newCeo);
|
||||
return res.status(httpStatus.OK).json({message: 'Information modified.'});
|
||||
});
|
||||
|
||||
router.get('/get_decrypt_keys', (req, res) => {
|
||||
// Get all departments
|
||||
return res.status(httpStatus.OK).json({
|
||||
message: 'Departments fetched.',
|
||||
data: departmentsDB.readFile()
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
|
||||
Reference in New Issue
Block a user