From 7db58a1a2dd25f57c2ac9faef8d7b8dfe96e80f2 Mon Sep 17 00:00:00 2001 From: Dan Zajdband Date: Fri, 17 Jul 2015 16:34:29 -0300 Subject: [PATCH] Coupons: Added endpoints and tests. --- Readme.md | 1 + lib/index.js | 2 + lib/resources/coupons.js | 72 +++++++++++++++++++++++++++++ package.json | 2 +- test/coupons.js | 97 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 173 insertions(+), 1 deletion(-) create mode 100644 lib/resources/coupons.js create mode 100644 test/coupons.js diff --git a/Readme.md b/Readme.md index 4788bad..39dcf1f 100644 --- a/Readme.md +++ b/Readme.md @@ -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 diff --git a/lib/index.js b/lib/index.js index d896295..22c747b 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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 @@ -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'); } diff --git a/lib/resources/coupons.js b/lib/resources/coupons.js new file mode 100644 index 0000000..92175cd --- /dev/null +++ b/lib/resources/coupons.js @@ -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); +}; diff --git a/package.json b/package.json index 6f4884f..a710227 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/test/coupons.js b/test/coupons.js new file mode 100644 index 0000000..aa44e77 --- /dev/null +++ b/test/coupons.js @@ -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); + }); + }); + }); + }); +});