/* eslint-disable sonarjs/cognitive-complexity */ import { createValidationReaction } from '../tools'; import type { ProcessContext } from '../types'; import { createValidationSchema } from './validation'; import { selectRequirementTelematic } from '@/config/default-options'; import * as CRMTypes from '@/graphql/crm.types'; import { getCurrentISODate } from '@/utils/date'; import { normalizeOptions } from '@/utils/entity'; import { debouncedReaction } from '@/utils/mobx'; import { reaction, toJS } from 'mobx'; export default function reactions({ store, apolloClient }: ProcessContext) { const { $calculation, $tables } = store; reaction( () => $calculation.$values.getValues(['leasingPeriod', 'leaseObjectType', 'maxMass']), async ({ leasingPeriod, leaseObjectType, maxMass }) => { if (maxMass >= 40_000) { $calculation.element('selectTechnicalCard').setOptions([]); return; } const currentDate = getCurrentISODate(); const { data: { evo_addproduct_types }, } = await apolloClient.query({ query: CRMTypes.GetTechnicalCardsDocument, variables: { currentDate }, }); const options = evo_addproduct_types?.filter( (evo_addproduct_type) => Boolean( evo_addproduct_type?.evo_min_period && evo_addproduct_type.evo_min_period <= leasingPeriod ) && Boolean( evo_addproduct_type?.evo_max_period && evo_addproduct_type.evo_max_period >= leasingPeriod ) && Boolean( leaseObjectType && evo_addproduct_type?.evo_leasingobject_types?.find( (x) => x?.evo_leasingobject_typeid === leaseObjectType ) ) ); $calculation.element('selectTechnicalCard').setOptions(normalizeOptions(options)); const currentTechnicalCardId = $calculation.element('selectTechnicalCard').getValue(); if (currentTechnicalCardId) { const { data: { evo_addproduct_type }, } = await apolloClient.query({ query: CRMTypes.GetAddProductTypeDocument, variables: { addproductTypeId: currentTechnicalCardId }, }); const nextTechnicalCard = options?.find( (x) => x?.evo_helpcard_type === evo_addproduct_type?.evo_helpcard_type ); if (nextTechnicalCard) { $calculation.element('selectTechnicalCard').setValue(nextTechnicalCard?.value); } else { $calculation.element('selectTechnicalCard').resetValue(); } } }, { fireImmediately: true, } ); reaction( () => $calculation.$values.getValues(['leasingPeriod', 'leaseObjectType']), async ({ leasingPeriod, leaseObjectType }) => { const currentDate = getCurrentISODate(); const { data: { evo_addproduct_types }, } = await apolloClient.query({ query: CRMTypes.GetFuelCardsDocument, variables: { currentDate }, }); const options = evo_addproduct_types?.filter( (evo_addproduct_type) => Boolean( evo_addproduct_type?.evo_min_period && evo_addproduct_type.evo_min_period <= leasingPeriod ) && Boolean( evo_addproduct_type?.evo_max_period && evo_addproduct_type.evo_max_period >= leasingPeriod ) && Boolean( leaseObjectType && evo_addproduct_type?.evo_leasingobject_types?.find( (x) => x?.evo_leasingobject_typeid === leaseObjectType ) ) ); $calculation.element('selectFuelCard').setOptions(normalizeOptions(options)); }, { fireImmediately: true, } ); reaction( () => { const values = $calculation.$values.getValues(['leasingPeriod', 'leasingWithoutKasko']); const kaskoInsured = toJS($tables.insurance.row('kasko').getValue('insured')); const fingapInsured = toJS($tables.insurance.row('fingap').getValue('insured')); return { ...values, fingapInsured, kaskoInsured, }; }, ({ leasingPeriod, fingapInsured, kaskoInsured, leasingWithoutKasko }) => { if ( leasingPeriod < 12 || leasingWithoutKasko || kaskoInsured === 100_000_001 || (fingapInsured === 100_000_001 && leasingPeriod < 36) ) { $calculation.element('selectInsNSIB').resetValue().block(); } else { $calculation.element('selectInsNSIB').unblock(); } }, { delay: 10, fireImmediately: true, } ); debouncedReaction( () => $calculation.$values.getValues([ 'leasingPeriod', 'leaseObjectType', 'plPriceRub', 'discountRub', 'addEquipmentPrice', 'importProgramSum', 'firstPaymentPerc', ]), async ({ addEquipmentPrice, discountRub, firstPaymentPerc, importProgramSum, leaseObjectType: leaseObjectTypeId, leasingPeriod, plPriceRub, }) => { const currentDate = getCurrentISODate(); const { data: { evo_addproduct_types }, } = await apolloClient.query({ context: { disableModify: true, }, query: CRMTypes.GetInsNsibTypesDocument, variables: { currentDate }, }); const options = evo_addproduct_types?.filter( (x) => x && Boolean(x?.evo_max_period !== null && x.evo_max_period >= leasingPeriod) && Boolean(x?.evo_min_period !== null && x.evo_min_period <= leasingPeriod) && Boolean( x?.evo_max_price !== null && x.evo_max_price >= plPriceRub - discountRub - importProgramSum + addEquipmentPrice ) && Boolean( x?.evo_min_price !== null && x.evo_min_price <= plPriceRub - discountRub - importProgramSum + addEquipmentPrice ) && x.evo_leasingobject_types?.find( (evo_leasingobject_type) => evo_leasingobject_type?.evo_leasingobject_typeid === leaseObjectTypeId ) && x.evo_visible_calc && Boolean( x.evo_min_first_payment_perc !== null && x.evo_min_first_payment_perc <= firstPaymentPerc ) && Boolean( x.evo_max_first_payment_perc !== null && x.evo_max_first_payment_perc >= firstPaymentPerc ) ); $calculation.element('selectInsNSIB').setOptions(normalizeOptions(options)); }, { delay: 10, wait: 100, } ); reaction( () => $calculation.$values.getValues(['recalcWithRevision', 'leaseObjectType', 'engineType']), async ({ recalcWithRevision, leaseObjectType: leaseObjectTypeId, engineType }) => { if (recalcWithRevision === false) { $calculation .element('selectRequirementTelematic') .setOptions( selectRequirementTelematic.filter((x) => [100_000_000, 100_000_001, 100_000_002, 100_000_003].includes(x.value) ) ); if (engineType === 100_000_003) { $calculation.element('selectRequirementTelematic').setValue(100_000_003).block(); } else { $calculation.element('selectRequirementTelematic').unblock(); if (leaseObjectTypeId) { const { data: { evo_leasingobject_type }, } = await apolloClient.query({ query: CRMTypes.GetLeaseObjectTypeDocument, variables: { leaseObjectTypeId }, }); if (evo_leasingobject_type?.evo_id === '8') { $calculation .element('selectRequirementTelematic') .setOptions( selectRequirementTelematic.filter((x) => [100_000_000, 100_000_001].includes(x.value) ) ); } if (evo_leasingobject_type?.evo_id === '11') { $calculation.element('selectRequirementTelematic').setValue(100_000_002).block(); } else { $calculation.element('selectRequirementTelematic').unblock(); } } } } else { $calculation.element('selectRequirementTelematic').resetOptions(); } }, { fireImmediately: true, } ); reaction( () => $calculation.$values.getValues([ 'requirementTelematic', 'recalcWithRevision', 'leaseObjectType', 'engineType', ]), async ({ requirementTelematic, recalcWithRevision, leaseObjectType: leaseObjectTypeId, engineType, }) => { const currentDate = getCurrentISODate(); const { data: { evo_addproduct_types: trackerTypes }, } = await apolloClient.query({ query: CRMTypes.GetTrackerTypesDocument, variables: { currentDate }, }); const { data: { evo_addproduct_types: telematicTypes }, } = await apolloClient.query({ query: CRMTypes.GetTelematicTypesDocument, variables: { currentDate }, }); let filteredTrackerTypes = trackerTypes?.filter( (x) => requirementTelematic && x?.evo_controls_program?.includes(requirementTelematic) ); let filteredTelematicTypes = telematicTypes?.filter( (x) => requirementTelematic && x?.evo_controls_program?.includes(requirementTelematic) ); if (!recalcWithRevision) { filteredTrackerTypes = filteredTrackerTypes?.filter((x) => x?.evo_visible_calc === true); filteredTelematicTypes = filteredTelematicTypes?.filter( (x) => x?.evo_visible_calc === true ); } if (engineType === 100_000_003) { filteredTelematicTypes = telematicTypes?.filter( (x) => x?.evo_controls_program?.includes(100_000_003) && x.label?.toLowerCase().includes('аво') && !x.label?.toLowerCase().includes('pro') ); $calculation.element('selectTracker').resetValue().block(); $calculation.element('selectTelematic').block(); } else { $calculation.element('selectTracker').unblock(); $calculation.element('selectTelematic').unblock(); if (leaseObjectTypeId) { const { data: { evo_leasingobject_type }, } = await apolloClient.query({ query: CRMTypes.GetLeaseObjectTypeDocument, variables: { leaseObjectTypeId }, }); if (evo_leasingobject_type?.evo_id === '11') { filteredTelematicTypes = telematicTypes?.filter( (x) => x?.evo_controls_program?.includes(100_000_002) && x.label?.toLowerCase().includes('delta') && !x.label?.toLowerCase().includes('pro') ); } } } $calculation.element('selectTracker').setOptions(normalizeOptions(filteredTrackerTypes)); $calculation.element('selectTelematic').setOptions(normalizeOptions(filteredTelematicTypes)); }, { fireImmediately: true, } ); } export const validation = createValidationReaction(createValidationSchema);