import type { ProcessContext } from '../types'; import * as CRMTypes from '@/graphql/crm.types'; import { normalizeOptions } from '@/utils/entity'; 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, $process } = store; $process.add('CreateKP'); $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); const elt = { kasko: toJS($tables.elt.kasko.getSelectedRow), osago: toJS($tables.elt.osago.getSelectedRow), }; trpcClient.createQuote .mutate({ elt, 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, recalcWithRevision: false }); notification.success({ key, message: successMessage, }); const leadid = $calculation.element('selectLead').getValue(); if (leadid) { const { data: { quotes }, } = await apolloClient.query({ fetchPolicy: 'network-only', query: CRMTypes.GetQuotesDocument, variables: { leadid, }, }); $calculation .element('selectQuote') .setOptions(normalizeOptions(quotes)) .setValue(values.quote); } } }) .catch((error) => { notification.error({ description: JSON.stringify(error), key, message: errorMessage, }); }) .finally(() => { $process.delete('CreateKP'); }); }