0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-04-15 03:01:37 -05:00

Added ability to filter members on subscription data (#13214)

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

This will allow us to filter for members which have a canceled
subscription or for members which are currently on trial.
This commit is contained in:
Fabien 'egg' O'Carroll 2021-08-12 10:10:51 +01:00 committed by GitHub
parent 19fd16649a
commit 6dba643ef9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 162 additions and 0 deletions

View file

@ -48,6 +48,15 @@ const Member = ghostBookshelf.Model.extend({
joinTable: 'members_products',
joinFrom: 'member_id',
joinTo: 'product_id'
},
subscriptions: {
tableName: 'members_stripe_customers_subscriptions',
tableNameAs: 'subscriptions',
type: 'manyToMany',
joinTable: 'members_stripe_customers',
joinFrom: 'member_id',
joinTo: 'customer_id',
joinToForeign: 'customer_id'
}
};
},

View file

@ -345,5 +345,158 @@ describe('Member Model', function run() {
should.not.exist(foundMemberInPodcast, 'Member should not have been included in products filter');
});
});
describe('Filtering', function () {
it('Should allow filtering on name', async function () {
const context = testUtils.context.admin;
await Member.add({
name: 'Name Filter Test',
email: 'name-filter-test@test.member',
products: [{
name: 'VIP',
slug: 'vip'
}]
}, context);
const member = await Member.findOne({
email: 'name-filter-test@test.member'
}, context);
should.exist(member, 'Member should have been created');
const membersByName = await Member.findPage({filter: `name:'Name Filter Test'`});
const foundMember = membersByName.data.find(model => model.id === member.id);
should.exist(foundMember, 'Member should have been included in name filter');
});
it('Should allow filtering on email', async function () {
const context = testUtils.context.admin;
await Member.add({
email: 'email-filter-test@test.member',
products: [{
name: 'VIP',
slug: 'vip'
}]
}, context);
const member = await Member.findOne({
email: 'email-filter-test@test.member'
}, context);
should.exist(member, 'Member should have been created');
const membersByName = await Member.findPage({filter: `email:email-filter-test@test.member`});
const foundMember = membersByName.data.find(model => model.id === member.id);
should.exist(foundMember, 'Member should have been included in name filter');
});
it('Should allow filtering on subscriptions', async function () {
const context = testUtils.context.admin;
const member = await Member.add({
email: 'test@test.member',
labels: []
}, context);
const product = await Product.add({
name: 'Ghost Product',
slug: 'ghost-product'
}, context);
await StripeProduct.add({
product_id: product.get('id'),
stripe_product_id: 'fake_product_id'
}, context);
await StripePrice.add({
stripe_price_id: 'fake_plan_id',
stripe_product_id: 'fake_product_id',
amount: 5000,
interval: 'monthly',
active: 1,
nickname: 'Monthly',
currency: 'USD',
type: 'recurring'
}, context);
await MemberStripeCustomer.add({
member_id: member.get('id'),
customer_id: 'fake_customer_id1'
}, context);
await MemberStripeCustomer.add({
member_id: member.get('id'),
customer_id: 'fake_customer_id2'
}, context);
const subscription1 = await StripeCustomerSubscription.add({
customer_id: 'fake_customer_id1',
subscription_id: 'fake_subscription_id1',
plan_id: 'fake_plan_id',
stripe_price_id: 'fake_plan_id',
plan_amount: 1337,
plan_nickname: 'e-LEET',
plan_interval: 'year',
plan_currency: 'btc',
status: 'active',
start_date: new Date(),
current_period_end: new Date(),
cancel_at_period_end: false
}, context);
const subscription2 = await StripeCustomerSubscription.add({
customer_id: 'fake_customer_id2',
subscription_id: 'fake_subscription_id2',
plan_id: 'fake_plan_id',
stripe_price_id: 'fake_plan_id',
plan_amount: 1337,
plan_nickname: 'e-LEET',
plan_interval: 'year',
plan_currency: 'btc',
status: 'canceled',
start_date: new Date(),
current_period_end: new Date(),
cancel_at_period_end: false
}, context);
{
const members = await Member.findPage({filter: `subscriptions.status:canceled+subscriptions.status:-active`});
should.equal(members.data.length, 0, 'Can search for members with canceled subscription and no active ones');
}
await StripeCustomerSubscription.edit({
status: 'canceled'
}, {
id: subscription1.id,
...context
});
{
const members = await Member.findPage({filter: `subscriptions.status:canceled+subscriptions.status:-active`});
should.equal(members.data.length, 1, 'Can search for members with canceled subscription and no active ones');
}
{
const members = await Member.findPage({filter: `subscriptions.plan_interval:year`});
should.equal(members.data.length, 1, 'Can search for members by plan_interval');
}
await StripeCustomerSubscription.edit({
plan_interval: 'month'
}, {
id: subscription2.id,
...context
});
{
const members = await Member.findPage({filter: `subscriptions.plan_interval:month+subscriptions.plan_interval:-year`});
should.equal(members.data.length, 0, 'Can search for members by plan_interval');
}
});
});
});