69 lines
1.9 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 fingap = $tables.fingap.getSelectedRisks();
const paymentRelations = toJS($tables.payments.values);
const paymentSums = toJS($tables.payments.sums);
trpcClient.calculate
.mutate({
fingap,
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_) => {
const error = error_ as Error;
notification.error({
description: error.message,
key,
message: errorMessage,
placement: 'bottomRight',
});
})
.finally(() => {
$process.delete('Calculate');
});
}