Skip to content

Commit

Permalink
Merge branch 'impronunciable-feature-coupons'
Browse files Browse the repository at this point in the history
  • Loading branch information
pazguille committed Jul 20, 2015
2 parents 35f81e5 + 7db58a1 commit ab7e644
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 1 deletion.
1 change: 1 addition & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ You can also work with all the other resources authenticated with a secret API K
- [Queue](https://developers.getmango.com/en/api/queue/?platform=node)
- [Installments](https://developers.getmango.com/en/api/installments/?platform=node)
- [Promotions](https://developers.getmango.com/en/api/promotions/?platform=node)
- [Coupons](https://developers.getmango.com/en/api/coupons/?platform=node)

## Tests

Expand Down
2 changes: 2 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var Refunds = require('./resources/refunds');
var Queue = require('./resources/queue');
var Installments = require('./resources/installments');
var Promotions = require('./resources/promotions');
var Coupons = require('./resources/coupons');

/**
* Expose constructor
Expand Down Expand Up @@ -51,6 +52,7 @@ function Mango(options) {
this.Queue = new Queue(this);
this.Installments = new Installments(this);
this.Promotions = new Promotions(this);
this.Coupons = new Coupons(this);

debug('Client intialized');
}
72 changes: 72 additions & 0 deletions lib/resources/coupons.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@

/**
* Module dependencies
*/

var util = require('util');
var Resource = require('./');

/**
* Expose constructor
*/

module.exports = Coupons;

/**
* Coupons constructor
*/

function Coupons(mango) {
Resource.call(this, mango);
}

util.inherits(Coupons, Resource);

/**
* Get coupon
*
* @param {String} uid
* @param {Function} callback
* @api public
*/

Coupons.prototype.get = function(uid, fn) {
return this.request('get', '/coupons/' + uid + '/', fn);
};

/**
* List coupons
*
* @param {Object} options
* @param {Function} callback
* @api public
*/

Coupons.prototype.list = function(options, fn) {
return this.request('get', '/coupons/', options, fn);
};

/**
* Create coupon
*
* @param {Object} options
* @param {Function} callback
* @api public
*/

Coupons.prototype.create = function(options, fn) {
return this.request('post', '/coupons/', options, fn);
};

/**
* Update coupon
*
* @param {string} uid
* @param {object} options
* @param {function} callback
* @api public
*/

Coupons.prototype.update = function(uid, options, fn) {
return this.request('patch', '/coupons/' + uid + '/', options, fn);
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mango",
"version": "0.0.3",
"version": "0.0.4",
"description": "Mango API client for node.js",
"main": "lib/index.js",
"scripts": {
Expand Down
97 changes: 97 additions & 0 deletions test/coupons.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@

/**
* Module dependencies
*/

var request = require('superagent');

describe('Coupons', function(){
describe('base', function(){
it('Inherits from Resource', function(){
assert(mango.Coupons instanceof require('../lib/resources'));
});

it('Has all required methods', function(){
['get', 'list', 'create', 'update'].forEach(function(method){
assert('function' == typeof mango.Coupons[method]);
});
});
});

describe('#get', function(){
it('Return error for bad uid', function(done){
mango.Coupons.get('not_coupon', function(err, data){
assert(err);
assert(err.status === 404);
done();
});
});

it('Get a coupon', function(done){
mango.Coupons.create({
'amount': 2000,
'type':'pagofacil',
'first_due_date': new Date(Date.now() + 1000 * 60 * 60 * 24 * 30).toISOString().split('T')[0],
'second_due_date': new Date(Date.now() + 1000 * 60 * 60 * 24 * 90).toISOString().split('T')[0],
'surcharge': 20
},function(err, data){
mango.Coupons.get(data.uid, function(err, coupon){
assert('object' == typeof coupon);
assert(coupon.amount === 2000);
assert(coupon.type === 'pagofacil');
done(err);
});
});
});
});

describe('#list', function(){
it('List coupons', function(done){
mango.Coupons.list(function(err, data){
assert(Array.isArray(data));
assert(data.length);
assert(data[0].amount);
assert(data[0].uid);
done(err);
});
});
});

describe('#create', function(){
it('Create coupon', function(done){
mango.Coupons.create({
'amount': 3000,
'type':'rapipago',
'first_due_date': new Date(Date.now() + 1000 * 60 * 60 * 24 * 20).toISOString().split('T')[0],
'second_due_date': new Date(Date.now() + 1000 * 60 * 60 * 24 * 70).toISOString().split('T')[0],
'surcharge': 200
},function(err, coupon){
assert('object' == typeof coupon);
assert(coupon.amount === 3000);
assert(coupon.surcharge === 200);
assert(coupon.type === 'rapipago');
done(err);
});
});
});

describe('#update', function(){
it('Update coupon', function(done){
mango.Coupons.create({
'amount': 3000,
'type':'rapipago',
'first_due_date': new Date(Date.now() + 1000 * 60 * 60 * 24 * 20).toISOString().split('T')[0],
'second_due_date': new Date(Date.now() + 1000 * 60 * 60 * 24 * 70).toISOString().split('T')[0],
'surcharge': 200
},function(err, coupon){
assert(coupon.paid === false);
mango.Coupons.update(coupon.uid, {
'paid': true
}, function(err, updatedCoupon){
assert(updatedCoupon.paid === true);
done(err);
});
});
});
});
});

0 comments on commit ab7e644

Please sign in to comment.