2023-07-06 11:18:06 +03:00

308 lines
9.3 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 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,
leaseObjectUsed: true,
leaseObjectYear: true,
leasingPeriod: true,
leasingWithoutKasko: true,
legalClientRegion: true,
maxMass: true,
mileage: true,
model: true,
objectRegistration: true,
opportunity: true,
quote: true,
townRegistration: true,
vin: true,
withTrailer: true,
}).superRefine(
async (
{
insDecentral,
leaseObjectCategory,
leaseObjectType: leaseObjectTypeId,
leaseObjectUseFor,
leasingWithoutKasko,
legalClientRegion,
townRegistration,
lead: leadid,
opportunity: opportunityid,
objectRegistration,
brand,
model,
leaseObjectMotorPower,
leaseObjectPrice,
leaseObjectYear,
leasingPeriod,
vin,
mileage,
leaseObjectUsed,
quote: quoteId,
},
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 (!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 (!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 !== '8' && !leaseObjectMotorPower) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'Не заполнено поле ' + titles.tbxLeaseObjectMotorPower,
path: ['eltKasko', 'eltOsago'],
});
}
if (quoteId) {
const {
data: { quote },
} = await apolloClient.query({
query: CRMTypes.GetQuoteDocument,
variables: { quoteId },
});
if (quote?.evo_untype_insurance) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'Запрещен расчет в ЭЛТ при нестандартных условиях страхования',
path: ['eltKasko', 'eltOsago'],
});
}
}
// Проверяем на мотоцикл
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', '9', '10'].includes(evo_leasingobject_type?.evo_id)
) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'Для выбранной категории ТС расчет в ЭЛТ недоступен',
path: ['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'],
});
}
}
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', 'eltOsago'],
});
}
if (objectRegistration === 100_000_000 && !legalClientRegion) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'Не указан Регион по юр.адресу клиента',
path: ['eltKasko', 'eltOsago'],
});
}
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', 'eltOsago'],
});
}
}
if (leaseObjectUsed) {
if (!vin) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'Не указан ' + titles.tbxVIN,
path: ['eltKasko'],
});
}
if (!mileage) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'Не указан ' + titles.tbxMileage,
path: ['eltKasko'],
});
}
}
if (insDecentral) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'Децентрализованное страхование не может быть расчитано в ЭЛТ',
path: ['eltKasko', 'eltOsago'],
});
}
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', 'eltOsago'],
});
}
}
);
}