Skip to content

Commit

Permalink
Extracted metadata get/set methods into internal metadata module
Browse files Browse the repository at this point in the history
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 TryGhost/Members#105 (review)
  • Loading branch information
naz committed Jan 13, 2020
1 parent a122aa0 commit 08fbcf2
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 5 deletions.
12 changes: 7 additions & 5 deletions ghost/members-api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -25,8 +26,8 @@ module.exports = function MembersApi({
getHTML,
getSubject
},
setMetadata,
getMetadata,
memberStripeCustomerModel,
stripeCustomerSubscriptionModel,
memberModel,
logger
}) {
Expand All @@ -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;
Expand Down
57 changes: 57 additions & 0 deletions ghost/members-api/lib/metadata.js
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
};
};

0 comments on commit 08fbcf2

Please sign in to comment.