/* eslint-disable @typescript-eslint/naming-convention */ 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({ countSeats: true, engineType: true, engineVolume: true, leaseObjectCategory: true, leaseObjectMotorPower: true, leaseObjectType: true, maxMass: true, }).superRefine( async ( { leaseObjectType: leaseObjectTypeId, engineVolume, engineType, leaseObjectMotorPower, countSeats, maxMass, leaseObjectCategory, }, ctx ) => { 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'], }); } } } ); }