gg
This commit is contained in:
@@ -141,7 +141,7 @@ export class LimitResetterTask {
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset subscription status for expired users
|
||||
* Downgrade cancelled subscriptions that have passed their cancel effective date
|
||||
*/
|
||||
@Cron("0 0 * * *", { timeZone: "Europe/Istanbul" })
|
||||
async checkSubscriptions() {
|
||||
@@ -155,21 +155,55 @@ export class LimitResetterTask {
|
||||
try {
|
||||
const now = new Date();
|
||||
|
||||
const result = await this.prisma.user.updateMany({
|
||||
// Find subscriptions with passed cancel effective date
|
||||
const expiredSubs = await this.prisma.subscription.findMany({
|
||||
where: {
|
||||
subscriptionStatus: "active",
|
||||
subscriptionExpiresAt: { lt: now },
|
||||
},
|
||||
data: {
|
||||
subscriptionStatus: "expired",
|
||||
plan: "cancelled",
|
||||
cancelEffectiveDate: { lt: now },
|
||||
},
|
||||
select: { id: true, userId: true },
|
||||
});
|
||||
|
||||
if (result.count > 0) {
|
||||
this.logger.log(`${result.count} subscriptions marked as expired`);
|
||||
for (const sub of expiredSubs) {
|
||||
// Downgrade to free
|
||||
await this.prisma.user.update({
|
||||
where: { id: sub.userId },
|
||||
data: { subscriptionStatus: "free" },
|
||||
});
|
||||
|
||||
// Sync limits to free tier
|
||||
await this.prisma.usageLimit.upsert({
|
||||
where: { userId: sub.userId },
|
||||
update: { maxAnalyses: 3, maxCoupons: 1 },
|
||||
create: {
|
||||
userId: sub.userId,
|
||||
analysisCount: 0,
|
||||
couponCount: 0,
|
||||
maxAnalyses: 3,
|
||||
maxCoupons: 1,
|
||||
lastResetDate: new Date(),
|
||||
},
|
||||
});
|
||||
|
||||
// Reset subscription to free
|
||||
await this.prisma.subscription.update({
|
||||
where: { id: sub.id },
|
||||
data: {
|
||||
plan: "free",
|
||||
cancelledAt: null,
|
||||
cancelEffectiveDate: null,
|
||||
},
|
||||
});
|
||||
}
|
||||
} catch (error: any) {
|
||||
this.logger.error(`Subscription check failed: ${error.message}`);
|
||||
|
||||
if (expiredSubs.length > 0) {
|
||||
this.logger.log(
|
||||
`${expiredSubs.length} cancelled subscriptions downgraded to free`,
|
||||
);
|
||||
}
|
||||
} catch (error: unknown) {
|
||||
const err = error as Error;
|
||||
this.logger.error(`Subscription check failed: ${err.message}`);
|
||||
}
|
||||
},
|
||||
this.logger,
|
||||
|
||||
Reference in New Issue
Block a user