fix(api): standardize error messages for customer and slot retrieval, and improve validation logic in slots service
This commit is contained in:
parent
620d2eaff4
commit
d8e88d8934
@ -3,8 +3,8 @@ import { getClientWithToken } from '../apollo/client';
|
||||
import * as GQL from '../types';
|
||||
|
||||
export const ERRORS = {
|
||||
CUSTOMER_NOT_FOUND: 'Пользователь не найден',
|
||||
MISSING_TELEGRAM_ID: 'Не указан Telegram ID',
|
||||
NOT_FOUND_CUSTOMER: 'Пользователь не найден',
|
||||
};
|
||||
|
||||
type UserProfile = {
|
||||
@ -32,7 +32,7 @@ export class BaseService {
|
||||
|
||||
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 };
|
||||
}
|
||||
|
||||
@ -8,18 +8,18 @@ import { getMinutes, isBeforeToday } from '@repo/utils/datetime-format';
|
||||
import dayjs from 'dayjs';
|
||||
|
||||
export const ERRORS = {
|
||||
HAS_ORDERS: 'Слот имеет активные заказы',
|
||||
INACTIVE_MASTER: 'Пользователь не является активным или мастером',
|
||||
INVALID_TIME: 'Некорректное время',
|
||||
MASTER_NOT_FOUND: 'Мастер не найден',
|
||||
MISSING_DATETIME_END: 'Не указана дата окончания',
|
||||
MISSING_DATETIME_START: 'Не указана дата начала',
|
||||
MISSING_SERVICE_ID: 'Не указана услуга',
|
||||
NO_PAST_SLOT: 'Нельзя создать слот в прошлом',
|
||||
NO_PERMISSION: 'Нет доступа',
|
||||
NOT_FOUND_MASTER: 'Мастер не найден',
|
||||
NOT_FOUND_SERVICE: 'Сервис не найден',
|
||||
NOT_FOUND_SLOT: 'Слот не найден',
|
||||
OVERLAPPING_TIME: 'Время пересекается с другими слотами',
|
||||
PAST_SLOT: 'Нельзя создать слот в прошлом',
|
||||
SERVICE_NOT_FOUND: 'Сервис не найден',
|
||||
SLOT_NOT_FOUND: 'Слот не найден',
|
||||
SLOT_HAS_ORDERS: 'Слот имеет активные заказы',
|
||||
};
|
||||
|
||||
export class SlotsService extends BaseService {
|
||||
@ -53,7 +53,7 @@ export class SlotsService extends BaseService {
|
||||
const { slot } = await this.getSlot({ documentId: variables.documentId });
|
||||
|
||||
if (slot?.orders?.length) {
|
||||
throw new Error(ERRORS.HAS_ORDERS);
|
||||
throw new Error(ERRORS.SLOT_HAS_ORDERS);
|
||||
}
|
||||
|
||||
const { mutate } = await getClientWithToken();
|
||||
@ -96,7 +96,7 @@ export class SlotsService extends BaseService {
|
||||
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);
|
||||
|
||||
@ -183,7 +183,7 @@ export class SlotsService extends BaseService {
|
||||
// Проверка, что мастер существует и активен
|
||||
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') {
|
||||
throw new Error(ERRORS.INACTIVE_MASTER);
|
||||
@ -191,7 +191,7 @@ export class SlotsService extends BaseService {
|
||||
|
||||
// Проверка, что слот не создаётся в прошлом
|
||||
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>) {
|
||||
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;
|
||||
|
||||
@ -250,7 +250,7 @@ export class SlotsService extends BaseService {
|
||||
].includes(order.state),
|
||||
)
|
||||
) {
|
||||
throw new Error(ERRORS.HAS_ORDERS);
|
||||
throw new Error(ERRORS.SLOT_HAS_ORDERS);
|
||||
}
|
||||
|
||||
const { documentId } = slot;
|
||||
@ -276,7 +276,7 @@ export class SlotsService extends BaseService {
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user