-
Notifications
You must be signed in to change notification settings - Fork 188
Fix/subscription metadata priceid remove #15
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 | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -51,7 +51,6 @@ const createCheckout = async ({ | |||||||||||
| customerEmail: customerEmail, | ||||||||||||
| metadata: { | ||||||||||||
| ...metadata, | ||||||||||||
| priceId: productPriceId, | ||||||||||||
| }, | ||||||||||||
| }; | ||||||||||||
|
|
||||||||||||
|
|
@@ -185,7 +184,7 @@ export const checkUserSubscriptionStatus = query({ | |||||||||||
| } | ||||||||||||
| tokenIdentifier = identity.subject; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| const user = await ctx.db | ||||||||||||
| .query("users") | ||||||||||||
| .withIndex("by_token", (q) => q.eq("tokenIdentifier", tokenIdentifier)) | ||||||||||||
|
|
@@ -194,10 +193,11 @@ export const checkUserSubscriptionStatus = query({ | |||||||||||
| if (!user) { | ||||||||||||
| return { hasActiveSubscription: false }; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| const subscription = await ctx.db | ||||||||||||
| .query("subscriptions") | ||||||||||||
| .withIndex("userId", (q) => q.eq("userId", user.tokenIdentifier)) | ||||||||||||
| .order("desc") // Order by createdAt in descending order | ||||||||||||
| .first(); | ||||||||||||
|
|
||||||||||||
| const hasActiveSubscription = subscription?.status === "active"; | ||||||||||||
|
|
@@ -234,6 +234,7 @@ export const checkUserSubscriptionStatusByClerkId = query({ | |||||||||||
| const subscription = await ctx.db | ||||||||||||
| .query("subscriptions") | ||||||||||||
| .withIndex("userId", (q) => q.eq("userId", user.tokenIdentifier)) | ||||||||||||
| .order("desc") // Order by createdAt in descending order | ||||||||||||
| .first(); | ||||||||||||
|
Comment on lines
+237
to
238
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. 🛠️ Refactor suggestion Same ordering issue here — switch to a compound (userId, startedAt) index This query also won’t reliably return the latest subscription for the user. Use the same index and ordering approach as above. Apply this change: - .withIndex("userId", (q) => q.eq("userId", user.tokenIdentifier))
- .order("desc") // Order by createdAt in descending order
+ .withIndex("by_userId_startedAt", (q) => q.eq("userId", user.tokenIdentifier))
+ .order("desc") // Latest by startedAt🤖 Prompt for AI Agents |
||||||||||||
|
|
||||||||||||
| const hasActiveSubscription = subscription?.status === "active"; | ||||||||||||
|
|
@@ -261,6 +262,7 @@ export const fetchUserSubscription = query({ | |||||||||||
| const subscription = await ctx.db | ||||||||||||
| .query("subscriptions") | ||||||||||||
| .withIndex("userId", (q) => q.eq("userId", user.tokenIdentifier)) | ||||||||||||
| .order("desc") // Order by createdAt in descending order | ||||||||||||
| .first(); | ||||||||||||
|
Comment on lines
+265
to
266
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. 🛠️ Refactor suggestion Same ordering issue — ensure you truly fetch the latest subscription Use the (userId, startedAt) compound index and order desc to select the most recent subscription. Apply this change: - .withIndex("userId", (q) => q.eq("userId", user.tokenIdentifier))
- .order("desc") // Order by createdAt in descending order
+ .withIndex("by_userId_startedAt", (q) => q.eq("userId", user.tokenIdentifier))
+ .order("desc") // Latest by startedAt📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||
|
|
||||||||||||
| return subscription; | ||||||||||||
|
|
@@ -328,6 +330,7 @@ export const handleWebhookEvent = mutation({ | |||||||||||
|
|
||||||||||||
| if (existingSub) { | ||||||||||||
| await ctx.db.patch(existingSub._id, { | ||||||||||||
| polarPriceId: args.body.data.price_id, | ||||||||||||
| amount: args.body.data.amount, | ||||||||||||
| status: args.body.data.status, | ||||||||||||
| currentPeriodStart: new Date( | ||||||||||||
|
|
||||||||||||
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.
💡 Verification agent
🧩 Analysis chain
“Order by createdAt” isn’t achieved; current index doesn’t guarantee latest subscription
.withIndex("userId", …).order("desc") will order by the index key (userId) and then by internal doc id, not by createdAt. This can return an arbitrary subscription for the user and lead to incorrect hasActiveSubscription results.
Prefer ordering by a time field that exists on the subscription document and is indexed as a secondary key. Since you already persist startedAt, index on (userId, startedAt) and order desc to get the most recent subscription for the user.
Apply this change in the query to use a compound index:
Add the corresponding index in your Convex schema (example):
Verification script to confirm the index exists in your schema and to find all places to update:
🏁 Script executed:
Length of output: 2415
Update subscription query to use a compound index on startedAt and add that index
The current query in
convex/subscriptions.tsis still using:That will sort by the index key (userId) and then by internal document ID—not by your timestamp field—so you won’t reliably get the most recent subscription.
Please make the following critical fixes:
• In
convex/subscriptions.ts(around lines 200–201), change to:• In your schema file (
convex/schema.ts), add the compound index onstartedAt:subscriptions: defineTable({ userId: v.optional(v.string()), // … other fields … startedAt: v.optional(v.number()), }) - .index("userId", ["userId"]) + .index("userId", ["userId"]) + .index("by_userId_startedAt", ["userId", "startedAt"]) .index("polarId", ["polarId"]),After deploying these changes, your
.order("desc")will correctly return the subscription with the higheststartedAtfor a given user.🤖 Prompt for AI Agents