-
Notifications
You must be signed in to change notification settings - Fork 5
fix: canonicalize subscription entitlements #134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,18 @@ | ||
| import { v } from "convex/values"; | ||
| import { internalQuery } from "../_generated/server"; | ||
| import { deriveEffectivePlan } from "../billing/plans"; | ||
| import { deriveEffectivePlan, getCanonicalSubscription } from "../billing/plans"; | ||
|
|
||
| /** Server-only entitlement check for actions that cannot access ctx.db. */ | ||
| export const getEffectivePlanForUser = internalQuery({ | ||
| args: { userId: v.string() }, | ||
| returns: v.union(v.literal("free"), v.literal("pro")), | ||
| handler: async (ctx, { userId }) => { | ||
| const subscription = await ctx.db | ||
| const subscriptions = await ctx.db | ||
| .query("subscriptions") | ||
| .withIndex("by_user", (q) => q.eq("userId", userId)) | ||
| .first(); | ||
| .order("desc") | ||
| .take(20); | ||
|
Comment on lines
12
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When an account has more than 20 subscription rows, this newest-by-creation window can exclude its durable manual lifetime grant. In particular, Useful? React with 👍 / 👎. |
||
|
|
||
| return deriveEffectivePlan(subscription); | ||
| return deriveEffectivePlan(getCanonicalSubscription(subscriptions)); | ||
| }, | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a user has multiple subscription rows and an older row is repaired,
migration/repair_subscriptions.tssets that row'supdatedAttoDate.now()even when merely correcting its customer ID;updateFromWebhooksimilarly timestamps updates by processing time. This selector therefore makes that historical row canonical over a genuinely newer subscription, so an old canceled row can revoke an active payer's Pro access or an old active row can restore access after cancellation. Use a billing-lifecycle/event ordering field, or keep non-lifecycle maintenance from changing the field used here.Useful? React with 👍 / 👎.