       subPage    : require '../../render/loggedout/subpage'
 
 
-  save_ = (label, model, queue, callback) ->
-    model.save (err) ->
-      return callback err  if err
-      console.log "#{label} is saved"
-      queue.next()
-
-
   @create = (client, groupData, owner, callback) ->
 
     # bongo doesnt set array values as their defaults


  { Relationship } = require 'jraphical'

  { Inflector, ObjectId, ObjectRef, secure, daisy, race, dash, signature } = require 'bongo'

  JPermissionSet = require './permissionset'
  { permit }     = JPermissionSet

  JAccount       = require '../account'

  KodingError    = require '../../error'
  Validators     = require './validators'
  { throttle, extend }     = require 'underscore'

  PERMISSION_EDIT_GROUPS = [

    defaultPermissionSet  = new JPermissionSet {}, { privacy: group.privacy }
    { sessionToken }      = client

    save_ = (label, model, callback) ->
      model.save (err) ->
        return callback err  if err
        console.log "#{label} is saved"
        callback null

    queue = [

      (next) ->
        group.useSlug group.slug, (err, slug) ->
          return next err  if err
          return next new KodingError 'Couldn\'t claim the slug!'  unless slug?

          console.log "created a slug #{slug.slug}"
          group.slug  = slug.slug
          group.slug_ = slug.slug
          next()

      (next) ->
        save_ 'group', group, (err) ->
          return next()  unless err
          JName.release group.slug, ->
            next err

      (next) ->
        selector = { clientId : sessionToken }
        params   = { $set : { groupName : group.slug } }

        JSession.update selector, params, next

      (next) ->
        group.addMember owner, next

      (next) ->
        group.addAdmin owner, next

      (next) ->
        group.addOwner owner, next

      (next) ->
        save_ 'default permission set', defaultPermissionSet, next

      (next) ->
        group.addDefaultPermissionSet defaultPermissionSet, next

      (next) ->
        group.addDefaultRoles next

      (next) ->
        group.createSocialApiChannels client, (err) ->
          console.error err  if err
          console.log 'created socialApiId ids'
          next()

      (next) ->
        { shareCredentials } = require '../computeproviders/teamutils'
        account = client.connection.delegate

        shareCredentials { group, account }, (err) ->
          console.log 'shared credentials', err
          next()

    ]

    if 'private' is group.privacy
      queue.push (next) ->
        group.createMembershipPolicy groupData.requestType, -> next()

    async.series queue, (err) =>
      return callback err  if err
      @emit 'GroupCreated', { group, creator: owner }
      callback null, group


  @create$ = secure (client, formData, callback) ->
