2023-08-01 10:03:10 +03:00

108 lines
3.2 KiB
TypeScript

/* eslint-disable sonarjs/cognitive-complexity */
import eltHelper from '../elt/lib/helper';
import { message } from '@/Components/Common/Notification';
import type { ProcessContext } from '@/process/types';
import { captureException, withScope } from '@sentry/nextjs';
import { reaction } from 'mobx';
import { omit } from 'radash';
const key = 'KP_LOADING_INFO';
export function common({ store, trpcClient, apolloClient, user }: ProcessContext) {
const { $calculation, $process, $tables } = store;
const { init: initElt } = eltHelper({ apolloClient, store });
reaction(
() => $calculation.$values.getValue('quote'),
async () => {
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,
});
const eltInitialValues = await initElt();
trpcClient.getQuote
.query({
values: {
quote: quote.value,
...$calculation.$values.getValues(['lead', 'opportunity', 'recalcWithRevision']),
},
})
.then(({ values, payments, insurance, fingap, elt }) => {
$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.setSelectedKeys(fingap.keys);
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,
});
})
.catch((error_) => {
message.error({
content: `Ошибка во время загрузки КП ${quote.label}`,
key,
});
$calculation.element('selectQuote').resetValue();
withScope((scope) => {
scope.setExtra('quote', quote);
scope.setExtra('user', user);
captureException(error_);
});
})
.finally(() => {
$process.delete('LoadKP');
});
}
);
}