refactor(api/orders): simplify order creation logic by removing unnecessary validations and improving error handling
This commit is contained in:
parent
9b464c6fac
commit
a64d974832
@ -1,22 +1,15 @@
|
||||
/* eslint-disable sonarjs/cognitive-complexity */
|
||||
/* eslint-disable canonical/id-match */
|
||||
import { getClientWithToken } from '../apollo/client';
|
||||
import * as GQL from '../types';
|
||||
import { Enum_Slot_State } from '../types';
|
||||
import { BaseService } from './base';
|
||||
import { CustomersService } from './customers';
|
||||
import { NotifyService } from './notify';
|
||||
import { ServicesService } from './services';
|
||||
import { SlotsService } from './slots';
|
||||
import { type VariablesOf } from '@graphql-typed-document-node/core';
|
||||
import { isCustomerMaster } from '@repo/utils/customer';
|
||||
import { formatTime, sumTime } from '@repo/utils/datetime-format';
|
||||
|
||||
const ERRORS = {
|
||||
INVALID_CLIENT: 'Invalid client',
|
||||
INVALID_MASTER: 'Invalid master',
|
||||
INVALID_SERVICE_DURATION: 'Invalid service duration',
|
||||
MISSING_CLIENT: 'Missing client id',
|
||||
MISSING_ORDER: 'Order not found',
|
||||
MISSING_SERVICE_ID: 'Missing service id',
|
||||
MISSING_SERVICES: 'Missing services',
|
||||
@ -24,69 +17,30 @@ const ERRORS = {
|
||||
MISSING_START_TIME: 'Missing time start',
|
||||
MISSING_USER: 'User not found',
|
||||
NO_PERMISSION: 'No permission',
|
||||
SLOT_CLOSED: 'Slot is closed',
|
||||
};
|
||||
|
||||
export class OrdersService extends BaseService {
|
||||
async createOrder(variables: {
|
||||
input: Omit<VariablesOf<typeof GQL.CreateOrderDocument>['input'], 'time_end'>;
|
||||
}) {
|
||||
// Проверки на существование обязательных полей для предотвращения ошибок типов
|
||||
if (!variables.input.slot) throw new Error(ERRORS.MISSING_SLOT);
|
||||
if (!variables.input.client) throw new Error(ERRORS.MISSING_CLIENT);
|
||||
if (!variables.input.services?.length) throw new Error(ERRORS.MISSING_SERVICES);
|
||||
if (!variables.input.services[0]) throw new Error(ERRORS.MISSING_SERVICE_ID);
|
||||
if (!variables.input.time_start) throw new Error(ERRORS.MISSING_START_TIME);
|
||||
|
||||
const customersService = new CustomersService(this.customer);
|
||||
const slotsService = new SlotsService(this.customer);
|
||||
const servicesService = new ServicesService(this.customer);
|
||||
|
||||
const { customer } = await customersService.getCustomer(this.customer);
|
||||
|
||||
if (!customer) throw new Error(ERRORS.MISSING_USER);
|
||||
|
||||
const { slot } = await slotsService.getSlot({ documentId: variables.input.slot });
|
||||
|
||||
if (slot?.state === Enum_Slot_State.Closed) {
|
||||
throw new Error(ERRORS.SLOT_CLOSED);
|
||||
}
|
||||
|
||||
const isMaster = isCustomerMaster(customer);
|
||||
|
||||
if (!isMaster) {
|
||||
if (customer.documentId !== variables.input.client) {
|
||||
throw new Error(ERRORS.INVALID_CLIENT);
|
||||
}
|
||||
|
||||
const { customers } = await customersService.getMasters(this.customer);
|
||||
const masters = customers.at(0)?.masters;
|
||||
|
||||
const masterId = slot?.master?.documentId;
|
||||
if (!masters?.some((master) => master?.documentId === masterId)) {
|
||||
throw new Error(ERRORS.INVALID_MASTER);
|
||||
}
|
||||
}
|
||||
|
||||
if (isMaster) {
|
||||
const { customers: myMasters } = await customersService.getMasters(this.customer);
|
||||
|
||||
const clientId = variables.input.client;
|
||||
const isTryingToRecordMaster = myMasters.some((master) => master?.documentId === clientId);
|
||||
|
||||
if (isTryingToRecordMaster) throw new Error(ERRORS.INVALID_CLIENT);
|
||||
}
|
||||
|
||||
if (isMaster && slot?.master?.documentId !== customer.documentId) {
|
||||
throw new Error(ERRORS.INVALID_MASTER);
|
||||
}
|
||||
|
||||
const { service } = await servicesService.getService({
|
||||
documentId: variables.input.services[0],
|
||||
});
|
||||
|
||||
if (!service?.duration) throw new Error(ERRORS.INVALID_SERVICE_DURATION);
|
||||
|
||||
const endTime = sumTime(variables.input.time_start, service?.duration);
|
||||
const endTime = sumTime(variables.input.time_start, service.duration);
|
||||
|
||||
const { mutate } = await getClientWithToken();
|
||||
|
||||
@ -96,7 +50,9 @@ export class OrdersService extends BaseService {
|
||||
...variables,
|
||||
input: {
|
||||
...variables.input,
|
||||
state: isMaster ? GQL.Enum_Order_State.Approved : GQL.Enum_Order_State.Created,
|
||||
state: isCustomerMaster(customer)
|
||||
? GQL.Enum_Order_State.Approved
|
||||
: GQL.Enum_Order_State.Created,
|
||||
time_end: formatTime(endTime).db(),
|
||||
},
|
||||
},
|
||||
@ -107,7 +63,7 @@ export class OrdersService extends BaseService {
|
||||
|
||||
// Уведомление об создании заказа
|
||||
const notifyService = new NotifyService(this.customer);
|
||||
notifyService.orderCreated(variables, isMaster);
|
||||
notifyService.orderCreated(variables, isCustomerMaster(customer));
|
||||
|
||||
return mutationResult.data;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user