Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

fix: make billing dunning email state retriable

Blue Mouse85bb10d70fdb
diff --git a/backend/apps/cloud/src/common/templates/en/subscription-payment-failed.html b/backend/apps/cloud/src/common/templates/en/subscription-payment-failed.html
index 909a3df72..5c65156da 100644
--- a/backend/apps/cloud/src/common/templates/en/subscription-payment-failed.html
+++ b/backend/apps/cloud/src/common/templates/en/subscription-payment-failed.html
@@ -1,9 +1,9 @@
We could not process your latest Swetrix subscription payment{{#if
amountFormatted}} for {{amountFormatted}}{{/if}}.<br /><br />
-Your dashboard remains available while Paddle retries the payment{{#if
-nextRetryAtFormatted}} on {{nextRetryAtFormatted}}{{/if}}. Please update your
-payment details to avoid an access lock.<br /><br />
+Your dashboard remains available while Paddle (our payment provider) retries the
+payment{{#if nextRetryAtFormatted}} on {{nextRetryAtFormatted}}{{/if}}. Please
+update your payment details to avoid an access lock.<br /><br />
<a href="{{billingUrl}}" target="_blank">Update payment details</a>
<br /><br />
diff --git a/backend/apps/cloud/src/common/templates/en/subscription-payment-final-warning.html b/backend/apps/cloud/src/common/templates/en/subscription-payment-final-warning.html
index cbd85b9c3..c99ff5408 100644
--- a/backend/apps/cloud/src/common/templates/en/subscription-payment-final-warning.html
+++ b/backend/apps/cloud/src/common/templates/en/subscription-payment-final-warning.html
@@ -2,8 +2,8 @@ Your Swetrix subscription payment still has not gone through.<br /><br />
Your dashboard is scheduled to lock{{#if suspendsAtFormatted}} on
{{suspendsAtFormatted}}{{/if}}, but we will keep your projects and analytics
-data safe. Update your payment details before then to keep access uninterrupted.
-<br /><br />
+data safe. Update your payment details before then to keep access
+uninterrupted.<br /><br />
<a href="{{billingUrl}}" target="_blank">Update payment details</a>
<br /><br />
diff --git a/backend/apps/cloud/src/mailer/mailer.service.ts b/backend/apps/cloud/src/mailer/mailer.service.ts
index 91ef9de48..4f13ab9c3 100644
--- a/backend/apps/cloud/src/mailer/mailer.service.ts
+++ b/backend/apps/cloud/src/mailer/mailer.service.ts
@@ -125,18 +125,18 @@ const metaInfoJson = {
[LetterTemplate.UsageOverLimit]: {
subject: {
en: () =>
- '[ACTION REQUIRED] You have exceeded your Swetrix subscription plan',
+ 'Action required: you have exceeded your Swetrix subscription plan',
},
},
[LetterTemplate.DashboardLockedExceedingLimits]: {
subject: {
- en: () => '[ACTION REQUIRED] Your Swetrix dashboard has been locked',
+ en: () => 'Action required: your Swetrix dashboard has been locked',
},
},
[LetterTemplate.DashboardLockedPaymentFailure]: {
subject: {
en: () =>
- '[ACTION REQUIRED] Your Swetrix dashboard has been locked due to a payment issue',
+ 'Action required: your Swetrix dashboard has been locked due to a payment issue',
},
},
[LetterTemplate.SocialIdentityLinked]: {
diff --git a/backend/apps/cloud/src/task-manager/task-manager.service.ts b/backend/apps/cloud/src/task-manager/task-manager.service.ts
index 67135027e..c1c576b7f 100644
--- a/backend/apps/cloud/src/task-manager/task-manager.service.ts
+++ b/backend/apps/cloud/src/task-manager/task-manager.service.ts
@@ -1497,6 +1497,13 @@ export class TaskManagerService {
_map(suspensionDunnings, async (dunning) => {
const user = dunning.user
+ await this.mailerService.sendEmail(
+ user.email,
+ LetterTemplate.DashboardLockedPaymentFailure,
+ {
+ billingUrl: user.subUpdateURL || BILLING_URL,
+ },
+ )
await this.userService.update(user.id, {
dashboardBlockReason: DashboardBlockReason.payment_failed,
isAccountBillingSuspended: true,
@@ -1506,13 +1513,6 @@ export class TaskManagerService {
emailStage: BillingDunningEmailStage.locked,
})
await this.projectService.clearProjectsRedisCache(user.id)
- await this.mailerService.sendEmail(
- user.email,
- LetterTemplate.DashboardLockedPaymentFailure,
- {
- billingUrl: user.subUpdateURL || BILLING_URL,
- },
- )
}),
)
diff --git a/backend/apps/cloud/src/user/user.service.ts b/backend/apps/cloud/src/user/user.service.ts
index 23b758a66..c0196860f 100644
--- a/backend/apps/cloud/src/user/user.service.ts
+++ b/backend/apps/cloud/src/user/user.service.ts
@@ -2841,18 +2841,53 @@ export class UserService {
async saveSubscriptionDunning(
dunning: Partial<SubscriptionDunning>,
): Promise<SubscriptionDunning> {
- if (dunning.id) {
- const { id, ...update } = dunning
+ const { id, ...dunningParams } = dunning
- await this.subscriptionDunningRepository.update(id, update)
+ if (id) {
+ await this.subscriptionDunningRepository.update(id, dunningParams)
return this.subscriptionDunningRepository.findOne({
where: { id },
})
}
- return this.subscriptionDunningRepository.save(
- this.subscriptionDunningRepository.create(dunning),
+ if (!dunningParams.userId) {
+ return this.subscriptionDunningRepository.save(
+ this.subscriptionDunningRepository.create(dunningParams),
+ )
+ }
+
+ const userId = dunningParams.userId
+
+ return this.subscriptionDunningRepository.manager.transaction(
+ async (manager) => {
+ await this.getLockedUser(manager, userId)
+
+ const repository = manager.getRepository(SubscriptionDunning)
+ const existingDunning = await repository.findOne({
+ where: {
+ userId,
+ status: In([
+ SubscriptionDunningStatus.active,
+ SubscriptionDunningStatus.locked,
+ ]),
+ },
+ order: {
+ createdAt: 'DESC',
+ },
+ lock: { mode: 'pessimistic_write' },
+ })
+
+ if (existingDunning) {
+ await repository.update(existingDunning.id, dunningParams)
+
+ return repository.findOne({
+ where: { id: existingDunning.id },
+ })
+ }
+
+ return repository.save(repository.create(dunningParams))
+ },
)
}
diff --git a/backend/apps/cloud/src/webhook/webhook.controller.ts b/backend/apps/cloud/src/webhook/webhook.controller.ts
index da65fce71..fc980a938 100644
--- a/backend/apps/cloud/src/webhook/webhook.controller.ts
+++ b/backend/apps/cloud/src/webhook/webhook.controller.ts
@@ -314,7 +314,6 @@ export class WebhookController {
? SubscriptionDunningStatus.locked
: SubscriptionDunningStatus.active,
attempt,
- emailStage,
startedAt: dunning?.startedAt || now,
lastFailedAt: now,
nextRetryAt,
@@ -337,7 +336,8 @@ export class WebhookController {
})
}
- await this.userService.saveSubscriptionDunning(updateParams)
+ const savedDunning =
+ await this.userService.saveSubscriptionDunning(updateParams)
await this.projectService.clearProjectsRedisCache(user.id)
const billingUrl = updateUrl || this.getBillingUrl(user)
@@ -349,6 +349,9 @@ export class WebhookController {
LetterTemplate.DashboardLockedPaymentFailure,
{ billingUrl },
)
+ await this.userService.updateSubscriptionDunning(savedDunning.id, {
+ emailStage,
+ })
}
return
@@ -371,6 +374,9 @@ export class WebhookController {
suspendsAtFormatted: this.formatDateForEmail(suspendsAt),
},
)
+ await this.userService.updateSubscriptionDunning(savedDunning.id, {
+ emailStage,
+ })
}
return
@@ -392,6 +398,9 @@ export class WebhookController {
nextRetryAtFormatted: this.formatDateForEmail(nextRetryAt),
},
)
+ await this.userService.updateSubscriptionDunning(savedDunning.id, {
+ emailStage,
+ })
}
}