Repository navigation
fix: disable auto top-up after 7 days #1918
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
Changes from 2 commits
f65e3b3
154efd9
2534d41
cfa8653
fc9a58e
3f18768
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 |
|---|---|---|
|
|
@@ -568,6 +568,7 @@ async function recordCreditTopUp({ | |
| credits: sql`${tables.organization.credits} + ${finalCreditAmount}`, | ||
| paymentFailureCount: 0, | ||
| lastPaymentFailureAt: null, | ||
| paymentFailureStartedAt: null, | ||
| }) | ||
| .where(eq(tables.organization.id, organizationId)); | ||
|
|
||
|
|
@@ -810,6 +811,7 @@ async function handlePaymentIntentSucceeded( | |
| credits: sql`${tables.organization.credits} + ${finalCreditAmount}`, | ||
| paymentFailureCount: 0, | ||
| lastPaymentFailureAt: null, | ||
| paymentFailureStartedAt: null, | ||
| }) | ||
| .where(eq(tables.organization.id, organizationId)); | ||
|
|
||
|
|
@@ -1027,6 +1029,7 @@ async function handlePaymentIntentFailed( | |
| // Calculate new failure count and check if we should send an email | ||
| const previousFailureCount = organization.paymentFailureCount ?? 0; | ||
| const previousFailureAt = organization.lastPaymentFailureAt; | ||
| const failureStartedAt = organization.paymentFailureStartedAt ?? new Date(); | ||
|
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.
This assignment updates the 7-day disable timer for every Useful? React with 👍 / 👎. |
||
| const newFailureCount = previousFailureCount + 1; | ||
|
|
||
| // Update organization with new failure count and timestamp | ||
|
|
@@ -1035,6 +1038,7 @@ async function handlePaymentIntentFailed( | |
| .set({ | ||
| paymentFailureCount: newFailureCount, | ||
| lastPaymentFailureAt: new Date(), | ||
| paymentFailureStartedAt: failureStartedAt, | ||
| }) | ||
|
Comment on lines
1032
to
1042
|
||
| .where(eq(tables.organization.id, organizationId)); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -67,6 +67,9 @@ const AUTO_TOPUP_LOCK_KEY = "auto_topup_check"; | |||||||||||||||||||||||||||||||||||||||||||||
| const CREDIT_PROCESSING_LOCK_KEY = "credit_processing"; | ||||||||||||||||||||||||||||||||||||||||||||||
| const DATA_RETENTION_LOCK_KEY = "data_retention_cleanup"; | ||||||||||||||||||||||||||||||||||||||||||||||
| const LOCK_DURATION_MINUTES = 5; | ||||||||||||||||||||||||||||||||||||||||||||||
| const AUTO_TOPUP_DISABLE_AFTER_DAYS = 7; | ||||||||||||||||||||||||||||||||||||||||||||||
| const AUTO_TOPUP_DISABLE_AFTER_MS = | ||||||||||||||||||||||||||||||||||||||||||||||
| AUTO_TOPUP_DISABLE_AFTER_DAYS * 24 * 60 * 60 * 1000; | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| // Configuration for batch processing | ||||||||||||||||||||||||||||||||||||||||||||||
| const BATCH_SIZE = Number(process.env.CREDIT_BATCH_SIZE) || 100; | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -161,7 +164,7 @@ async function releaseLock(key: string): Promise<void> { | |||||||||||||||||||||||||||||||||||||||||||||
| await db.delete(tables.lock).where(eq(tables.lock.key, key)); | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| async function processAutoTopUp(): Promise<void> { | ||||||||||||||||||||||||||||||||||||||||||||||
| export async function processAutoTopUp(): Promise<void> { | ||||||||||||||||||||||||||||||||||||||||||||||
| const lockAcquired = await acquireLock(AUTO_TOPUP_LOCK_KEY); | ||||||||||||||||||||||||||||||||||||||||||||||
| if (!lockAcquired) { | ||||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -215,6 +218,27 @@ async function processAutoTopUp(): Promise<void> { | |||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| if ( | ||||||||||||||||||||||||||||||||||||||||||||||
| org.paymentFailureStartedAt && | ||||||||||||||||||||||||||||||||||||||||||||||
| Date.now() - org.paymentFailureStartedAt.getTime() >= | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+221
to
+223
|
||||||||||||||||||||||||||||||||||||||||||||||
| if ( | |
| org.paymentFailureStartedAt && | |
| Date.now() - org.paymentFailureStartedAt.getTime() >= | |
| // Backfill paymentFailureStartedAt for legacy organizations if needed | |
| let paymentFailureStartedAt = org.paymentFailureStartedAt; | |
| if ( | |
| !paymentFailureStartedAt && | |
| org.paymentFailureCount > 0 && | |
| org.lastPaymentFailureAt | |
| ) { | |
| await db | |
| .update(tables.organization) | |
| .set({ | |
| paymentFailureStartedAt: org.lastPaymentFailureAt, | |
| }) | |
| .where(eq(tables.organization.id, org.id)); | |
| paymentFailureStartedAt = org.lastPaymentFailureAt; | |
| } | |
| if ( | |
| paymentFailureStartedAt && | |
| Date.now() - paymentFailureStartedAt.getTime() >= |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ALTER TABLE "organization" ADD COLUMN "payment_failure_started_at" timestamp; |
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.
paymentFailureStartedAtis initialized for everypayment_intent.payment_failedevent, including non-auto-top-up failures (e.g., manual top-ups or subscription-related payment intents resolved via customer lookup). With this commit’s worker logic (apps/worker/src/worker.ts) disabling auto top-up after 7 days based on this field, unrelated payment failures can now shut off auto top-up for an organization even when auto top-up itself hasn’t been failing. Restrict setting this streak start (and the associated counters used for disablement) to verified auto top-up failures only (such as events carrying the auto top-up transaction metadata).Useful? React with 👍 / 👎.