import type { Elements } from '@/Components/Calculation/config/map/values'; import * as CRMTypes from '@/graphql/crm.types'; import type { ProcessContext } from '@/process/types'; import { normalizeOptions } from '@/utils/entity'; import { debouncedReaction, disposableReaction } from '@/utils/mobx'; import dayjs from 'dayjs'; import utc from 'dayjs/plugin/utc'; import { reaction } from 'mobx'; import { first, sort } from 'radash'; dayjs.extend(utc); export default function valuesReactions({ store, apolloClient }: ProcessContext) { const { $calculation, $process } = store; debouncedReaction( () => $calculation.$values.getValues([ 'product', 'leasingPeriod', 'leaseObjectUsed', 'deliveryTime', 'firstPaymentPerc', 'lastPaymentPerc', 'leaseObjectType', ]), async ({ product, leasingPeriod, leaseObjectUsed, deliveryTime, firstPaymentPerc, lastPaymentPerc, leaseObjectType: leaseObjectTypeId, }) => { const currentDate = dayjs().utc(false).format('YYYY-MM-DD'); let { data: { evo_tarifs = [] }, } = await apolloClient.query({ fetchPolicy: 'network-only', query: CRMTypes.GetTarifsDocument, variables: { currentDate, }, }); $calculation.element('selectTarif').setOptions(normalizeOptions(evo_tarifs)).resetValue(); if (evo_tarifs) { evo_tarifs = evo_tarifs ?.filter( (tarif) => tarif && product && tarif.evo_baseproductid === product && tarif.evo_min_period !== null && tarif.evo_min_period <= leasingPeriod && tarif.evo_max_period !== null && tarif.evo_max_period >= leasingPeriod && tarif.evo_delivery_time?.includes(deliveryTime) && tarif.evo_min_first_payment !== null && tarif.evo_min_first_payment <= firstPaymentPerc && tarif.evo_max_first_payment !== null && tarif.evo_max_first_payment >= firstPaymentPerc && tarif.evo_min_last_payment !== null && tarif.evo_min_last_payment <= lastPaymentPerc && tarif.evo_max_last_payment !== null && tarif.evo_max_last_payment >= lastPaymentPerc ) .filter( (tarif) => !tarif?.evo_leasingobject_types?.length || (leaseObjectTypeId && tarif.evo_leasingobject_types .map((x) => x?.evo_leasingobject_typeid) .includes(leaseObjectTypeId)) ) .filter((tarif) => { if (leaseObjectUsed === true) { if (!tarif?.evo_pl_use_type?.length || tarif.evo_pl_use_type.includes(100_000_001)) { return true; } } else if ( !tarif?.evo_pl_use_type?.length || tarif.evo_pl_use_type.includes(100_000_000) ) { return true; } return false; }); } $calculation.element('selectTarif').setValue(evo_tarifs?.at(0)?.evo_tarifid || null); }, { delay: 10, fireImmediately: true, wait: 50, } ); disposableReaction( () => $process.has('LoadKP'), () => $calculation.element('selectTarif').getValue(), async (tarifId) => { if (!tarifId) { $calculation.element('tbxIRR_Perc').resetValue(); return; } const { data: { evo_tarif }, } = await apolloClient.query({ fetchPolicy: 'network-only', query: CRMTypes.GetTarifDocument, variables: { tarifId, }, }); if (evo_tarif?.evo_irr) { $calculation.element('tbxIRR_Perc').setValue(evo_tarif?.evo_irr); } else { $calculation.element('tbxIRR_Perc').resetValue(); } } ); reaction( () => $calculation.element('selectTarif').getValue(), async (tarifId) => { if (!tarifId) { $calculation.element('selectRate').resetValue(); return; } const currentDate = dayjs().utc(false).format('YYYY-MM-DD'); const { data: { evo_rates }, } = await apolloClient.query({ fetchPolicy: 'network-only', query: CRMTypes.GetRatesDocument, variables: { currentDate, }, }); $calculation.element('selectRate').setOptions(normalizeOptions(evo_rates)); const { data: { evo_tarif }, } = await apolloClient.query({ query: CRMTypes.GetTarifDocument, variables: { tarifId }, }); const targetRate = evo_tarif?.evo_rates && first(sort(evo_tarif?.evo_rates, (x) => dayjs(x?.evo_datefrom).date())); if (targetRate) { $calculation.element('selectRate').setValue(targetRate?.evo_rateid); } else { const baseRate = evo_rates?.find((x) => x?.evo_id === 'BASE'); if (baseRate) $calculation.element('selectRate').setValue(baseRate?.value); } } ); reaction( () => $calculation.element('selectRate').getValue(), async (rateId) => { if (!rateId) { $calculation.element('tbxCreditRate').resetValue(); return; } const { data: { evo_rate }, } = await apolloClient.query({ fetchPolicy: 'network-only', query: CRMTypes.GetRateDocument, variables: { rateId, }, }); if (evo_rate?.evo_base_rate === null || evo_rate?.evo_base_rate === undefined) { $calculation.element('tbxCreditRate').resetValue(); return; } $calculation.element('tbxCreditRate').setValue(evo_rate?.evo_base_rate); } ); ( [ 'selectProduct', 'selectLeaseObjectType', 'selectBrand', 'selectModel', 'selectConfiguration', 'selectTracker', 'selectTelematic', 'selectTechnicalCard', 'selectFuelCard', 'selectRegistration', 'selectTownRegistration', 'radioCalcType', ] as Elements[] ).forEach((elementName) => { reaction( () => $calculation.element(elementName).getOptions(), (options) => { if (options.length === 1) { $calculation.element(elementName).setValue(options[0]?.value); } } ); }); }