process: add createValidationReaction

This commit is contained in:
vchikalkin 2023-04-13 13:39:30 +03:00
parent 5a78a0f909
commit a286813d4d
18 changed files with 151 additions and 536 deletions

View File

@ -1,16 +1,12 @@
import { createValidationReaction } from '../tools';
import type { ProcessContext } from '../types';
import { createValidationSchema } from './validation';
import type { Elements } from '@/Components/Calculation/config/map/values';
import { selectRequirementTelematic } from '@/config/default-options';
import * as CRMTypes from '@/graphql/crm.types';
import type { Values } from '@/stores/calculation/values/types';
import ValidationHelper from '@/stores/validation/helper';
import { normalizeOptions } from '@/utils/entity';
import { debouncedReaction } from '@/utils/mobx';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import { reaction } from 'mobx';
import { uid } from 'radash';
dayjs.extend(utc);
@ -202,35 +198,4 @@ export default function reactions({ store, apolloClient }: ProcessContext) {
);
}
const key = uid(7);
export function validation(context: ProcessContext) {
const { store } = context;
const { $calculation } = store;
const validationSchema = createValidationSchema(context);
const helper = new ValidationHelper();
debouncedReaction(
() =>
$calculation.$values.getValues(Object.keys(validationSchema._def.schema.shape) as Values[]),
async (values) => {
helper.removeErrors();
const validationResult = await validationSchema.safeParseAsync(values);
if (validationResult.success === false) {
validationResult.error.errors.forEach(({ path, message }) => {
(path as Elements[]).forEach((elementName) => {
const removeError = $calculation.element(elementName).setError({ key, message });
if (removeError) helper.add(removeError);
});
});
} else {
helper.removeErrors();
}
},
{
delay: 1,
wait: 100,
}
);
}
export const validation = createValidationReaction(createValidationSchema);

View File

@ -1,2 +1,2 @@
export { default as common } from './common';
export { default as validation } from './validation';
export * from './validation';

View File

@ -1,37 +1,4 @@
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 { debouncedReaction } from '@/utils/mobx';
import { uid } from 'radash';
import { createValidationReaction } from '@/process/tools';
const key = uid(7);
export default function reactions(context: ProcessContext) {
const { store } = context;
const { $calculation } = store;
const validationSchema = createValidationSchema(context);
const helper = new ValidationHelper();
debouncedReaction(
() => $calculation.$values.getValues(['product', 'saleBonus']),
async (values) => {
const validationResult = await validationSchema.safeParseAsync(values);
if (validationResult.success === false) {
validationResult.error.errors.forEach(({ path, message }) => {
(path as Elements[]).forEach((elementName) => {
const removeError = $calculation.element(elementName).setError({ key, message });
if (removeError) helper.add(removeError);
});
});
} else {
helper.removeErrors();
}
},
{
delay: 1,
wait: 100,
}
);
}
export const validation = createValidationReaction(createValidationSchema);

View File

@ -1,4 +1,4 @@
export { default as filters } from './filters';
export { default as unlimited } from './unlimited';
export { default as validation } from './validation';
export * from './validation';
export { default as values } from './values';

View File

@ -1,37 +1,4 @@
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 { debouncedReaction } from '@/utils/mobx';
import { uid } from 'radash';
import { createValidationReaction } from '@/process/tools';
const key = uid(7);
export default function reactions(context: ProcessContext) {
const { store } = context;
const { $calculation } = store;
const validationSchema = createValidationSchema(context);
const helper = new ValidationHelper();
debouncedReaction(
() => $calculation.$values.getValues(['parmentsDecreasePercent', 'tarif']),
async (values) => {
const validationResult = await validationSchema.safeParseAsync(values);
if (validationResult.success === false) {
validationResult.error.errors.forEach(({ path, message }) => {
(path as Elements[]).forEach((elementName) => {
const removeError = $calculation.element(elementName).setError({ key, message });
if (removeError) helper.add(removeError);
});
});
} else {
helper.removeErrors();
}
},
{
delay: 1,
wait: 100,
}
);
}
export const validation = createValidationReaction(createValidationSchema);

View File

@ -1,19 +1,17 @@
import { createValidationReaction } from '../tools';
import type { ProcessContext } from '../types';
import helper from './lib/helper';
import { createValidationSchema } from './validation';
import { getTransTax } from '@/api/1c/query';
import type { Elements } from '@/Components/Calculation/config/map/values';
import { selectObjectCategoryTax } from '@/config/default-options';
import { STALE_TIME } from '@/constants/request';
import * as CRMTypes from '@/graphql/crm.types';
import ValidationHelper from '@/stores/validation/helper';
import { normalizeOptions } from '@/utils/entity';
import { debouncedReaction, disposableReaction } from '@/utils/mobx';
import { disposableReaction } from '@/utils/mobx';
import type { QueryFunctionContext } from '@tanstack/react-query';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import { reaction } from 'mobx';
import { uid } from 'radash';
dayjs.extend(utc);
@ -509,49 +507,4 @@ export function common({ store, apolloClient, queryClient }: ProcessContext) {
);
}
const key = uid(7);
export function validation(context: ProcessContext) {
const { store } = context;
const { $calculation } = store;
const validationSchema = createValidationSchema(context);
const validationHelper = new ValidationHelper();
debouncedReaction(
() =>
$calculation.$values.getValues([
'leaseObjectCategory',
'maxMass',
'leaseObjectType',
'typePTS',
'objectRegistration',
'objectCategoryTax',
'insNSIB',
'vehicleTaxInYear',
'vehicleTaxInLeasingPeriod',
'objectRegionRegistration',
'regionRegistration',
'townRegistration',
'registration',
]),
async (values) => {
validationHelper.removeErrors();
const validationResult = await validationSchema.safeParseAsync(values);
if (validationResult.success === false) {
validationResult.error.errors.forEach(({ path, message }) => {
(path as Elements[]).forEach((elementName) => {
const removeError = $calculation.element(elementName).setError({ key, message });
if (removeError) validationHelper.add(removeError);
});
});
} else {
validationHelper.removeErrors();
}
},
{
delay: 1,
wait: 100,
}
);
}
export const validation = createValidationReaction(createValidationSchema);

View File

@ -1,14 +1,12 @@
/* eslint-disable sonarjs/cognitive-complexity */
import { createValidationReaction } from '../tools';
import type { ProcessContext } from '../types';
import { createValidationSchema } from './validation';
import type { Elements } from '@/Components/Calculation/config/map/values';
import { selectLeaseObjectUseFor } from '@/config/default-options';
import * as CRMTypes from '@/graphql/crm.types';
import ValidationHelper from '@/stores/validation/helper';
import { normalizeOptions } from '@/utils/entity';
import { debouncedReaction } from '@/utils/mobx';
import { comparer, reaction, toJS } from 'mobx';
import { uid } from 'radash';
import { reaction } from 'mobx';
export function common({ store, apolloClient }: ProcessContext) {
const { $calculation, $tables } = store;
@ -273,59 +271,4 @@ export function common({ store, apolloClient }: ProcessContext) {
);
}
const key = uid(7);
export function validation(context: ProcessContext) {
const { $calculation, $tables } = context.store;
const validationSchema = createValidationSchema(context);
const helper = new ValidationHelper();
debouncedReaction(
() => {
const values = $calculation.$values.getValues([
'leasingPeriod',
'quote',
'recalcWithRevision',
'leasingWithoutKasko',
'insDecentral',
]);
return {
insurance: {
values: {
fingap: toJS($tables.insurance.row('fingap').getValues()),
kasko: toJS($tables.insurance.row('kasko').getValues()),
osago: toJS($tables.insurance.row('osago').getValues()),
},
},
...values,
};
},
async (values) => {
helper.removeErrors();
const validationResult = await validationSchema.safeParseAsync(values);
if (validationResult.success === false) {
validationResult.error.errors.forEach(({ path, message }) => {
(path as Array<Elements & 'insurance'>).forEach((elementName) => {
if (elementName === 'insurance') {
const removeError = $tables.insurance.setError({ key, message });
if (removeError) helper.add(removeError);
} else {
const removeError = $calculation.element(elementName).setError({ key, message });
if (removeError) helper.add(removeError);
}
});
});
} else {
helper.removeErrors();
}
},
{
delay: 1,
equals: comparer.structural,
wait: 100,
}
);
}
export const validation = createValidationReaction(createValidationSchema);

View File

@ -1,2 +1,2 @@
export { default as common } from './common';
export { default as validation } from './validation';
export * from './validation';

View File

@ -1,68 +1,4 @@
import { createValidationReaction } from '../../tools';
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 { debouncedReaction } from '@/utils/mobx';
import { autorun } from 'mobx';
import { uid } from 'radash';
const key = uid(7);
export default function reactions(context: ProcessContext) {
const { $calculation } = context.store;
/**
* Если model содержит данные и по связи Модель-Комплектация в CRM у данной модели есть связанные активные записи Комплектаций,
* то configuration становится обязательным для заполнения, иначе configuration не обязателен для заполнения
*/
autorun(
() => {
const selectConfiguration = $calculation.element('selectConfiguration');
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();
debouncedReaction(
() =>
$calculation.$values.getValues([
'leaseObjectType',
'engineVolume',
'engineType',
'leaseObjectMotorPower',
'countSeats',
'maxMass',
'leaseObjectCategory',
]),
async (values) => {
const validationResult = await validationSchema.safeParseAsync(values);
if (validationResult.success === false) {
validationResult.error.errors.forEach(({ path, message }) => {
(path as Elements[]).forEach((elementName) => {
const removeError = $calculation.element(elementName).setError({ key, message });
if (removeError) helper.add(removeError);
});
});
} else {
helper.removeErrors();
}
},
{
delay: 1,
wait: 100,
}
);
}
export const validation = createValidationReaction(createValidationSchema);

View File

@ -1,2 +1,2 @@
export { default as common } from './common';
export { default as validation } from './validation';
export * from './validation';

View File

@ -1,58 +1,4 @@
/* eslint-disable sonarjs/cognitive-complexity */
import { createValidationReaction } from '../../tools';
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 { comparer, reaction, toJS } from 'mobx';
import { uid } from 'radash';
const key = uid(7);
export default function reactions(context: ProcessContext) {
const { $calculation, $tables } = context.store;
const validationSchema = createValidationSchema();
const helper = new ValidationHelper();
reaction(
() => {
const payments = toJS($tables.payments.values);
const values = $calculation.$values.getValues([
'graphType',
'highSeasonStart',
'leasingPeriod',
'seasonType',
'insNSIB',
'lastPaymentRub',
]);
return {
payments: { values: payments },
...values,
};
},
async (values) => {
const validationResult = await validationSchema.safeParseAsync(values);
if (validationResult.success === false) {
validationResult.error.errors.forEach(({ path, message }) => {
(path as Array<Elements & 'payments'>).forEach((elementName) => {
if (elementName === 'payments') {
const removeError = $tables.payments.setError({ key, message });
if (removeError) helper.add(removeError);
} else {
const removeError = $calculation.element(elementName).setError({ key, message });
if (removeError) helper.add(removeError);
}
});
});
} else {
helper.removeErrors();
}
},
{
delay: 100,
equals: comparer.structural,
}
);
}
export const validation = createValidationReaction(createValidationSchema);

View File

@ -1,3 +1,3 @@
export { default as common } from './common';
export { default as computed } from './computed';
export { default as validation } from './validation';
export * from './validation';

View File

@ -1,46 +1,4 @@
import { createValidationReaction } from '../../tools';
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 { debouncedReaction } from '@/utils/mobx';
import { uid } from 'radash';
const key = uid(7);
export default function reactions(context: ProcessContext) {
const { store } = context;
const { $calculation } = store;
const validationSchema = createValidationSchema(context);
const helper = new ValidationHelper();
debouncedReaction(
() =>
$calculation.$values.getValues([
'VATInLeaseObjectPrice',
'leaseObjectPriceWthtVAT',
'product',
'supplierDiscountRub',
'plPriceRub',
'firstPaymentRub',
'subsidySum',
]),
async (values) => {
const validationResult = await validationSchema.safeParseAsync(values);
if (validationResult.success === false) {
validationResult.error.errors.forEach(({ path, message }) => {
(path as Elements[]).forEach((elementName) => {
const removeError = $calculation.element(elementName).setError({ key, message });
if (removeError) helper.add(removeError);
});
});
} else {
helper.removeErrors();
}
},
{
delay: 1,
wait: 100,
}
);
}
export const validation = createValidationReaction(createValidationSchema);

View File

@ -1,13 +1,11 @@
import type { ProcessContext } from '../types';
import { createValidationSchema } from './validation';
import type { Elements } from '@/Components/Calculation/config/map/values';
import type { Values } from '@/stores/calculation/values/types';
import ValidationHelper from '@/stores/validation/helper';
import { debouncedReaction, disposableReaction } from '@/utils/mobx';
import { createValidationReaction } from '@/process/tools';
import { disposableReaction } from '@/utils/mobx';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import { reaction } from 'mobx';
import { uid } from 'radash';
dayjs.extend(utc);
@ -142,34 +140,4 @@ export function common({ store }: ProcessContext) {
}
}
const key = uid(7);
export function validation(context: ProcessContext) {
const { store } = context;
const { $calculation } = store;
const validationSchema = createValidationSchema(context);
const helper = new ValidationHelper();
debouncedReaction(
() =>
$calculation.$values.getValues(Object.keys(validationSchema._def.schema.shape) as Values[]),
async (values) => {
const validationResult = await validationSchema.safeParseAsync(values);
if (validationResult.success === false) {
validationResult.error.errors.forEach(({ path, message }) => {
(path as Elements[]).forEach((elementName) => {
const removeError = $calculation.element(elementName).setError({ key, message });
if (removeError) helper.add(removeError);
});
});
} else {
helper.removeErrors();
}
},
{
delay: 1,
wait: 100,
}
);
}
export const validation = createValidationReaction(createValidationSchema);

View File

@ -11,4 +11,4 @@ function common(context: ProcessContext) {
export { common };
export { default as validation } from './validation';
export * from './validation';

View File

@ -1,72 +1,4 @@
import { createValidationReaction } from '../../tools';
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 { debouncedReaction } from '@/utils/mobx';
import { uid } from 'radash';
const key = uid(7);
export default function reactions(context: ProcessContext) {
const { store } = context;
const { $calculation } = store;
const validationSchema = createValidationSchema(context);
const helper = new ValidationHelper();
debouncedReaction(
() => {
const values = $calculation.$values.getValues([
'calcBrokerRewardCondition',
'calcBrokerRewardSum',
'calcDoubleAgentRewardCondition',
'calcDoubleAgentRewardSumm',
'dealer',
'dealerBroker',
'calcFinDepartment',
'dealerBrokerRewardCondition',
'dealerBrokerRewardSumm',
'dealerPerson',
'dealerRewardCondition',
'dealerRewardSumm',
'finDepartmentRewardCondtion',
'finDepartmentRewardSumm',
'indAgent',
'indAgentRewardCondition',
'indAgentRewardSumm',
'calcDoubleAgent',
'calcBroker',
]);
const options = (
[
'selectCalcBrokerRewardCondition',
'selectCalcDoubleAgentRewardCondition',
'selectDealerBrokerRewardCondition',
'selectDealerRewardCondition',
'selectFinDepartmentRewardCondtion',
'selectIndAgentRewardCondition',
] as Elements[]
).map((elementName) => $calculation.element(elementName).getOptions());
return { options, values };
},
async ({ values }) => {
const validationResult = await validationSchema.safeParseAsync(values);
if (validationResult.success === false) {
validationResult.error.errors.forEach(({ path, message }) => {
(path as Elements[]).forEach((elementName) => {
const removeError = $calculation.element(elementName).setError({ key, message });
if (removeError) helper.add(removeError);
});
});
} else {
helper.removeErrors();
}
},
{
delay: 1,
wait: 100,
}
);
}
export const validation = createValidationReaction(createValidationSchema);

120
apps/web/process/tools.ts Normal file
View File

@ -0,0 +1,120 @@
/* eslint-disable sonarjs/cognitive-complexity */
import { debouncedReaction } from '../utils/mobx';
import type { ProcessContext } from './types';
import type { Elements } from '@/Components/Calculation/config/map/values';
import type { Values } from '@/stores/calculation/values/types';
import ValidationHelper from '@/stores/validation/helper';
import { comparer, toJS } from 'mobx';
import { uid } from 'radash';
import type { ZodTypeAny } from 'zod';
export function createValidationReaction<T extends ZodTypeAny>(
createValidationSchema: (context: ProcessContext) => T
) {
const key = uid(7);
return (context: ProcessContext) => {
const validationSchema = createValidationSchema(context);
const shapeValues = Object.keys(validationSchema._def.schema.shape) as string[];
const { store } = context;
const { $calculation, $tables } = store;
const helper = new ValidationHelper();
if (shapeValues.includes('insurance')) {
debouncedReaction(
() => ({
...$calculation.$values.getValues(shapeValues as Values[]),
insurance: {
values: {
fingap: toJS($tables.insurance.row('fingap').getValues()),
kasko: toJS($tables.insurance.row('kasko').getValues()),
osago: toJS($tables.insurance.row('osago').getValues()),
},
},
}),
async (values) => {
helper.removeErrors();
const validationResult = await validationSchema.safeParseAsync(values);
if (validationResult.success === false) {
validationResult.error.errors.forEach(({ path, message }) => {
(path as Array<Elements & 'insurance'>).forEach((elementName) => {
if (elementName === 'insurance') {
const removeError = $tables.insurance.setError({ key, message });
if (removeError) helper.add(removeError);
} else {
const removeError = $calculation.element(elementName).setError({ key, message });
if (removeError) helper.add(removeError);
}
});
});
} else {
helper.removeErrors();
}
},
{
delay: 1,
equals: comparer.structural,
wait: 100,
}
);
} else if (shapeValues.includes('payments')) {
debouncedReaction(
() => ({
...$calculation.$values.getValues(shapeValues as Values[]),
payments: { values: toJS($tables.payments.values) },
}),
async (values) => {
helper.removeErrors();
const validationResult = await validationSchema.safeParseAsync(values);
if (validationResult.success === false) {
validationResult.error.errors.forEach(({ path, message }) => {
(path as Array<Elements & 'payments'>).forEach((elementName) => {
if (elementName === 'payments') {
const removeError = $tables.payments.setError({ key, message });
if (removeError) helper.add(removeError);
} else {
const removeError = $calculation.element(elementName).setError({ key, message });
if (removeError) helper.add(removeError);
}
});
});
} else {
helper.removeErrors();
}
},
{
delay: 1,
equals: comparer.structural,
wait: 100,
}
);
} else {
debouncedReaction(
() => $calculation.$values.getValues(shapeValues as Values[]),
async (values) => {
helper.removeErrors();
const validationResult = await validationSchema.safeParseAsync(values);
if (validationResult.success === false) {
validationResult.error.errors.forEach(({ path, message }) => {
(path as Elements[]).forEach((elementName) => {
const removeError = $calculation.element(elementName).setError({ key, message });
if (removeError) helper.add(removeError);
});
});
} else {
helper.removeErrors();
}
},
{
delay: 1,
wait: 100,
}
);
}
};
}

View File

@ -1,11 +1,8 @@
import type { ProcessContext } from '../types';
import { createValidationSchema } from './validation';
import type { Elements } from '@/Components/Calculation/config/map/values';
import * as CRMTypes from '@/graphql/crm.types';
import ValidationHelper from '@/stores/validation/helper';
import { debouncedReaction } from '@/utils/mobx';
import { createValidationReaction } from '@/process/tools';
import { reaction } from 'mobx';
import { uid } from 'radash';
export function common({ store, apolloClient }: ProcessContext) {
const { $calculation } = store;
@ -241,41 +238,4 @@ export function common({ store, apolloClient }: ProcessContext) {
);
}
const key = uid(7);
export function validation(context: ProcessContext) {
const { store } = context;
const { $calculation } = store;
const validationSchema = createValidationSchema(context);
const helper = new ValidationHelper();
debouncedReaction(
() =>
$calculation.$values.getValues([
'engineHours',
'leaseObjectCategory',
'leaseObjectType',
'leaseObjectUsed',
'mileage',
'vin',
]),
async (values) => {
const validationResult = await validationSchema.safeParseAsync(values);
if (validationResult.success === false) {
validationResult.error.errors.forEach(({ path, message }) => {
(path as Elements[]).forEach((elementName) => {
const removeError = $calculation.element(elementName).setError({ key, message });
if (removeError) helper.add(removeError);
});
});
} else {
helper.removeErrors();
}
},
{
delay: 1,
wait: 100,
}
);
}
export const validation = createValidationReaction(createValidationSchema);