baucis-decorator-upgrade
v1.0.3
Published
Adds `level` upgrade/downgrade functionality specific to users.
Downloads
12
Maintainers
Readme
baucis-decorator-upgrade
Adds level
upgrade/downgrade functionality specific to users.
Install
npm install baucis-decorators baucis-decorator-upgrade --save
Usage
This is a WIP, but if you want to try it out, add the decorator with your stripeSecret
and add the level
, stripeCustomerId
, and stripeSubscriptionId
properties as shown below.
Example
controllers/User.js
var baucis = require('baucis');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var extend = require('deep-extend');
var ResourceProps = require('../props/Resource.js');
var ResourceController = require('../controllers/Resource.js');
var UserProps = extend({}, ResourceProps);
UserProps.level = {
type: Number,
default: 0,
min: 0,
max: 9001,
reserved: true
};
UserProps.stripeCustomerId = {
type: String,
reserved: true,
select: false
};
UserProps.stripeSubscriptionId = {
type: String,
reserved: true,
select: false
};
var UserSchema = new Schema(UserProps);
var UserModel = mongoose.model('User', UserSchema);
var UserController = baucis.rest('User');
var decorators = require('baucis-decorators');
var upgrade = require('baucis-decorator-upgrade');
var stripeSecret = 'abc123';
// decorate controller
decorators.add.call(UserController, [
upgrade(stripeSecret), // allows `User` resources' `level` property to be upgraded using Stripe
ResourceController // `UserController` will inherit all of `ResourceController`'s decorators
]);
/**
* Expose controller.
*/
module.exports = UserController;
Then users can upgrade their account level
by POSTing the following parameters to /api/users/upgrade
:
{
"id": "user ID",
"name": "user name (if no ID)",
"stripeToken": "only required when creating subscription",
"plan": "'Private' (level 1) or 'Business' (level 2)",
"numUsers": "useful for organizations, defaults to 1"
}