70 lines
2.1 KiB
TypeScript
70 lines
2.1 KiB
TypeScript
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 payments = toJS($tables.payments.values);
|
|
|
|
trpcClient.createQuote
|
|
.mutate({
|
|
fingap,
|
|
insurance: { values: insurance },
|
|
payments: { values: payments },
|
|
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');
|
|
});
|
|
}
|