2023-04-21 11:13:58 +03:00

168 lines
4.6 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 sonarjs/cognitive-complexity */
/* eslint-disable complexity */
import type { ValidationContext } from '../types';
import ValuesSchema from '@/config/schema/values';
import * as CRMTypes from '@/graphql/crm.types';
import { z } from 'zod';
export function createValidationSchema({ apolloClient }: ValidationContext) {
return ValuesSchema.pick({
brand: true,
configuration: true,
countSeats: true,
engineType: true,
engineVolume: true,
leaseObjectCategory: true,
leaseObjectMotorPower: true,
leaseObjectType: true,
leaseObjectUseFor: true,
maxMass: true,
model: true,
}).superRefine(
async (
{
leaseObjectType: leaseObjectTypeId,
engineVolume,
engineType,
leaseObjectMotorPower,
countSeats,
maxMass,
leaseObjectCategory,
brand,
model,
leaseObjectUseFor,
configuration,
},
ctx
) => {
if (!leaseObjectTypeId) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'Не заполнено поле',
path: ['selectLeaseObjectType'],
});
}
if (!brand) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'Не заполнено поле',
path: ['selectBrand'],
});
}
if (!model) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'Не заполнено поле',
path: ['selectModel'],
});
}
if (model && !configuration) {
const {
data: { evo_equipments },
} = await apolloClient.query({
query: CRMTypes.GetConfigurationsDocument,
variables: { modelId: model },
});
if (evo_equipments?.length) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'Не заполнено поле',
path: ['selectConfiguration'],
});
}
}
if (!leaseObjectUseFor) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'Не заполнено поле',
path: ['selectLeaseObjectUseFor'],
});
}
if (leaseObjectTypeId) {
const {
data: { evo_leasingobject_type },
} = await apolloClient.query({
query: CRMTypes.GetLeaseObjectTypeDocument,
variables: {
leaseObjectTypeId,
},
});
const isNotTrailer =
evo_leasingobject_type?.evo_id !== null && evo_leasingobject_type?.evo_id !== '8';
if (isNotTrailer && engineVolume <= 0) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'Не заполнено поле',
path: ['tbxEngineVolume'],
});
}
if (isNotTrailer && !engineType) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'Не заполнено поле',
path: ['selectEngineType'],
});
}
if (isNotTrailer && leaseObjectMotorPower <= 0) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'Не заполнено поле',
path: ['tbxLeaseObjectMotorPower'],
});
}
if (evo_leasingobject_type?.evo_id === '1' && countSeats >= 9) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'Количество мест должно быть меньше 9',
path: ['tbxCountSeats'],
});
}
if (
(evo_leasingobject_type?.evo_id === '4' || evo_leasingobject_type?.evo_id === '5') &&
countSeats <= 8
) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'Количество мест должно быть больше 8',
path: ['tbxCountSeats'],
});
}
if (evo_leasingobject_type?.evo_id === '2' && maxMass <= 0) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'Не заполнено поле',
path: ['tbxMaxMass'],
});
}
if (
!leaseObjectCategory &&
Boolean(
evo_leasingobject_type?.evo_id &&
!['6', '9', '10'].includes(evo_leasingobject_type?.evo_id)
)
) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'Не заполнено поле',
path: ['selectLeaseObjectCategory'],
});
}
}
}
);
}