src/modules/users/controller.js
/**
 * Saves new users or updates users where e-mail was not verified. An email is sent to verify user's email.
 * 
 * @route
 *      POST /api/users
 * @params
 *      payload:
 *          - name
 *          - email
 *          - facebookId
 *          - accessToken
 *          - username
 *          - password
 *          - skills
 * @returns
 *      success message - if email is sent successfully
 *      error message - if error occurs.
 */
Saves new users or updates users where e-mail was not verified. An email is sent to verify user's email.


Saves new users or updates users where e - mail was not verified . An email is sent to verify user ' s email .
function Save(request,reply){
    let createUserParams = {
        name : request.payload.name,
        email : request.payload.email,
        facebookId : request.payload.facebookId,
        username : request.payload.username,
        password : request.payload.password,
        skills : request.payload.skills,
        description : request.payload.description
    }
    let searchFilter = {
        email : request.payload.email,
        facebookId : request.payload.facebookId
    }
    UserRepository.findById(searchFilter,function(err,user){
        if(user){
            UserRepository.update(user,createUserParams)
                .then(function(user){
                    return generateVerification(user);
                })
                .catch(function(err){
                     console.log(err);
                     reply({msg:'error'}).code(500);
                });
        }else{
            UserRepository.create(createUserParams) 
                .then(function(user){
                    return generateVerification(user);
                })
                .catch(function(err){
                     console.log(err);
                     reply({msg:'error'}).code(500);
                });
        }
    });
    function generateVerification(user){
        const stringSize = 50; 
        var verificationID = StringUtils.generate(stringSize);
        let userEmail = user.getDataValue('email');
        EmailValidationRepository.create({id : verificationID, email : userEmail},function(err, emailVal){
            if(err){
                console.log(err);
                reply({msg:'error'}).code(500);
            }
            var host = process.env.frontClient || 'sociaid.com';
            MailService.sendVerificationEmail(host,verificationID,user.getDataValue('email'),function(err,response){
                if(err){
                    console.log(err);
                    return reply({msg: 'error'}).code(500);
                }
                reply({msg: 'emailSent', success: true});
            });
        });
    }    
}
function Save ( request , reply ) { let createUserParams = { name : request . payload . name , email : request . payload . email , facebookId : request . payload . facebookId , username : request . payload . username , password : request . payload . password , skills : request . payload . skills , description : request . payload . description } let searchFilter = { email : request . payload . email , facebookId : request . payload . facebookId } UserRepository . findById ( searchFilter , function ( err , user ) { if ( user ) { UserRepository . update ( user , createUserParams ) . then ( function ( user ) { return generateVerification ( user ) ; } ) . catch ( function ( err ) { console . log ( err ) ; reply ( { msg : 'error' } ) . code ( 500 ) ; } ) ; } else { UserRepository . create ( createUserParams ) . then ( function ( user ) { return generateVerification ( user ) ; } ) . catch ( function ( err ) { console . log ( err ) ; reply ( { msg : 'error' } ) . code ( 500 ) ; } ) ; } } ) ; function generateVerification ( user ) { const stringSize = 50 ; var verificationID = StringUtils . generate ( stringSize ) ; let userEmail = user . getDataValue ( 'email' ) ; EmailValidationRepository . create ( { id : verificationID , email : userEmail } , function ( err , emailVal ) { if ( err ) { console . log ( err ) ; reply ( { msg : 'error' } ) . code ( 500 ) ; } var host = process . env . frontClient || 'sociaid.com' ; MailService . sendVerificationEmail ( host , verificationID , user . getDataValue ( 'email' ) , function ( err , response ) { if ( err ) { console . log ( err ) ; return reply ( { msg : 'error' } ) . code ( 500 ) ; } reply ( { msg : 'emailSent' , success : true } ) ; } ) ; } ) ; } }
Saves new users or updates users where e-mail was not verified.
Saves new users or updates users where e - mail was not verified .
//TODO photo //encriptar