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!

process cancellationEffectiveDate

Blue Mouse45a42d779d71
diff --git a/src/task-manager/task-manager.service.ts b/src/task-manager/task-manager.service.ts
index ab8e66643..367ed26b9 100644
--- a/src/task-manager/task-manager.service.ts
+++ b/src/task-manager/task-manager.service.ts
@@ -20,7 +20,9 @@ import { AlertService } from 'src/alert/alert.service'
import { ActionTokenType } from '../action-tokens/action-token.entity'
import { LetterTemplate } from '../mailer/letter'
import { AnalyticsService } from '../analytics/analytics.service'
-import { ReportFrequency, ACCOUNT_PLANS, PlanCode } from '../user/entities/user.entity'
+import {
+ ReportFrequency, ACCOUNT_PLANS, PlanCode, BillingFrequency,
+} from '../user/entities/user.entity'
import {
clickhouse,
redis,
@@ -380,6 +382,30 @@ export class TaskManagerService {
}
}
+ @Cron(CronExpression.EVERY_2_HOURS)
+ async cleanUpUnpaidSubUsers(): Promise<void> {
+ const users = await this.userService.find({
+ where: {
+ cancellationEffectiveDate: Not(IsNull()),
+ }
+ })
+
+ for (let i = 0; i < _size(users); ++i) {
+ const user = users[i]
+ const cancellationEffectiveDate = new Date(user.cancellationEffectiveDate)
+ const now = new Date()
+
+ if (now > cancellationEffectiveDate) {
+ await this.userService.update(user.id, {
+ cancellationEffectiveDate: null,
+ planCode: PlanCode.none,
+ nextBillDate: null,
+ billingFrequency: BillingFrequency.Monthly,
+ })
+ }
+ }
+ }
+
@Cron(CronExpression.EVERY_5_MINUTES)
// @Cron(CronExpression.EVERY_5_SECONDS)
async checkOnlineUsersAlerts(): Promise<void> {
diff --git a/src/user/entities/user.entity.ts b/src/user/entities/user.entity.ts
index 81abebc9a..72b8b72b8 100644
--- a/src/user/entities/user.entity.ts
+++ b/src/user/entities/user.entity.ts
@@ -239,6 +239,9 @@ export class User {
@Column({ type: 'date', nullable: true })
nextBillDate: Date
+ @Column({ type: 'date', nullable: true })
+ cancellationEffectiveDate: Date
+
@Column({
type: 'varchar',
length: 36,
diff --git a/src/user/user.controller.ts b/src/user/user.controller.ts
index 2dd85a842..fbc303908 100644
--- a/src/user/user.controller.ts
+++ b/src/user/user.controller.ts
@@ -569,6 +569,7 @@ export class UserController {
'isTwoFactorAuthenticationEnabled',
'isTelegramChatIdConfirmed',
'trialEndDate',
+ 'cancellationEffectiveDate',
])
await this.userService.update(id, userToUpdate)
diff --git a/src/webhook/webhook.controller.ts b/src/webhook/webhook.controller.ts
index 9ac50188f..8a08b1b90 100644
--- a/src/webhook/webhook.controller.ts
+++ b/src/webhook/webhook.controller.ts
@@ -21,6 +21,8 @@ import { AppLoggerService } from '../logger/logger.service'
import { WebhookService } from './webhook.service'
import { SelfhostedGuard } from '../common/guards/selfhosted.guard'
+const MAX_PAYMENT_ATTEMPTS = 5
+
@ApiTags('Webhook')
@Controller('webhook')
export class WebhookController {
@@ -107,18 +109,44 @@ export class WebhookController {
break
}
- case 'subscription_cancelled':
- case 'subscription_payment_failed':
+ case 'subscription_cancelled': {
+ const { subscription_id, cancellation_effective_date } = body
+
+ await this.userService.updateBySubID(subscription_id, {
+ billingFrequency: BillingFrequency.Monthly,
+ nextBillDate: null,
+ cancellationEffectiveDate: cancellation_effective_date,
+ })
+
+ break
+ }
+
case 'subscription_payment_refunded': {
const { subscription_id } = body
await this.userService.updateBySubID(subscription_id, {
planCode: PlanCode.none,
billingFrequency: BillingFrequency.Monthly,
+ nextBillDate: null,
})
break
}
+
+ case 'subscription_payment_failed': {
+ const { subscription_id, attempt_number } = body
+
+ if (parseInt(attempt_number) >= MAX_PAYMENT_ATTEMPTS) {
+ await this.userService.updateBySubID(subscription_id, {
+ planCode: PlanCode.none,
+ billingFrequency: BillingFrequency.Monthly,
+ nextBillDate: null,
+ })
+ }
+
+ break
+ }
+
default:
throw new BadRequestException('Unexpected event type')
}