process/leasing-object: use new validation
This commit is contained in:
parent
bb9bded60f
commit
95bb7e0132
@ -8,7 +8,7 @@ import * as gibdd from '@/process/gibdd';
|
||||
import { useProcess } from '@/process/hooks';
|
||||
import * as insurance from '@/process/insurance';
|
||||
import * as leadOpportunity from '@/process/lead-opportunity';
|
||||
// import * as leasingObject from '@/process/leasing-object';
|
||||
import * as leasingObject from '@/process/leasing-object';
|
||||
import * as leasingWithoutKasko from '@/process/leasing-without-kasko';
|
||||
import * as loadKP from '@/process/load-kp';
|
||||
import * as payments from '@/process/payments';
|
||||
@ -27,7 +27,7 @@ export default function useReactions() {
|
||||
useProcess(fingap);
|
||||
useProcess(leasingWithoutKasko);
|
||||
useProcess(subsidy);
|
||||
// useProcess(leasingObject);
|
||||
useProcess(leasingObject);
|
||||
useProcess(configurator);
|
||||
useProcess(createKP);
|
||||
useProcess(bonuses);
|
||||
|
||||
@ -405,4 +405,42 @@ export default function reactions({ store, apolloClient }: ProcessContext) {
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
reaction(
|
||||
() =>
|
||||
$calculation.$values.getValues([
|
||||
'leaseObjectType',
|
||||
'engineVolume',
|
||||
'engineType',
|
||||
'leaseObjectMotorPower',
|
||||
]),
|
||||
async ({ leaseObjectType: leaseObjectTypeId }) => {
|
||||
if (!leaseObjectTypeId) {
|
||||
$calculation.element('selectEngineType').unblock();
|
||||
$calculation.element('tbxEngineVolume').unblock();
|
||||
$calculation.element('tbxLeaseObjectMotorPower').unblock();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
const {
|
||||
data: { evo_leasingobject_type },
|
||||
} = await apolloClient.query({
|
||||
query: CRMTypes.GetLeaseObjectTypeDocument,
|
||||
variables: {
|
||||
leaseObjectTypeId,
|
||||
},
|
||||
});
|
||||
|
||||
if (evo_leasingobject_type?.evo_id === '8') {
|
||||
$calculation.element('selectEngineType').resetValue().block();
|
||||
$calculation.element('tbxEngineVolume').resetValue().block();
|
||||
$calculation.element('tbxLeaseObjectMotorPower').resetValue().block();
|
||||
} else {
|
||||
$calculation.element('selectEngineType').unblock();
|
||||
$calculation.element('tbxEngineVolume').unblock();
|
||||
$calculation.element('tbxLeaseObjectMotorPower').unblock();
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@ -1,11 +1,15 @@
|
||||
/* eslint-disable @typescript-eslint/naming-convention */
|
||||
import * as CRMTypes from '@/graphql/crm.types';
|
||||
import { createValidationSchema } from '../validation';
|
||||
import type { Elements } from '@/Components/Calculation/config/map/values';
|
||||
import type { ProcessContext } from '@/process/types';
|
||||
import ValidationHelper from '@/stores/validation/helper';
|
||||
import { autorun, reaction } from 'mobx';
|
||||
import { uid } from 'radash';
|
||||
|
||||
export default function reactions({ store, apolloClient }: ProcessContext) {
|
||||
const { $calculation } = store;
|
||||
const key = uid(7);
|
||||
|
||||
export default function reactions(context: ProcessContext) {
|
||||
const { $calculation } = context.store;
|
||||
|
||||
/**
|
||||
* Если model содержит данные и по связи Модель-Комплектация в CRM у данной модели есть связанные активные записи Комплектаций,
|
||||
@ -14,16 +18,23 @@ export default function reactions({ store, apolloClient }: ProcessContext) {
|
||||
autorun(
|
||||
() => {
|
||||
const selectConfiguration = $calculation.element('selectConfiguration');
|
||||
selectConfiguration.validate({
|
||||
invalid: selectConfiguration.getOptions()?.length > 0 && !selectConfiguration.getValue(),
|
||||
message: 'Не заполнено поле',
|
||||
});
|
||||
if (selectConfiguration.getOptions()?.length > 0 && !selectConfiguration.getValue()) {
|
||||
selectConfiguration.setError({
|
||||
key,
|
||||
message: 'Не заполнено поле',
|
||||
});
|
||||
} else {
|
||||
selectConfiguration.removeError({ key });
|
||||
}
|
||||
},
|
||||
{
|
||||
delay: 10,
|
||||
}
|
||||
);
|
||||
|
||||
const validationSchema = createValidationSchema(context);
|
||||
const helper = new ValidationHelper();
|
||||
|
||||
reaction(
|
||||
() =>
|
||||
$calculation.$values.getValues([
|
||||
@ -31,134 +42,22 @@ export default function reactions({ store, apolloClient }: ProcessContext) {
|
||||
'engineVolume',
|
||||
'engineType',
|
||||
'leaseObjectMotorPower',
|
||||
'countSeats',
|
||||
'maxMass',
|
||||
'leaseObjectCategory',
|
||||
]),
|
||||
async ({
|
||||
engineType,
|
||||
engineVolume,
|
||||
leaseObjectType: leaseObjectTypeId,
|
||||
leaseObjectMotorPower,
|
||||
}) => {
|
||||
if (!leaseObjectTypeId) {
|
||||
$calculation.element('selectEngineType').unblock();
|
||||
$calculation.element('tbxEngineVolume').unblock();
|
||||
$calculation.element('tbxLeaseObjectMotorPower').unblock();
|
||||
async (values) => {
|
||||
helper.removeErrors();
|
||||
const validationResult = await validationSchema.safeParseAsync(values);
|
||||
|
||||
return;
|
||||
if (!validationResult.success) {
|
||||
validationResult.error.errors.forEach(({ path, message }) => {
|
||||
(path as Elements[]).forEach((elementName) => {
|
||||
const removeError = $calculation.element(elementName).setError({ key, message });
|
||||
if (removeError) helper.add(removeError);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
const {
|
||||
data: { evo_leasingobject_type },
|
||||
} = await apolloClient.query({
|
||||
query: CRMTypes.GetLeaseObjectTypeDocument,
|
||||
variables: {
|
||||
leaseObjectTypeId,
|
||||
},
|
||||
});
|
||||
|
||||
if (evo_leasingobject_type?.evo_id === '8') {
|
||||
$calculation.element('selectEngineType').resetValue().block();
|
||||
$calculation.element('tbxEngineVolume').resetValue().block();
|
||||
$calculation.element('tbxLeaseObjectMotorPower').resetValue().block();
|
||||
} else {
|
||||
$calculation.element('selectEngineType').unblock();
|
||||
$calculation.element('tbxEngineVolume').unblock();
|
||||
$calculation.element('tbxLeaseObjectMotorPower').unblock();
|
||||
}
|
||||
|
||||
const isNotTrailer =
|
||||
evo_leasingobject_type?.evo_id !== null && evo_leasingobject_type?.evo_id !== '8';
|
||||
|
||||
$calculation.element('tbxEngineVolume').validate({
|
||||
invalid: isNotTrailer && engineVolume <= 0,
|
||||
message: 'Не заполнено поле',
|
||||
});
|
||||
$calculation.element('selectEngineType').validate({
|
||||
invalid: isNotTrailer && !engineType,
|
||||
message: 'Не заполнено поле',
|
||||
});
|
||||
$calculation.element('tbxLeaseObjectMotorPower').validate({
|
||||
invalid: isNotTrailer && leaseObjectMotorPower <= 0,
|
||||
message: 'Не заполнено поле',
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
{
|
||||
const validationHelper = new ValidationHelper();
|
||||
|
||||
reaction(
|
||||
() => $calculation.$values.getValues(['leaseObjectType', 'countSeats', 'maxMass']),
|
||||
async ({ countSeats, leaseObjectType: leaseObjectTypeId, maxMass }) => {
|
||||
if (!leaseObjectTypeId) {
|
||||
validationHelper.removeErrors();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
const {
|
||||
data: { evo_leasingobject_type },
|
||||
} = await apolloClient.query({
|
||||
query: CRMTypes.GetLeaseObjectTypeDocument,
|
||||
variables: {
|
||||
leaseObjectTypeId,
|
||||
},
|
||||
});
|
||||
|
||||
$calculation.element('tbxCountSeats').validate({
|
||||
helper: validationHelper,
|
||||
invalid: evo_leasingobject_type?.evo_id === '1' && countSeats >= 9,
|
||||
message: 'Количество мест должно быть меньше 9',
|
||||
});
|
||||
|
||||
$calculation.element('tbxCountSeats').validate({
|
||||
helper: validationHelper,
|
||||
invalid:
|
||||
(evo_leasingobject_type?.evo_id === '4' || evo_leasingobject_type?.evo_id === '5') &&
|
||||
countSeats <= 8,
|
||||
message: 'Количество мест должно быть больше 8',
|
||||
});
|
||||
|
||||
$calculation.element('tbxMaxMass').validate({
|
||||
helper: validationHelper,
|
||||
invalid: evo_leasingobject_type?.evo_id === '2' && maxMass <= 0,
|
||||
message: 'Не заполнено поле',
|
||||
});
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
{
|
||||
const validationHelper = new ValidationHelper();
|
||||
|
||||
reaction(
|
||||
() => $calculation.$values.getValues(['leaseObjectType', 'leaseObjectCategory']),
|
||||
async ({ leaseObjectCategory, leaseObjectType: leaseObjectTypeId }) => {
|
||||
if (!leaseObjectTypeId) {
|
||||
validationHelper.removeErrors();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
const {
|
||||
data: { evo_leasingobject_type },
|
||||
} = await apolloClient.query({
|
||||
query: CRMTypes.GetLeaseObjectTypeDocument,
|
||||
variables: {
|
||||
leaseObjectTypeId,
|
||||
},
|
||||
});
|
||||
|
||||
$calculation.element('selectLeaseObjectCategory').validate({
|
||||
helper: validationHelper,
|
||||
invalid:
|
||||
!leaseObjectCategory &&
|
||||
Boolean(
|
||||
evo_leasingobject_type?.evo_id &&
|
||||
!['6', '9', '10'].includes(evo_leasingobject_type?.evo_id)
|
||||
),
|
||||
message: 'Не заполнено поле',
|
||||
});
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
109
apps/web/process/leasing-object/validation.ts
Normal file
109
apps/web/process/leasing-object/validation.ts
Normal file
@ -0,0 +1,109 @@
|
||||
/* eslint-disable @typescript-eslint/naming-convention */
|
||||
import type { ValidationContext } from '../types';
|
||||
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({
|
||||
countSeats: true,
|
||||
engineType: true,
|
||||
engineVolume: true,
|
||||
leaseObjectCategory: true,
|
||||
leaseObjectMotorPower: true,
|
||||
leaseObjectType: true,
|
||||
maxMass: true,
|
||||
}).superRefine(
|
||||
async (
|
||||
{
|
||||
leaseObjectType: leaseObjectTypeId,
|
||||
engineVolume,
|
||||
engineType,
|
||||
leaseObjectMotorPower,
|
||||
countSeats,
|
||||
maxMass,
|
||||
leaseObjectCategory,
|
||||
},
|
||||
ctx
|
||||
) => {
|
||||
if (leaseObjectTypeId) {
|
||||
const {
|
||||
data: { evo_leasingobject_type },
|
||||
} = await apolloClient.query({
|
||||
query: CRMTypes.GetLeaseObjectTypeDocument,
|
||||
variables: {
|
||||
leaseObjectTypeId,
|
||||
},
|
||||
});
|
||||
|
||||
const isNotTrailer =
|
||||
evo_leasingobject_type?.evo_id !== null && evo_leasingobject_type?.evo_id !== '8';
|
||||
|
||||
if (isNotTrailer && engineVolume <= 0) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: 'Не заполнено поле',
|
||||
path: ['tbxEngineVolume'],
|
||||
});
|
||||
}
|
||||
|
||||
if (isNotTrailer && !engineType) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: 'Не заполнено поле',
|
||||
path: ['selectEngineType'],
|
||||
});
|
||||
}
|
||||
|
||||
if (isNotTrailer && leaseObjectMotorPower <= 0) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: 'Не заполнено поле',
|
||||
path: ['tbxLeaseObjectMotorPower'],
|
||||
});
|
||||
}
|
||||
|
||||
if (evo_leasingobject_type?.evo_id === '1' && countSeats >= 9) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: 'Количество мест должно быть меньше 9',
|
||||
path: ['tbxCountSeats'],
|
||||
});
|
||||
}
|
||||
|
||||
if (
|
||||
(evo_leasingobject_type?.evo_id === '4' || evo_leasingobject_type?.evo_id === '5') &&
|
||||
countSeats <= 8
|
||||
) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: 'Количество мест должно быть больше 8',
|
||||
path: ['tbxCountSeats'],
|
||||
});
|
||||
}
|
||||
|
||||
if (evo_leasingobject_type?.evo_id === '2' && maxMass <= 0) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: 'Не заполнено поле',
|
||||
path: ['tbxMaxMass'],
|
||||
});
|
||||
}
|
||||
|
||||
if (
|
||||
!leaseObjectCategory &&
|
||||
Boolean(
|
||||
evo_leasingobject_type?.evo_id &&
|
||||
!['6', '9', '10'].includes(evo_leasingobject_type?.evo_id)
|
||||
)
|
||||
) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: 'Не заполнено поле',
|
||||
path: ['selectLeaseObjectCategory'],
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user