From eb90b72d736ffbcc441e093ca3e36cee477f9734 Mon Sep 17 00:00:00 2001 From: jysperm Date: Wed, 14 May 2014 12:05:22 +0800 Subject: [PATCH] plan/unsubscribe --- DOC/API/plan.md | 4 ++++ core/api/plan.coffee | 27 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/DOC/API/plan.md b/DOC/API/plan.md index ad00f96..d41ef05 100644 --- a/DOC/API/plan.md +++ b/DOC/API/plan.md @@ -25,3 +25,7 @@ Request: } No Response. + +Exception: + +* not_in_plan diff --git a/core/api/plan.coffee b/core/api/plan.coffee index d656fa5..8be7a20 100644 --- a/core/api/plan.coffee +++ b/core/api/plan.coffee @@ -40,3 +40,30 @@ module.exports = return res.json {} unsubscribe: (req, res) -> + mAccount.authenticate req.token, (account) -> + unless account + return res.json 400, error: 'auth_failed' + + unless req.body.plan in account.attribute.plans + return res.json 400, error: 'not_in_plan' + + billing.calcBiling account, (account) -> + async.each config.plans[req.body.plan].service, (serviceName, callback) -> + stillInService = do -> + for item in account.attribute.plans + if serviceName in config.plans[req.body.plan].service + return true + + return false + + if stillInService + callback() + else + mAccount.uodate _id: account._id, + $pull: + 'attribute.service': serviceName + , {}, -> + (plugin.get serviceName).service.delete account, -> + callback() + , -> + return res.json {}