<commit_before>koding  = require './../bongo'
async   = require 'async'
Tracker = require '../../../../workers/social/lib/social/models/tracker.coffee'
{ protocol, hostname } = require 'koding-config-manager'
emailsanitize = require '../../../../workers/social/lib/social/models/user/emailsanitize'

module.exports = (req, res) ->

  UNKNOWN_USER_ERROR = 'User not found'

  { email } = req.body
  { JUser } = koding.models

  return res.status(400).send 'Invalid email!'  unless email

  queue = [
    (next) ->
      sanitizedEmail = emailsanitize email, { excludeDots: yes, excludePlus: yes }
      JUser.one { sanitizedEmail }, (err, user) ->
        return next err  if err
        return next UNKNOWN_USER_ERROR  unless user
        next null, user

    (user, next) ->
      user.fetchOwnAccount (err, account) ->
        return next err  if err
        next null, account

    (account, next) ->
      account.fetchRelativeGroups (err, groups) ->
        next err, account, groups

    (account, groups, next) ->
      groups = groups.filter (group) -> group.slug isnt 'koding'

      { profile : { nickname } } = account

      Tracker.identify nickname, { email }, (err) ->
        return next err  if err
        Tracker.track nickname, {
          to      : email
          subject : Tracker.types.REQUESTED_TEAM_LIST
        }, {
          teams         : groups.map (group) -> helper.createTeamItem group
          findTeamUrl   : "#{protocol}//#{hostname}/Teams/FindTeam"
          createTeamUrl : "#{protocol}//#{hostname}/Teams/Create"
        }, next
  ]

  async.waterfall queue, (err) ->
    if err and err isnt UNKNOWN_USER_ERROR
      return res.status(403).send err.message ? err
    res.status(200).end()<commit_msg>FindTeam handler: code cleanup<commit_after>koding  = require './../bongo'
async   = require 'async'
Tracker = require '../../../../workers/social/lib/social/models/tracker.coffee'
{ protocol, hostname } = require 'koding-config-manager'
emailsanitize = require '../../../../workers/social/lib/social/models/user/emailsanitize'

module.exports = (req, res) ->

  UNKNOWN_USER_ERROR = 'User not found'

  { email } = req.body
  { JUser } = koding.models

  return res.status(400).send 'Invalid email!'  unless email

  queue = [
    (next) ->
      sanitizedEmail = emailsanitize email, { excludeDots: yes, excludePlus: yes }
      JUser.one { sanitizedEmail }, (err, user) ->
        return next err  if err
        return next UNKNOWN_USER_ERROR  unless user
        next null, user

    (user, next) ->
      user.fetchOwnAccount (err, account) ->
        return next err  if err
        next null, account

    (account, next) ->
      account.fetchRelativeGroups (err, groups) ->
        next err, account, groups

    (account, groups, next) ->
      groups = groups.filter (group) -> group.slug isnt 'koding'

      { profile : { nickname } } = account

      Tracker.identifyAndTrack nickname, {
        to      : email
        subject : Tracker.types.REQUESTED_TEAM_LIST
      }, {
        teams         : groups.map (group) -> helper.createTeamItem group
        findTeamUrl   : "#{protocol}//#{hostname}/Teams/FindTeam"
        createTeamUrl : "#{protocol}//#{hostname}/Teams/Create"
      }, next
  ]

  async.waterfall queue, (err) ->
    if err and err isnt UNKNOWN_USER_ERROR
      return res.status(403).send err.message ? err
    res.status(200).end()
{'fraction_non_alphanumeric': 0.1332550631053713, 'fraction_numerical': 0.005283240387437629, 'mean_word_length': 2.4114114114114114, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 26, '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': '3006884', 'n_tokens_mistral': 1058, 'n_tokens_neox': 1029, 'n_words': 257}