<commit_before>'use strict';

/**
 * Module dependencies.
 */
var mongoose = require('mongoose'),
	errorHandler = require('./errors'),
    Client = mongoose.model('Client'),
	_ = require('lodash'),
    appender = require('../services/ges/ges-appender.js'),
    gesEvent = require('../services/ges/eventData.js'),
    uuid = require('node-uuid'),
    that = this;


that.createCommand = function(vent, cmdName, cb){
    var metadata = {
        'CommitId':uuid.v1(),
        'CommandTypeName':cmdName
    };
    appender('CommandDispatch', new gesEvent(uuid.v1(), metadata.CommandTypeName, true, vent, metadata),cb);
};

/**
 * Create a Client
 */
exports.create = function(req, res) {
    console.log(req.body.item)
	var client = new Client(req.body.item);
    Client.findOne({ 'EmailAddress': client.EmailAddress }).exec(function (err, _client) {
        var msg = err? err : {errors:[{'message':'Client with email: '+client.EmailAddress+' already exists.'}]};
        if (err || _client) {
            return res.status(400).send({
                message: errorHandler.getErrorMessage(msg)
            });
        }
    });
    var _event = {
        Contact: {  FirstName: client.FirstName,
            LastName: client.LastName,
            EmailAddress: client.EmailAddress,
            PhoneMobile: client.PhoneMobile,
            PhoneSecondary: client.PhoneSecondary
        },
        TrainerId: client.TrainerId,
        SourceNotes: client.SourceNotes,
        StartDate: client.StartDate
    };

    var cb = function(err, body) {
        if (err) {
            return res.status(400).send({
                message: errorHandler.getErrorMessage(err)
            });
        } else {
            res.jsonp(body);
        }
    };

    that.createCommand(_event,req.body.cmdName, cb);

};

/**
 * Show the current Client
 */
exports.read = function(req, res) {
console.log(req.client);
	res.jsonp(req.client);
};

/**
 * Update a Client
 */
exports.update = function(req, res, next) {
    var clientId = req.params.clientId;
    var cmdName = req.params.cmdName;
	var client = req.client;
    that[cmdName](req,req.body);
//    client = _.extend(client , req.body);
//
//	client.save(function(err) {
//		if (err) {
//			return res.status(400).send({
//				message: errorHandler.getErrorMessage(err)
//			});
//		} else {
//			res.jsonp(client);
//		}
//	});
};

that.correctClientsName = function(req,client){
    var _event = {
        Contact: {  FirstName: client.FirstName,
            LastName: client.LastName
        }
    };
    that.createCommand(req,_event,'CorrectClientName');
//    var metadata = {
//        'CommitId':uuid.v1(),
//        'CommandTypeName':req.body.cmdName
//    };
//    var _event = {
//        Contact: {  FirstName: client.FirstName,
//            LastName: client.LastName
//        }
//    };
//
//    var cb = function(err, body) {
//        if (err) {
//            return res.status(400).send({
//                message: errorHandler.getErrorMessage(err)
//            });
//        } else {
//            res.jsonp(body);
//        }
//    };
//    appender('CommandDispatch', new gesEvent(uuid.v1(), metadata.CommandTypeName, true, _event, metadata),cb);
}

/**
 * Delete an Client
 */
exports.delete = function(req, res) {
	var client = req.client ;

	client.remove(function(err) {
		if (err) {
			return res.status(400).send({
				message: errorHandler.getErrorMessage(err)
			});
		} else {
			res.jsonp(client);
		}
	});
};

/**
 * Client middleware
 */
exports.clientByID = function(req, res, next, id) { Client.findById(id).populate('user', 'displayName').exec(function(err, client) {
    if (err) return next(err);
		if (! client) return next(new Error('Failed to load Client ' + id));
		req.client = client ;
		next();
	});
};<commit_msg>working on client.server.controller<commit_after>'use strict';

/**
 * Module dependencies.
 */
var mongoose = require('mongoose'),
	errorHandler = require('./errors'),
    Client = mongoose.model('Client'),
	_ = require('lodash'),
    appender = require('../services/ges/ges-appender.js'),
    gesEvent = require('../services/ges/eventData.js'),
    uuid = require('node-uuid'),
    that = this;


that.createCommand = function(res, vent, cmdName){
    var metadata = {
        'CommitId':uuid.v1(),
        'CommandTypeName':cmdName
    };
    var cb = function(err, body) {
        if (err) {
            return res.status(400).send({
                message: errorHandler.getErrorMessage(err)
            });
        } else {
            res.jsonp(body);
        }
    };
    appender('CommandDispatch', new gesEvent(uuid.v1(), metadata.CommandTypeName, true, vent, metadata),cb);
};

/**
 * Create a Client
 */
exports.create = function(req, res) {
    console.log(req.body.item)
	var client = new Client(req.body.item);
    Client.findOne({ 'EmailAddress': client.EmailAddress }).exec(function (err, _client) {
        var msg = err? err : {errors:[{'message':'Client with email: '+client.EmailAddress+' already exists.'}]};
        if (err || _client) {
            return res.status(400).send({
                message: errorHandler.getErrorMessage(msg)
            });
        }
    });
    var _event = {
        Contact: {  FirstName: client.FirstName,
            LastName: client.LastName,
            EmailAddress: client.EmailAddress,
            PhoneMobile: client.PhoneMobile,
            PhoneSecondary: client.PhoneSecondary
        },
        TrainerId: client.TrainerId,
        SourceNotes: client.SourceNotes,
        StartDate: client.StartDate
    };

    that.createCommand(res, _event,req.body.cmdName);

};

/**
 * Show the current Client
 */
exports.read = function(req, res) {
console.log(req.client);
	res.jsonp(req.client);
};

/**
 * Update a Client
 */
exports.update = function(req, res, next) {
    var clientId = req.params.clientId;
    var cmdName = req.params.cmdName;
	var client = req.body.item;
    console.log("params log");
    _.forEach(req.body,function(i){
        console.log(i+' value:'+req.body[i]);
    })
    that[cmdName](req, res, client);
};

that.correctClientsName = function(req,res, client){
    console.log("client log");
    console.log(client);
    _.forEach(Object.keys(client),function(i){
        console.log(i+' value:'+client[i]);
    })
    var _event = {
        Contact: {  FirstName: client.Contact.FirstName,
                    LastName: client.Contact.LastName
        }
    };
    that.createCommand(res,_event,'CorrectClientName');
}

/**
 * Delete an Client
 */
exports.delete = function(req, res) {
	var client = req.client ;

	client.remove(function(err) {
		if (err) {
			return res.status(400).send({
				message: errorHandler.getErrorMessage(err)
			});
		} else {
			res.jsonp(client);
		}
	});
};

/**
 * Client middleware
 */
exports.clientByID = function(req, res, next, id) { Client.findById(id).populate('user', 'displayName').exec(function(err, client) {
    if (err) return next(err);
		if (! client) return next(new Error('Failed to load Client ' + id));
		req.client = client ;
		next();
	});
};
{'fraction_non_alphanumeric': 0.16274150331406007, 'fraction_numerical': 0.004230714990833451, 'mean_word_length': 2.5620291310899046, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7649520', 'n_tokens_mistral': 2289, 'n_tokens_neox': 2219, 'n_words': 422}