Vlad Chikalkin 7d94521c8b
Issues/76 (#77)
* fix(jwt): update import path for isTokenExpired function to remove file extension

* packages/graphql: add slot tests

* fix(slots): update error handling for customer and slot retrieval, enhance time validation in slot updates

* fix(slots): update error messages for missing datetime fields and improve validation logic in slot updates

* fix(slots): update error messages and validation logic for slot creation and updates, including handling of datetime fields and master status

* refactor(slots): rename checkUpdateIsTimeChanging to checkUpdateDatetime for clarity in slot update validation

* test(slots): add comprehensive tests for getAvailableTimeSlots method, including edge cases and error handling

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

* refactor(slots): rename validation methods for clarity and consistency in slot service

* OrdersService: add checkBeforeCreate

* add orders.test.js

* test(orders): add validation test for missing datetime_end in order creation

* feat(orders): implement updateOrder functionality with comprehensive validation tests

- Added updateOrder method in OrdersService with checks for permissions, order state, and datetime validation.
- Implemented tests for various scenarios including successful updates, permission errors, and validation failures.
- Enhanced error handling for overlapping time and invalid state changes.
- Updated GraphQL operations to support sorting in GetOrders query.

* fix(orders): update datetime validation logic and test cases for order creation and completion

- Modified order creation tests to set datetime_start to one hour in the past for past orders.
- Updated the OrdersService to use isNowOrAfter for validating order completion against the start time.
- Enhanced datetime utility function to accept a unit parameter for more flexible comparisons.

* fix(calendar): initialize selected date in ScheduleCalendar component if not set

- Added useEffect to set the selected date to the current date if it is not already defined.
- Imported useEffect alongside useState for managing component lifecycle.

* fix(order-form): initialize selected date in DateSelect component if not set

- Added useEffect to set the selected date to the current date if it is not already defined.
- Renamed setDate to setSelectedDate for clarity in state management.

* refactor(orders): streamline order creation logic and enhance test setup

- Removed redundant variable assignments in the createOrder method for cleaner code.
- Updated test setup in orders.test.js to use global mocks for user and service retrieval, improving test clarity and maintainability.
- Added checks for required fields in order creation to ensure data integrity.
2025-08-11 16:25:14 +03:00

284 lines
8.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* eslint-disable @typescript-eslint/naming-convention */
import { getClientWithToken } from '../apollo/client';
import * as GQL from '../types';
import { BaseService } from './base';
import { ServicesService } from './services';
import { type VariablesOf } from '@graphql-typed-document-node/core';
import { getMinutes, isBeforeNow } from '@repo/utils/datetime-format';
import dayjs from 'dayjs';
export const ERRORS = {
INACTIVE_MASTER: 'Пользователь не является активным или мастером',
INVALID_TIME: 'Некорректное время',
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: 'Время пересекается с другими слотами',
SLOT_HAS_ORDERS: 'Слот имеет активные заказы',
};
export class SlotsService extends BaseService {
async createSlot(variables: VariablesOf<typeof GQL.CreateSlotDocument>) {
await this.checkBeforeCreate(variables);
const { customer } = await this._getUser();
const { mutate } = await getClientWithToken();
const mutationResult = await mutate({
mutation: GQL.CreateSlotDocument,
variables: {
...variables,
input: {
...variables.input,
master: customer?.documentId,
},
},
});
const error = mutationResult.errors?.at(0);
if (error) throw new Error(error.message);
return mutationResult.data;
}
async deleteSlot(variables: VariablesOf<typeof GQL.DeleteSlotDocument>) {
await this.checkPermission(variables);
const { slot } = await this.getSlot({ documentId: variables.documentId });
if (slot?.orders?.length) {
throw new Error(ERRORS.SLOT_HAS_ORDERS);
}
const { mutate } = await getClientWithToken();
const mutationResult = await mutate({
mutation: GQL.DeleteSlotDocument,
variables,
});
const error = mutationResult.errors?.at(0);
if (error) throw new Error(error.message);
return mutationResult.data;
}
async getAvailableTimeSlots(
variables: VariablesOf<typeof GQL.GetSlotsDocument>,
context: { service: GQL.ServiceFiltersInput },
) {
if (!variables.filters?.datetime_start) throw new Error(ERRORS.MISSING_DATETIME_START);
if (!context?.service?.documentId?.eq) throw new Error(ERRORS.MISSING_SERVICE_ID);
const { query } = await getClientWithToken();
const getSlotsResult = await query({
query: GQL.GetSlotsOrdersDocument,
variables: {
filters: {
...variables.filters,
state: { eq: GQL.Enum_Slot_State.Open },
},
},
});
if (getSlotsResult.error) throw new Error(getSlotsResult.error.message);
const servicesService = new ServicesService(this._user);
const { service } = await servicesService.getService({
documentId: context.service.documentId.eq,
});
if (!service) throw new Error(ERRORS.NOT_FOUND_SERVICE);
const serviceDuration = getMinutes(service.duration);
const slots = getSlotsResult.data.slots;
const times: Array<{ slotId: string; time: string }> = [];
const now = dayjs();
for (const slot of slots) {
if (!slot?.datetime_start || !slot?.datetime_end) continue;
let datetimeStart = dayjs(slot.datetime_start);
const datetimeEnd = dayjs(slot.datetime_end).subtract(serviceDuration, 'minutes');
while (datetimeStart.valueOf() <= datetimeEnd.valueOf()) {
const slotStartTime = datetimeStart;
const potentialDatetimeEnd = datetimeStart.add(serviceDuration, 'minutes');
const hasConflict = slot.orders.some(
(order) =>
order?.state &&
![GQL.Enum_Order_State.Cancelled].includes(order.state) &&
slotStartTime.isBefore(dayjs(order.datetime_end)) &&
potentialDatetimeEnd.isAfter(dayjs(order.datetime_start)),
);
if (!hasConflict && datetimeStart.isAfter(now, 'minute')) {
times.push({ slotId: slot.documentId, time: datetimeStart.toISOString() });
}
datetimeStart = datetimeStart.add(15, 'minutes');
}
}
return { slots, times };
}
async getSlot(variables: VariablesOf<typeof GQL.GetSlotDocument>) {
const { query } = await getClientWithToken();
const result = await query({
query: GQL.GetSlotDocument,
variables,
});
return result.data;
}
async getSlots(variables: VariablesOf<typeof GQL.GetSlotsDocument>) {
const { query } = await getClientWithToken();
const result = await query({
query: GQL.GetSlotsDocument,
variables,
});
return result.data;
}
async updateSlot(variables: VariablesOf<typeof GQL.UpdateSlotDocument>) {
await this.checkPermission(variables);
await this.checkBeforeUpdateDatetime(variables);
const { mutate } = await getClientWithToken();
const mutationResult = await mutate({
mutation: GQL.UpdateSlotDocument,
variables,
});
const error = mutationResult.errors?.at(0);
if (error) throw new Error(error.message);
return mutationResult.data;
}
private async checkBeforeCreate(variables: VariablesOf<typeof GQL.CreateSlotDocument>) {
const { datetime_end, datetime_start } = variables.input;
if (!datetime_start) throw new Error(ERRORS.MISSING_DATETIME_START);
if (!datetime_end) throw new Error(ERRORS.MISSING_DATETIME_END);
// Проверка, что мастер существует и активен
const { customer: masterEntity } = await this._getUser();
if (!masterEntity) throw new Error(ERRORS.NOT_FOUND_MASTER);
if (!masterEntity?.active || masterEntity.role !== 'master') {
throw new Error(ERRORS.INACTIVE_MASTER);
}
// Проверка, что слот не создаётся в прошлом
if (datetime_start && isBeforeNow(datetime_start)) {
throw new Error(ERRORS.NO_PAST_SLOT);
}
// Проверка валидности времени
if (!datetime_start || !datetime_end) {
throw new Error(ERRORS.INVALID_TIME);
}
if (new Date(datetime_end) <= new Date(datetime_start)) {
throw new Error(ERRORS.INVALID_TIME);
}
const overlappingEntities = await this.getSlots({
filters: {
datetime_end: { gt: datetime_start },
datetime_start: { lt: datetime_end },
master: { telegramId: { eq: this._user.telegramId } },
},
});
if (overlappingEntities?.slots?.length) {
throw new Error(ERRORS.OVERLAPPING_TIME);
}
}
private async checkBeforeUpdateDatetime(variables: VariablesOf<typeof GQL.UpdateSlotDocument>) {
const { slot } = await this.getSlot({ documentId: variables.documentId });
if (!slot) throw new Error(ERRORS.NOT_FOUND_SLOT);
const { datetime_end, datetime_start } = variables.data;
const isTimeChanging = datetime_start || datetime_end;
if (!isTimeChanging) return;
if (!datetime_start) throw new Error(ERRORS.MISSING_DATETIME_START);
if (!datetime_end) throw new Error(ERRORS.MISSING_DATETIME_END);
// Проверка валидности времени
if (new Date(datetime_end) <= new Date(datetime_start)) {
throw new Error(ERRORS.INVALID_TIME);
}
const orders = slot?.orders;
if (
orders?.length &&
orders?.some(
(order) =>
order?.state &&
[
GQL.Enum_Order_State.Approved,
GQL.Enum_Order_State.Cancelled,
GQL.Enum_Order_State.Completed,
GQL.Enum_Order_State.Scheduled,
].includes(order.state),
)
) {
throw new Error(ERRORS.SLOT_HAS_ORDERS);
}
const { documentId } = slot;
const overlappingEntities = await this.getSlots({
filters: {
datetime_end: { gt: datetime_start },
datetime_start: { lt: datetime_end },
documentId: { not: { eq: documentId } },
master: { telegramId: { eq: this._user.telegramId } },
},
});
if (overlappingEntities?.slots?.length) {
throw new Error(ERRORS.OVERLAPPING_TIME);
}
}
private async checkPermission(
variables: Pick<VariablesOf<typeof GQL.GetSlotDocument>, 'documentId'>,
) {
const { customer } = await this._getUser();
const { slot } = await this.getSlot({ documentId: variables.documentId });
if (!slot) throw new Error(ERRORS.NOT_FOUND_SLOT);
if (slot?.master?.documentId !== customer?.documentId) throw new Error(ERRORS.NO_PERMISSION);
}
}