<commit_before>/**
 * Copyright (c) 2016-present, Agoo.com.co <http://www.agoo.com.co>.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree or translated in the assets folder.
 */

// Load required packages
var logger = require('../config/logger').logger;
var User = require('../models/users').User;
var UserReset = require('../models/userReset').UserReset;

// ENDPOINT: /users/:id METHOD: DELETE
exports.deleteUser = function(req, res){
    User.findByIdAndRemove(req.params.id, function(err){
        // Check for errors and show message
        if(err){
            logger.error(err);
            res.send(err);
            return;
        }
        // success
        res.json({ message: 'User deleted successfully!' });
    });
};<commit_msg>basic user routes [working]<commit_after>/**
 * Copyright (c) 2016-present, Agoo.com.co <http://www.agoo.com.co>.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree or translated in the assets folder.
 */

// Load required packages
var logger = require('../config/logger').logger;
var User = require('../models/users').User;
var UserReset = require('../models/userReset').UserReset;

// ENDPOINT: /users METHOD: GET
exports.getUsers = function(req, res){
  
    // Use the 'User' model to find all users
    User.find(function (err, users) {
        // Check for errors and show message
        if(err){
            logger.error(err);
            res.send(err);
            return;
        }
        // success
        res.json(users);
    });
};

// ENDPOINT: /users/:id METHOD: GET
// ENDPOINT: /users/count METHOD: GET
exports.getUserById = function(req, res){
    // COUNT ENDPOINT CALLED
    if (req.params.id == 'count'){
        User.count({}, function (err, countUser) {
            // Check for errors and show message
            if(err){
                logger.error(err);
                res.send(err);
            }
            // Success
            res.json({ message:"The complete count of users", data: countUser });
        });
        return;
    }

    // Use the 'User' model to find all users
    User.findById(req.params.id, function (err, user) {
        // Check for errors and show message
        if(err){
            logger.error(err);
            res.send(err);
            return;
        }
        // success
        res.json(user);
    });
};

// ENDPOINT: /login METHOD: GET
exports.getLogin = function (req, res) {
    // Use the 'User' model to find all users
    User.findById(req.user._id, function (err, user) {
        // Check for errors and show message
        if(err){
            logger.error(err);
            res.send(err);
            return;
        }
        // success
        res.json({ message:"Login authenticated successfully", data: user });
    });
};

// ENDPOINT: /password/reset METHOD: POST
exports.postPasswordReset = function (req, res) {
    var emailRequest = req.body.email;
    // Use the 'User' model to find one user with this email address
    // Validate email provided exist on database
    User.find({ email: emailRequest }, function (err, user) {
        // Check for errors and show message
        if(err){
            logger.error(err);
            res.json({ message: 'Email provided doesn`t Exists'});
            //res.send(err);
            return;
        }

        // Get User data values
        var idUser = user[0]._id;
        var fullName = user[0].name + ' ' + user[0].lastName;

        // Generate random code, with the size of 8 characters mixing numbers and letters
        var chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        var sizeCode = 8;
        var codeResult = '';
        for (var i = sizeCode; i > 0; --i) codeResult += chars[Math.round(Math.random() * (chars.length - 1))];
        //logger.info(codeResult);

        //Get current year
        var currentYear = new Date().getFullYear();
        //logger.info(currentYear);

        // Get sent_at date
        //var sendAtDate = '2015-01-01 12:12:12';
        //logger.info(sendAtDate);

        res.json({ message: 'Email sent successfully!, ' + codeResult });
    });
};

// ENDPOINT: /password/reset/:code METHOD: PATCH
exports.PatchPasswordReset = function (req, res) {
    var codeRequest = req.params.code;
    // Use the 'UserReset' model to find one user with this email address
    UserReset.find({code: codeRequest}, function (err, userReset) {
        // Check for errors and show message
        if(err){
            logger.error(err);
            res.json({ message: 'Code doesn`t exists'});
            res.send(err);
            return;
        }

        // Change the password for the new sent
        //var emailRequest = req.body.email;
        //logger.info(emailRequest);
        var passRequest = req.body.password;
        logger.info(passRequest);
        //logger.info(userReset[0].idUser);
        var idUserRequest = userReset[0]._id;

        // Find the user and update de password value
        User.findById(userReset[0].idUser, function (err, user) {
            if(err){
                logger.error(err);
                res.send(err);
                return;
            }

            // Change the password value
            user.password = <PASSWORD>;

            user.save(function(err){
                // Check for errors and show message
                if(err){
                    logger.error(err);
                    res.send(err);
                    return;
                }
                // Now deleted the code from the collection
                UserReset.findByIdAndRemove(idUserRequest, function(err){
                    // Check for errors and show message
                    if(err){
                        logger.error(err);
                        res.send(err);
                        return;
                    }
                    // success
                    // success
                    res.json({ message: 'User password changed and code: ' + codeRequest +' deleted successfully!' });
                });
            });



        });
    });
};



// ENDPOINT: /users METHOD: POST
exports.postUser = function (req, res) {
    // Create a new instance of the User model
    var user = new User();

    // Set the User properties that came from the POST data
    user.name = req.body.name;
    user.lastName = req.body.lastName;
    user.email = req.body.email;
    user.password = <PASSWORD>;
    // TODO: rectificar insercion de array de objetos
    //user.rol = req.body.rol;
    //user.permissions = req.body.permissions;
    user.creationDate = Date.now();
    user.enabled = true;

    user.save(function(err){
        // Check for errors and show message
        if(err){
            logger.error(err);
            res.send(err);
            return;
        }
        //Success
        res.json({ message: 'User created successfully!', data: user });
    });
};

// ENDPOINT: /users/:id METHOD: PUT
exports.putUser = function(req, res){
    User.findById(req.params.id, function (err, user) {
        // Check for errors and show message
        if(err){
            logger.error(err);
            res.send(err);
            return;
        }

        // Set the User properties that came from the PUT data
        user.name = req.body.name;
        user.lastName = req.body.lastName;
        user.email = req.body.email;
        user.password = <PASSWORD>;
        user.creationDate = req.body.creationDate;
        user.enabled = req.body.enabled;
        // TODO: rectificar insercion de array de objetos
        //user.rol = req.body.rol;
        //user.permissions = req.body.permissions;
        user.save(function(err){
            // Check for errors and show message
            if(err){
                logger.error(err);
                res.send(err);
            }
            // success
            res.json({message: 'User updated successfully', data: user });
        });
    });
};

// ENDPOINT: /users/:id METHOD: PATCH
exports.patchUser = function(req, res){
    User.findById(req.params.id, function (err, user) {
        // Check for errors and show message
        if(err){
            logger.error(err);
            res.send(err);
            return;
        }

        user.enabled = req.body.enabled;

        user.save(function(err){
            // Check for errors and show message
            if(err){
                logger.error(err);
                res.send(err);
                return;
            }
            var message = '';
            if(user.enabled === true){
                message = 'User enabled successfully';
            }else{
                message = 'User disbled successfully';
            }
            // success
            res.json({message: message, data: user });
        });
    });
};

// ENDPOINT: /users/:id METHOD: DELETE
exports.deleteUser = function(req, res){
    User.findByIdAndRemove(req.params.id, function(err){
        // Check for errors and show message
        if(err){
            logger.error(err);
            res.send(err);
            return;
        }
        // success
        res.json({ message: 'User deleted successfully!' });
    });
};
{'fraction_non_alphanumeric': 0.12382478632478633, 'fraction_numerical': 0.004487179487179487, 'mean_word_length': 1.7259755387303437, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4113332', 'n_tokens_mistral': 2645, 'n_tokens_neox': 2534, 'n_words': 720}