/* eslint-disable sonarjs/cognitive-complexity */ /* eslint-disable complexity */ import type { ValidationContext } from '../types'; import titles from '@/Components/Calculation/config/elements-titles'; 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, insDecentral: true, lead: true, leaseObjectCategory: true, leaseObjectMotorPower: true, leaseObjectPrice: true, leaseObjectType: true, leaseObjectUseFor: true, leaseObjectYear: true, leasingPeriod: true, leasingWithoutKasko: true, legalClientRegion: true, maxMass: true, model: true, objectRegistration: true, opportunity: true, supplierDiscountRub: true, townRegistration: true, withTrailer: true, }).superRefine( async ( { insDecentral, leaseObjectCategory, leaseObjectType: leaseObjectTypeId, leaseObjectUseFor, leasingWithoutKasko, legalClientRegion, townRegistration, lead: leadid, opportunity: opportunityid, objectRegistration, brand, model, leaseObjectMotorPower, leaseObjectPrice, leaseObjectYear, leasingPeriod, supplierDiscountRub, }, ctx ) => { if (!brand) { ctx.addIssue({ code: z.ZodIssueCode.custom, message: 'Не заполенно поле ' + titles.selectBrand, path: ['eltKasko', 'eltOsago'], }); } if (!model) { ctx.addIssue({ code: z.ZodIssueCode.custom, message: 'Не заполнено поле ' + titles.selectModel, path: ['eltKasko', 'eltOsago'], }); } if (!leaseObjectPrice) { ctx.addIssue({ code: z.ZodIssueCode.custom, message: 'Не заполнено поле ' + titles.tbxLeaseObjectPrice, path: ['eltKasko', 'eltOsago'], }); } if (!supplierDiscountRub) { ctx.addIssue({ code: z.ZodIssueCode.custom, message: 'Не заполнено поле ' + titles.tbxSupplierDiscountRub, path: ['eltKasko', 'eltOsago'], }); } if (!leasingPeriod) { ctx.addIssue({ code: z.ZodIssueCode.custom, message: 'Не заполнено поле ' + titles.tbxLeasingPeriod, path: ['eltKasko', 'eltOsago'], }); } if (!leaseObjectYear) { ctx.addIssue({ code: z.ZodIssueCode.custom, message: 'Не заполнено поле ' + titles.tbxLeaseObjectYear, path: ['eltKasko', 'eltOsago'], }); } if (!leaseObjectMotorPower) { ctx.addIssue({ code: z.ZodIssueCode.custom, message: 'Не заполнено поле ' + titles.tbxLeaseObjectMotorPower, path: ['eltKasko', 'eltOsago'], }); } if (!leaseObjectUseFor) { ctx.addIssue({ code: z.ZodIssueCode.custom, message: 'Не заполнено поле ' + titles.selectLeaseObjectUseFor, path: ['eltKasko', 'eltOsago'], }); } if (leaseObjectTypeId) { const { data: { evo_leasingobject_type }, } = await apolloClient.query({ query: CRMTypes.GetLeaseObjectTypeDocument, variables: { leaseObjectTypeId, }, }); // Проверяем на мотоцикл if (evo_leasingobject_type?.evo_id && ['11'].includes(evo_leasingobject_type?.evo_id)) { ctx.addIssue({ code: z.ZodIssueCode.custom, message: 'По данному Типу предмета лизинга возможен только индивидуальный запрос тарифов КАСКО и ОСАГО. Просьба обратиться на адрес strakhovka@evoleasing.ru', path: ['eltKasko', 'eltOsago'], }); } if ( evo_leasingobject_type?.evo_id && !['1', '2', '3', '6', '7', '8', '9', '10'].includes(evo_leasingobject_type?.evo_id) ) { ctx.addIssue({ code: z.ZodIssueCode.custom, message: 'Для выбранной категории ТС расчет в ЭЛТ недоступен', path: ['eltKasko', 'eltOsago'], }); } } if ( leaseObjectUseFor && [100_000_001, 100_000_002, 100_000_003, 100_000_004, 100_000_006].includes( leaseObjectUseFor ) ) { ctx.addIssue({ code: z.ZodIssueCode.custom, message: 'По данной цели использования возможен только индивидуальный запрос тарифов КАСКО и ОСАГО. Просьба обратиться на адрес strakhovka@evoleasing.ru', path: ['eltKasko'], }); } if (objectRegistration === 100_000_000 && !legalClientRegion) { ctx.addIssue({ code: z.ZodIssueCode.custom, message: 'Не указан Регион по юр.адресу клиента', path: ['eltKasko'], }); } if (objectRegistration === 100_000_001 && !townRegistration) { ctx.addIssue({ code: z.ZodIssueCode.custom, message: 'Не указан Город регистрации', path: ['eltOsago'], }); } if (!leaseObjectCategory && leaseObjectTypeId) { const { data: { evo_leasingobject_type }, } = await apolloClient.query({ query: CRMTypes.GetLeaseObjectTypeDocument, variables: { leaseObjectTypeId, }, }); if ( evo_leasingobject_type?.evo_id && !['6', '9', '10'].includes(evo_leasingobject_type?.evo_id) ) { ctx.addIssue({ code: z.ZodIssueCode.custom, message: 'Не указана категория ТС', path: ['eltKasko'], }); } } if (insDecentral) { ctx.addIssue({ code: z.ZodIssueCode.custom, message: 'Децентрализованное страхование не может быть расчитано в ЭЛТ', path: ['eltKasko'], }); } if (leasingWithoutKasko) { ctx.addIssue({ code: z.ZodIssueCode.custom, message: 'Вы осуществляете расчет с Лизинг без КАСКО, расчет ЭЛТ в данном случае не требуется', path: ['eltKasko'], }); } let okved: string | null | undefined; if (leadid) { const { data: { lead }, } = await apolloClient.query({ query: CRMTypes.GetLeadDocument, variables: { leadid }, }); okved = lead?.accountidData?.evo_okved; } if (!okved && opportunityid) { const { data: { opportunity }, } = await apolloClient.query({ query: CRMTypes.GetOpportunityDocument, variables: { opportunityid }, }); okved = opportunity?.accountidData?.evo_okved; } if ( okved && [ '77.3', '49.32', '49.39', '49.3', '49.31', '49.31.2', '53.20.3', '53.20.32', '77.11', ].includes(okved) ) { ctx.addIssue({ code: z.ZodIssueCode.custom, message: 'По данному ОКВЭД Контрагента возможен только индивидуальный запрос тарифов КАСКО/ОСАГО. Просьба обратиться на адрес strakhovka@evoleasing.ru', path: ['eltKasko'], }); } } ); }