加入和离开套餐时计算 resources_limit

This commit is contained in:
jysperm
2014-05-24 20:26:04 +08:00
parent eaab296787
commit 4dc8143892
2 changed files with 16 additions and 0 deletions

View File

@@ -46,3 +46,13 @@ exports.calcRemainingTime = (account) ->
price += plan.price / 30 / 24
return account.attribute.balance / price
exports.calcResourcesLimit = (plans) ->
limit = {}
for plan in plans
for k, v of config.plans[plan].resources
limit[k] = 0 unless limit[k]
limit[k] += v
return limit

View File

@@ -3,6 +3,7 @@ crypto = require 'crypto'
auth = require '../auth'
db = require '../db'
billing = require '../billing'
cAccount = db.collection 'accounts'
@@ -68,6 +69,7 @@ exports.register = (username, email, passwd, callback = null) ->
balance: 0
last_billing: new Date()
arrears_at: null
resources_limit: []
tokens: []
, {}, callback
@@ -140,15 +142,19 @@ exports.inGroup = (account, group) ->
return group in account.group
exports.joinPlan = (account, plan, callback) ->
account.attribute.plans.push plan
exports.update _id: account._id,
$addToSet:
'attribute.plans': plan
'attribute.resources_limit': billing.calcResourcesLimit account.attribute.plans
, {}, callback
exports.leavePlan = (account, plan, callback) ->
account.attribute.plans = _.reject account.attribute.plans, (i) -> i == plan
exports.update _id: account._id,
$pull:
'attribute.plans': plan
'attribute.resources_limit': billing.calcResourcesLimit account.attribute.plans
, {}, callback
exports.incBalance = (account, amount, callback) ->