143 lines
4.4 KiB
TypeScript
143 lines
4.4 KiB
TypeScript
/* eslint-disable sonarjs/cognitive-complexity */
|
||
/* eslint-disable complexity */
|
||
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,
|
||
objectRegionRegistration: true,
|
||
objectRegistration: true,
|
||
regionRegistration: true,
|
||
registration: true,
|
||
townRegistration: true,
|
||
typePTS: true,
|
||
vehicleTaxInLeasingPeriod: true,
|
||
vehicleTaxInYear: true,
|
||
}).superRefine(
|
||
async (
|
||
{
|
||
leaseObjectCategory,
|
||
maxMass,
|
||
leaseObjectType: leaseObjectTypeId,
|
||
typePTS,
|
||
objectRegistration,
|
||
objectCategoryTax,
|
||
insNSIB,
|
||
vehicleTaxInYear,
|
||
vehicleTaxInLeasingPeriod,
|
||
objectRegionRegistration,
|
||
regionRegistration,
|
||
townRegistration,
|
||
registration,
|
||
},
|
||
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) {
|
||
if (!(vehicleTaxInYear > 0))
|
||
ctx.addIssue({
|
||
code: z.ZodIssueCode.custom,
|
||
message: 'Значение должно быть больше 0',
|
||
path: ['tbxVehicleTaxInYear'],
|
||
});
|
||
|
||
if (!(vehicleTaxInLeasingPeriod > 0))
|
||
ctx.addIssue({
|
||
code: z.ZodIssueCode.custom,
|
||
message: 'Значение должно быть больше 0',
|
||
path: ['tbxVehicleTaxInLeasingPeriod'],
|
||
});
|
||
|
||
if (!objectRegionRegistration)
|
||
ctx.addIssue({
|
||
code: z.ZodIssueCode.custom,
|
||
message: 'Не заполнено поле',
|
||
path: ['selectObjectRegionRegistration'],
|
||
});
|
||
|
||
if (!typePTS) {
|
||
ctx.addIssue({
|
||
code: z.ZodIssueCode.custom,
|
||
message: 'Не заполнено поле',
|
||
path: ['radioTypePTS'],
|
||
});
|
||
}
|
||
}
|
||
|
||
if (objectRegistration === 100_000_000 && !townRegistration)
|
||
ctx.addIssue({
|
||
code: z.ZodIssueCode.custom,
|
||
message: 'Не заполнено поле',
|
||
path: ['selectTownRegistration'],
|
||
});
|
||
|
||
if (!regionRegistration)
|
||
ctx.addIssue({
|
||
code: z.ZodIssueCode.custom,
|
||
message: 'Не заполнено поле',
|
||
path: ['selectRegionRegistration'],
|
||
});
|
||
|
||
if (!registration)
|
||
ctx.addIssue({
|
||
code: z.ZodIssueCode.custom,
|
||
message: 'Не заполнено поле',
|
||
path: ['selectRegistration'],
|
||
});
|
||
}
|
||
);
|
||
}
|