process/add-product: create validation
This commit is contained in:
parent
df708e40ae
commit
5a78a0f909
@ -1,2 +1,3 @@
|
||||
export * from './get-kp-data';
|
||||
export * as reactions from './reactions';
|
||||
export * from './validation';
|
||||
|
||||
@ -1,9 +1,16 @@
|
||||
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);
|
||||
|
||||
@ -117,4 +124,113 @@ export default function reactions({ store, apolloClient }: ProcessContext) {
|
||||
fireImmediately: true,
|
||||
}
|
||||
);
|
||||
|
||||
reaction(
|
||||
() => $calculation.$values.getValues(['recalcWithRevision', 'leaseObjectType']),
|
||||
async ({ recalcWithRevision, leaseObjectType: leaseObjectTypeId }) => {
|
||||
if (recalcWithRevision === false) {
|
||||
$calculation
|
||||
.element('selectRequirementTelematic')
|
||||
.setOptions(
|
||||
selectRequirementTelematic.filter((x) =>
|
||||
[100_000_000, 100_000_001, 100_000_002, 100_000_003].includes(x.value)
|
||||
)
|
||||
);
|
||||
|
||||
if (leaseObjectTypeId) {
|
||||
const {
|
||||
data: { evo_leasingobject_type },
|
||||
} = await apolloClient.query({
|
||||
query: CRMTypes.GetLeaseObjectTypeDocument,
|
||||
variables: { leaseObjectTypeId },
|
||||
});
|
||||
|
||||
if (evo_leasingobject_type?.evo_id === '11') {
|
||||
$calculation.element('selectRequirementTelematic').setValue(100_000_000).block();
|
||||
} else {
|
||||
$calculation.element('selectRequirementTelematic').unblock();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$calculation.element('selectRequirementTelematic').resetOptions();
|
||||
}
|
||||
},
|
||||
{
|
||||
fireImmediately: true,
|
||||
}
|
||||
);
|
||||
|
||||
reaction(
|
||||
() => $calculation.$values.getValues(['requirementTelematic', 'recalcWithRevision']),
|
||||
async ({ requirementTelematic, recalcWithRevision }) => {
|
||||
const currentDate = dayjs().utc(false).format('YYYY-MM-DD');
|
||||
const {
|
||||
data: { evo_addproduct_types: trackerTypes },
|
||||
} = await apolloClient.query({
|
||||
query: CRMTypes.GetTrackerTypesDocument,
|
||||
variables: { currentDate },
|
||||
});
|
||||
|
||||
const {
|
||||
data: { evo_addproduct_types: telematicTypes },
|
||||
} = await apolloClient.query({
|
||||
query: CRMTypes.GetTelematicTypesDocument,
|
||||
variables: { currentDate },
|
||||
});
|
||||
|
||||
let filteredTrackerTypes = trackerTypes?.filter(
|
||||
(x) => requirementTelematic && x?.evo_controls_program?.includes(requirementTelematic)
|
||||
);
|
||||
|
||||
let filteredTelematicTypes = telematicTypes?.filter(
|
||||
(x) => requirementTelematic && x?.evo_controls_program?.includes(requirementTelematic)
|
||||
);
|
||||
|
||||
if (!recalcWithRevision) {
|
||||
filteredTrackerTypes = filteredTrackerTypes?.filter((x) => x?.evo_visible_calc === true);
|
||||
filteredTelematicTypes = filteredTelematicTypes?.filter(
|
||||
(x) => x?.evo_visible_calc === true
|
||||
);
|
||||
}
|
||||
|
||||
$calculation.element('selectTracker').setOptions(normalizeOptions(filteredTrackerTypes));
|
||||
$calculation.element('selectTelematic').setOptions(normalizeOptions(filteredTelematicTypes));
|
||||
},
|
||||
{
|
||||
fireImmediately: true,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
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,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
28
apps/web/process/add-product/validation.ts
Normal file
28
apps/web/process/add-product/validation.ts
Normal file
@ -0,0 +1,28 @@
|
||||
import type { ValidationContext } from '../types';
|
||||
import ValuesSchema from '@/config/schema/values';
|
||||
import { z } from 'zod';
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
export function createValidationSchema(context: ValidationContext) {
|
||||
return ValuesSchema.pick({
|
||||
requirementTelematic: true,
|
||||
telematic: true,
|
||||
tracker: true,
|
||||
}).superRefine(async ({ requirementTelematic, telematic, tracker }, ctx) => {
|
||||
if (requirementTelematic !== 100_000_004 && !telematic && !tracker) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: 'Не заполнено поле',
|
||||
path: ['selectTracker', 'selectTelematic'],
|
||||
});
|
||||
}
|
||||
|
||||
if (requirementTelematic === 100_000_004 && (telematic || tracker)) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: 'Не требуется',
|
||||
path: ['selectTracker', 'selectTelematic'],
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -1,11 +1,8 @@
|
||||
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, disposableReaction } from '@/utils/mobx';
|
||||
import dayjs from 'dayjs';
|
||||
import utc from 'dayjs/plugin/utc';
|
||||
@ -14,7 +11,7 @@ import { uid } from 'radash';
|
||||
|
||||
dayjs.extend(utc);
|
||||
|
||||
export function common({ store, apolloClient }: ProcessContext) {
|
||||
export function common({ store }: ProcessContext) {
|
||||
const { $calculation, $tables } = store;
|
||||
|
||||
reaction(
|
||||
@ -56,76 +53,6 @@ export function common({ store, apolloClient }: ProcessContext) {
|
||||
}
|
||||
);
|
||||
|
||||
reaction(
|
||||
() => $calculation.$values.getValues(['recalcWithRevision', 'leaseObjectType']),
|
||||
async ({ recalcWithRevision, leaseObjectType: leaseObjectTypeId }) => {
|
||||
if (recalcWithRevision === false) {
|
||||
$calculation
|
||||
.element('selectRequirementTelematic')
|
||||
.setOptions(
|
||||
selectRequirementTelematic.filter((x) =>
|
||||
[100_000_000, 100_000_001, 100_000_002, 100_000_003].includes(x.value)
|
||||
)
|
||||
);
|
||||
|
||||
if (leaseObjectTypeId) {
|
||||
const {
|
||||
data: { evo_leasingobject_type },
|
||||
} = await apolloClient.query({
|
||||
query: CRMTypes.GetLeaseObjectTypeDocument,
|
||||
variables: { leaseObjectTypeId },
|
||||
});
|
||||
|
||||
if (evo_leasingobject_type?.evo_id === '11') {
|
||||
$calculation.element('selectRequirementTelematic').setValue(100_000_000).block();
|
||||
} else {
|
||||
$calculation.element('selectRequirementTelematic').unblock();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$calculation.element('selectRequirementTelematic').resetOptions();
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
reaction(
|
||||
() => $calculation.$values.getValues(['requirementTelematic', 'recalcWithRevision']),
|
||||
async ({ requirementTelematic, recalcWithRevision }) => {
|
||||
const currentDate = dayjs().utc(false).format('YYYY-MM-DD');
|
||||
const {
|
||||
data: { evo_addproduct_types: trackerTypes },
|
||||
} = await apolloClient.query({
|
||||
query: CRMTypes.GetTrackerTypesDocument,
|
||||
variables: { currentDate },
|
||||
});
|
||||
|
||||
const {
|
||||
data: { evo_addproduct_types: telematicTypes },
|
||||
} = await apolloClient.query({
|
||||
query: CRMTypes.GetTelematicTypesDocument,
|
||||
variables: { currentDate },
|
||||
});
|
||||
|
||||
let filteredTrackerTypes = trackerTypes?.filter(
|
||||
(x) => requirementTelematic && x?.evo_controls_program?.includes(requirementTelematic)
|
||||
);
|
||||
|
||||
let filteredTelematicTypes = telematicTypes?.filter(
|
||||
(x) => requirementTelematic && x?.evo_controls_program?.includes(requirementTelematic)
|
||||
);
|
||||
|
||||
if (!recalcWithRevision) {
|
||||
filteredTrackerTypes = filteredTrackerTypes?.filter((x) => x?.evo_visible_calc === true);
|
||||
filteredTelematicTypes = filteredTelematicTypes?.filter(
|
||||
(x) => x?.evo_visible_calc === true
|
||||
);
|
||||
}
|
||||
|
||||
$calculation.element('selectTracker').setOptions(normalizeOptions(filteredTrackerTypes));
|
||||
$calculation.element('selectTelematic').setOptions(normalizeOptions(filteredTelematicTypes));
|
||||
}
|
||||
);
|
||||
|
||||
{
|
||||
const elements: Elements[] = [
|
||||
'cbxLeaseObjectUsed',
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
import type { CalculateInput, Context } from '../types';
|
||||
import elementsTitles from '@/Components/Calculation/config/elements-titles';
|
||||
import type { Elements } from '@/Components/Calculation/config/map/values';
|
||||
import * as addProduct from '@/process/add-product';
|
||||
import * as bonuses from '@/process/bonuses';
|
||||
import * as configurator from '@/process/configurator';
|
||||
import * as gibdd from '@/process/gibdd';
|
||||
@ -20,6 +21,7 @@ const processes = [
|
||||
leasingObject,
|
||||
gibdd,
|
||||
insuranceProcess,
|
||||
addProduct,
|
||||
];
|
||||
|
||||
const titles = Object.assign(elementsTitles, {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user