2023-03-28 09:33:17 +03:00

116 lines
3.7 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 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({
insDecentral: true,
leasingPeriod: true,
leasingWithoutKasko: true,
quote: true,
recalcWithRevision: true,
})
.extend({
insurance: InsuranceSchema,
})
.superRefine(
async (
{
leasingPeriod,
recalcWithRevision,
quote: quoteId,
leasingWithoutKasko,
insDecentral,
insurance,
},
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 (insCost > 0 && !insuranceCompany) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `Укажите страховую компанию ${policyType}`,
path: ['insurance'],
});
}
if (insCost > 0 && !insTerm) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `Укажите срок страхования ${policyType}`,
path: ['insurance'],
});
}
if (insCost > 0 && !insured) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `Укажите плательщика ${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'],
});
}
}
}
);
}