<commit_before>'use strict';

const Boom = require('boom');
const Team = require('../../models/Team');
const Code = require('../../../../config/errorCodes');
const findUserInTeam = require('../../util/teamFunctions').findUserInTeam;
// TOOD: figure out a way to store a config admin can edit on the website
// NOTE: ex. (Max manager in a team: 2, Max backend developers in a team: 4, max time team can be inacive before deletion: 2W )

module.exports = (req, res) => {
  Team.findById(req.params.id, (err, team) => {
    if (req.Token.id === team.owner) {
      if (findUserInTeam(user, [team.manager, team.backend, team.frontend])) {
        res(Code.userInTeam);
      }else {
        switch (req.payload.role) {
          case 'manager':
            if (team.manager.lenght >= 1) {
              res(Code.maxUsersInRole);
            } else {
              team.manager.push({user: req.payload.user, role: req.payload.level});
            }
            break;
          case 'backend':
            if (team.backend.lenght >= 2) {
              res(Code.maxUsersInRole);
            }else {
              team.backend.push({user: req.payload.user, role: req.payload.level});
            }
            break;
          case 'frontend':
            if (team.frontend.lenght >= 2) {
              res(Code.maxUsersInRole);
            }else {
              team.frontend.push({user: req.payload.user, role: req.payload.level});
            }
            break;
          default:
            // This shouldn't happend
            console.log(req.payload);
        }
        team.meta.members.push({id: req.payload.user});
        res(team);
      }
    }
  });
};<commit_msg>Fix a bug where team didn't save after adding a new user<commit_after>'use strict';

const Boom = require('boom');
const Team = require('../../models/Team');
const Code = require('../../../../config/errorCodes');
const findUserInTeam = require('../../util/teamFunctions').findUserInTeam;
// TOOD: figure out a way to store a config admin can edit on the website
// NOTE: ex. (Max manager in a team: 2, Max backend developers in a team: 4, max time team can be inacive before deletion: 2W )

module.exports = (req, res) => {
  Team.findById(req.params.id, (err, team) => {
    if (req.Token.id == team.owner) {
      if (findUserInTeam(req.payload.user, [team.manager, team.backend, team.frontend])) {
        res(Code.userInTeam);
      }else {
        switch (req.payload.role) {
          case 'manager':
            if (team.manager.lenght >= 1) {
              res(Code.maxUsersInRole);
            } else {
              team.manager.push({user: req.payload.user, role: req.payload.level});
            }
            break;
          case 'backend':
            if (team.backend.lenght >= 2) {
              res(Code.maxUsersInRole);
            }else {
              team.backend.push({user: req.payload.user, role: req.payload.level});
            }
            break;
          case 'frontend':
            if (team.frontend.lenght >= 2) {
              res(Code.maxUsersInRole);
            }else {
              team.frontend.push({user: req.payload.user, role: req.payload.level});
            }
            break;
          default:
            // This shouldn't happend
            console.log(req.payload);
        }
        team.meta.members.push({id: req.payload.user});
        team.save((err, team) => {
          if (err) {
            console.log(err);
            res(Boom.badImplementation(err));
          }else {
            res(team);
          }
        });
      }
    }else {
      res(Code.notOwner);
    }
  });
};
{'fraction_non_alphanumeric': 0.148424543946932, 'fraction_numerical': 0.003316749585406302, 'mean_word_length': 1.7625954198473281, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 14, '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': '2969232', 'n_tokens_mistral': 1079, 'n_tokens_neox': 1063, 'n_words': 250}