/* eslint-disable @typescript-eslint/naming-convention */ import dayjs from 'dayjs'; import utc from 'dayjs/plugin/utc'; import { autorun, reaction } from 'mobx'; import * as CRMTypes from 'graphql/crm.types'; import type { ReactionsContext } from 'process/types'; import { makeDisposable, normalizeOptions } from 'tools'; dayjs.extend(utc); export default function valuesReactions({ store, apolloClient }: ReactionsContext) { const { $calculation, $process } = store; autorun( async () => { const { product, leasingPeriod, leaseObjectUsed, deliveryTime, firstPaymentPerc, lastPaymentPerc, } = $calculation.$values.values; const currentDate = dayjs().utc(false).toISOString(); let { data: { evo_tarifs = [] }, } = await apolloClient.query({ query: CRMTypes.GetTarifsDocument, variables: { currentDate, }, fetchPolicy: 'network-only', }); if (product && leasingPeriod && deliveryTime && evo_tarifs) { evo_tarifs = evo_tarifs?.filter( (tarif) => tarif && 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 ); } else { $calculation.element('selectTarif').resetValue(); } if (leaseObjectUsed === true && evo_tarifs) { evo_tarifs = evo_tarifs?.filter((tarif) => { if (leaseObjectUsed === true) { return tarif?.evo_used; } return true; }); } $calculation .element('selectTarif') .setOptions(normalizeOptions(evo_tarifs)) .setValue(evo_tarifs?.at(0)?.evo_tarifid || null); }, { delay: 10, } ); makeDisposable( () => reaction( () => $calculation.element('selectTarif').getValue(), async (tarifId) => { if (!tarifId) { $calculation.element('tbxIRR_Perc').resetValue(); return; } const { data: { evo_tarif }, } = await apolloClient.query({ 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(); } } ), () => $process.has('LoadKP') ); reaction( () => $calculation.element('selectTarif').getValue(), async (tarifId) => { if (!tarifId) { $calculation.element('selectRate').resetValue(); return; } const currentDate = dayjs().utc(false).toISOString(); const { data: { evo_rates }, } = await apolloClient.query({ query: CRMTypes.GetRatesDocument, variables: { currentDate, }, }); const rates = evo_rates?.filter((rate) => rate?.evo_tarifs?.filter((tarif) => tarif?.evo_tarifid === tarifId) ); const baseRate = rates?.find((x) => x?.evo_id === 'BASE'); $calculation .element('selectRate') .setOptions(normalizeOptions(rates)) .setValue(baseRate?.value ?? null); } ); }