diff --git a/apps/web/Components/Output/Validation.jsx b/apps/web/Components/Output/Validation.jsx index f5f3547..e404ded 100644 --- a/apps/web/Components/Output/Validation.jsx +++ b/apps/web/Components/Output/Validation.jsx @@ -75,6 +75,23 @@ function getInsuranceTableErrors({ $tables, $process }) { )); } +function getFingapTableErrors({ $tables, $process }) { + const { fingap } = $tables; + const errors = fingap.validation.getErrors(); + const title = fingap.validation.params.err_title; + + return errors.map(({ key, message }) => ( + + + + )); +} + const Errors = observer(() => { const store = useStore(); const { $calculation, $tables } = store; @@ -92,8 +109,9 @@ const Errors = observer(() => { const elementsErrors = getElementsErrors(store); const paymentsErrors = getPaymentsTableErrors(store); const insuranceErrors = getInsuranceTableErrors(store); + const fingapErrors = getFingapTableErrors(store); - const errors = [...elementsErrors, ...paymentsErrors, ...insuranceErrors]; + const errors = [...elementsErrors, ...paymentsErrors, ...insuranceErrors, ...fingapErrors]; return {errors}; }); diff --git a/apps/web/config/schema/fingap.ts b/apps/web/config/schema/fingap.ts index ca63baa..b699760 100644 --- a/apps/web/config/schema/fingap.ts +++ b/apps/web/config/schema/fingap.ts @@ -10,3 +10,5 @@ export const RiskSchema = z.object({ riskName: z.string(), sum: z.number(), }); + +export const FinGAPSchema = RiskSchema.array(); diff --git a/apps/web/process/calculate/action.ts b/apps/web/process/calculate/action.ts index d11c45f..ea87d90 100644 --- a/apps/web/process/calculate/action.ts +++ b/apps/web/process/calculate/action.ts @@ -21,11 +21,14 @@ export async function action({ store, trpcClient }: ProcessContext) { osago: toJS($tables.insurance.row('osago').getValues()), }; + const fingap = $tables.fingap.getSelectedRisks(); + const paymentRelations = toJS($tables.payments.values); const paymentSums = toJS($tables.payments.sums); trpcClient.calculate .mutate({ + fingap, insurance: { values: insurance }, payments: { sums: paymentSums, values: paymentRelations }, values, diff --git a/apps/web/process/fingap/reactions/common.ts b/apps/web/process/fingap/reactions/common.ts index 93517a5..ec8cce9 100644 --- a/apps/web/process/fingap/reactions/common.ts +++ b/apps/web/process/fingap/reactions/common.ts @@ -112,11 +112,13 @@ export default function reactions({ store, apolloClient, queryClient }: ProcessC const discountRub = $calculation.$values.getValue('discountRub'); const firstPaymentRub = $calculation.element('tbxFirstPaymentRub').getValue(); const leasingPeriod = $calculation.element('tbxLeasingPeriod').getValue(); + const hasPaymentsErrors = $tables.payments.validation.hasErrors; return { discountRub, finGAPInsuranceCompany, firstPaymentRub, + hasPaymentsErrors, leasingPeriod, paymentsValues, plPriceRub, @@ -129,8 +131,13 @@ export default function reactions({ store, apolloClient, queryClient }: ProcessC discountRub, firstPaymentRub, leasingPeriod, + hasPaymentsErrors, }) => { - if (!finGAPInsuranceCompany || $tables.payments.validation.hasErrors) return; + if (!finGAPInsuranceCompany || hasPaymentsErrors) { + $tables.fingap.clear(); + + return; + } const { data: { evo_addproduct_types }, diff --git a/apps/web/process/fingap/reactions/index.ts b/apps/web/process/fingap/reactions/index.ts index cc8d68a..c8077ba 100644 --- a/apps/web/process/fingap/reactions/index.ts +++ b/apps/web/process/fingap/reactions/index.ts @@ -1,2 +1 @@ export { default as common } from './common'; -export { default as validation } from './validation'; diff --git a/apps/web/process/fingap/reactions/validation.ts b/apps/web/process/fingap/reactions/validation.ts deleted file mode 100644 index 580208e..0000000 --- a/apps/web/process/fingap/reactions/validation.ts +++ /dev/null @@ -1,41 +0,0 @@ -import type { ProcessContext } from '@/process/types'; -import ValidationHelper from '@/stores/validation/helper'; -import { reaction } from 'mobx'; -import { uid } from 'radash'; - -const key = uid(7); - -export default function reactions({ store }: ProcessContext) { - const { $tables } = store; - - const helper = new ValidationHelper(); - reaction( - () => { - const hasPaymentsErrors = $tables.payments.validation.hasErrors; - const finGAPInsuranceCompany = $tables.insurance.row('fingap').getValue('insuranceCompany'); - - return { - finGAPInsuranceCompany, - hasPaymentsErrors, - }; - }, - ({ hasPaymentsErrors, finGAPInsuranceCompany }) => { - if (finGAPInsuranceCompany !== null && hasPaymentsErrors) { - const removeError = $tables.fingap.setError({ - key, - message: 'Неверно заполнены платежи', - }); - helper.add(removeError); - } else { - helper.removeErrors(); - } - - if (hasPaymentsErrors) { - $tables.fingap.clear(); - } - }, - { - fireImmediately: true, - } - ); -} diff --git a/apps/web/process/insurance/validation.ts b/apps/web/process/insurance/validation.ts index d8071c1..f07a7f4 100644 --- a/apps/web/process/insurance/validation.ts +++ b/apps/web/process/insurance/validation.ts @@ -3,6 +3,7 @@ /* eslint-disable zod/require-strict */ import type { ValidationContext } from '../types'; import type * as Insurance from '@/Components/Calculation/Form/Insurance/InsuranceTable/types'; +import { FinGAPSchema } from '@/config/schema/fingap'; import { InsuranceSchema } from '@/config/schema/insurance'; import ValuesSchema from '@/config/schema/values'; import * as CRMTypes from '@/graphql/crm.types'; @@ -18,6 +19,7 @@ export function createValidationSchema({ apolloClient }: ValidationContext) { recalcWithRevision: true, }) .extend({ + fingap: FinGAPSchema, insurance: InsuranceSchema, }) @@ -31,6 +33,7 @@ export function createValidationSchema({ apolloClient }: ValidationContext) { insDecentral, insurance, brand: brandId, + fingap: fingapRisks, }, ctx ) => { @@ -118,6 +121,14 @@ export function createValidationSchema({ apolloClient }: ValidationContext) { }); } + if (fingap.insuranceCompany && !fingapRisks?.length) { + ctx.addIssue({ + code: z.ZodIssueCode.custom, + message: `Выберите риски`, + path: ['fingap'], + }); + } + if ( !leasingWithoutKasko && !insDecentral && diff --git a/apps/web/process/tools.ts b/apps/web/process/tools.ts index a827b6d..0e86e39 100644 --- a/apps/web/process/tools.ts +++ b/apps/web/process/tools.ts @@ -29,6 +29,7 @@ export function createValidationReaction( if (shapeValues.includes('insurance')) return { ...values, + fingap: $tables.fingap.getSelectedRisks(), insurance: { values: { fingap: toJS($tables.insurance.row('fingap').getValues()), @@ -54,11 +55,16 @@ export function createValidationReaction( if (validationResult.success === false) { validationResult.error.errors.forEach(({ path, message }) => { ( - path as Array + path as Array< + Elements & ('eltKasko' | 'eltOsago' | 'fingap' | 'insurance' | 'payments') + > ).forEach((elementName) => { if (elementName === 'insurance') { const removeError = $tables.insurance.setError({ key, message }); if (removeError) helper.add(removeError); + } else if (elementName === 'fingap') { + const removeError = $tables.fingap.setError({ key, message }); + if (removeError) helper.add(removeError); } else if (elementName === 'payments') { const removeError = $tables.payments.setError({ key, message }); if (removeError) helper.add(removeError); diff --git a/apps/web/server/routers/calculate/lib/validation.ts b/apps/web/server/routers/calculate/lib/validation.ts index 87c055a..85b2ef7 100644 --- a/apps/web/server/routers/calculate/lib/validation.ts +++ b/apps/web/server/routers/calculate/lib/validation.ts @@ -25,6 +25,7 @@ const processes = [ ]; const titles = Object.assign(elementsTitles, { + fingap: 'Таблица Safe Finance', insurance: 'Таблица страхования', payments: 'Таблица платежей', }); @@ -42,6 +43,7 @@ export async function validate({ input, context }: { context: Context; input: Ca const validationSchema = createValidationSchema(context); const validationResult = await validationSchema.safeParseAsync({ ...input.values, + fingap: input.fingap, insurance: input.insurance, payments: input.payments, }); diff --git a/apps/web/server/routers/calculate/types.ts b/apps/web/server/routers/calculate/types.ts index d17f387..c508af1 100644 --- a/apps/web/server/routers/calculate/types.ts +++ b/apps/web/server/routers/calculate/types.ts @@ -1,3 +1,4 @@ +import { FinGAPSchema } from '@/config/schema/fingap'; import { InsuranceSchema } from '@/config/schema/insurance'; import PaymentsSchema from '@/config/schema/payments'; import { ResultPaymentsSchema, ResultValuesSchema } from '@/config/schema/results'; @@ -9,6 +10,7 @@ export type Context = Pick