0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-10 23:36:14 -05:00

Added dummy subscriptions for comped members

refs https://github.com/TryGhost/Team/issues/873

This adds a dummy subscription for each product that a member has
without an associated stripe subscription. It allows clients to deal
with things like a created date for comped members.
This commit is contained in:
Fabien O'Carroll 2021-08-25 13:33:13 +02:00
parent c17442cf4b
commit 86f5879432

View file

@ -1,3 +1,5 @@
const moment = require('moment');
module.exports = class MemberBREADService {
/**
* @param {object} deps
@ -18,6 +20,10 @@ module.exports = class MemberBREADService {
const withRelated = new Set((options.withRelated || []).concat(defaultWithRelated));
if (!withRelated.has('productEvents')) {
withRelated.add('productEvents');
}
if (withRelated.has('email_recipients')) {
withRelated.add('email_recipients.email');
}
@ -29,6 +35,51 @@ module.exports = class MemberBREADService {
const member = model.toJSON(options);
const subscriptionProducts = member.subscriptions.map(sub => sub.price.product.product_id);
for (const product of member.products) {
if (!subscriptionProducts.includes(product.id)) {
const productAddEvent = member.productEvents.find(event => event.product_id === product.id);
if (!productAddEvent || productAddEvent.action !== 'added') {
// There's something very wrong here. They have been comped this product - why is there no event????
}
const startDate = moment(productAddEvent.created_at);
member.subscriptions.push({
id: '',
customer: {
id: '',
name: member.name,
email: member.email
},
plan: {
id: '',
nickname: 'Complimentary',
interval: 'year',
currency: 'USD',
amount: 0
},
status: 'active',
start_date: startDate,
default_payment_card_last4: '****',
cancel_at_period_end: false,
cancellation_reason: null,
current_period_end: moment(startDate).add(1, 'year'),
price: {
id: '',
price_id: '',
nickname: 'Complimentary',
amount: 0,
interval: 'year',
type: 'recurring',
currency: 'USD',
product: {
id: '',
product_id: product.id
}
}
});
}
}
return member;
}
};