<commit_before>JBundle = require '../bundle'

module.exports = class JGroupBundle extends JBundle

  KodingError = require '../../error'

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

  JRecurlyPlan         = require '../recurly'
  JRecurlySubscription = require '../recurly/subscription'
  JVM                  = require '../vm'
  async                = require 'async'

  @share()

  @trait __dirname, '../../traits/protected'

  @set
    sharedEvents      :
      static          : []
      instance        : []
    sharedMethods     :
      static          : []
      instance        : []
    permissions       :
      'manage payment methods'  : []
      'change bundle'           : []
      'request bundle change'   : ['member','moderator']
      'commission resources'    : ['member','moderator']
    schema            :
      overagePolicy   :
        type          : String
        enum          : [
          'unknown value for overage'
          ['allowed', 'by permission', 'not allowed']
        ]
        default       : 'not allowed'
      sharedVM        :
        type          : Boolean
        default       : no
      paymentPlan     :
        type          : String
        default       : ""
      allocation      :
        type          : Number
        default       : 0

  canCreateVM: (account, group, data, callback)->
    {type, planCode} = data

    if type is 'user'
      planOwner = "user_#{account._id}"
    else if type is 'group'
      planOwner = "group_#{group._id}"

    JRecurlySubscription.getSubscriptionsAll planOwner,
      userCode: planOwner
      planCode: planCode
      $or: [
        {status: 'active'}
        {status: 'canceled'}
      ]
    , (err, subs)=>
      return callback new KodingError "Payment backend error: #{err}"  if err

      paidVMs     = 0
      subs.forEach (sub)->
        paidVMs = sub.quantity

      createdVMs = 0
      JVM.someData
        planOwner: planOwner
        planCode : planCode
      ,
        name     : 1
      , {}, (err, cursor)=>
        cursor.toArray (err, arr)=>
          return callback err  if err
          arr.forEach (vm)->
            createdVMs += 1

          firstVM = group.slug is 'koding' and createdVMs == 0 and planCode is 'free'<commit_msg>no need to loop the arr and incr<commit_after>JBundle = require '../bundle'

module.exports = class JGroupBundle extends JBundle

  KodingError = require '../../error'

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

  JRecurlyPlan         = require '../recurly'
  JRecurlySubscription = require '../recurly/subscription'
  JVM                  = require '../vm'
  async                = require 'async'

  @share()

  @trait __dirname, '../../traits/protected'

  @set
    sharedEvents      :
      static          : []
      instance        : []
    sharedMethods     :
      static          : []
      instance        : []
    permissions       :
      'manage payment methods'  : []
      'change bundle'           : []
      'request bundle change'   : ['member','moderator']
      'commission resources'    : ['member','moderator']
    schema            :
      overagePolicy   :
        type          : String
        enum          : [
          'unknown value for overage'
          ['allowed', 'by permission', 'not allowed']
        ]
        default       : 'not allowed'
      sharedVM        :
        type          : Boolean
        default       : no
      paymentPlan     :
        type          : String
        default       : ""
      allocation      :
        type          : Number
        default       : 0

  canCreateVM: (account, group, data, callback)->
    {type, planCode} = data

    if type is 'user'
      planOwner = "user_#{account._id}"
    else if type is 'group'
      planOwner = "group_#{group._id}"

    JRecurlySubscription.getSubscriptionsAll planOwner,
      userCode: planOwner
      planCode: planCode
      $or: [
        {status: 'active'}
        {status: 'canceled'}
      ]
    , (err, subs)=>
      return callback new KodingError "Payment backend error: #{err}"  if err

      paidVMs     = 0
      subs.forEach (sub)->
        paidVMs = sub.quantity

      createdVMs = 0
      JVM.someData
        planOwner: planOwner
        planCode : planCode
      ,
        name     : 1
      , {}, (err, cursor)=>
        cursor.toArray (err, arr)=>
          return callback err  if err

          createdVMs = arr.length
          firstVM    = group.slug is 'koding' and createdVMs == 0 and planCode is 'free'
{'fraction_non_alphanumeric': 0.10849789216773907, 'fraction_numerical': 0.002440647881073885, 'mean_word_length': 1.5269058295964126, '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': '6985089', 'n_tokens_mistral': 1271, 'n_tokens_neox': 1253, 'n_words': 334}