195 lines
5.4 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,
maxSpeed: true,
model: true,
}).superRefine(
async (
{
leaseObjectType: leaseObjectTypeId,
engineVolume,
engineType,
leaseObjectMotorPower,
countSeats,
maxMass,
leaseObjectCategory,
brand,
model: modelId,
leaseObjectUseFor,
configuration,
maxSpeed,
},
ctx
) => {
if (!leaseObjectTypeId) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'Не заполнено поле',
path: ['selectLeaseObjectType'],
});
}
if (!brand) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'Не заполнено поле',
path: ['selectBrand'],
});
}
if (!modelId) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'Не заполнено поле',
path: ['selectModel'],
});
}
if (modelId && !configuration) {
const {
data: { evo_equipments },
} = await apolloClient.query({
query: CRMTypes.GetConfigurationsDocument,
variables: { modelId },
});
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,
},
});
if (modelId) {
const {
data: { evo_model },
} = await apolloClient.query({
query: CRMTypes.GetModelDocument,
variables: {
modelId,
},
});
const isNotTrailer =
evo_leasingobject_type?.evo_id !== null &&
evo_leasingobject_type?.evo_id !== '8' &&
evo_model?.evo_trailer_sign !== true;
if (isNotTrailer && engineType !== 100_000_003 && 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 <= 0 || countSeats > 9)) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'Количество мест должно быть от 0 до 9',
path: ['tbxCountSeats'],
});
}
if (
(evo_leasingobject_type?.evo_id === '4' || evo_leasingobject_type?.evo_id === '5') &&
countSeats <= 9
) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'Количество мест должно быть не меньше 9',
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'],
});
}
if (
evo_leasingobject_type?.evo_id &&
['6', '9', '10'].includes(evo_leasingobject_type?.evo_id) &&
!maxSpeed
) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'Не заполнено поле',
path: ['tbxMaxSpeed'],
});
}
}
}
);
}