Skip to content

Commit

Permalink
chore: move cron func to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
darkskygit committed Jan 10, 2025
1 parent c4bf3f1 commit dc2b414
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 76 deletions.
70 changes: 70 additions & 0 deletions packages/backend/server/src/plugins/payment/cron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,76 @@ export class SubscriptionCronJobs {
private readonly event: EventEmitter
) {}

private getDateRange(after: number, base = new Date()) {
const start = new Date(base);
start.setDate(start.getDate() + after);
start.setHours(0, 0, 0, 0);

const end = new Date(start);
end.setHours(23, 59, 59, 999);

return { start, end };
}

@Cron(CronExpression.EVERY_DAY_AT_MIDNIGHT /* everyday at 12am */)
async notifyExpiredWorkspace() {
const { start: after30DayStart, end: after30DayEnd } =
this.getDateRange(30);
const { start: todayStart, end: todayEnd } = this.getDateRange(0);
const { start: before150DaysStart, end: before150DaysEnd } =
this.getDateRange(-150);
const { start: before180DaysStart, end: before180DaysEnd } =
this.getDateRange(-180);

const subscriptions = await this.db.subscription.findMany({
where: {
plan: SubscriptionPlan.Team,
OR: [
{
// subscription will cancel after 30 days
status: 'active',
canceledAt: { not: null },
end: { gte: after30DayStart, lte: after30DayEnd },
},
{
// subscription will cancel today
status: 'active',
canceledAt: { not: null },
end: { gte: todayStart, lte: todayEnd },
},
{
// subscription has been canceled for 150 days
// workspace becomes delete after 180 days
status: 'canceled',
canceledAt: { gte: before150DaysStart, lte: before150DaysEnd },
},
{
// subscription has been canceled for 180 days
// workspace becomes delete after 180 days
status: 'canceled',
canceledAt: { gte: before180DaysStart, lte: before180DaysEnd },
},
],
},
});

for (const subscription of subscriptions) {
const end = subscription.end;
if (!end) {
// should not reach here
continue;
}
this.event.emit('workspace.subscription.notify', {
workspaceId: subscription.targetId,
expirationDate: end,
deletionDate:
subscription.status === 'canceled'
? this.getDateRange(180, end).end
: undefined,
});
}
}

@Cron(CronExpression.EVERY_HOUR)
async cleanExpiredOnetimeSubscriptions() {
const subscriptions = await this.db.subscription.findMany({
Expand Down
77 changes: 1 addition & 76 deletions packages/backend/server/src/plugins/payment/quota.ts
Original file line number Diff line number Diff line change
@@ -1,94 +1,19 @@
import { Injectable } from '@nestjs/common';
import { OnEvent } from '@nestjs/event-emitter';
import { Cron, CronExpression } from '@nestjs/schedule';
import { PrismaClient } from '@prisma/client';

import { EventEmitter, type EventPayload } from '../../base';
import { type EventPayload } from '../../base';
import { PermissionService } from '../../core/permission';
import { QuotaManagementService, QuotaType } from '../../core/quota';
import { WorkspaceService } from '../../core/workspaces/resolvers';
import { SubscriptionPlan } from './types';

@Injectable()
export class TeamQuotaOverride {
constructor(
private readonly db: PrismaClient,
private readonly event: EventEmitter,
private readonly manager: QuotaManagementService,
private readonly permission: PermissionService,
private readonly workspace: WorkspaceService
) {}

private getDateRange(after: number, base = new Date()) {
const start = new Date(base);
start.setDate(start.getDate() + after);
start.setHours(0, 0, 0, 0);

const end = new Date(start);
end.setHours(23, 59, 59, 999);

return { start, end };
}

@Cron(CronExpression.EVERY_DAY_AT_MIDNIGHT /* everyday at 12am */)
async cleanupExpiredHistory() {
const { start: after30DayStart, end: after30DayEnd } =
this.getDateRange(30);
const { start: todayStart, end: todayEnd } = this.getDateRange(0);
const { start: before150DaysStart, end: before150DaysEnd } =
this.getDateRange(-150);
const { start: before180DaysStart, end: before180DaysEnd } =
this.getDateRange(-180);

const subscriptions = await this.db.subscription.findMany({
where: {
plan: SubscriptionPlan.Team,
OR: [
{
// subscription will cancel after 30 days
status: 'active',
canceledAt: { not: null },
end: { gte: after30DayStart, lte: after30DayEnd },
},
{
// subscription will cancel today
status: 'active',
canceledAt: { not: null },
end: { gte: todayStart, lte: todayEnd },
},
{
// subscription has been canceled for 150 days
// workspace becomes delete after 180 days
status: 'canceled',
canceledAt: { gte: before150DaysStart, lte: before150DaysEnd },
},
{
// subscription has been canceled for 180 days
// workspace becomes delete after 180 days
status: 'canceled',
canceledAt: { gte: before180DaysStart, lte: before180DaysEnd },
},
],
},
});

for (const subscription of subscriptions) {
const end = subscription.end;
if (!end) {
// should not reach here
continue;
}
this.event.emit('workspace.subscription.notify', {
workspaceId: subscription.targetId,
expirationDate: end,
deletionDate:
subscription.status === 'canceled'
? this.getDateRange(180, end).end
: undefined,
});
}
}

@OnEvent('workspace.subscription.activated')
async onSubscriptionUpdated({
workspaceId,
Expand Down

0 comments on commit dc2b414

Please sign in to comment.