fix(Users): Add employee_id after creating user

This commit is contained in:
Josepablo C
2024-04-06 15:08:17 -06:00
parent 857ca28f43
commit d00f7bbfa7
4 changed files with 24 additions and 9 deletions

View File

@@ -1,6 +1,6 @@
"use strict"; "use strict";
const { ROOT_PATH, HANDLERS_PATH } = process.env; const { ROOT_PATH, HANDLERS_PATH } = process.env;
const { getUserById, findUsers, patchUserData, createUserWithinCompany, deleteUserWithinCompany } = require( `${ROOT_PATH}/${HANDLERS_PATH}/Users.handler.js` ); const { getUserById, findUsers, patchUserData, createUserWithinCompany, deleteUserWithinCompany } = require( "../../../lib/Handlers/Users.handler" );
const findList = async(req, res) => { const findList = async(req, res) => {
try{ try{

View File

@@ -3,8 +3,8 @@ const jsonwebtoken = require('jsonwebtoken');
const { API_CONFIG, ROOT_PATH, LIB_PATH, HANDLERS_PATH } = process.env; const { API_CONFIG, ROOT_PATH, LIB_PATH, HANDLERS_PATH } = process.env;
const apiConfig = require( `${ROOT_PATH}/${API_CONFIG}` ); const apiConfig = require( `${ROOT_PATH}/${API_CONFIG}` );
const { genKey, toSha256 } = require( `${ROOT_PATH}/${LIB_PATH}/Misc.js` ); const { genKey, toSha256 } = require( `${ROOT_PATH}/${LIB_PATH}/Misc.js` );
const { emailEvent , EMAIL_EVENTS } = require( `${ROOT_PATH}/${HANDLERS_PATH}/MailClient` ); const { emailEvent , EMAIL_EVENTS } = require( '../../../lib/Handlers/MailClient' );
const { create_account, already_exists, verify_driver_account, login, login_with_session_token, reset_password } = require( `${ROOT_PATH}/${HANDLERS_PATH}/Account` ); const { create_account, already_exists, verify_driver_account, login, login_with_session_token, reset_password } = require( '../../../lib/Handlers/Account' );
const { Validator } = require( "jsonschema" ); const { Validator } = require( "jsonschema" );
const jwtSecret = apiConfig.authentication.jwtSecret; const jwtSecret = apiConfig.authentication.jwtSecret;

View File

@@ -18,6 +18,13 @@ async function create_account( email, password ){
has_password : true has_password : true
}); });
await user.save(); await user.save();
// Create user code
const id = "" + user._id;
const employee_id = "E-" + id.substring( 0 , 6 );
await UserModel.findByIdAndUpdate( id , {
employee_id
});
} }
async function reset_password( email, password ){ async function reset_password( email, password ){

View File

@@ -150,10 +150,20 @@ async function createUserWithinCompany( companyId , data ){
data.isVerified = false; data.isVerified = false;
const user = new usersModel( data ); const user = new usersModel( data );
await user.save(); await user.save();
// Create user code
const id = "" + user._id;
const employee_id = "E-" + id.substring( 0 , 6 );
await usersModel.findByIdAndUpdate( id , {
employee_id
});
return user; return user;
} }
async function deleteUserWithinCompany( manager_id , user_to_remove_id ){ async function deleteUserWithinCompany( manager_id , user_to_remove_id ){
if( manager_id === user_to_remove_id ){ throw "Manager can not remove it self"; }
const manager = await usersModel.findById( manager_id ).populate( "company" ); const manager = await usersModel.findById( manager_id ).populate( "company" );
const company = manager.company; const company = manager.company;
if( !manager ){ throw "Invalid manager or owner"; } if( !manager ){ throw "Invalid manager or owner"; }
@@ -164,13 +174,11 @@ async function deleteUserWithinCompany( manager_id , user_to_remove_id ){
} ); } );
if( !user ){ throw "User is invalid"; } if( !user ){ throw "User is invalid"; }
user.is_deleted = true; await usersModel.findOneAndDelete( {
user.isVerified = false; _id : user_to_remove_id ,
user.email = user.id; company : manager.company.id
user.password = null; } );
user.deleted_at = new Date();
await user.save();
return user; return user;
} }