diff --git a/apps/web/Components/Calculation/config/map/values.ts b/apps/web/Components/Calculation/config/map/values.ts index 0e0a3e1..77cb28a 100644 --- a/apps/web/Components/Calculation/config/map/values.ts +++ b/apps/web/Components/Calculation/config/map/values.ts @@ -159,3 +159,9 @@ export type Elements = keyof ElementsTypes; export function getValueName(elementName: Elements) { return elementsToValues[elementName]; } + +export function getElementName(valueName: Values) { + return (Object.keys(elementsToValues) as Elements[]).find( + (elementName) => elementsToValues[elementName] === valueName + ); +} diff --git a/apps/web/process/configurator/validation.ts b/apps/web/process/configurator/validation.ts index c24707e..00bc534 100644 --- a/apps/web/process/configurator/validation.ts +++ b/apps/web/process/configurator/validation.ts @@ -1,40 +1,119 @@ 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 * as CRMTypes from '@/graphql/crm.types'; +import type { InputNumberProps } from 'antd/es/input-number'; import { z } from 'zod'; -export function createValidationSchema({ apolloClient }: ValidationContext) { - 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, - }, - }); +const Schema = ValuesSchema.pick({ + IRR_Perc: true, + VATInLeaseObjectPrice: true, + addEquipmentPrice: true, + bonusCoefficient: true, + calcBrokerRewardSum: true, + calcDoubleAgentRewardSumm: true, + comissionPerc: true, + comissionRub: true, + countSeats: true, + creditRate: true, + dealerBrokerRewardSumm: true, + dealerRewardSumm: true, + engineHours: true, + engineVolume: true, + finDepartmentRewardSumm: true, + firstPaymentPerc: true, + 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 ( - evo_tarif?.evo_min_decreasing_perc && - parmentsDecreasePercent < evo_tarif?.evo_min_decreasing_perc - ) { +type Values = keyof z.infer; + +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; + const value = values[valueName] as number | null | undefined; + if (props?.min && value !== undefined && value !== null && value < props.min) { ctx.addIssue({ code: z.ZodIssueCode.custom, - message: `Процент убывания не может быть меньше минимального значения по данному тарифу - ${evo_tarif?.evo_min_decreasing_perc}`, - path: ['tbxParmentsDecreasePercent'], + message: `Значение поля меньше минимального: ${props.min}`, + path: [elementName], + }); + } + + if (props?.max && value !== undefined && value !== null && value > props.max) { + ctx.addIssue({ + code: z.ZodIssueCode.custom, + message: `Значение поля больше максимального: ${props.max}`, + path: [elementName], }); } } - } - ); + }); + }); }