<commit_before>'use strict';

const mongoose = require('mongoose');
const Promise = mongoose.Promise = require('bluebird');
const eachAsync = require('each-async');
const log = require('../logger');

const db = require('./common');

const exp = {};

module.exports = exp;

const User = mongoose.models.User;
const Team = mongoose.models.Team;
const TeamUser = mongoose.models.TeamUser;
const TeamEvent = mongoose.models.TeamEvent;
const Program = mongoose.models.Program;
const Event = mongoose.models.Event;

exp.getTeamDetails = function(user, teamId) {
    return new Promise(async function (fulfill, reject) {
        console.log("GET-TEAM-DETAILS teamId=",teamId);
        try {
            const p = await exp.teamPermissions(user, teamId);
            const t = await Team.findById(teamId,{},{lean:true});
            if (!t) {
                console.log('Team not found id=',teamId);
                return reject(null);
            }
            t.id = ''+ t._id;
            const pr = await Program.findOneActive({_id:t.programId});
            if (pr)
                t.programName = pr.name;
            else
                t.programName = "-- undefined --";

            t.events = [];
            const evList = await TeamEvent.find({teamId:t.id});
            const evDetails = await Event.populate(evList,'eventId');
            evDetails.forEach(e => t.events.push({_id:e.eventId._id, name:e.eventId.name, programId:e.programId, registeredOn:e.registeredOn}));

            // apply permissions
            switch (p){
                case 'P1':
                    t.billingContact = {};
                    t.shippingContact = {};
                    break;
                case 'X':
                    t.billingContact = {};
                    t.shippingContact = {};
                    t.billingAdr = {};
                    t.shippingAdr = {};
                    break;
            }

            return fulfill(t);

        } catch (err) {
            log.WARN('Error getting teamDetails. err='+err.message);
            return reject(err);
        }

    });
};

exp.getTeamCoaches = function(user, teamId) {
    return new Promise(async function (fulfill, reject) {
        console.log("GET-TEAM-COACHES");
        const coaches = [];
        try {
            const p = await exp.teamPermissions(user, teamId);
            const t = await Team.findById(teamId);
            if (!t) {
                console.log('Team not found id=',teamId);
                return reject(null);
            }
            console.log("Found team: ", t.name, t.id);
            let tc = [];
            if (p != 'R1')
                tc = await TeamUser.find({teamId: teamId, role:'coach'});
            console.log('TEAM coaches',tc.length);

            eachAsync(
                tc,
                async function(item, index, done) {
                    console.log("Searching coach ",item.userId);
                    let u = await User.findOneActive({_id: item.userId});
                    let ud = {};
                    switch (p) {
                        case 'A':
                        case 'W':
                        case 'R':
                            if (u.dateOfBirth) ud.dateOfBirth = u.dateOfBirth;
                        case 'R2':
                            ud.fullName = u.fullName;
                            ud._id = u._id;
                            break;
                    }
                    coaches.push(ud);
                    done();
                },
                function(err) {
                    if (err) return reject(err);
                    console.log("Coaches loaded");
                    return fulfill(coaches);
                }
            )

        } catch (err) {
            log.WARN('Error getting team coaches. err='+err.message);
            return reject(err);
        }

    });
};

exp.getTeamMembers = function(user, teamId) {
    return new Promise(async function (fulfill, reject) {
        console.log("GET-TEAM-MEMBERS");
        const members = [];
        try {
            const p = await exp.teamPermissions(user, teamId);
            const t = await Team.findById(teamId);
            if (!t) {
                console.log('Team not found id=',teamId);
                return reject(null);
            }
            console.log("Found team: ", t.name, t.id);
            let tm = [];
            if (p != 'R1')
                tm = await TeamUser.find({teamId: teamId, role:'member'});
            console.log('Found members',tm.length);

            eachAsync(
                tm,
                async function(item, index, done) {
                    let u = await User.findOneActive({_id: item.userId});
                    let ud = {};
                    switch (p) {
                        case 'A':
                        case 'W':
                        case 'R':
                            if (u.dateOfBirth) ud.dateOfBirth = u.dateOfBirth;
                            if (u.email) ud.email = u.email;
                        case 'R2':
                            ud.fullName = u.fullName;
                            ud.id = u.id;
                            break;
                    }
                    members.push(ud);
                    return done();
                },
                function(err) {
                    if (err) return reject(err);
                    console.log("Members loaded");
                    return fulfill(members);
                }
            )

        } catch (err) {
            log.WARN('Error getting team members. err='+err.message);
            return reject(err);
        }

    });
};

exp.saveTeamDetails = function(user, teamId, doc) {
    return new Promise(async function (fulfill, reject) {
        console.log("SAVE-TEAM-DETAILS");

        try {
            const p = await exp.teamPermissions(user, teamId);
            const t = await Team.findOneActive({_id: teamId});
            if (!t) {
                console.log('Team not found id=',teamId);
                return reject(null);
            }<commit_msg>forgotten underscore & fixing #47<commit_after>'use strict';

const mongoose = require('mongoose');
const Promise = mongoose.Promise = require('bluebird');
const eachAsync = require('each-async');
const log = require('../logger');

const db = require('./common');

const exp = {};

module.exports = exp;

const User = mongoose.models.User;
const Team = mongoose.models.Team;
const TeamUser = mongoose.models.TeamUser;
const TeamEvent = mongoose.models.TeamEvent;
const Program = mongoose.models.Program;
const Event = mongoose.models.Event;

exp.getTeamDetails = function(user, teamId) {
    return new Promise(async function (fulfill, reject) {
        console.log("GET-TEAM-DETAILS teamId=",teamId);
        try {
            const p = await exp.teamPermissions(user, teamId);
            const t = await Team.findById(teamId,{},{lean:true});
            if (!t) {
                console.log('Team not found id=',teamId);
                return reject(null);
            }
            t.id = ''+ t._id;
            const pr = await Program.findOneActive({_id:t.programId});
            if (pr)
                t.programName = pr.name;
            else
                t.programName = "-- undefined --";

            t.events = [];
            const evList = await TeamEvent.find({teamId:t.id});
            const evDetails = await Event.populate(evList,'eventId');
            evDetails.forEach(e => t.events.push({_id:e.eventId._id, name:e.eventId.name, programId:e.programId, registeredOn:e.registeredOn}));

            // apply permissions
            switch (p){
                case 'P1':
                    t.billingContact = {};
                    t.shippingContact = {};
                    break;
                case 'X':
                    t.billingContact = {};
                    t.shippingContact = {};
                    t.billingAdr = {};
                    t.shippingAdr = {};
                    break;
            }

            return fulfill(t);

        } catch (err) {
            log.WARN('Error getting teamDetails. err='+err.message);
            return reject(err);
        }

    });
};

exp.getTeamCoaches = function(user, teamId) {
    return new Promise(async function (fulfill, reject) {
        console.log("GET-TEAM-COACHES");
        const coaches = [];
        try {
            const p = await exp.teamPermissions(user, teamId);
            const t = await Team.findById(teamId);
            if (!t) {
                console.log('Team not found id=',teamId);
                return reject(null);
            }
            console.log("Found team: ", t.name, t.id);
            let tc = [];
            if (p != 'R1')
                tc = await TeamUser.find({teamId: teamId, role:'coach'});
            console.log('TEAM coaches',tc.length);

            eachAsync(
                tc,
                async function(item, index, done) {
                    console.log("Searching coach ",item.userId);
                    let u = await User.findOneActive({_id: item.userId});
                    let ud = {};
                    switch (p) {
                        case 'A':
                        case 'W':
                        case 'R':
                            if (u.dateOfBirth) ud.dateOfBirth = u.dateOfBirth;
                        case 'R2':
                            ud.fullName = u.fullName;
                            ud._id = u._id;
                            break;
                    }
                    coaches.push(ud);
                    done();
                },
                function(err) {
                    if (err) return reject(err);
                    console.log("Coaches loaded");
                    return fulfill(coaches);
                }
            )

        } catch (err) {
            log.WARN('Error getting team coaches. err='+err.message);
            return reject(err);
        }

    });
};

exp.getTeamMembers = function(user, teamId) {
    return new Promise(async function (fulfill, reject) {
        console.log("GET-TEAM-MEMBERS");
        const members = [];
        try {
            const p = await exp.teamPermissions(user, teamId);
            const t = await Team.findById(teamId);
            if (!t) {
                console.log('Team not found id=',teamId);
                return reject(null);
            }
            console.log("Found team: ", t.name, t.id);
            let tm = [];
            if (p != 'R1')
                tm = await TeamUser.find({teamId: teamId, role:'member'});
            console.log('Found members',tm.length);

            eachAsync(
                tm,
                async function(item, index, done) {
                    let u = await User.findOneActive({_id: item.userId});
                    let ud = {};
                    switch (p) {
                        case 'A':
                        case 'W':
                        case 'R':
                            if (u.dateOfBirth) ud.dateOfBirth = u.dateOfBirth;
                            if (u.email) ud.email = u.email;
                        case 'R2':
                            ud.fullName = u.fullName;
                            ud._id = u._id;
                            break;
                    }
                    members.push(ud);
                    return done();
                },
                function(err) {
                    if (err) return reject(err);
                    console.log("Members loaded");
                    return fulfill(members);
                }
            )

        } catch (err) {
            log.WARN('Error getting team members. err='+err.message);
            return reject(err);
        }

    });
};

exp.saveTeamDetails = function(user, teamId, doc) {
    return new Promise(async function (fulfill, reject) {
        console.log("SAVE-TEAM-DETAILS");

        try {
            const p = await exp.teamPermissions(user, teamId);
            const t = await Team.findOneActive({_id: teamId});
            if (!t) {
                console.log('Team not found id=',teamId);
                return reject(null);
            }
{'fraction_non_alphanumeric': 0.12059642798623628, 'fraction_numerical': 0.0009831230542356219, 'mean_word_length': 1.2509680988382814, 'pattern_counts': {'":': 0, '<': 3, '<?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': '1594477', 'n_tokens_mistral': 3284, 'n_tokens_neox': 3171, 'n_words': 715}