       return res.status(500).send "Couldn't create the group."
 
     queue = [
+
       # add other parallel operations here
       -> createInvitations client, invitees, (err) ->
           console.error "Err while creating invitations", err  if err
           queue.fin()
+
     ]
+
     dash queue, (err)->
       # do not block group creation
       console.error "Error while creating group artifacts", body, err if err

    invitees
    # domains holds allowed domains for signinup without invitation, comma separated
    domains
    # newsletter holds announcements config.
    newsletter
    # is group creator already a member
    alreadyMember
    # username of the user, can be already registered or a new one for creation
    username
    email
  } = body

  { JUser, JGroup, JInvitation } = koding.models

module.exports = (req, res, next) ->

  { body }                       = req
  { # slug is team slug, unique name. Can not be changed
    slug
    email
    # newsletter holds announcements config.
    newsletter
    # is group creator already a member
    alreadyMember }              = body
  { JUser, JGroup, JInvitation } = koding.models

  clientId                       = getClientId req, res

  return handleClientIdNotFound res, req  unless clientId

  client              = {}
  context             = { group: slug }
  clientIPAddress     = req.headers['x-forwarded-for'] || req.connection.remoteAddress
  # parsing booling from string
  alreadyMember       = alreadyMember is 'true'
  # this function will be bound with necessary data in the queue later
  handleGroupCreation = ->

  queue = [

    ->
      koding.fetchClient clientId, context, (client_) ->

        client = client_
        # when there is an error in the fetchClient, it returns message in it
        if client.message
          console.error JSON.stringify {req, client}
          return res.status(500).send client.message

        client.clientIP = (clientIPAddress.split ',')[0]

        # subscribe to koding marketing mailings or not
        body.emailFrequency         or= {}
        # convert string boolean to boolean
        body.emailFrequency.marketing = newsletter is 'true'
        queue.next()

    ->
      # checking if group slug was already used
      JGroup.one { slug }, (err, group) ->
        return res.status(500).send 'an error occured'  if err
        return res.status(403).send "Sorry,
          Team URL '#{slug}.koding.com' is already in use"  if group
        queue.next()

    ->
      # checking if user exists by email
      JUser.one { email }, (err, user) ->
        return res.status(500).send 'an error occured'  if err
        alreadyMember = true  if user
        queue.next()

    ->
      # binding createGroup function to be used as callback in both login and convert
      handleGroupCreation = createGroup.bind null, client, req, res, body
      queue.next()

    ->
      if alreadyMember
      then JUser.login client.sessionToken, body, handleGroupCreation
      else JUser.convert client, body, handleGroupCreation

  ]

  daisy queue


createGroup = (client, req, res, body, err, result) ->

  return res.status(400).send getErrorMessage err  if err?

