172 lines
5.5 KiB
TypeScript
172 lines
5.5 KiB
TypeScript
/* eslint-disable sonarjs/cognitive-complexity */
|
||
/* eslint-disable zod/require-strict */
|
||
import type { ValidationContext } from '../types';
|
||
import type * as Insurance from '@/Components/Calculation/Form/Insurance/InsuranceTable/types';
|
||
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({
|
||
insurance: InsuranceSchema,
|
||
})
|
||
|
||
.superRefine(
|
||
async (
|
||
{
|
||
leasingPeriod,
|
||
recalcWithRevision,
|
||
quote: quoteId,
|
||
leasingWithoutKasko,
|
||
insDecentral,
|
||
insurance,
|
||
brand: brandId,
|
||
},
|
||
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'],
|
||
});
|
||
}
|
||
}
|
||
|
||
(['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 (
|
||
!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'],
|
||
});
|
||
}
|
||
}
|
||
}
|
||
}
|
||
);
|
||
}
|