diff --git a/packages/graphql/api/subscriptions.ts b/packages/graphql/api/subscriptions.ts index c6a79f4..0084e74 100644 --- a/packages/graphql/api/subscriptions.ts +++ b/packages/graphql/api/subscriptions.ts @@ -5,6 +5,15 @@ import { OrdersService } from './orders'; import { type VariablesOf } from '@graphql-typed-document-node/core'; import dayjs from 'dayjs'; +export const ERRORS = { + FAILED_TO_CREATE_TRIAL_SUBSCRIPTION: 'Не удалось создать пробную подписку', + SUBSCRIPTION_PRICES_NOT_FOUND: 'Цены подписки не найдены', + SUBSCRIPTION_SETTING_NOT_FOUND: 'Настройки подписки не найдены', + TRIAL_PERIOD_ALREADY_USED: 'Пробный период уже был использован', + TRIAL_PERIOD_NOT_ACTIVE: 'Пробный период неактивен', + TRIAL_PERIOD_NOT_FOUND: 'Пробный период не найден', +}; + export class SubscriptionsService extends BaseService { async createSubscription(variables: VariablesOf) { await this.checkIsBanned(); @@ -56,20 +65,20 @@ export class SubscriptionsService extends BaseService { item?.state === GQL.Enum_Subscriptionhistory_State.Success, ); if (hasUsedTrial) { - throw new Error('Пробный период уже был использован'); + throw new Error(ERRORS.TRIAL_PERIOD_ALREADY_USED); } } // Получаем цены подписки для определения длительности пробного периода const { subscriptionPrices } = await this.getSubscriptionPrices({ isActive: true }); - if (!subscriptionPrices) throw new Error('Subscription prices not found'); + if (!subscriptionPrices) throw new Error(ERRORS.SUBSCRIPTION_PRICES_NOT_FOUND); // Ищем пробный период const trialPrice = subscriptionPrices.find( (price) => price?.period === GQL.Enum_Subscriptionprice_Period.Trial, ); - if (!trialPrice) throw new Error('Trial period not found'); - if (!trialPrice.isActive) throw new Error('Trial period is not active'); + if (!trialPrice) throw new Error(ERRORS.TRIAL_PERIOD_NOT_FOUND); + if (!trialPrice.isActive) throw new Error(ERRORS.TRIAL_PERIOD_NOT_ACTIVE); const trialPeriodDays = trialPrice?.days; const now = dayjs(); @@ -86,7 +95,7 @@ export class SubscriptionsService extends BaseService { }); if (!subscriptionData?.createSubscription) { - throw new Error('Failed to create trial subscription'); + throw new Error(ERRORS.FAILED_TO_CREATE_TRIAL_SUBSCRIPTION); } const subscription = subscriptionData.createSubscription; @@ -222,7 +231,7 @@ export class SubscriptionsService extends BaseService { const { subscriptionSetting } = await this.getSubscriptionSettings(); - if (!subscriptionSetting) throw new Error('Subscription setting not found'); + if (!subscriptionSetting) throw new Error(ERRORS.SUBSCRIPTION_SETTING_NOT_FOUND); const { maxOrdersPerMonth } = subscriptionSetting;