vchikalkin 9e6f3d451c Добавить валидацию на нажатие кнопки Рассчитать btnCalculate:
Если Пересчет без пересмотра recalcWthtRevision = True

И в поле Предложение Quote указана запись, у которой поле "Акция"quote.evo_promotion содержит значение "ЭвоЛайт_China" 100 000 000 или "ЭвоЛайт_ЛКТ" 100 000 001 И в поле "Программа принятия решения" quote.evo_programsolution значение = ЭвоЛайт 100 000 000,

И значение поля "Плательщик КАСКО" в Предложении quote.evo_kasko_payer не равно значению Плательщик КАСКО insuredKASKO в калькуляторе,

то не считать график и выводить сообщение "По акции <значение поля "Акция" из Предложение> после одобрения нельзя менять Плательщика по КАСКО"

иначе ничего не проверяем
2023-12-11 15:37:56 +03:00

216 lines
7.0 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 complexity */
/* eslint-disable sonarjs/cognitive-complexity */
import type { ValidationContext } from '../types';
import type * as Insurance from '@/Components/Calculation/Form/Insurance/InsuranceTable/types';
import { FinGAPSchema } from '@/config/schema/fingap';
import { InsuranceSchema } from '@/config/schema/insurance';
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,
leasingPeriod: true,
leasingWithoutKasko: true,
quote: true,
recalcWithRevision: true,
})
.extend({
fingap: FinGAPSchema,
insurance: InsuranceSchema,
})
.superRefine(
async (
{
leasingPeriod,
recalcWithRevision,
quote: quoteId,
leasingWithoutKasko,
insDecentral,
insurance,
brand: brandId,
fingap: fingapRisks,
},
ctx
) => {
if (quoteId) {
const {
data: { quote },
} = await apolloClient.query({
query: CRMTypes.GetQuoteDocument,
variables: { quoteId },
});
if (
recalcWithRevision &&
quote?.evo_one_year_insurance === true &&
leasingPeriod > 15 &&
insurance.values.kasko.insTerm === 100_000_001
) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message:
'Срок страхования КАСКО должен быть 12 месяцев, т.к. оформляется Однолетний полис',
path: ['insurance'],
});
}
const { kasko } = insurance.values;
if (
recalcWithRevision &&
quote?.evo_promotion &&
[100_000_000, 100_000_001].some((x) => quote.evo_promotion?.includes(x)) &&
quote.evo_programsolution &&
quote.evo_programsolution === 100_000_000 &&
quote.evo_kasko_payer !== kasko.insured
) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'По акции после одобрения нельзя менять Плательщика по КАСКО',
path: ['insurance'],
});
}
}
(['osago', 'kasko'] as Insurance.Keys[]).forEach((key) => {
const { insCost, insured, policyType, insuranceCompany, insTerm } = insurance.values[key];
if (insured === 100_000_001 && insCost < 1000) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `Укажите стоимость ${policyType}, включаемую в график (>1000)`,
path: ['insurance'],
});
}
if (!insuranceCompany) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `Укажите страховую компанию ${policyType}`,
path: ['insurance'],
});
}
if (!insTerm) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `Укажите срок страхования ${policyType}`,
path: ['insurance'],
});
}
if (!insured) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `Укажите плательщика ${policyType}`,
path: ['insurance'],
});
}
});
const { fingap } = insurance.values;
if (fingap.insuranceCompany && !fingap.insured) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `Укажите плательщика ${fingap.policyType}`,
path: ['insurance'],
});
}
if (fingap.insuranceCompany && fingap.insCost < 1000) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `Укажите стоимость ${fingap.policyType}, включаемую в график (>1000)`,
path: ['insurance'],
});
}
if (fingap.insuranceCompany && !fingap.insTerm) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `Укажите срок страхования ${fingap.policyType}`,
path: ['insurance'],
});
}
if (fingap.insuranceCompany && !fingapRisks?.length) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `Выберите риски`,
path: ['fingap'],
});
}
if (!fingap.insuranceCompany && fingap.insCost > 0) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `Укажите страховую компанию ${fingap.policyType}`,
path: ['insurance'],
});
}
if (
!leasingWithoutKasko &&
!insDecentral &&
insurance.values.osago.insuranceCompany &&
insurance.values.kasko.insuranceCompany !== insurance.values.osago.insuranceCompany
) {
const {
data: { account },
} = await apolloClient.query({
query: CRMTypes.GetInsuranceCompanyDocument,
variables: { accountId: insurance.values.osago.insuranceCompany },
});
if (account?.evo_osago_with_kasko) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message:
'Невозможно страхование ОСАГО отдельно от КАСКО - страховая компания должна быть одна!',
path: ['insurance'],
});
}
}
if (brandId) {
const {
data: { evo_brand },
} = await apolloClient.query({
query: CRMTypes.GetBrandDocument,
variables: { brandId },
});
const {
data: { accounts },
} = await apolloClient.query({
query: CRMTypes.GetInsuranceCompaniesDocument,
});
const defaultKaskoOptions = accounts?.filter((x) =>
x?.evo_type_ins_policy?.includes(100_000_000)
);
if (evo_brand?.evo_id === 'BRAND49') {
const renessansCompany = defaultKaskoOptions?.find((x) => x?.evo_inn === '7725497022');
if (
renessansCompany &&
insurance.values.kasko.insuranceCompany !== renessansCompany.value
) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message:
'Для Harley-Davidson СК для КАСКО может быть только ' + renessansCompany.label,
path: ['insurance'],
});
}
}
}
}
);
}