import { updateSelectQuote } from '../lead-opportunity/reactions/common'; import type { ProcessContext } from '../types'; import { toJS } from 'mobx'; import { notification } from 'ui/elements'; const key = 'ACTION_CREATEKP'; const errorMessage = 'Ошибка во время создания КП!'; const successMessage = 'КП создано!'; export function action({ store, trpcClient, apolloClient }: ProcessContext) { const { $calculation, $tables, $results } = store; $calculation.$status.setStatus('btnCalculate', 'Loading'); $calculation.$status.setStatus('btnCreateKP', 'Loading'); $calculation.$status.setStatus('btnCreateKPMini', 'Loading'); $results.clear(); const values = $calculation.$values.getValues(); const insurance = { fingap: toJS($tables.insurance.row('fingap').getValues()), kasko: toJS($tables.insurance.row('kasko').getValues()), 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.createQuote .mutate({ fingap, insurance: { values: insurance }, payments: { sums: paymentSums, values: paymentRelations }, values, }) .then(async (res) => { if (res.success === false) { notification.error({ description: res.error, key, message: errorMessage, }); } else { $results.setPayments(res.data.resultPayments); $results.setValues(res.data.resultValues); $calculation.$values.setValues(res.data.values); notification.success({ key, message: successMessage, }); await updateSelectQuote({ apolloClient, store }); } }) .catch((error) => { notification.error({ description: JSON.stringify(error), key, message: errorMessage, }); }) .finally(() => { $calculation.$status.setStatus('btnCalculate', 'Default'); $calculation.$status.setStatus('btnCreateKP', 'Default'); $calculation.$status.setStatus('btnCreateKPMini', 'Default'); }); }