'user strict'; const { ROOT_PATH, API_CONFIG, MODELS_PATH, LIB_PATH } = process.env; const { getModel } = require( `${ROOT_PATH}/${MODELS_PATH}` ); const apiConfig = require( `${ROOT_PATH}/${API_CONFIG}` ); const { toSha256 } = require( `${ROOT_PATH}/${LIB_PATH}/Misc.js` ); const UserModel = getModel('users'); const companiesModels = getModel('companies'); const pwd_secret = apiConfig.authentication.pwdSecret; async function create_account( email, password ){ let safe_password = toSha256( password + pwd_secret ); const user = new UserModel({ email, password : safe_password, job_role : 'owner',//Always a new user created from signup is owner isVerified : true, 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 ){ let safe_password = toSha256( password + pwd_secret ); const user = await UserModel.findOne({ email }); user.password = safe_password; user.isVerified = true; user.has_password = true; await user.save(); return user; } async function already_exists( email ){ const user = await UserModel.findOne( { email } ); if( !user ){ return false; }else{ return true; } } async function verify_driver_account( email ){ const user = await UserModel.findOne( { email } ); const retVal = { has_account:false, isVerified:false, has_password:false }; if( !user ){ retVal.has_account = false; retVal.isVerified = false; retVal.has_password = false; }else{ retVal.has_account = true; retVal.isVerified = user.isVerified; retVal.has_password = ( !user.password )? false : true; } return retVal; } async function login( email , password ){ let safe_password = toSha256( password + pwd_secret ); const user = await UserModel.findOne({ email , password : safe_password },{ password : 0 , session_token : 0 , session_token_exp : 0 }).populate(['company','vehicle']); return user; } async function login_with_session_token( session_token ){ const user = await UserModel.findOne({ session_token, session_token_exp : { $gte: new Date() } },{ password : 0 , session_token : 0 , session_token_exp : 0 }).populate(['company','vehicle']); return user; } async function complete_register( userId , data ){ let { company_type } = data; let permissions; if( company_type.toLowerCase() === "shipper" ){ company_type = "Shipper"; permissions = "role_shipper"; }else if( company_type.toLowerCase() === "carrier" ){ company_type = "Carrier"; permissions = "role_carrier"; }else{ throw "Invalid company type"; } data.company_type = company_type; const user = await UserModel.findById( userId , { password : 0 , session_token : 0 , session_token_exp : 0 } ); if( user.company ){ throw "User already register"; } const company = new companiesModels( data ); /// Generate company.id await company.save(); /// Use company.id to create company_code const company_id = "" + company._id; const company_code = "C-" + company_id.substring( 0 , 6 ); await companiesModels.findByIdAndUpdate( company._id , { company_code }); user.company = company; user.job_role = "owner"; user.permissions = permissions; user.isVerified = true; await user.save(); return company; } module.exports = { create_account, already_exists, verify_driver_account, login, login_with_session_token, reset_password, complete_register };