Skip to content
This repository has been archived by the owner on Oct 30, 2018. It is now read-only.

Commit

Permalink
Merge pull request #67 from bryanchriswhite/master
Browse files Browse the repository at this point in the history
Refactoring
  • Loading branch information
phutchins committed Feb 28, 2017
2 parents 3428740 + cc925af commit 1c59da2
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 26 deletions.
28 changes: 21 additions & 7 deletions lib/models/credit.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,16 @@ Credit.pre('validate', function(next) {

// NB: Validate for docs that have promo amount
Credit.pre('validate', function(next) {
if (this.promo_amount) {
// disallow coexistence of promo_amount and paid_amount/invoiced_amount
if (this.paid_amount || this.invoiced_amount) {
return next(new errors.BadRequestError(
'promo_amount cannot exist with invoiced_amount and/or paid_amount'
));
}
// disallow coexistence of promo_amount and paid_amount/invoiced_amount
if (this.promo_amount !== null && this.promo_amount >= 0 &&
(this.paid_amount || this.invoiced_amount)) {
return next(new errors.BadRequestError(
'a positive or zero promo_amount cannot exist ' +
'with invoiced_amount and/or paid_amount'
));
}

if (this.promo_amount !== null && this.promo_amount >= 0) {
if (!this.promo_code) {
return next(new errors.BadRequestError(
'promo_amount must have valid promo_code'
Expand All @@ -130,6 +132,18 @@ Credit.pre('validate', function(next) {
}

return next();
} else {
if (this.promo_code) {
return next(new errors.BadRequestError(
'promo_code cannot exist with a negative promo_amount'
));
}

if (this.promo_expires) {
return next(new errors.BadRequestError(
'promo_expires cannot exist with a negative promo_amount'
));
}
}

next();
Expand Down
8 changes: 2 additions & 6 deletions lib/models/payment-processor-adapters/stripe.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ const stripeAdapter = {
}
},

balanceDebits: function(customerId, debits) {
balanceDebits: function(customerId, periodDebitTotal) {
// -- get invoices
// -- do "stripe balance" calcualation
// -- check for min amount
Expand All @@ -254,11 +254,7 @@ const stripeAdapter = {
console.log('got invoices...');
console.log('invoiceTotal: %j', invoiceTotal);

const debitTotal = (debits
.reduce((result, debit) => result + debit.amount, 0.0) + 0.0);
console.log('debitTotal: %j', debitTotal);

const stripeBalance = debitTotal - invoiceTotal;
const stripeBalance = periodDebitTotal - invoiceTotal;
console.log('stripeBalance: %j', stripeBalance);

return new Promise((resolve, reject) => {
Expand Down
43 changes: 30 additions & 13 deletions test/credit.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ describe('Storage/models/Credit', function() {

describe('#create - paid_amount and invoiced_amount relations', function() {

it('should fail if trying to save paid without invoiced', function(done) {
it('should fail if trying to save paid without invoiced_amount', function(done) {
const newCredit = new Credit({
user: 'user@domain.tld',
type: CREDIT_TYPES.MANUAL,
Expand Down Expand Up @@ -173,7 +173,7 @@ describe('Storage/models/Credit', function() {
});
});

it('should set paid=true if paid=invoiced && both > 0', function(done) {
it('should set paid=true if paid_amount=invoiced_amount && both > 0', function(done) {
const newCredit = new Credit({
user: 'user@domain.tld',
type: CREDIT_TYPES.MANUAL,
Expand All @@ -191,7 +191,7 @@ describe('Storage/models/Credit', function() {
});
});

it('should set paid=false if paid!=invoiced && both > 0', function(done) {
it('should set paid=false if paid_amount!=invoiced_amount && both > 0', function(done) {
const newCredit = new Credit({
user: 'user@domain.tld',
type: CREDIT_TYPES.MANUAL,
Expand Down Expand Up @@ -247,9 +247,9 @@ describe('Storage/models/Credit', function() {

});

describe('#create - promo vs paid_amount/invoiced_amount', function() {
describe('#create - promo_amount vs paid_amount/invoiced_amount', function() {

it('should fail if trying to save invoiced and promo', function(done) {
it('should fail if trying to save invoiced_amount and promo_amount', function(done) {
const newCredit = new Credit({
user: 'user@domain.tld',
type: CREDIT_TYPES.MANUAL,
Expand All @@ -260,13 +260,13 @@ describe('Storage/models/Credit', function() {
newCredit.save(function(err) {
expect(err).to.be.an.instanceOf(Error);
expect(err.message).to.equal(
'promo_amount cannot exist with invoiced_amount and/or paid_amount'
'a positive or zero promo_amount cannot exist with invoiced_amount and/or paid_amount'
);
done();
});
});

it('should fail if trying to save paid_amount and promo', function(done) {
it('should fail if trying to save paid_amount and promo_amount', function(done) {
const newCredit = new Credit({
user: 'user@domain.tld',
type: CREDIT_TYPES.MANUAL,
Expand All @@ -277,13 +277,13 @@ describe('Storage/models/Credit', function() {
newCredit.save(function(err) {
expect(err).to.be.an.instanceOf(Error);
expect(err.message).to.equal(
'promo_amount cannot exist with invoiced_amount and/or paid_amount'
'a positive or zero promo_amount cannot exist with invoiced_amount and/or paid_amount'
);
done();
});
});

it('should have no paid/invoice if promo exists', function(done) {
it('should have no paid_amount/invoiced_amount if promo_amount exists', function(done) {
const newCredit = new Credit({
user: 'user@domain.tld',
type: CREDIT_TYPES.MANUAL,
Expand All @@ -308,7 +308,7 @@ describe('Storage/models/Credit', function() {
});
});

it('should have no promo if paid/invoice exists', function(done) {
it('should have no promo if paid_amount/invoiced_amount exists', function(done) {
const newCredit = new Credit({
user: 'user@domain.tld',
type: CREDIT_TYPES.MANUAL,
Expand All @@ -329,7 +329,7 @@ describe('Storage/models/Credit', function() {

});

describe('#create - promo validations', function() {
describe('#create - promo_amount validations', function() {

it('should have promo_code if promo_amount is > 0', function(done) {
const newCredit = new Credit({
Expand All @@ -350,6 +350,23 @@ describe('Storage/models/Credit', function() {
});
});

it('should fail if promo_amount is negative and there is a promo_expires', function(done) {
const newCredit = new Credit({
user: 'user@domain.tld',
type: CREDIT_TYPES.MANUAL,
promo_amount: -1,
promo_expires: PROMO_EXPIRES.NEW_SIGNUP
});

newCredit.save(function(err) {
expect(err).to.be.instanceOf(Error);
expect(err.message).to.equal(
'promo_expires cannot exist with a negative promo_amount'
);
done();
});
});

it('should fail if promo_amount > 0 && !promo_code', function(done) {
const newCredit = new Credit({
user: 'user@domain.tld',
Expand Down Expand Up @@ -377,7 +394,7 @@ describe('Storage/models/Credit', function() {

newCredit.save(function(err) {
expect(err).to.be.an.instanceOf(Error);
expect(err.message).to.equal('Credit validation failed');
expect(err.message).to.equal('promo_code cannot exist with a negative promo_amount');
done();
});
});
Expand Down Expand Up @@ -515,7 +532,7 @@ describe('Storage/models/Credit', function() {
});
});

it('should have specified fields for promo', function(done) {
it('should have specified fields for creating promo', function(done) {
const newCredit = new Credit({
user: 'user@domain.tld',
type: CREDIT_TYPES.MANUAL,
Expand Down

0 comments on commit 1c59da2

Please sign in to comment.