<commit_before>'use strict';

let Response = require('../../tools/response');
let mongoose = require('mongoose');
let async = require('async');

module.exports = function (teamSchema) {

    /* Controllers methods */

    teamSchema.statics.create = function (params, callback) {
        let Self = this;

        console.log(params);

        let team = new Self(params);

        team.save(callback);
    };

    teamSchema.statics.edit = function (params, callback) {
        let Self = this;

        async.waterfall([
            (next) => Self.findById(params._id, next),
            (team, next) => {
                if (!team) {
                    next('Team not found');
                }

                if (team.members.leader.equals(params.leaderId)) {
                    Self.update({_id: params._id}, {$set: params}, next);
                } else {
                    next('Logged user isn\'t the leader of the team');
                }
            }
        ], (err, team) => {
            if (err) {
                return callback(err);
            }

            callback(null, team);
        });
    };

    teamSchema.statics.delete = function (params, callback) {
        let Self = this;

        async.waterfall([
            (next) => Self.findById(params._id, next),
            (team, next) => {
                if (!team) {
                    next('Team not found');
                }

                if (team.members.leader.equals(params.leaderId)) {
                    mongoose.model('Team').remove({_id: params._id}, next);
                } else {
                    next('Logged user isn\'t the leader of the team');
                }
            }
        ], (err, team) => {
            if (err) {
                return callback(err);
            }

            callback(null, team);
        });
    };

    /* Express methods verifications */

    function checkParametersExistsForCreate(req, res, callback) {
        if (!req.body || !req.body.name) {
            Response.missing(res, 'name', -11);
        } else if (!req.body.email) {
            Response.missing(res, 'email', -12);
        } else {
            return callback();
        }

        callback({alreadySent: true});
    }

    function checkParametersExistsForEdit(req, res, callback) {
        if (!req.body || !req.body._id) {
            Response.missing(res, '_id', -11);
            return callback({alreadySent: true});
        }

        if (req.body.members){
            req.body.members = undefined;
        }

        if (req.body.applications){
            req.body.applications = undefined;
        }

        if (req.body.data){
            req.body.data = undefined;
        }

        callback();
    }

    function checkParametersExistsForDelete(req, res, callback) {
        if (!req.body || !req.body._id) {
            Response.missing(res, '_id', -11);
        } else {
            return callback();
        }

        callback({alreadySent: true});
    }

    /* Express calls */

    teamSchema.statics.exCreate = function (req, res) {
        if (!req.isLogged()) {
            return Response.notAllowed(res);
        }

        async.waterfall([
            (next) => checkParametersExistsForCreate(req, res, next),
            (next) => {
                if (!req.body.members) {
                    req.body.members = {};
                }

                req.body.members.leader = req.user._id;
                mongoose.model('Team').create(req.body, next);
            }
        ], (err, team) => {
            if (err && err.alreadySent) {
                return;
            }

            if (err && err.code === Response.MongoCodes.alreadyExist) {
                if (err.message.indexOf('email') > -1) {
                    return Response.alreadyExist(res, 'email');
                } else {
                    return Response.alreadyExist(res, 'name');
                }
            } else if (err) {
                return Response.insertError(res, err);
            }

            return Response.success(res, 'Team added', team);
        });
    };

    teamSchema.statics.exEdit = function (req, res) {
        if (!req.isLogged()) {
            return Response.notAllowed(res);
        }

        async.waterfall([
            (next) => checkParametersExistsForEdit(req, res, next),
            (next) => {
                req.body.leaderId = req.user._id;
                mongoose.model('Team').edit(req.body, next);<commit_msg>fix(server): Add team verification in POST /team<commit_after>'use strict';

let Response = require('../../tools/response');
let mongoose = require('mongoose');
let async = require('async');

module.exports = function (teamSchema) {

    /* Controllers methods */

    teamSchema.statics.create = function (params, callback) {
        let Self = this;

        console.log(params);

        let team = new Self(params);

        team.save(callback);
    };

    teamSchema.statics.edit = function (params, callback) {
        let Self = this;

        async.waterfall([
            (next) => Self.findById(params._id, next),
            (team, next) => {
                if (!team) {
                    next('Team not found');
                }

                if (team.members.leader.equals(params.leaderId)) {
                    Self.update({_id: params._id}, {$set: params}, next);
                } else {
                    next('Logged user isn\'t the leader of the team');
                }
            }
        ], (err, team) => {
            if (err) {
                return callback(err);
            }

            callback(null, team);
        });
    };

    teamSchema.statics.delete = function (params, callback) {
        let Self = this;

        async.waterfall([
            (next) => Self.findById(params._id, next),
            (team, next) => {
                if (!team) {
                    next('Team not found');
                }

                if (team.members.leader.equals(params.leaderId)) {
                    mongoose.model('Team').remove({_id: params._id}, next);
                } else {
                    next('Logged user isn\'t the leader of the team');
                }
            }
        ], (err, team) => {
            if (err) {
                return callback(err);
            }

            callback(null, team);
        });
    };

    teamSchema.statics.findTeamForUser = function (params, callback) {
        mongoose.model('Team').findOne({
            $or: [{
                'members.list': params.user
            }, {
                'members.leader': params.user
            }]
        }, callback);
    };

    /* Express methods verifications */

    function checkParametersExistsForCreate(req, res, callback) {
        if (!req.body || !req.body.name) {
            Response.missing(res, 'name', -11);
        } else if (!req.body.email) {
            Response.missing(res, 'email', -12);
        } else {
            return callback();
        }

        callback({alreadySent: true});
    }

    function checkParametersExistsForEdit(req, res, callback) {
        if (!req.body || !req.body._id) {
            Response.missing(res, '_id', -11);
            return callback({alreadySent: true});
        }

        if (req.body.members) {
            req.body.members = undefined;
        }

        if (req.body.applications) {
            req.body.applications = undefined;
        }

        if (req.body.data) {
            req.body.data = undefined;
        }

        callback();
    }

    function checkParametersExistsForDelete(req, res, callback) {
        if (!req.body || !req.body._id) {
            Response.missing(res, '_id', -11);
        } else {
            return callback();
        }

        callback({alreadySent: true});
    }

    /* Express calls */

    teamSchema.statics.exCreate = function (req, res) {
        if (!req.isLogged()) {
            return Response.notAllowed(res);
        }

        async.waterfall([
            (next) => checkParametersExistsForCreate(req, res, next),
            // Check if user already have team
            (next) => mongoose.model('Team').findTeamForUser({user: req.user._id}, next),
            (team, next) => {
                if (!team || !team._id){
                    return next();
                }

                next('User already have team');
            },
            // Create team
            (next) => {
                if (!req.body.members) {
                    req.body.members = {};
                }

                req.body.members.leader = req.user._id;
                mongoose.model('Team').create(req.body, next);
            },
            // Add team to user
            (team, entryCreated, next) => mongoose.model('User').update(
                {_id: req.user._id},
                {team: team._id},
                (err) => {

                    console.log(team, next);
                    if (err) {
                        return next(err);
                    }

                    next(null, team);
                })
        ], (err, team) => {
            if (err && err.alreadySent) {
                return;
            }

            if (err && err.code === Response.MongoCodes.alreadyExist) {
                if (err.message.indexOf('email') > -1) {
                    return Response.alreadyExist(res, 'email');
                } else {
                    return Response.alreadyExist(res, 'name');
                }
            } else if (err) {
                return Response.insertError(res, err);
            }

            return Response.success(res, 'Team added', team);
        });
    };

    teamSchema.statics.exEdit = function (req, res) {
        if (!req.isLogged()) {
            return Response.notAllowed(res);
        }

        async.waterfall([
            (next) => checkParametersExistsForEdit(req, res, next),
            (next) => {
                req.body.leaderId = req.user._id;
                mongoose.model('Team').edit(req.body, next);
{'fraction_non_alphanumeric': 0.13684733963202386, 'fraction_numerical': 0.0017901541521631028, 'mean_word_length': 1.3650047036688617, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 31, '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': '397803', 'n_tokens_mistral': 2808, 'n_tokens_neox': 2648, 'n_words': 556}