Skip to content

Commit

Permalink
fix racecondition
Browse files Browse the repository at this point in the history
  • Loading branch information
deepsyx committed Apr 3, 2018
1 parent 3a2ece3 commit 3f38d14
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mongoose-subscriptions-braintree",
"version": "1.6.1",
"version": "1.6.2",
"description": "Braintree processor for mongoose-subscriptions",
"main": "src/index.js",
"repository": "[email protected]:enhancv/mongoose-subscriptions-braintree.git",
Expand Down
39 changes: 22 additions & 17 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,26 @@ 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(() => {
delete customer.__v;
customer.save();
})
: Promise.resolve();
async function saveCollection(name, saveItem, customer) {
if (!some(isChanged, customer[name])) {
return Promise.resolve(customer);
}

let _customer = customer;

for (let index = 0; index < _customer[name].length; index++) {
_customer = await saveItem(_customer, _customer[name][index], index);
}

return _customer.save();
}

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

return saveItem(customer).then(customer => customer.save());
}

class BraintreeProcessor extends AbstractProcessor {
Expand All @@ -43,12 +47,13 @@ class BraintreeProcessor extends AbstractProcessor {

save(customer) {
return saveCustomer(customerProcessor.save(this), customer)
.then(() => saveCollection("addresses", addressProcessor.save(this), customer))
.then(() =>
.then(customer => saveCollection("addresses", addressProcessor.save(this), customer))
.then(customer =>
saveCollection("paymentMethods", paymentMethodProcessor.save(this), customer)
)
.then(() => saveCollection("subscriptions", subscriptionProcessor.save(this), customer))
.then(() => customer);
.then(customer =>
saveCollection("subscriptions", subscriptionProcessor.save(this), customer)
);
}

cancelSubscription(customer, subscriptionId) {
Expand Down

0 comments on commit 3f38d14

Please sign in to comment.