This repository has been archived by the owner on Oct 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extracted metadata get/set methods into internal metadata module
no issue - This is the refactor similar to what has been done with Memeber model being passed in directly in the constructor - Relevent discussion here #105 (review)
- Loading branch information
Showing
2 changed files
with
64 additions
and
5 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
let MemberStripeCustomer; | ||
let StripeCustomerSubscription; | ||
|
||
async function setMetadata(module, metadata) { | ||
if (module !== 'stripe') { | ||
return; | ||
} | ||
|
||
if (metadata.customer) { | ||
await MemberStripeCustomer.upsert(metadata.customer, { | ||
customer_id: metadata.customer.customer_id | ||
}); | ||
} | ||
|
||
if (metadata.subscription) { | ||
await StripeCustomerSubscription.upsert(metadata.subscription, { | ||
subscription_id: metadata.subscription.subscription_id | ||
}); | ||
} | ||
|
||
return; | ||
} | ||
|
||
async function getMetadata(module, member) { | ||
if (module !== 'stripe') { | ||
return; | ||
} | ||
|
||
const customers = (await MemberStripeCustomer.findAll({ | ||
filter: `member_id:${member.id}` | ||
})).toJSON(); | ||
|
||
const subscriptions = await customers.reduce(async (subscriptionsPromise, customer) => { | ||
const customerSubscriptions = await StripeCustomerSubscription.findAll({ | ||
filter: `customer_id:${customer.customer_id}` | ||
}); | ||
return (await subscriptionsPromise).concat(customerSubscriptions.toJSON()); | ||
}, []); | ||
|
||
return { | ||
customers: customers, | ||
subscriptions: subscriptions | ||
}; | ||
} | ||
|
||
module.exports = function ({ | ||
memberStripeCustomerModel, | ||
stripeCustomerSubscriptionModel | ||
}) { | ||
MemberStripeCustomer = memberStripeCustomerModel; | ||
StripeCustomerSubscription = stripeCustomerSubscriptionModel; | ||
|
||
return { | ||
setMetadata, | ||
getMetadata | ||
}; | ||
}; |