2024-09-23 09:59:51 +03:00

105 lines
2.8 KiB
TypeScript

import helper from '../lib/helper';
import { IRR_THRESHOLD } from '@/constants/values';
import type { ProcessContext } from '@/process/types';
import { disposableReaction } from '@/utils/mobx';
import { comparer, reaction } from 'mobx';
import { omit } from 'radash';
export default function reactions({ store, apolloClient }: ProcessContext) {
const { $calculation, $tables, $results, $process } = store;
reaction(
() => $calculation.element('radioCalcType').getValue(),
(calcType) => {
switch (calcType) {
case 100_000_001: {
$calculation.element('tbxIRR_Perc').block();
$calculation.element('tbxTotalPayments').unblock();
break;
}
case 100_000_000:
default: {
$calculation.element('tbxIRR_Perc').unblock();
$calculation.element('tbxTotalPayments').block();
break;
}
}
},
{
fireImmediately: true,
}
);
const { getIrr } = helper({ apolloClient });
disposableReaction(
() => $process.has('LoadKP'),
() => $calculation.$values.getValues(['product', 'tarif', 'bonusCoefficient']),
async (values) => {
const { min, max } = await getIrr(values);
$calculation.element('labelIrrInfo').setValue({ max, min });
}
);
// костыль
disposableReaction(
() =>
$process.has('LoadKP') ||
$calculation.element('radioLastPaymentRule').getValue() === 100_000_002,
() => $calculation.element('labelIrrInfo').getValue(),
({ min }) => {
if ($calculation.element('radioLastPaymentRule').getValue() === 100_000_002) return;
$calculation.element('tbxIRR_Perc').setValue(min + IRR_THRESHOLD);
}
);
disposableReaction(
() => $process.has('Calculate') || $process.has('CreateKP'),
() => {
const values = $calculation.$values.getValues();
return {
insurance: {
fingap: $tables.insurance.row('fingap').getValues(),
kasko: $tables.insurance.row('kasko').getValues(),
osago: $tables.insurance.row('osago').getValues(),
},
payments: $tables.payments.getValues,
values: omit(values, [
'IRR_Perc',
'irrInfo',
'downloadKp',
'lastPaymentRub',
'lastPaymentPerc',
'pi',
'totalPayments',
'tarif',
'rate',
'creditRate',
'recalcWithRevision',
'quote',
'depreciationGroup',
'registrationDescription',
'leaseObjectCount',
'kpUrl',
'leadUrl',
'quoteUrl',
'opportunityUrl',
'subsidySum',
'insKaskoPriceLeasePeriod',
'leaseObjectRiskName',
]),
};
},
() => {
$results.clear();
},
{
delay: 10,
equals: comparer.structural,
}
);
}