un stadiu mai avansat cu overlay the confirmare a parolei CEO

This commit is contained in:
andrei-mihnea-cerbu
2024-04-01 17:15:35 +03:00
parent b7aad6cbe2
commit 79e508d3de
41 changed files with 1295 additions and 207 deletions
+1 -7
View File
@@ -1,7 +1 @@
[
{
"id": "5ad08e32-9d5a-4054-b96f-ba361c02f30a",
"dir_config": "{dir_1: {}}",
"total_space": 0.1
}
]
{}
+1 -1
View File
@@ -1,4 +1,4 @@
{
"adminKey": "590dceb42261808e4a804607b38eb119a09c93d84554bac0d23d705d5c6e8ca5",
"ceoID": "5ad08e32-9d5a-4054-b96f-ba361c02f30a"
"ceoID": "a7635c7a-d6a0-43dc-8e24-552ab33239b8"
}
+2 -2
View File
@@ -1,7 +1,7 @@
[
{
"id": "6cd67946-6fe7-46df-9740-25eea7b95f69",
"id": "f1444987-c0c2-4ce8-89e3-b21f302bff7f",
"name": "Programatori",
"key": "a2bb3329528704715e29c27fd778892d177bcacfd73e38bde818aef560f4d549"
"key": "52a50f932fabf2ba64e337664051804a4bf1e1dcadc548cfe85be92b8c1d470e"
}
]
+1 -1
View File
@@ -1,3 +1,3 @@
{
"1": "6cd67946-6fe7-46df-9740-25eea7b95f69"
"1": "f1444987-c0c2-4ce8-89e3-b21f302bff7f"
}
+10 -4
View File
@@ -1,8 +1,14 @@
[
{
"id": "5ad08e32-9d5a-4054-b96f-ba361c02f30a",
"name": "Andrei Cerbu",
"email": "a@c.com",
"password": "4dd45a4455b1db1fd9c204a6d7c26f30"
"id": "a7635c7a-d6a0-43dc-8e24-552ab33239b8",
"name": "CEO",
"email": "ceo@yourfirm.com",
"password": "405ee6885be5385223830874bd6dcfca"
},
{
"id": "a48e1913-01f5-4df3-a2ba-476e9baa39e4",
"name": "Andrei",
"email": "a@b.com",
"password": "5447045dc3cfaafdb49b365e31dad41eb88616b0b1449930e55fd3a70bd8cee4"
}
]
@@ -0,0 +1,8 @@
class EmailAlreadyInSystemError extends Error {
constructor(message = "Email is already in system!") {
super(message);
this.name = 'EmailAlreadyInSystem';
}
}
module.exports = EmailAlreadyInSystemError
+7
View File
@@ -0,0 +1,7 @@
const Joi = require('joi');
const emailVerificationModel = Joi.object({
email: Joi.string().email().required()
});
module.exports = emailVerificationModel;
+6 -2
View File
@@ -1,15 +1,19 @@
const usersRegisterModelSchema = require('./usersRegisterModel');
const usersLoginModelSchema = require('./usersLoginModel');
const usersModifyModelSchema = require('./usersModifyModel');
const dirStructureModelSchema = require('./dirStructureModel');
const departmentRegisterModelSchema = require('./departmentRegisterModel');
const ceoRegisterModelSchema = require('./ceoRegisterModel')
const ceoRegisterModelSchema = require('./ceoRegisterModel');
const emailVerificationSchema = require('./emailVerificationModel');
module.exports = {
schemas: {
dirStructureModelSchema,
usersRegisterModelSchema,
usersLoginModelSchema,
usersModifyModelSchema,
departmentRegisterModelSchema,
ceoRegisterModelSchema
ceoRegisterModelSchema,
emailVerificationSchema
},
};
+9
View File
@@ -0,0 +1,9 @@
const Joi = require('joi');
const usersModifyModel = Joi.object({
name: Joi.string().required(),
email: Joi.string().email().required(),
password: Joi.string().min(8).max(20).required()
});
module.exports = usersModifyModel;
+1 -1
View File
@@ -4,7 +4,7 @@ const usersRegisterModel = Joi.object({
name: Joi.string().required(),
email: Joi.string().email().required(),
password: Joi.string().min(8).max(20).required(),
department: Joi.string().required()
department: Joi.string().uuid().required()
});
module.exports = usersRegisterModel;
+96 -2
View File
@@ -1,7 +1,7 @@
const express = require('express');
const fs = require('fs');
const path = require('path');
const { v4: uuidv4 } = require('uuid');
const { v4: uuidv4, validate} = require('uuid');
const crypto = require('crypto');
const { sendResponse } = require('../helpers/responseHelper');
@@ -11,6 +11,8 @@ const {schemas} = require("../models/schemaMapper");
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 router = express.Router();
@@ -99,7 +101,13 @@ router.post('/login', validateLoginBody, async (req, res) => {
throw new UserNotExistingError('Invalid email or password');
}
return sendResponse(res, httpStatus.OK, 'Login successful');
return sendResponse(res, httpStatus.OK, {
id: user.id,
name: user.name,
email: email,
password: password,
department: user.department
});
} catch (error) {
console.error(`Error: ${error.message}`);
@@ -111,6 +119,37 @@ router.post('/login', validateLoginBody, async (req, res) => {
}
});
const validateEmailValidationBody = (req, res, next) => {
const { error, value } = schemas.emailVerificationSchema.validate(req.body);
if (error) {
return sendResponse(res, httpStatus.BAD_REQUEST, error.details[0].message);
}
req.jsonModel = value;
next();
}
router.post('/validate_email', validateEmailValidationBody, (req, res) => {
try {
const {email} = req.jsonModel;
const usersFilePath = path.join(__dirname, '..', 'db', 'users.json');
const usersData = fs.readFileSync(usersFilePath, 'utf8');
const usersJson = JSON.parse(usersData);
if (usersJson.some(user => user.email === email)) {
throw new EmailAlreadyInSystemError();
}
return sendResponse(res, httpStatus.OK, httpStatusMessages[httpStatus.OK]);
}catch(error){
console.error(`Error: ${error.message}`);
if (error instanceof EmailAlreadyInSystemError) {
return sendResponse(res, httpStatus.CONFLICT, error.message);
}
return sendResponse(res, httpStatus.INTERNAL_SERVER_ERROR, httpStatusMessages[httpStatus.INTERNAL_SERVER_ERROR]);
}
});
const validateDirStructureBody = (req, res, next) => {
const { error, value } = schemas.dirStructureModelSchema.validate(req.body);
@@ -192,6 +231,61 @@ router.delete('/:id', async (req, res) => {
}
});
const validateModifyUserBody = (req, res, next) => {
const { error, value } = schemas.usersModifyModelSchema.validate(req.body);
if (error) {
return sendResponse(res, httpStatus.BAD_REQUEST, error.details[0].message);
}
req.jsonModel = value;
next();
};
router.put('/:id', validateModifyUserBody, async (req, res) => {
const id = req.params.id;
try {
const usersFilePath = path.join(__dirname, '..', 'db', 'users.json');
const usersData = fs.readFileSync(usersFilePath, 'utf8');
const usersJson = JSON.parse(usersData);
const {name, email, password} = req.jsonModel;
if (usersJson.some(user => user.email === email && user.id !== id)) {
throw new EmailAlreadyInSystemError();
}
let index = usersJson.findIndex(user => user.id === id);
if (index === -1) {
throw new UserNotExistingError();
}
const hashedPassword = crypto.createHash('sha256').update(password).digest('hex');
usersJson[index] = {
id: id,
name: name,
email: email,
password: hashedPassword
}
fs.writeFileSync(usersFilePath, JSON.stringify(usersJson, null, 2));
return sendResponse(res, httpStatus.OK, httpStatusMessages[httpStatus.OK]);
} catch (error) {
console.error(`Error: ${error.message}`);
if (error instanceof EmailAlreadyInSystemError){
return sendResponse(res, httpStatus.CONFLICT, error.message);
}
if (error instanceof UserNotExistingError) {
return sendResponse(res, httpStatus.NOT_FOUND, error.message);
}
return sendResponse(res, httpStatus.INTERNAL_SERVER_ERROR, httpStatusMessages[httpStatus.INTERNAL_SERVER_ERROR]);
}
});
router.use((req, res) => {
return sendResponse(res, httpStatus.NOT_FOUND, "Path not found");