* refactor(profile): comment out change role feature * refactor(orders): update OrderServices and ServiceSelect components to utilize ServiceCard, and enhance service fields with duration in GraphQL types * refactor(schedule): implement forbidden order states to disable editing slots with active orders * fix(deploy): update SSH configuration to use dynamic port from secrets for improved flexibility * refactor(api/orders): simplify order creation logic by removing unnecessary validations and improving error handling * refactor(contact-row): replace role display logic with useIsMaster hook for improved clarity * refactor(profile/orders-list): update header text from "Общие записи" to "Недавние записи" for better clarity gql: GetOrders add sort slot.date:desc * refactor(profile/orders-list): enhance OrderCard component by adding avatarSource prop based on user role * feat(order-form): implement date selection with event highlighting and monthly view for available time slots * refactor(i18n/config): update timeZone from 'Europe/Amsterdam' to 'Europe/Moscow' * refactor(order-form/datetime-select): enhance date selection logic to include slot availability check * refactor(datetime-format): integrate dayjs timezone support with default Moscow timezone for date and time formatting * fix(contact-row): replace useIsMaster hook with isCustomerMaster utility for role display logic * refactor(service-card): replace formatTime with getMinutes for duration display * refactor(order-datetime): update date and time handling to use datetime_start and datetime_end for improved consistency * refactor(profile): streamline profile and slot pages by integrating session user retrieval and updating booking logic with BookButton component * fix(navigation): append query parameter to bottom-nav links and enhance back navigation logic in success page
148 lines
4.2 KiB
TypeScript
148 lines
4.2 KiB
TypeScript
import { getClientWithToken } from '../apollo/client';
|
|
import * as GQL from '../types';
|
|
import { BaseService } from './base';
|
|
import { CustomersService } from './customers';
|
|
import { ServicesService } from './services';
|
|
import { type VariablesOf } from '@graphql-typed-document-node/core';
|
|
import { getMinutes } from '@repo/utils/datetime-format';
|
|
import dayjs from 'dayjs';
|
|
|
|
export class SlotsService extends BaseService {
|
|
async createSlot(variables: VariablesOf<typeof GQL.CreateSlotDocument>) {
|
|
const customerService = new CustomersService(this.customer);
|
|
|
|
const { customer } = await customerService.getCustomer(this.customer);
|
|
|
|
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>) {
|
|
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('Missing date');
|
|
if (!context?.service?.documentId?.eq) throw new Error('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.customer);
|
|
|
|
const { service } = await servicesService.getService({
|
|
documentId: context.service.documentId.eq,
|
|
});
|
|
|
|
if (!service) throw new Error('Service not found');
|
|
|
|
const serviceDuration = getMinutes(service.duration);
|
|
|
|
const slots = getSlotsResult.data.slots;
|
|
|
|
const times: Array<{ slotId: string; time: string }> = [];
|
|
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) {
|
|
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>) {
|
|
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;
|
|
}
|
|
}
|