fix(api): standardize error messages for customer and slot retrieval, and improve validation logic in slots service

This commit is contained in:
vchikalkin 2025-08-05 13:31:08 +03:00
parent 620d2eaff4
commit d8e88d8934
2 changed files with 14 additions and 14 deletions

View File

@ -3,8 +3,8 @@ import { getClientWithToken } from '../apollo/client';
import * as GQL from '../types'; import * as GQL from '../types';
export const ERRORS = { export const ERRORS = {
CUSTOMER_NOT_FOUND: 'Пользователь не найден',
MISSING_TELEGRAM_ID: 'Не указан Telegram ID', MISSING_TELEGRAM_ID: 'Не указан Telegram ID',
NOT_FOUND_CUSTOMER: 'Пользователь не найден',
}; };
type UserProfile = { type UserProfile = {
@ -32,7 +32,7 @@ export class BaseService {
const customer = result.data.customers.at(0); const customer = result.data.customers.at(0);
if (!customer) throw new Error(ERRORS.CUSTOMER_NOT_FOUND); if (!customer) throw new Error(ERRORS.NOT_FOUND_CUSTOMER);
return { customer }; return { customer };
} }

View File

@ -8,18 +8,18 @@ import { getMinutes, isBeforeToday } from '@repo/utils/datetime-format';
import dayjs from 'dayjs'; import dayjs from 'dayjs';
export const ERRORS = { export const ERRORS = {
HAS_ORDERS: 'Слот имеет активные заказы',
INACTIVE_MASTER: 'Пользователь не является активным или мастером', INACTIVE_MASTER: 'Пользователь не является активным или мастером',
INVALID_TIME: 'Некорректное время', INVALID_TIME: 'Некорректное время',
MASTER_NOT_FOUND: 'Мастер не найден',
MISSING_DATETIME_END: 'Не указана дата окончания', MISSING_DATETIME_END: 'Не указана дата окончания',
MISSING_DATETIME_START: 'Не указана дата начала', MISSING_DATETIME_START: 'Не указана дата начала',
MISSING_SERVICE_ID: 'Не указана услуга', MISSING_SERVICE_ID: 'Не указана услуга',
NO_PAST_SLOT: 'Нельзя создать слот в прошлом',
NO_PERMISSION: 'Нет доступа', NO_PERMISSION: 'Нет доступа',
NOT_FOUND_MASTER: 'Мастер не найден',
NOT_FOUND_SERVICE: 'Сервис не найден',
NOT_FOUND_SLOT: 'Слот не найден',
OVERLAPPING_TIME: 'Время пересекается с другими слотами', OVERLAPPING_TIME: 'Время пересекается с другими слотами',
PAST_SLOT: 'Нельзя создать слот в прошлом', SLOT_HAS_ORDERS: 'Слот имеет активные заказы',
SERVICE_NOT_FOUND: 'Сервис не найден',
SLOT_NOT_FOUND: 'Слот не найден',
}; };
export class SlotsService extends BaseService { export class SlotsService extends BaseService {
@ -53,7 +53,7 @@ export class SlotsService extends BaseService {
const { slot } = await this.getSlot({ documentId: variables.documentId }); const { slot } = await this.getSlot({ documentId: variables.documentId });
if (slot?.orders?.length) { if (slot?.orders?.length) {
throw new Error(ERRORS.HAS_ORDERS); throw new Error(ERRORS.SLOT_HAS_ORDERS);
} }
const { mutate } = await getClientWithToken(); const { mutate } = await getClientWithToken();
@ -96,7 +96,7 @@ export class SlotsService extends BaseService {
documentId: context.service.documentId.eq, documentId: context.service.documentId.eq,
}); });
if (!service) throw new Error(ERRORS.SERVICE_NOT_FOUND); if (!service) throw new Error(ERRORS.NOT_FOUND_SERVICE);
const serviceDuration = getMinutes(service.duration); const serviceDuration = getMinutes(service.duration);
@ -183,7 +183,7 @@ export class SlotsService extends BaseService {
// Проверка, что мастер существует и активен // Проверка, что мастер существует и активен
const { customer: masterEntity } = await this._getUser(); const { customer: masterEntity } = await this._getUser();
if (!masterEntity) throw new Error(ERRORS.MASTER_NOT_FOUND); if (!masterEntity) throw new Error(ERRORS.NOT_FOUND_MASTER);
if (!masterEntity?.active || masterEntity.role !== 'master') { if (!masterEntity?.active || masterEntity.role !== 'master') {
throw new Error(ERRORS.INACTIVE_MASTER); throw new Error(ERRORS.INACTIVE_MASTER);
@ -191,7 +191,7 @@ export class SlotsService extends BaseService {
// Проверка, что слот не создаётся в прошлом // Проверка, что слот не создаётся в прошлом
if (datetime_start && isBeforeToday(datetime_start)) { if (datetime_start && isBeforeToday(datetime_start)) {
throw new Error(ERRORS.PAST_SLOT); throw new Error(ERRORS.NO_PAST_SLOT);
} }
// Проверка валидности времени // Проверка валидности времени
@ -219,7 +219,7 @@ export class SlotsService extends BaseService {
private async checkUpdateDatetime(variables: VariablesOf<typeof GQL.UpdateSlotDocument>) { private async checkUpdateDatetime(variables: VariablesOf<typeof GQL.UpdateSlotDocument>) {
const { slot } = await this.getSlot({ documentId: variables.documentId }); const { slot } = await this.getSlot({ documentId: variables.documentId });
if (!slot) throw new Error(ERRORS.SLOT_NOT_FOUND); if (!slot) throw new Error(ERRORS.NOT_FOUND_SLOT);
const { datetime_end, datetime_start } = variables.data; const { datetime_end, datetime_start } = variables.data;
@ -250,7 +250,7 @@ export class SlotsService extends BaseService {
].includes(order.state), ].includes(order.state),
) )
) { ) {
throw new Error(ERRORS.HAS_ORDERS); throw new Error(ERRORS.SLOT_HAS_ORDERS);
} }
const { documentId } = slot; const { documentId } = slot;
@ -276,7 +276,7 @@ export class SlotsService extends BaseService {
const { slot } = await this.getSlot({ documentId: variables.documentId }); const { slot } = await this.getSlot({ documentId: variables.documentId });
if (!slot) throw new Error(ERRORS.SLOT_NOT_FOUND); if (!slot) throw new Error(ERRORS.NOT_FOUND_SLOT);
if (slot?.master?.documentId !== customer?.documentId) throw new Error(ERRORS.NO_PERMISSION); if (slot?.master?.documentId !== customer?.documentId) throw new Error(ERRORS.NO_PERMISSION);
} }