2023-03-16 15:35:44 +03:00

95 lines
2.9 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.

import type { ValidationContext } from '../types';
import ValuesSchema from '@/config/schema/values';
import { MAX_MASS } from '@/constants/values';
import * as CRMTypes from '@/graphql/crm.types';
import { z } from 'zod';
export function createValidationSchema({ apolloClient }: ValidationContext) {
return ValuesSchema.pick({
insNSIB: true,
leaseObjectCategory: true,
leaseObjectType: true,
maxMass: true,
objectCategoryTax: true,
objectRegistration: true,
typePTS: true,
vehicleTaxInYear: true,
}).superRefine(
async (
{
leaseObjectCategory,
maxMass,
leaseObjectType: leaseObjectTypeId,
typePTS,
objectRegistration,
objectCategoryTax,
insNSIB,
vehicleTaxInYear,
},
ctx
) => {
if (leaseObjectCategory === 100_000_001 && maxMass > MAX_MASS) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `При категории ТС = В Разрешенная макс.масса не может быть больше ${MAX_MASS} кг`,
path: ['tbxMaxMass'],
});
}
if (leaseObjectCategory === 100_000_002 && maxMass <= MAX_MASS) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `При категории ТС = С Разрешенная макс.масса не может быть меньше ${MAX_MASS} кг`,
path: ['tbxMaxMass'],
});
}
if (leaseObjectTypeId) {
const {
data: { evo_leasingobject_type },
} = await apolloClient.query({
query: CRMTypes.GetLeaseObjectTypeDocument,
variables: { leaseObjectTypeId },
});
if (
objectRegistration === 100_000_001 &&
typePTS === 100_000_001 &&
objectCategoryTax === null &&
Boolean(evo_leasingobject_type?.evo_category_tr?.length)
) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'Не заполнено поле',
path: ['selectObjectCategoryTax'],
});
}
if (evo_leasingobject_type?.evo_id === '11' && !insNSIB) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'Страхование НСИБ обязательно для мотоциклистов',
path: ['selectInsNSIB'],
});
}
}
if (objectRegistration === 100_000_001 && !(vehicleTaxInYear > 0)) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'Значение должно быть больше 0',
path: ['tbxVehicleTaxInYear'],
});
}
if (objectRegistration === 100_000_001 && !typePTS) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'Не заполнено поле',
path: ['radioTypePTS'],
});
}
}
);
}