UI User relativ functional

This commit is contained in:
andrei-mihnea-cerbu
2024-04-01 22:44:09 +03:00
parent 79e508d3de
commit fb957a6f97
18 changed files with 381 additions and 83 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
{
"adminKey": "590dceb42261808e4a804607b38eb119a09c93d84554bac0d23d705d5c6e8ca5",
"adminKey": "9ded8e96a7a03fb7ce23a19b4f6a3d08ac42926aa96664af941e4d02e0b93f0d",
"ceoID": "a7635c7a-d6a0-43dc-8e24-552ab33239b8"
}
+7 -2
View File
@@ -1,7 +1,12 @@
[
{
"id": "f1444987-c0c2-4ce8-89e3-b21f302bff7f",
"id": "16183532-3090-4e24-8def-aa29f316c1c5",
"name": "Programatori",
"key": "52a50f932fabf2ba64e337664051804a4bf1e1dcadc548cfe85be92b8c1d470e"
"key": "66238d3abd92e43486a1f8526ff83646a2481b4fb669667daf81ecc71e8211e3"
},
{
"id": "897b883b-7638-452e-bed9-81e8da2ec0c3",
"name": "Contabili",
"key": "55956d2a1cad9e3952f3756ded721724f71baada95c9addbf46f72defa06d354"
}
]
+3 -1
View File
@@ -1,3 +1,5 @@
{
"1": "f1444987-c0c2-4ce8-89e3-b21f302bff7f"
"1": "f1444987-c0c2-4ce8-89e3-b21f302bff7f",
"2": "16183532-3090-4e24-8def-aa29f316c1c5",
"3": "897b883b-7638-452e-bed9-81e8da2ec0c3"
}
+4 -3
View File
@@ -6,9 +6,10 @@
"password": "405ee6885be5385223830874bd6dcfca"
},
{
"id": "a48e1913-01f5-4df3-a2ba-476e9baa39e4",
"id": "ffc90aca-bb31-4641-9a43-30b343924e8a",
"name": "Andrei",
"email": "a@b.com",
"password": "5447045dc3cfaafdb49b365e31dad41eb88616b0b1449930e55fd3a70bd8cee4"
"email": "a@c.com",
"password": "5447045dc3cfaafdb49b365e31dad41eb88616b0b1449930e55fd3a70bd8cee4",
"department": "16183532-3090-4e24-8def-aa29f316c1c5"
}
]
+3 -1
View File
@@ -5,6 +5,7 @@ const dirStructureModelSchema = require('./dirStructureModel');
const departmentRegisterModelSchema = require('./departmentRegisterModel');
const ceoRegisterModelSchema = require('./ceoRegisterModel');
const emailVerificationSchema = require('./emailVerificationModel');
const userDepartmentPatchSchema = require('./userDepartmentPatchModel');
module.exports = {
schemas: {
@@ -14,6 +15,7 @@ module.exports = {
usersModifyModelSchema,
departmentRegisterModelSchema,
ceoRegisterModelSchema,
emailVerificationSchema
emailVerificationSchema,
userDepartmentPatchSchema
},
};
@@ -0,0 +1,7 @@
const Joi = require("joi");
const UserDepartmentPatchModel = Joi.object({
department: Joi.string().uuid().required()
});
module.exports = UserDepartmentPatchModel;
+1
View File
@@ -117,6 +117,7 @@ router.post('/validate_admin_password', (req, res) => {
try {
const requestAdminKey = req.headers['admin-key'];
if (requestAdminKey !== res.locals.adminKey) {
console.log(res.locals.adminKey);
throw new InvalidKeyErrorException();
}
+62 -15
View File
@@ -1,23 +1,25 @@
const express = require('express');
const fs = require('fs');
const path = require('path');
const { v4: uuidv4, validate} = require('uuid');
const {v4: uuidv4, validate} = require('uuid');
const crypto = require('crypto');
const { sendResponse } = require('../helpers/responseHelper');
const {sendResponse} = require('../helpers/responseHelper');
const {httpStatus, httpStatusMessages} = require('../helpers/httpResponses')
const {schemas} = require("../models/schemaMapper");
const UserAlreadyExistsException =require('../exceptions/userAlreadyExistsError');
const UserAlreadyExistsException = require('../exceptions/userAlreadyExistsError');
const UserNotExistingError = require("../exceptions/userNotExistingError");
const ImproperDirStructureError = require("../exceptions/improperDirStructureError");
const EmailAlreadyInSystemError = require("../exceptions/emailAlreadyInSystemError");
const {HttpStatusCode} = require("axios");
const DepartmentAlreadyExistsError = require("../exceptions/departmentAlreadyExistsError");
const DepartmentNotExistingError = require("../exceptions/departmentNotExistingError");
const router = express.Router();
router.get('/', async(req, res) => {
router.get('/', async (req, res) => {
try {
const usersFilePath = path.join(__dirname, '..', 'db', 'users.json');
const usersData = fs.readFileSync(usersFilePath, 'utf8');
@@ -30,7 +32,7 @@ router.get('/', async(req, res) => {
});
const validateRegisterBody = (req, res, next) => {
const { error, value } = schemas.usersRegisterModelSchema.validate(req.body);
const {error, value} = schemas.usersRegisterModelSchema.validate(req.body);
if (error) {
return sendResponse(res, httpStatus.BAD_REQUEST, error.details[0].message);
}
@@ -44,7 +46,7 @@ router.post('/register', validateRegisterBody, async (req, res) => {
const usersData = fs.readFileSync(usersFilePath, 'utf8');
const usersJson = JSON.parse(usersData);
const { name, email, password, department} = req.jsonModel;
const {name, email, password, department} = req.jsonModel;
const existingUser = usersJson.find(user => user.email === email);
if (existingUser) {
@@ -79,7 +81,7 @@ router.post('/register', validateRegisterBody, async (req, res) => {
const validateLoginBody = (req, res, next) => {
const { error, value } = schemas.usersLoginModelSchema.validate(req.body);
const {error, value} = schemas.usersLoginModelSchema.validate(req.body);
if (error) {
return sendResponse(res, httpStatus.BAD_REQUEST, error.details[0].message);
}
@@ -93,7 +95,7 @@ router.post('/login', validateLoginBody, async (req, res) => {
const usersData = fs.readFileSync(usersFilePath, 'utf8');
const usersJson = JSON.parse(usersData);
const { email, password } = req.jsonModel;
const {email, password} = req.jsonModel;
const hashedPassword = crypto.createHash('sha256').update(password).digest('hex');
const user = usersJson.find(user => user.email === email && user.password === hashedPassword);
@@ -120,7 +122,7 @@ router.post('/login', validateLoginBody, async (req, res) => {
});
const validateEmailValidationBody = (req, res, next) => {
const { error, value } = schemas.emailVerificationSchema.validate(req.body);
const {error, value} = schemas.emailVerificationSchema.validate(req.body);
if (error) {
return sendResponse(res, httpStatus.BAD_REQUEST, error.details[0].message);
}
@@ -140,7 +142,7 @@ router.post('/validate_email', validateEmailValidationBody, (req, res) => {
}
return sendResponse(res, httpStatus.OK, httpStatusMessages[httpStatus.OK]);
}catch(error){
} catch (error) {
console.error(`Error: ${error.message}`);
if (error instanceof EmailAlreadyInSystemError) {
return sendResponse(res, httpStatus.CONFLICT, error.message);
@@ -150,9 +152,8 @@ router.post('/validate_email', validateEmailValidationBody, (req, res) => {
});
const validateDirStructureBody = (req, res, next) => {
const { error, value } = schemas.dirStructureModelSchema.validate(req.body);
const {error, value} = schemas.dirStructureModelSchema.validate(req.body);
if (error) {
return sendResponse(res, httpStatus.BAD_REQUEST, error.details[0].message);
}
@@ -162,7 +163,7 @@ const validateDirStructureBody = (req, res, next) => {
router.put('/dir_structure', validateDirStructureBody, (req, res) => {
try {
const { id, dir_config, total_space } = req.jsonModel;
const {id, dir_config, total_space} = req.jsonModel;
const backupFilePath = path.join(__dirname, '..', 'db', 'backup_schemas.json');
let backupArray = [];
@@ -202,6 +203,52 @@ router.put('/dir_structure', validateDirStructureBody, (req, res) => {
}
});
const validateUserDepartmentPatch = (req, res, next) => {
const {error, value} = schemas.userDepartmentPatchSchema.validate(req.body);
if (error) {
return sendResponse(res, httpStatus.BAD_REQUEST, error.details[0].message);
}
req.jsonModel = value;
next();
};
router.patch('/:id', validateUserDepartmentPatch, async (req, res) => {
const id = req.params.id;
console.log('esti aici');
try {
const department_id = req.jsonModel.department;
const usersFilePath = path.join(__dirname, '..', 'db', 'users.json');
const usersData = fs.readFileSync(usersFilePath, 'utf8');
const usersJson = JSON.parse(usersData);
const index = usersJson.findIndex(user => user.id === id);
if (index === -1) {
throw new UserNotExistingError();
}
const departmentsFilePath = path.join(__dirname, '..', 'db', 'departments.json');
const departmentsData = fs.readFileSync(departmentsFilePath, 'utf8');
const departments = JSON.parse(departmentsData);
console.log(departments);
const indexDepartment = departments.findIndex(department => department.id === department_id);
if (indexDepartment === -1) {
throw new DepartmentNotExistingError();
}
usersJson[index].department = department_id;
fs.writeFileSync(usersFilePath, JSON.stringify(usersJson, null, 2));
return sendResponse(res, httpStatus.OK, httpStatusMessages[httpStatus.OK]);
}catch(error){
console.log(error);
return sendResponse(res, httpStatus.INTERNAL_SERVER_ERROR, httpStatusMessages[httpStatus.INTERNAL_SERVER_ERROR]);
}
})
router.delete('/:id', async (req, res) => {
const id = req.params.id;
@@ -232,7 +279,7 @@ router.delete('/:id', async (req, res) => {
});
const validateModifyUserBody = (req, res, next) => {
const { error, value } = schemas.usersModifyModelSchema.validate(req.body);
const {error, value} = schemas.usersModifyModelSchema.validate(req.body);
if (error) {
return sendResponse(res, httpStatus.BAD_REQUEST, error.details[0].message);
}
@@ -274,7 +321,7 @@ router.put('/:id', validateModifyUserBody, async (req, res) => {
} catch (error) {
console.error(`Error: ${error.message}`);
if (error instanceof EmailAlreadyInSystemError){
if (error instanceof EmailAlreadyInSystemError) {
return sendResponse(res, httpStatus.CONFLICT, error.message);
}