Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions apps/backend/src/lib/payments.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1182,6 +1182,64 @@ describe('getSubscriptions - defaults behavior', () => {
expect(ids).toContain('freeUngrouped');
});

it('includes include-by-default product when only inactive subscription exists in line', async () => {
const tenancy = createMockTenancy({
items: {},
productLines: { g1: { displayName: 'G1', customerType: 'user' } },
products: {
freeG1: {
displayName: 'Free',
productLineId: 'g1',
customerType: 'user',
freeTrial: undefined,
serverOnly: false,
stackable: false,
prices: 'include-by-default',
includedItems: {},
isAddOnTo: false,
},
paidG1: {
displayName: 'Paid',
productLineId: 'g1',
customerType: 'user',
freeTrial: undefined,
serverOnly: false,
stackable: false,
prices: {},
includedItems: {},
isAddOnTo: false,
},
},
});

const prisma = createMockPrisma({
subscription: {
findMany: async () => [{
id: 'sub-1',
productId: 'paidG1',
product: tenancy.config.payments.products['paidG1'],
quantity: 1,
currentPeriodStart: new Date('2025-01-01T00:00:00.000Z'),
currentPeriodEnd: new Date('2025-02-01T00:00:00.000Z'),
cancelAtPeriodEnd: false,
status: 'canceled',
createdAt: new Date('2025-01-01T00:00:00.000Z'),
stripeSubscriptionId: null,
}],
},
} as any);

const subs = await getSubscriptions({
prisma,
tenancy,
customerType: 'user',
customerId: 'user-1',
});

const ids = subs.map(s => s.productId);
expect(ids).toContain('freeG1');
});

it('throws error when multiple include-by-default products exist in same line', async () => {
const tenancy = createMockTenancy({
items: {},
Expand Down
7 changes: 4 additions & 3 deletions apps/backend/src/lib/payments.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ export async function getSubscriptions(options: {
const productLinesWithDbSubscriptions = new Set<string>();
for (const s of dbSubscriptions) {
const product = s.product as yup.InferType<typeof productSchema>;
subscriptions.push({
const subscription: Subscription = {
id: s.id,
productId: s.productId,
product,
Expand All @@ -343,8 +343,9 @@ export async function getSubscriptions(options: {
status: s.status,
createdAt: s.createdAt,
stripeSubscriptionId: s.stripeSubscriptionId,
});
if (product.productLineId !== undefined) {
};
subscriptions.push(subscription);
if (product.productLineId !== undefined && isActiveSubscription(subscription)) {
productLinesWithDbSubscriptions.add(product.productLineId);
}
}
Expand Down