feat(subscriptions): add trial period validation for subscriptions

- Implemented a check to verify if a user has already utilized their trial period before allowing access to subscription services.
- Enhanced error handling to provide a clear message when a trial period has been previously used, improving user experience and subscription management.
This commit is contained in:
vchikalkin 2025-09-02 20:13:10 +03:00
parent 9903fe4233
commit da51d45882

View File

@ -44,6 +44,22 @@ export class SubscriptionsService extends BaseService {
// Получаем пользователя и проверяем бан
const { customer } = await this.checkIsBanned();
// Проверяем, не использовал ли пользователь уже пробный период
const { subscription: existingSubscription } = await this.getSubscription({
telegramId: customer.telegramId,
});
if (existingSubscription) {
// Проверяем, есть ли в истории успешная пробная подписка
const hasUsedTrial = existingSubscription.subscriptionHistories?.some(
(item) =>
item?.period === GQL.Enum_Subscriptionhistory_Period.Trial &&
item?.state === GQL.Enum_Subscriptionhistory_State.Success,
);
if (hasUsedTrial) {
throw new Error('Пробный период уже был использован');
}
}
// Получаем цены подписки для определения длительности пробного периода
const { subscriptionPrices } = await this.getSubscriptionPrices({ isActive: true });
if (!subscriptionPrices) throw new Error('Subscription prices not found');