-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'impronunciable-feature-coupons'
- Loading branch information
Showing
5 changed files
with
173 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |