Compare commits

...

2 Commits

Author SHA1 Message Date
vchikalkin
0c2053e2f4 optimize: use only number fields 2023-07-05 12:34:39 +03:00
vchikalkin
967f65846c process/configurator: add min, mix check to validation 2023-07-05 11:59:12 +03:00
2 changed files with 113 additions and 28 deletions

View File

@ -159,3 +159,9 @@ export type Elements = keyof ElementsTypes;
export function getValueName(elementName: Elements) { export function getValueName(elementName: Elements) {
return elementsToValues[elementName]; return elementsToValues[elementName];
} }
export function getElementName(valueName: Values) {
return (Object.keys(elementsToValues) as Elements[]).find(
(elementName) => elementsToValues[elementName] === valueName
);
}

View File

@ -1,40 +1,119 @@
import type { ValidationContext } from '../types'; import type { ValidationContext } from '../types';
import elementsProps from '@/Components/Calculation/config/elements-props';
import { getElementName } from '@/Components/Calculation/config/map/values';
import ValuesSchema from '@/config/schema/values'; import ValuesSchema from '@/config/schema/values';
import * as CRMTypes from '@/graphql/crm.types'; import * as CRMTypes from '@/graphql/crm.types';
import type { InputNumberProps } from 'antd/es/input-number';
import { z } from 'zod'; import { z } from 'zod';
export function createValidationSchema({ apolloClient }: ValidationContext) { const Schema = ValuesSchema.pick({
return ValuesSchema.pick({ parmentsDecreasePercent: true, tarif: true }).superRefine( IRR_Perc: true,
async ({ parmentsDecreasePercent, tarif: tarifId }, ctx) => { VATInLeaseObjectPrice: true,
/** addEquipmentPrice: true,
* На изменение поля Процет убывания платежей tbxParmentsDecreasePercent необходимо заложить проверку: bonusCoefficient: true,
* Если значение поля меньше значения в поле "Минимальный % убывания платежей" evo_min_decreasing_perc из записи, calcBrokerRewardSum: true,
* указанной в поле ТарифselectTarif , то поле Процет убывания платежей tbxParmentsDecreasePercent должно обводиться красной рамкой calcDoubleAgentRewardSumm: true,
* и выводиться сообщение "Процент убывания не может быть меньше минимального значения по данному тарифу comissionPerc: true,
* - <указывается значение из поля "Минимальный % убывания платежей">, иначе красная рамка снимается. comissionRub: true,
* При красной рамке в данном поле нельзя осуществить расчет графика. countSeats: true,
*/ creditRate: true,
if (tarifId) { dealerBrokerRewardSumm: true,
const { dealerRewardSumm: true,
data: { evo_tarif }, engineHours: true,
} = await apolloClient.query({ engineVolume: true,
query: CRMTypes.GetTarifDocument, finDepartmentRewardSumm: true,
variables: { firstPaymentPerc: true,
tarifId, firstPaymentRub: true,
}, importProgramSum: true,
}); importerRewardPerc: true,
importerRewardRub: true,
indAgentRewardSumm: true,
insFranchise: true,
insKaskoPriceLeasePeriod: true,
lastPaymentPerc: true,
lastPaymentRub: true,
leaseObjectCount: true,
leaseObjectMotorPower: true,
leaseObjectPrice: true,
leaseObjectPriceWthtVAT: true,
leaseObjectYear: true,
leasingPeriod: true,
maxMass: true,
maxPriceChange: true,
maxSpeed: true,
mileage: true,
minPriceChange: true,
parmentsDecreasePercent: true,
pi: true,
redemptionPaymentSum: true,
saleBonus: true,
subsidySum: true,
supplierDiscountPerc: true,
supplierDiscountRub: true,
tarif: true,
totalPayments: true,
vehicleTaxInLeasingPeriod: true,
vehicleTaxInYear: true,
});
if ( type Values = keyof z.infer<typeof Schema>;
evo_tarif?.evo_min_decreasing_perc &&
parmentsDecreasePercent < evo_tarif?.evo_min_decreasing_perc export function createValidationSchema({ apolloClient }: ValidationContext) {
) { return Schema.superRefine(async (values, ctx) => {
/**
* На изменение поля Процет убывания платежей tbxParmentsDecreasePercent необходимо заложить проверку:
* Если значение поля меньше значения в поле "Минимальный % убывания платежей" evo_min_decreasing_perc из записи,
* указанной в поле ТарифselectTarif , то поле Процет убывания платежей tbxParmentsDecreasePercent должно обводиться красной рамкой
* и выводиться сообщение "Процент убывания не может быть меньше минимального значения по данному тарифу
* - <указывается значение из поля "Минимальный % убывания платежей">, иначе красная рамка снимается.
* При красной рамке в данном поле нельзя осуществить расчет графика.
*/
const { tarif: tarifId, parmentsDecreasePercent } = values;
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'],
});
}
}
(Object.keys(values) as Values[]).forEach((valueName) => {
const elementName = getElementName(valueName);
if (elementName) {
const props = elementsProps[elementName] as InputNumberProps<number>;
const value = values[valueName] as number | null | undefined;
if (props?.min && value !== undefined && value !== null && value < props.min) {
ctx.addIssue({ ctx.addIssue({
code: z.ZodIssueCode.custom, code: z.ZodIssueCode.custom,
message: `Процент убывания не может быть меньше минимального значения по данному тарифу - ${evo_tarif?.evo_min_decreasing_perc}`, message: `Значение поля меньше минимального: ${props.min}`,
path: ['tbxParmentsDecreasePercent'], path: [elementName],
});
}
if (props?.max && value !== undefined && value !== null && value > props.max) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `Значение поля больше максимального: ${props.max}`,
path: [elementName],
}); });
} }
} }
} });
); });
} }