feat(billing): subscription cancelAt + cancelAtPeriodEnd lifecycle fields#3741
Conversation
…e fields Port pending-cancellation fields verbatim from trawl_node into devkit so every downstream gets them. Required gate for the customer.subscription.updated webhook handler (C.2) which writes these fields on Stripe cancel_at_period_end events. Closes #3740
- Rename Unix-seconds test to clarify z.coerce.date() treats numbers as ms (not seconds); add exact date assertion to make the ×1000 contract explicit - Rename null-rejection test so name matches assertion (was "accept both null", now "reject null cancelAtPeriodEnd — not nullable")
|
Warning Review limit reached
More reviews will be available in 25 minutes and 55 seconds. Learn how PR review limits work. Your organization has run out of usage credits. Purchase more in the billing tab. ⌛ How to resolve this issue?After more reviews become available, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available. Please see our Fair Usage Limits Policy for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (3)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #3741 +/- ##
=======================================
Coverage 89.92% 89.92%
=======================================
Files 148 148
Lines 4865 4865
Branches 1533 1533
=======================================
Hits 4375 4375
Misses 385 385
Partials 105 105
Flags with carried forward coverage won't be shown. Click here to find out more. Continue to review full report in Codecov by Sentry.
🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
Adds Stripe pending-cancellation lifecycle fields to the Billing Subscription data model so upcoming customer.subscription.updated webhook work (Task C.2 / #3740) can persist cancel_at_period_end + cancel_at without overloading plan/status semantics.
Changes:
- Extend the Billing Subscription Zod schema with
cancelAtPeriodEnd(optional boolean) andcancelAt(optional nullable coerced Date). - Extend the Subscription Mongoose model with
cancelAtPeriodEnd(Boolean, defaultnull) andcancelAt(Date, defaultnull). - Add focused unit tests covering coercion, nullability rules, and
SubscriptionUpdatepartial patches for the new fields.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| modules/billing/tests/billing.subscription.schema.unit.tests.js | Adds unit coverage for the new cancellation lifecycle fields and update-schema patch behavior. |
| modules/billing/models/billing.subscription.schema.js | Introduces the new fields to the Zod base shape (including Date coercion + nullability semantics). |
| modules/billing/models/billing.subscription.model.mongoose.js | Persists the new fields in Mongo via the Subscription Mongoose schema with default: null for legacy/unsynced docs. |
Copilot review: "Unix date" was ambiguous. Changed to "Date (converted from Stripe's cancel_at Unix seconds)" for clarity.
…#1250) (#3743) * fix(billing): resolve plan via priceId map, not price.metadata.planId (#3742) Port trawl's #1250 fix upstream: `customer.subscription.updated` was reading `price.metadata.planId` which is empty in real Stripe webhook payloads (planId lives on the Product, not the Price). Every paid org's plan was silently reset to `free` on every Stripe subscription update event. Fix: - Add `buildPriceIdToPlanMap()` — builds `Map<priceId, planName>` from `config.stripe.prices` at boot (monthly + annual per plan) - Rewrite `resolvePlan()` — looks up plan via priceId map first; falls back to `price.metadata.planId` for legacy test fixtures; final fallback `free` + warn - Sync `cancelAt`/`cancelAtPeriodEnd` on `customer.subscription.updated` — writes the fields added by #3741 so the UI can show pending cancellation dates - Use priceId map in `previousPlan` detection (plan change block in updated handler) - Strip trawl-specific `analytics.capture('subscription_changed')` calls (not ported) Also ports `retryWithBackoff` + `isNonTransientStripeError` patterns (already in devkit from a prior PR — kept identical to trawl). Adds 20 unit tests covering: priceId map resolution, annual/monthly variants, unknown priceId fallback to free, legacy metadata fallback, priority of map over metadata, cancelAt unix→Date conversion, null cancel clear, absent field guard, handleSubscriptionCreated race safety (E11000 idempotency), and Dashboard subscription creation path. Closes #3742 * docs(billing): add ERRORS.md entry for price.metadata.planId empty in Stripe webhooks
Summary
cancelAtPeriodEnd(Boolean, default null) andcancelAt(Date, default null) verbatim from trawl_node into devkit Node's Subscription Mongoose model and Zod schemadefault: null(honest semantics: null = not yet synced from Stripe, vs false = explicitly not cancelling)Key design note
z.coerce.date()treats numbers as milliseconds, not seconds. The C.2 webhook handler must multiply Stripe'scancel_at(Unix seconds) by 1000 before passing to the schema:cancelAt: new Date(stripeEvent.cancel_at * 1000).Zod/Mongoose asymmetry (intentional)
cancelAtPeriodEnd:type: Boolean, default: null— DB can hold null for legacy/unsynced docscancelAtPeriodEnd:z.boolean().optional()(NOT.nullable()) — new writes via schema validation must be boolean or omitted; null is rejected at the validation layerThis layering is intentional and matches the trawl_node production pattern.
Test coverage
New file
billing.subscription.schema.unit.tests.js(15 tests) covers:Pre-push gate
Phase 0 DeepSeek review: OK with nits (2 test naming issues — fixed before push). 0 critical, 0 high findings.
Closes #3740