65 lines
1.8 KiB
TypeScript
65 lines
1.8 KiB
TypeScript
import type { ProcessContext } from '../types';
|
|
import { notification } from '@/Components/Common/Notification';
|
|
import { toJS } from 'mobx';
|
|
|
|
const key = 'ACTION_CALCULATE';
|
|
const errorMessage = 'Ошибка во время расчета графика!';
|
|
const successMessage = 'Расчет графика завершен успешно!';
|
|
|
|
export async function action({ store, trpcClient }: ProcessContext) {
|
|
const { $calculation, $tables, $results, $process } = store;
|
|
|
|
$process.add('Calculate');
|
|
|
|
$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 paymentRelations = toJS($tables.payments.values);
|
|
const paymentSums = toJS($tables.payments.sums);
|
|
|
|
trpcClient.calculate
|
|
.mutate({
|
|
insurance: { values: insurance },
|
|
payments: { sums: paymentSums, values: paymentRelations },
|
|
values,
|
|
})
|
|
.then((res) => {
|
|
if (res.success === false) {
|
|
notification.error({
|
|
description: res.error,
|
|
key,
|
|
message: errorMessage,
|
|
placement: 'bottomRight',
|
|
});
|
|
} else {
|
|
$results.setPayments(res.data.resultPayments);
|
|
$results.setValues(res.data.resultValues);
|
|
$calculation.$values.setValues(res.data.values);
|
|
|
|
notification.success({
|
|
key,
|
|
message: successMessage,
|
|
placement: 'bottomRight',
|
|
});
|
|
}
|
|
})
|
|
.catch((error) => {
|
|
notification.error({
|
|
description: JSON.stringify(error),
|
|
key,
|
|
message: errorMessage,
|
|
placement: 'bottomRight',
|
|
});
|
|
})
|
|
.finally(() => {
|
|
$process.delete('Calculate');
|
|
});
|
|
}
|