<commit_before>/**
 * Created by jianzhiqiang on 2017/5/11.
 */
"use strict";

var bcrypt = require('bcryptjs');
var nconf = require('nconf');
var async = require('async');
var _ = require('lodash');
var validator = require('validator');
var logger = require('../logger');
var utils = require('../utils');
var CustomerSuccess = require('../database/customerSuccess');

var customerSuccessController = module.exports;

customerSuccessController.register = function (req, res, next) {
    var customerSuccess = {
        name: req.body.name,
        displayName: req.body.displayName || '',
        email: req.body.email,
        passwd: req.body.passwd,
        photo: req.body.photo || '',
        timezone: req.body.timezone
    };

    if (!validateEmail(customerSuccess.email)) return res.status(400).json({
        code: 1000, msg: 'email_validate_error'
    });

    if (!validatePasswordLength(customerSuccess.passwd)) return res.status(400).json({
        code: 1001, msg: 'passwd_validate_error'
    });

    customerSuccess.name = customerSuccess.name || customerSuccess.email.split('@')[0];

    async.waterfall([
        function (callback) {
            hashPasswdWithSalt(customerSuccess.passwd, callback);
        },
        function (hash, callback) {
            customerSuccess.passwd = hash;
            CustomerSuccess.create(customerSuccess, callback);
        }
    ], function (err, user) {
        if (err) {
            if(err.name === 'SequelizeUniqueConstraintError'){
                return res.status(403).json({
                    code: 1005, msg: 'email_already_exists'
                });
            }
            return next(err);
        }

        createCSSocket(req, user);

        res.json({
            code: 200, msg: {
                csid: user.csid,
                name: user.name,
                displayName: user.displayName,
                email: user.email,
                photo: user.photo,
                timezone: user.timezone
            }
        });
    });
};

customerSuccessController.login = function (req, res, next) {
    var customerSuccess = {
        email: req.body.email,
        passwd: req.body.passwd
    };

    if (!customerSuccess.email || !customerSuccess.passwd)
        return res.status(400).json({
            code: 1002,
            msg: 'params_missing'
        });

    async.waterfall([
        function (callback) {

            var condition = {email: customerSuccess.email};

            CustomerSuccess.findOne(condition, callback);
        },
        function (user, callback) {

            if (!user) return res.status(404).json({code: 1003, msg: 'email_not_found'});

            bcrypt.compare(customerSuccess.passwd, user.passwd).then(function (result) {

                if (!result) return res.status(403).json({code: 1004, msg: 'passwd_error'});

                callback(null, user);

            }).catch(function (err) {

                logger.error(err);

                callback(err);
            });
        }
    ], function (err, user) {

        if (err) return next(err);

        createCSSocket(req, user);

        res.json({
            code: 200,
            msg: {
                csid: user.csid,
                name: user.name,
                displayName: user.displayName,
                email: user.email,
                photo: user.photo,
                timezone: user.timezone
            }
        });
    });
};

customerSuccessController.loginConsole = function (req, res, next) {
    var customerSuccess = {
        email: req.body.email,
        passwd: req.body.<PASSWORD>
    };

    if (!customerSuccess.email || !customerSuccess.passwd)
        return res.status(400).json({
            code: 1002,
            msg: 'params_missing'
        });

    async.waterfall([
        function (callback) {

            var condition = {};
            var order = [['createdAt', 'ASC']];

            CustomerSuccess.list(condition, order, 1, 0, callback);
        },
        function (users, callback) {

            if (!users || ! users.length) return res.status(404).json({code: 1003, msg: 'email_not_found'});

            var user = users[0];

            if(user.email !== customerSuccess.email) return res.status(401).json({code: 1003, msg: 'forbid_login'});

            bcrypt.compare(customerSuccess.passwd, user.passwd).then(function (result) {

                if (!result) return res.status(403).json({code: 1004, msg: 'passwd_error'});

                callback(null, user);

            }).catch(function (err) {

                callback(err);
            });
        }
    ], function (err, user) {

        if (err) return next(err);

        createCSSocket(req, user);

        res.json({
            code: 200,
            msg: {
                csid: user.csid,
                name: user.name,
                displayName: user.displayName,
                email: user.email,
                photo: user.photo,
                timezone: user.timezone
            }
        });
    });
};

customerSuccessController.logout = function (req, res, next) {
    req.session.destroy();
    res.json({code: 200, msg: 'success logout'});
};

customerSuccessController.update = function (req, res, next) {
    var customerSuccess = {
        'name': req.body.name,
        'displayName': req.body.displayName,
        'timezone': req.body.timezone
    };

    var condition = {csid: req.params.csid};

    return CustomerSuccess.update(customerSuccess, condition, function (err, data) {

        if (err) return next(err);

        return res.json({code: 200, msg: 'success update'});
    });
};

customerSuccessController.delete = function (req, res, next) {
    var condition = {csid: req.params.csid};

    CustomerSuccess.delete(condition, function (err, data) {

        if (err) return next(err);

        res.json({code: 200, msg: 'success delete'});
    });
};


customerSuccessController.getAvatar = function (req, res, next) {

    CustomerSuccess.findById(req.params.csid, function (err, customerSuccess) {

        if (err) return next(err);

        res.json({code: 200, msg: {photo: customerSuccess.photo}});
    });
};

customerSuccessController.uploadAvatar = function (req, res, next) {
    var filePath = req.file.path;
    var customerSuccess = {photo: filePath};
    var condition = {csid: req.params.csid};

    return CustomerSuccess.update(customerSuccess, condition, function (err, data) {

        if (err) return next(err);
        req.session.photo = filePath;
        return res.json({code: 200, msg: {photo: filePath}});
    });
};

customerSuccessController.updatePasswd = function (req, res, next) {
    var customerSuccess = {<commit_msg>CS modify photo and displayName sync to customer<commit_after>/**
 * Created by jianzhiqiang on 2017/5/11.
 */
"use strict";

var bcrypt = require('bcryptjs');
var nconf = require('nconf');
var async = require('async');
var _ = require('lodash');
var validator = require('validator');
var logger = require('../logger');
var utils = require('../utils');
var CustomerSuccess = require('../database/customerSuccess');

var customerSuccessController = module.exports;

customerSuccessController.register = function (req, res, next) {
    var customerSuccess = {
        name: req.body.name,
        displayName: req.body.displayName || '',
        email: req.body.email,
        passwd: req.body.passwd,
        photo: req.body.photo || '',
        timezone: req.body.timezone
    };

    if (!validateEmail(customerSuccess.email)) return res.status(400).json({
        code: 1000, msg: 'email_validate_error'
    });

    if (!validatePasswordLength(customerSuccess.passwd)) return res.status(400).json({
        code: 1001, msg: 'passwd_validate_error'
    });

    customerSuccess.name = customerSuccess.name || customerSuccess.email.split('@')[0];

    async.waterfall([
        function (callback) {
            hashPasswdWithSalt(customerSuccess.passwd, callback);
        },
        function (hash, callback) {
            customerSuccess.passwd = <PASSWORD>;
            CustomerSuccess.create(customerSuccess, callback);
        }
    ], function (err, user) {
        if (err) {
            if(err.name === 'SequelizeUniqueConstraintError'){
                return res.status(403).json({
                    code: 1005, msg: 'email_already_exists'
                });
            }
            return next(err);
        }

        createCSSocket(req, user);

        res.json({
            code: 200, msg: {
                csid: user.csid,
                name: user.name,
                displayName: user.displayName,
                email: user.email,
                photo: user.photo,
                timezone: user.timezone
            }
        });
    });
};

customerSuccessController.login = function (req, res, next) {
    var customerSuccess = {
        email: req.body.email,
        passwd: req.body.passwd
    };

    if (!customerSuccess.email || !customerSuccess.passwd)
        return res.status(400).json({
            code: 1002,
            msg: 'params_missing'
        });

    async.waterfall([
        function (callback) {

            var condition = {email: customerSuccess.email};

            CustomerSuccess.findOne(condition, callback);
        },
        function (user, callback) {

            if (!user) return res.status(404).json({code: 1003, msg: 'email_not_found'});

            bcrypt.compare(customerSuccess.passwd, user.passwd).then(function (result) {

                if (!result) return res.status(403).json({code: 1004, msg: 'passwd_error'});

                callback(null, user);

            }).catch(function (err) {

                logger.error(err);

                callback(err);
            });
        }
    ], function (err, user) {

        if (err) return next(err);

        createCSSocket(req, user);

        res.json({
            code: 200,
            msg: {
                csid: user.csid,
                name: user.name,
                displayName: user.displayName,
                email: user.email,
                photo: user.photo,
                timezone: user.timezone
            }
        });
    });
};

customerSuccessController.loginConsole = function (req, res, next) {
    var customerSuccess = {
        email: req.body.email,
        passwd: req.body.passwd
    };

    if (!customerSuccess.email || !customerSuccess.passwd)
        return res.status(400).json({
            code: 1002,
            msg: 'params_missing'
        });

    async.waterfall([
        function (callback) {

            var condition = {};
            var order = [['createdAt', 'ASC']];

            CustomerSuccess.list(condition, order, 1, 0, callback);
        },
        function (users, callback) {

            if (!users || ! users.length) return res.status(404).json({code: 1003, msg: 'email_not_found'});

            var user = users[0];

            if(user.email !== customerSuccess.email) return res.status(401).json({code: 1003, msg: 'forbid_login'});

            bcrypt.compare(customerSuccess.passwd, user.passwd).then(function (result) {

                if (!result) return res.status(403).json({code: 1004, msg: 'passwd_error'});

                callback(null, user);

            }).catch(function (err) {

                callback(err);
            });
        }
    ], function (err, user) {

        if (err) return next(err);

        createCSSocket(req, user);

        res.json({
            code: 200,
            msg: {
                csid: user.csid,
                name: user.name,
                displayName: user.displayName,
                email: user.email,
                photo: user.photo,
                timezone: user.timezone
            }
        });
    });
};

customerSuccessController.logout = function (req, res, next) {
    req.session.destroy();
    res.json({code: 200, msg: 'success logout'});
};

customerSuccessController.update = function (req, res, next) {
    var customerSuccess = {
        'name': req.body.name,
        'displayName': req.body.displayName,
        'timezone': req.body.timezone
    };

    var condition = {csid: req.params.csid};

    req.session.csName = req.body.displayName; // set display name to session
    var cs = require('../socket.io/customerSuccess');
    cs.get(req.session.csid).name = req.body.displayName;
    return CustomerSuccess.update(customerSuccess, condition, function (err, data) {

        if (err) return next(err);

        return res.json({code: 200, msg: 'success update'});
    });
};

customerSuccessController.delete = function (req, res, next) {
    var condition = {csid: req.params.csid};

    CustomerSuccess.delete(condition, function (err, data) {

        if (err) return next(err);

        res.json({code: 200, msg: 'success delete'});
    });
};


customerSuccessController.getAvatar = function (req, res, next) {

    CustomerSuccess.findById(req.params.csid, function (err, customerSuccess) {

        if (err) return next(err);

        res.json({code: 200, msg: {photo: customerSuccess.photo}});
    });
};

customerSuccessController.uploadAvatar = function (req, res, next) {
    var filePath = req.file.path;
    var customerSuccess = {photo: filePath};
    var condition = {csid: req.params.csid};

    return CustomerSuccess.update(customerSuccess, condition, function (err, data) {

        if (err) return next(err);
        req.session.photo = filePath;
        var cs = require('../socket.io/customerSuccess');
        cs.get(req.session.csid).photo = filePath;
        return res.json({code: 200, msg: {photo: filePath}});
    });
};

customerSuccessController.updatePasswd = function (req, res, next) {
    var customerSuccess = {
{'fraction_non_alphanumeric': 0.1349229427159058, 'fraction_numerical': 0.015266065716778134, 'mean_word_length': 2.0852209015474323, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5758693', 'n_tokens_mistral': 4150, 'n_tokens_neox': 3831, 'n_words': 858}