Updated MemberRepostitory with subscription methods

https://github.com/TryGhost/Team/issues/530

These are required by the smart_cancel functionality
This commit is contained in:
Fabien O'Carroll 2021-03-25 12:47:45 +00:00
parent a302ff1597
commit e2a46863d8

View File

@ -370,6 +370,55 @@ module.exports = class MemberRepository {
} }
} }
async getSubscription(data, options) {
if (!this._stripeAPIService.configured) {
throw new Error('Cannot get Stripe Subscription with no Stripe Connection');
}
const member = await this._Member.findOne({
email: data.email
});
const subscription = await member.related('stripeSubscriptions').query({
where: {
subscription_id: data.subscription.subscription_id
}
}).fetchOne(options);
if (!subscription) {
throw new Error('Subscription not found');
}
return subscription.toJSON();
}
async cancelSubscription(data, options) {
if (!this._stripeAPIService.configured) {
throw new Error('Cannot update Stripe Subscription with no Stripe Connection');
}
const member = await this._Member.findOne({
email: data.email
});
const subscription = await member.related('stripeSubscriptions').query({
where: {
subscription_id: data.subscription.subscription_id
}
}).fetchOne(options);
if (!subscription) {
throw new Error('Subscription not found');
}
const updatedSubscription = await this._stripeAPIService.cancelSubscription(data.subscription.subscription_id);
await this.linkSubscription({
id: member.id,
subscription: updatedSubscription
}, options);
}
async updateSubscription(data, options) { async updateSubscription(data, options) {
if (!this._stripeAPIService.configured) { if (!this._stripeAPIService.configured) {
throw new Error('Cannot update Stripe Subscription with no Stripe Connection'); throw new Error('Cannot update Stripe Subscription with no Stripe Connection');
@ -414,7 +463,7 @@ module.exports = class MemberRepository {
await this.linkSubscription({ await this.linkSubscription({
id: member.id, id: member.id,
subscription: updatedSubscription subscription: updatedSubscription
}); }, options);
} }
} }