feat: add validation to prevent multiple free plan purchases within a month

- Add FREE_PLAN_LIMIT_EXCEEDED error message in Persian
- Implement validation logic in subscribeToPlan method to check for existing free plan subscriptions
- Users can only purchase free plans once per month
- Added MoreThan import from TypeORM for date comparison
This commit is contained in:
mahyargdz
2025-08-17 09:35:12 +03:30
parent d8e91231b3
commit ca0510f2c2
2 changed files with 20 additions and 1 deletions
@@ -4,7 +4,7 @@ import { Queue } from "bullmq";
import dayjs from "dayjs";
import Decimal from "decimal.js";
import slugify from "slugify";
import { DataSource, In, Not, QueryRunner } from "typeorm";
import { DataSource, In, MoreThan, Not, QueryRunner } from "typeorm";
import { ServiceMessage, SubscriptionMessage } from "../../../common/enums/message.enum";
import { DanakServicesService } from "../../danak-services/providers/danak-services.service";
@@ -225,6 +225,24 @@ export class SubscriptionsService {
});
if (!plan) throw new BadRequestException(SubscriptionMessage.NOT_FOUND);
if (plan.isFree) {
const oneMonthAgo = dayjs().subtract(1, "month").toDate();
const existingFreePlanSubscription = await queryRunner.manager.findOne(UserSubscription, {
where: {
user: { id: userId },
plan: { isFree: true },
startDate: MoreThan(oneMonthAgo),
},
relations: {
plan: true,
},
});
if (existingFreePlanSubscription) {
throw new BadRequestException(SubscriptionMessage.FREE_PLAN_LIMIT_EXCEEDED);
}
}
const startDate = dayjs().toDate();
const endDate = dayjs().add(plan.duration, "day").toDate();