mirror of
https://github.com/HackPlan/RootPanel.git
synced 2026-04-30 04:25:49 +08:00
billing_trigger.time
This commit is contained in:
@@ -7,5 +7,3 @@ billing = exports
|
||||
billing.start = ->
|
||||
|
||||
billing.runTimeBilling = ->
|
||||
|
||||
billing.triggerTimeBilling = ->
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
{_} = app.libs
|
||||
{config, logger} = app
|
||||
{Account, Financials} = app.models
|
||||
|
||||
process.nextTick ->
|
||||
{Account, Financials} = app.models
|
||||
|
||||
{available_plugins} = config.plugin
|
||||
|
||||
@@ -29,5 +33,69 @@ module.exports = class Plan
|
||||
unless _.isArray info.default
|
||||
info.default = [info.default]
|
||||
|
||||
# @param callback(err, account)
|
||||
triggerBilling: (account, callback) ->
|
||||
callback()
|
||||
unless account.inPlan @name
|
||||
return callback null, callback
|
||||
|
||||
if @billing_trigger.time
|
||||
@triggerTimeBilling account, (err, account) ->
|
||||
if account.balance < 0 and !account.arrears_at
|
||||
Account.findByIdAndUpdate account._id,
|
||||
$set:
|
||||
arrears_at: new Date()
|
||||
, callback
|
||||
else if account.balance > 0 and account.arrears_at
|
||||
Account.findByIdAndUpdate account._id,
|
||||
$set:
|
||||
arrears_at: null
|
||||
, callback
|
||||
else
|
||||
callback null, callback
|
||||
else
|
||||
callback null, callback
|
||||
|
||||
# @param callback(err, account)
|
||||
triggerTimeBilling: (account, callback) ->
|
||||
{interval, price, prepaid} = @billing_trigger.time
|
||||
{expired_at} = account.plans[@name].billing_state.time
|
||||
|
||||
if prepaid
|
||||
expect_paid = new Date Date.now() + interval
|
||||
else
|
||||
expect_paid = new Date()
|
||||
|
||||
console.log expired_at, expect_paid
|
||||
|
||||
unless expired_at < expect_paid
|
||||
return callback null, account
|
||||
|
||||
billing_time_range = expect_paid.getTime() - expired_at.getTime()
|
||||
billing_unit_count = Math.floor billing_time_range / interval
|
||||
|
||||
unless billing_unit_count > 0
|
||||
return callback null, account
|
||||
|
||||
new_expired_at = new Date expired_at.getTime() + billing_unit_count * interval
|
||||
amount = billing_unit_count * price
|
||||
|
||||
modifier =
|
||||
$set: {}
|
||||
$inc:
|
||||
balance: -amount
|
||||
|
||||
modifier.$set["plans.#{@name}.billing_state.time.expired_at"] = new_expired_at
|
||||
|
||||
Account.findByIdAndUpdate account._id, modifier, (err, account) =>
|
||||
Financials.create
|
||||
account_id: account._id
|
||||
type: 'billing'
|
||||
amount: -amount
|
||||
payload:
|
||||
plan_name: @name
|
||||
billing_trigger: 'time'
|
||||
billing_unit_count: billing_unit_count
|
||||
expired_at: new_expired_at
|
||||
amount_inc: -amount
|
||||
, (err) ->
|
||||
callback err, account
|
||||
|
||||
@@ -72,13 +72,9 @@ Account = mongoose.Schema
|
||||
preferences:
|
||||
type: Object
|
||||
|
||||
plans: [
|
||||
name:
|
||||
type: String
|
||||
|
||||
billing_state:
|
||||
type: Object
|
||||
]
|
||||
plans:
|
||||
type: Object
|
||||
default: {}
|
||||
|
||||
balance:
|
||||
type: Number
|
||||
@@ -240,7 +236,7 @@ Account.methods.inGroup = (group) ->
|
||||
return group in @groups
|
||||
|
||||
Account.methods.inPlan = (plan_name) ->
|
||||
return plan_name in _.pluck(@plans, 'name')
|
||||
return plan_name in _.keys @plans
|
||||
|
||||
Account.methods.createSecurityLog = (type, token, payload, callback) ->
|
||||
SecurityLog.create
|
||||
@@ -251,7 +247,7 @@ Account.methods.createSecurityLog = (type, token, payload, callback) ->
|
||||
, callback
|
||||
|
||||
Account.methods.getComponents = (type, callback) ->
|
||||
if _.isArray
|
||||
if _.isArray type
|
||||
query =
|
||||
type:
|
||||
$in: type
|
||||
@@ -265,23 +261,23 @@ Account.methods.getComponents = (type, callback) ->
|
||||
callback components
|
||||
|
||||
Account.methods.getAvailableComponentsTypes = ->
|
||||
return _.compact _.map _.pluck(@plans, 'name'), (plan_name) ->
|
||||
return _.compact _.map _.keys(@plans), (plan_name) ->
|
||||
return _.keys Plan.get(plan_name).available_components
|
||||
|
||||
# callback(err)
|
||||
Account.methods.joinPlan = (plan_name, callback) ->
|
||||
plan = Plan.get plan_name
|
||||
|
||||
plan.triggerBilling @, =>
|
||||
app.models.Account.findByIdAndUpdate @_id,
|
||||
$push:
|
||||
plans:
|
||||
name: plan.name
|
||||
billing_state:
|
||||
time:
|
||||
expired_at: new Date()
|
||||
modifier =
|
||||
$set: {}
|
||||
|
||||
, (err, account) ->
|
||||
modifier.$set["plans.#{plan.name}"] =
|
||||
billing_state:
|
||||
time:
|
||||
expired_at: new Date()
|
||||
|
||||
plan.triggerBilling @, =>
|
||||
app.models.Account.findByIdAndUpdate @_id, modifier, (err, account) ->
|
||||
async.each _.keys(plan.available_components), (component_type, callback) =>
|
||||
plan_component_info = plan.available_components[component_type]
|
||||
component_type = pluggable.components[component_type]
|
||||
@@ -304,16 +300,16 @@ Account.methods.joinPlan = (plan_name, callback) ->
|
||||
Account.methods.leavePlan = (plan_name, callback) ->
|
||||
plan = Plan.get plan_name
|
||||
|
||||
plan.triggerBilling @, =>
|
||||
app.models.Account.findByIdAndUpdate @_id,
|
||||
$pull:
|
||||
plans:
|
||||
name: plan.name
|
||||
modifier =
|
||||
$unset: {}
|
||||
|
||||
, (err, account) ->
|
||||
modifier.$unset["plans.#{plan.name}"] = true
|
||||
|
||||
plan.triggerBilling @, =>
|
||||
app.models.Account.findByIdAndUpdate @_id, modifier, (err, account) ->
|
||||
available_component_types = account.getAvailableComponentsTypes()
|
||||
|
||||
@getComponents null, (components) ->
|
||||
account.getComponents null, (components) ->
|
||||
async.each components, (component, callback) ->
|
||||
if component.component_type in available_component_types
|
||||
return callback()
|
||||
|
||||
@@ -34,7 +34,7 @@ module.exports =
|
||||
available_components: {}
|
||||
resource_limit: {}
|
||||
|
||||
billing:
|
||||
billing_trigger:
|
||||
time:
|
||||
interval: 24 * 3600 * 1000
|
||||
price: 10 / 30
|
||||
|
||||
@@ -52,7 +52,7 @@ module.exports =
|
||||
transfer: 39
|
||||
memory: 27
|
||||
|
||||
billing:
|
||||
billing_trigger:
|
||||
time:
|
||||
interval: 24 * 3600 * 1000
|
||||
price: 10 / 30
|
||||
|
||||
@@ -41,7 +41,7 @@ module.exports =
|
||||
|
||||
resource_limit: {}
|
||||
|
||||
billing:
|
||||
billing_trigger:
|
||||
'shadowsocks.traffic':
|
||||
bucket: 100 * 1000 * 1000
|
||||
price: 0.06
|
||||
|
||||
Reference in New Issue
Block a user