feat(subscriptions): enhance error handling with centralized error messages

- Introduced a centralized `ERRORS` object in the `subscriptions.ts` file to standardize error messages related to trial subscriptions.
- Updated error handling in the `createSubscription` method to utilize the new error messages, improving maintainability and clarity for subscription-related errors.
This commit is contained in:
vchikalkin 2025-09-02 21:08:28 +03:00
parent 66f347cc42
commit 685027de1d

View File

@ -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<typeof GQL.CreateSubscriptionDocument>) {
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;