Repository navigation
fix(billing): align prompts to new plan's engine set on every plan-change path - #112
Merged
Merged
Conversation
…ange path Issue #78 only fixed the initial-checkout case. Existing orgs that upgrade from Starter to Growth (or downgrade back) still kept their prompts on the original engine set — a paying Growth customer would track only 2 of the 7 engines until they manually edited every prompt. Three plan-change entry points all converge on a shared helper now: - web/src/lib/plan-engines.ts: extract alignPromptsToPlanForOrg(orgId, planId) as the single SQL surface — walks brands → prompt_sets → prompts and rewrites platforms + models to match the plan's allowedScrapers / allowedModels. Idempotent; handles both expansion (Starter → Growth: 2 → 7 engines) and contraction (Growth → Starter: 7 → 2). Returns the rewritten count so callers can log it. - web/src/app/api/stripe/subscription/route.ts (PATCH): the in-app upgrade / downgrade path optimistically writes the new plan to the org row itself. Without this call, the matching subscription.updated webhook below would see plan-unchanged (because the optimistic write already landed) and skip alignment. Now PATCH triggers alignment directly after the optimistic write, so the engine set follows the plan immediately. - web/src/app/api/stripe/webhook/route.ts: defense-in-depth for out-of-band plan changes (Stripe Dashboard edits, customer portal, manual admin moves). Three paths covered: - checkout.session.completed → align after the org update - customer.subscription.updated → snapshot pre-update plan, only align when newPlanId !== orgBefore.plan (skips routine renewal events) - customer.subscription.deleted → align to 'starter' so a downgraded org doesn't keep paying scraper cost on engines the plan no longer covers - web/src/app/api/stripe/success/route.ts: refactor #78's inline alignment SQL to use the shared helper so all four call sites have identical behavior. Verified locally with Stripe CLI forwarding + an in-app downgrade → upgrade roundtrip on a test account; logs and prompt rows reflect the expected engine sets at every transition. Closes #79.
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
#78 fixed onboarding's initial-checkout case. Existing orgs that upgrade from Starter to Growth (or downgrade back) still kept their prompts on the original engine set — a paying Growth customer would track only 2 of the 7 engines until they manually edited every prompt.
Live-verified during testing: a real upgrade in the dashboard left `organizations.plan = 'growth'` but `prompts.platforms = ['chatgpt-web', 'perplexity-web']`. Tracking worker only consults `prompt.platforms` at runtime — never the plan, `allowedScrapers`, or `brand_platforms` — so the bill goes up while the engine coverage doesn't.
Why the in-app upgrade was the missing path
The in-app upgrade flow goes `PATCH /api/stripe/subscription`:
```ts
// Stripe update + optimistic plan write
await stripe.subscriptions.update(subscriptionId, { items: [...], metadata: { plan_id: newPlanId, ... } });
await supabase.from('organizations').update({ plan: newPlanId }).eq('id', org.id);
```
By the time Stripe fires the matching `customer.subscription.updated` webhook, the snapshot guard added there sees `orgBefore.plan === newPlanId` (because the optimistic write already landed) and skips alignment. So the webhook fix alone wasn't enough — the PATCH route itself needs to trigger alignment immediately after its own optimistic write.
Shape of the fix
Three plan-change entry points, one shared helper:
Files
How verified
Local e2e against cloud Stripe test mode with localhost web + Stripe CLI:
```
[stripe/subscription] Aligned 1 prompt(s) to plan=growth (7 scrapers, 1 models) for org ...
```
Acceptance (from #79)
Closes #79.