diff --git a/package.json b/package.json index 2355584..166c518 100644 --- a/package.json +++ b/package.json @@ -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": "git@github.com:enhancv/mongoose-subscriptions-braintree.git", @@ -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" } diff --git a/src/index.js b/src/index.js index 36375f7..8a8cbf7 100644 --- a/src/index.js +++ b/src/index.js @@ -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) { @@ -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); } diff --git a/yarn.lock b/yarn.lock index cf8fa87..1055917 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6,10 +6,6 @@ abbrev@1, abbrev@1.0.x: 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" @@ -428,19 +424,19 @@ mongodb@2.2.25: 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" @@ -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" + xml2js@0.1.13: version "0.1.13" resolved "https://registry.yarnpkg.com/xml2js/-/xml2js-0.1.13.tgz#438ff3b1d85a51ad659ffc2ebe83403e10c98722"