83 lines
2.6 KiB
TypeScript
83 lines
2.6 KiB
TypeScript
import type { ValidationContext } from '../types';
|
||
import type * as Insurance from '@/Components/Calculation/Form/Insurance/InsuranceTable/types';
|
||
import { RowSchema } 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({
|
||
leasingPeriod: true,
|
||
quote: true,
|
||
recalcWithRevision: true,
|
||
})
|
||
.extend({
|
||
insurance: z
|
||
.object({
|
||
fingap: RowSchema,
|
||
kasko: RowSchema,
|
||
osago: RowSchema,
|
||
})
|
||
.strict(),
|
||
})
|
||
.superRefine(async ({ leasingPeriod, recalcWithRevision, quote: quoteId, 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.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[key];
|
||
|
||
if (insCost === 0 && insured === 100_000_001) {
|
||
ctx.addIssue({
|
||
code: z.ZodIssueCode.custom,
|
||
message: `Укажите стоимость ${policyType}, включаемую в график`,
|
||
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'],
|
||
});
|
||
}
|
||
});
|
||
});
|
||
}
|