mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-10 23:36:14 -05:00
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 https://github.com/TryGhost/Members/pull/105#pullrequestreview-324254267
This commit is contained in:
parent
a122aa0119
commit
08fbcf25ec
2 changed files with 64 additions and 5 deletions
|
@ -5,6 +5,7 @@ const StripePaymentProcessor = require('./lib/stripe');
|
|||
|
||||
const Tokens = require('./lib/tokens');
|
||||
const Users = require('./lib/users');
|
||||
const Metadata = require('./lib/metadata');
|
||||
const common = require('./lib/common');
|
||||
|
||||
module.exports = function MembersApi({
|
||||
|
@ -25,8 +26,8 @@ module.exports = function MembersApi({
|
|||
getHTML,
|
||||
getSubject
|
||||
},
|
||||
setMetadata,
|
||||
getMetadata,
|
||||
memberStripeCustomerModel,
|
||||
stripeCustomerSubscriptionModel,
|
||||
memberModel,
|
||||
logger
|
||||
}) {
|
||||
|
@ -35,13 +36,14 @@ module.exports = function MembersApi({
|
|||
}
|
||||
|
||||
const {encodeIdentityToken, decodeToken} = Tokens({privateKey, publicKey, issuer});
|
||||
const metadata = Metadata({memberStripeCustomerModel, stripeCustomerSubscriptionModel});
|
||||
|
||||
const stripeStorage = {
|
||||
async get(member) {
|
||||
return getMetadata('stripe', member);
|
||||
return metadata.getMetadata('stripe', member);
|
||||
},
|
||||
async set(metadata) {
|
||||
return setMetadata('stripe', metadata);
|
||||
async set(data) {
|
||||
return metadata.setMetadata('stripe', data);
|
||||
}
|
||||
};
|
||||
const stripe = paymentConfig.stripe ? new StripePaymentProcessor(paymentConfig.stripe, stripeStorage, common.logging) : null;
|
||||
|
|
57
ghost/members-api/lib/metadata.js
Normal file
57
ghost/members-api/lib/metadata.js
Normal file
|
@ -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
|
||||
};
|
||||
};
|
Loading…
Add table
Reference in a new issue