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:
@@ -488,6 +488,7 @@ export const enum SubscriptionMessage {
|
|||||||
SLUG_STRING = "اسلاگ باید یک رشته باشد",
|
SLUG_STRING = "اسلاگ باید یک رشته باشد",
|
||||||
SLUG_LENGTH = "اسلاگ باید بین ۴ تا ۲۵۰ کاراکتر باشد",
|
SLUG_LENGTH = "اسلاگ باید بین ۴ تا ۲۵۰ کاراکتر باشد",
|
||||||
SLUG_EXISTS = "این اسلاگ قبلا ثبت شده است",
|
SLUG_EXISTS = "این اسلاگ قبلا ثبت شده است",
|
||||||
|
FREE_PLAN_LIMIT_EXCEEDED = "شما در یک ماه گذشته از پلن رایگان استفاده کردهاید و نمیتوانید مجدداً استفاده کنید",
|
||||||
USER_DOES_NOT_HAVE_ACCESS = "شما دسترسی به این سرویس ندارید",
|
USER_DOES_NOT_HAVE_ACCESS = "شما دسترسی به این سرویس ندارید",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { Queue } from "bullmq";
|
|||||||
import dayjs from "dayjs";
|
import dayjs from "dayjs";
|
||||||
import Decimal from "decimal.js";
|
import Decimal from "decimal.js";
|
||||||
import slugify from "slugify";
|
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 { ServiceMessage, SubscriptionMessage } from "../../../common/enums/message.enum";
|
||||||
import { DanakServicesService } from "../../danak-services/providers/danak-services.service";
|
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) 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 startDate = dayjs().toDate();
|
||||||
const endDate = dayjs().add(plan.duration, "day").toDate();
|
const endDate = dayjs().add(plan.duration, "day").toDate();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user