import type { ValidationContext } from '../types'; import helper from './lib/helper'; import ValuesSchema from '@/config/schema/values'; import { round } from 'tools'; import { z } from 'zod'; export function createValidationSchema(context: ValidationContext) { const { getCoefficient } = helper(context); return ValuesSchema.pick({ product: true, saleBonus: true }).superRefine( async ({ product, saleBonus }, ctx) => { const coefficient = await getCoefficient(product); const minBonus = (coefficient?.evo_correction_coefficient || 0) * 100; const maxBonus = (coefficient?.evo_sot_coefficient || 0) * 100; if (round(saleBonus, 2) < round(minBonus, 2)) { ctx.addIssue({ code: z.ZodIssueCode.custom, message: 'Бонус не может быть ниже установленного по СОТ', path: ['tbxSaleBonus'], }); } if (round(saleBonus, 2) > round(maxBonus, 2)) { ctx.addIssue({ code: z.ZodIssueCode.custom, message: 'Бонус не может быть выше установленного по СОТ', path: ['tbxSaleBonus'], }); } } ); }