/* eslint-disable sonarjs/cognitive-complexity */ import eltHelper from '../elt/lib/helper'; import { message } from '@/Components/Common/Notification'; import type { ProcessContext } from '@/process/types'; import { normalizeOptions } from '@/utils/entity'; import { reaction } from 'mobx'; import { omit } from 'radash'; const key = 'KP_LOADING_INFO'; export function common({ store, trpcClient, apolloClient }: ProcessContext) { const { $calculation, $process, $tables } = store; const { init: initElt } = eltHelper({ apolloClient, store }); reaction( () => $calculation.$values.getValue('quote'), () => { const quote = $calculation.element('selectQuote').getOption(); if (!quote || $process.has('LoadKP') || $process.has('Calculate') || $process.has('CreateKP')) return; $process.add('LoadKP'); message.loading({ content: `Загружаем КП ${quote?.label}...`, key, onClick: () => message.destroy(key), }); trpcClient.getQuote .query({ values: { quote: quote.value, ...$calculation.$values.getValues(['lead', 'opportunity', 'recalcWithRevision']), }, }) .then(async ({ values, payments, insurance, fingap, elt, options }) => { if (options?.selectTarif) { $calculation.element('selectTarif').setOptions(normalizeOptions(options.selectTarif)); } else { $calculation.element('selectTarif').resetOptions(); } $calculation.$values.setValues( omit(values, [ 'lead', 'opportunity', 'quote', 'leadUrl', 'opportunityUrl', 'quoteUrl', 'recalcWithRevision', 'plPriceRub', 'discountRub', 'user', ]) ); $tables.payments.setValues(payments.values); if (insurance.values.osago) { $tables.insurance.row('osago').setValues(insurance.values.osago); } if (insurance.values.kasko) { $tables.insurance.row('kasko').setValues(insurance.values.kasko); } if (insurance.values.fingap) { $tables.insurance.row('fingap').setValues(insurance.values.fingap); } if (fingap) { $tables.fingap.setRisks(fingap.risks); $tables.fingap.setSelectedKeys(fingap.keys); } const eltInitialValues = await initElt(); if (eltInitialValues) { $tables.elt.kasko.setRows(eltInitialValues.kasko); $tables.elt.osago.setRows(eltInitialValues.osago); } if (elt?.kasko) { $tables.elt.kasko.setRow(elt.kasko); $tables.elt.kasko.setSelectedKey(elt.kasko.key); } if (elt?.osago) { $tables.elt.osago.setRow(elt.osago); $tables.elt.osago.setSelectedKey(elt.osago.key); } message.success({ content: `КП ${quote.label} загружено`, key, onClick: () => message.destroy(key), }); }) .catch(() => { message.error({ content: `Ошибка во время загрузки КП ${quote.label}`, key, onClick: () => message.destroy(key), }); $calculation.element('selectQuote').resetValue(); }) .finally(() => { $process.delete('LoadKP'); }); } ); }