Skip to content

Commit

Permalink
Reduced unneeded saves on unchanged collections
Browse files Browse the repository at this point in the history
  • Loading branch information
ivank committed Aug 21, 2017
1 parent 587c42d commit ce6ef38
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 26 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mongoose-subscriptions-braintree",
"version": "1.4.5",
"version": "1.4.6",
"description": "Braintree processor for mongoose-subscriptions",
"main": "src/index.js",
"repository": "[email protected]:enhancv/mongoose-subscriptions-braintree.git",
Expand All @@ -24,7 +24,7 @@
"istanbul": "^0.4.5",
"mocha": "^3.2.0",
"mongoose": "^4.8",
"mongoose-subscriptions": "^1.9.3",
"mongoose-subscriptions": "^1.13.7",
"prettier": "^1.4.4",
"sinon": "^2.1.0"
}
Expand Down
33 changes: 21 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,21 @@ const addressProcessor = require("./addressProcessor");
const paymentMethodProcessor = require("./paymentMethodProcessor");
const subscriptionProcessor = require("./subscriptionProcessor");
const transactionProcessor = require("./transactionProcessor");
const { some } = require("lodash/fp");

function isChanged(item) {
return [ProcessorItem.CHANGED, ProcessorItem.INITIAL].includes(item.processor.state);
}

function saveCollection(name, saveItem, customer) {
return some(isChanged, customer[name])
? Promise.all(customer[name].map(saveItem(customer))).then(() => customer.save())
: Promise.resolve();
}

function saveCustomer(saveItem, customer) {
return isChanged(customer) ? saveItem(customer).then(() => customer.save()) : Promise.resolve();
}

class BraintreeProcessor extends AbstractProcessor {
constructor(gateway, plans) {
Expand All @@ -19,18 +34,12 @@ class BraintreeProcessor extends AbstractProcessor {
}

save(customer) {
const saveAddress = addressProcessor.save(this, customer);
const savePaymentMethod = paymentMethodProcessor.save(this, customer);
const saveSubscription = subscriptionProcessor.save(this, customer);

return customerProcessor
.save(this, customer)
.then(() => customer.save())
.then(() => Promise.all(customer.addresses.map(saveAddress)))
.then(() => customer.save())
.then(() => Promise.all(customer.paymentMethods.map(savePaymentMethod)))
.then(() => customer.save())
.then(() => Promise.all(customer.subscriptions.map(saveSubscription)))
return saveCustomer(customerProcessor.save(this), customer)
.then(() => saveCollection("addresses", addressProcessor.save(this), customer))
.then(() =>
saveCollection("paymentMethods", paymentMethodProcessor.save(this), customer)
)
.then(() => saveCollection("subscriptions", subscriptionProcessor.save(this), customer))
.then(() => customer);
}

Expand Down
24 changes: 12 additions & 12 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ abbrev@1, [email protected]:
version "1.0.9"
resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.0.9.tgz#91b4792588a7738c25f35dd6f63752a2f8776135"

addmonths@^0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/addmonths/-/addmonths-0.2.0.tgz#2d90aeb5fc31600f00b58d32ae2e4e8bcbd9de7d"

align-text@^0.1.1, align-text@^0.1.3:
version "0.1.4"
resolved "https://registry.yarnpkg.com/align-text/-/align-text-0.1.4.tgz#0cd90a561093f35d0a99256c22b7069433fad117"
Expand Down Expand Up @@ -428,19 +424,19 @@ [email protected]:
mongodb-core "2.1.9"
readable-stream "2.1.5"

mongoose-originals@^1.3.1:
version "1.3.1"
resolved "https://registry.yarnpkg.com/mongoose-originals/-/mongoose-originals-1.3.1.tgz#d376cc434b0269b853655c5c6ee483d39220cca3"
mongoose-originals@^1.3.2:
version "1.3.2"
resolved "https://registry.yarnpkg.com/mongoose-originals/-/mongoose-originals-1.3.2.tgz#914fa2e8125cfd2db5a645f213c8e0f3e0c09c28"
dependencies:
lodash "^4.17.4"

mongoose-subscriptions@^1.9.3:
version "1.9.3"
resolved "https://registry.yarnpkg.com/mongoose-subscriptions/-/mongoose-subscriptions-1.9.3.tgz#b7abf09796f51d5369fec3f5d9a96af969ae9837"
mongoose-subscriptions@^1.13.7:
version "1.13.7"
resolved "https://registry.yarnpkg.com/mongoose-subscriptions/-/mongoose-subscriptions-1.13.7.tgz#c5ecdc72851751f5c0460c91206f4164884df59a"
dependencies:
addmonths "^0.2.0"
mongoose-originals "^1.3.1"
mongoose-originals "^1.3.2"
shortid "^2.2.8"
xdate "^0.8.2"

mongoose@^4.8:
version "4.9.3"
Expand Down Expand Up @@ -725,6 +721,10 @@ wrappy@1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f"

xdate@^0.8.2:
version "0.8.2"
resolved "https://registry.yarnpkg.com/xdate/-/xdate-0.8.2.tgz#d7b033c00485d02695baf0044f4eacda3fc961a3"

[email protected]:
version "0.1.13"
resolved "https://registry.yarnpkg.com/xml2js/-/xml2js-0.1.13.tgz#438ff3b1d85a51ad659ffc2ebe83403e10c98722"
Expand Down

0 comments on commit ce6ef38

Please sign in to comment.