fix(Users): Add employee_id after creating user
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
"use strict";
|
||||
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) => {
|
||||
try{
|
||||
|
||||
@@ -3,8 +3,8 @@ const jsonwebtoken = require('jsonwebtoken');
|
||||
const { API_CONFIG, ROOT_PATH, LIB_PATH, HANDLERS_PATH } = process.env;
|
||||
const apiConfig = require( `${ROOT_PATH}/${API_CONFIG}` );
|
||||
const { genKey, toSha256 } = require( `${ROOT_PATH}/${LIB_PATH}/Misc.js` );
|
||||
const { emailEvent , EMAIL_EVENTS } = require( `${ROOT_PATH}/${HANDLERS_PATH}/MailClient` );
|
||||
const { create_account, already_exists, verify_driver_account, login, login_with_session_token, reset_password } = require( `${ROOT_PATH}/${HANDLERS_PATH}/Account` );
|
||||
const { emailEvent , EMAIL_EVENTS } = require( '../../../lib/Handlers/MailClient' );
|
||||
const { create_account, already_exists, verify_driver_account, login, login_with_session_token, reset_password } = require( '../../../lib/Handlers/Account' );
|
||||
const { Validator } = require( "jsonschema" );
|
||||
|
||||
const jwtSecret = apiConfig.authentication.jwtSecret;
|
||||
|
||||
@@ -18,6 +18,13 @@ async function create_account( email, password ){
|
||||
has_password : true
|
||||
});
|
||||
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 ){
|
||||
|
||||
@@ -150,10 +150,20 @@ async function createUserWithinCompany( companyId , data ){
|
||||
data.isVerified = false;
|
||||
const user = new usersModel( data );
|
||||
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;
|
||||
}
|
||||
|
||||
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 company = manager.company;
|
||||
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"; }
|
||||
|
||||
user.is_deleted = true;
|
||||
user.isVerified = false;
|
||||
user.email = user.id;
|
||||
user.password = null;
|
||||
user.deleted_at = new Date();
|
||||
await usersModel.findOneAndDelete( {
|
||||
_id : user_to_remove_id ,
|
||||
company : manager.company.id
|
||||
} );
|
||||
|
||||
await user.save();
|
||||
return user;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user