process/configurator: use new validation
This commit is contained in:
parent
91899164c0
commit
49d652c7fe
@ -33,10 +33,10 @@ const Validation = observer(() => {
|
||||
const store = useStore();
|
||||
const { payments } = store.$tables;
|
||||
|
||||
const messages = payments.validation.getMessages();
|
||||
const errors = payments.validation.getErrors();
|
||||
|
||||
if (messages?.length) {
|
||||
return <Alert type="error" banner message={messages[0]} />;
|
||||
if (errors?.length) {
|
||||
return <Alert type="error" banner message={errors[0].message} />;
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
// import * as addProduct from '@/process/add-product';
|
||||
import * as bonuses from '@/process/bonuses';
|
||||
// import * as calculate from '@/process/calculate';
|
||||
// import * as configurator from '@/process/configurator';
|
||||
import * as configurator from '@/process/configurator';
|
||||
// import * as createKP from '@/process/create-kp';
|
||||
// import * as fingap from '@/process/fingap';
|
||||
// import * as gibdd from '@/process/gibdd';
|
||||
@ -28,7 +28,7 @@ export default function useReactions() {
|
||||
// useProcess(leasingWithoutKasko);
|
||||
// useProcess(subsidy);
|
||||
// useProcess(leasingObject);
|
||||
// useProcess(configurator);
|
||||
useProcess(configurator);
|
||||
// useProcess(createKP);
|
||||
useProcess(bonuses);
|
||||
// useProcess(usedPl);
|
||||
|
||||
@ -1,44 +1,32 @@
|
||||
/* 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 { reaction } from 'mobx';
|
||||
import { uid } from 'radash';
|
||||
|
||||
export default function reactions({ store, apolloClient }: ProcessContext) {
|
||||
const key = uid(7);
|
||||
|
||||
export default function reactions(context: ProcessContext) {
|
||||
const { store } = context;
|
||||
const { $calculation } = store;
|
||||
/**
|
||||
* На изменение поля Процет убывания платежей tbxParmentsDecreasePercent необходимо заложить проверку:
|
||||
* Если значение поля меньше значения в поле "Минимальный % убывания платежей" evo_min_decreasing_perc из записи,
|
||||
* указанной в поле ТарифselectTarif , то поле Процет убывания платежей tbxParmentsDecreasePercent должно обводиться красной рамкой
|
||||
* и выводиться сообщение "Процент убывания не может быть меньше минимального значения по данному тарифу
|
||||
* - <указывается значение из поля "Минимальный % убывания платежей">, иначе красная рамка снимается.
|
||||
* При красной рамке в данном поле нельзя осуществить расчет графика.
|
||||
*/
|
||||
const validationSchema = createValidationSchema(context);
|
||||
|
||||
const helper = new ValidationHelper();
|
||||
reaction(
|
||||
() => ({
|
||||
parmentsDecreasePercent: $calculation.element('tbxParmentsDecreasePercent').getValue(),
|
||||
tarifId: $calculation.element('selectTarif').getValue(),
|
||||
}),
|
||||
async ({ parmentsDecreasePercent, tarifId }) => {
|
||||
let evo_tarif: CRMTypes.GetTarifQuery['evo_tarif'] = null;
|
||||
() => $calculation.$values.getValues(['parmentsDecreasePercent', 'tarif']),
|
||||
async (values) => {
|
||||
helper.removeErrors();
|
||||
const validationResult = await validationSchema.safeParseAsync(values);
|
||||
|
||||
if (tarifId) {
|
||||
const { data } = await apolloClient.query({
|
||||
query: CRMTypes.GetTarifDocument,
|
||||
variables: {
|
||||
tarifId,
|
||||
},
|
||||
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);
|
||||
});
|
||||
});
|
||||
|
||||
({ evo_tarif } = data);
|
||||
}
|
||||
|
||||
$calculation.element('tbxParmentsDecreasePercent').validate({
|
||||
invalid: Boolean(
|
||||
evo_tarif?.evo_min_decreasing_perc &&
|
||||
parmentsDecreasePercent < evo_tarif?.evo_min_decreasing_perc
|
||||
),
|
||||
message: `Процент убывания не может быть меньше минимального значения по данному тарифу - ${evo_tarif?.evo_min_decreasing_perc}`,
|
||||
});
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
41
apps/web/process/configurator/validation.ts
Normal file
41
apps/web/process/configurator/validation.ts
Normal file
@ -0,0 +1,41 @@
|
||||
/* eslint-disable @typescript-eslint/naming-convention */
|
||||
import type { ProcessContext } from '../types';
|
||||
import ValuesSchema from '@/config/schema/values';
|
||||
import * as CRMTypes from '@/graphql/crm.types';
|
||||
import { z } from 'zod';
|
||||
|
||||
export function createValidationSchema({ apolloClient }: ProcessContext) {
|
||||
return ValuesSchema.pick({ parmentsDecreasePercent: true, tarif: true }).superRefine(
|
||||
async ({ parmentsDecreasePercent, tarif: tarifId }, ctx) => {
|
||||
/**
|
||||
* На изменение поля Процет убывания платежей tbxParmentsDecreasePercent необходимо заложить проверку:
|
||||
* Если значение поля меньше значения в поле "Минимальный % убывания платежей" evo_min_decreasing_perc из записи,
|
||||
* указанной в поле ТарифselectTarif , то поле Процет убывания платежей tbxParmentsDecreasePercent должно обводиться красной рамкой
|
||||
* и выводиться сообщение "Процент убывания не может быть меньше минимального значения по данному тарифу
|
||||
* - <указывается значение из поля "Минимальный % убывания платежей">, иначе красная рамка снимается.
|
||||
* При красной рамке в данном поле нельзя осуществить расчет графика.
|
||||
*/
|
||||
if (tarifId) {
|
||||
const {
|
||||
data: { evo_tarif },
|
||||
} = await apolloClient.query({
|
||||
query: CRMTypes.GetTarifDocument,
|
||||
variables: {
|
||||
tarifId,
|
||||
},
|
||||
});
|
||||
|
||||
if (
|
||||
evo_tarif?.evo_min_decreasing_perc &&
|
||||
parmentsDecreasePercent < evo_tarif?.evo_min_decreasing_perc
|
||||
) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: `Процент убывания не может быть меньше минимального значения по данному тарифу - ${evo_tarif?.evo_min_decreasing_perc}`,
|
||||
path: ['tbxParmentsDecreasePercent'],
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user