/* eslint-disable sonarjs/cognitive-complexity */ /* eslint-disable complexity */ import type { BaseConvertResponseInput } from './types'; import * as CRMTypes from '@/graphql/crm.types'; import { getCurrentDateString } from '@/utils/date'; import dayjs from 'dayjs'; import { sort } from 'radash'; type GetInsuranceConditionParams = { cost: number; riskType: 'osago' | 'kasko'; }; type Rule = | NonNullable[number] | undefined; export async function getInsuranceRule( input: BaseConvertResponseInput, { cost, riskType }: GetInsuranceConditionParams ) { const { context, row } = input; const { apolloClient, store } = context; const { $calculation } = store; const leasingPeriod = $calculation.element('tbxLeasingPeriod').getValue(); const currentDate = getCurrentDateString(); const leaseObjectCategory = $calculation.element('selectLeaseObjectCategory').getValue(); const leaseObjectYear = $calculation.element('tbxLeaseObjectYear').getValue(); const leaseObjectMotorPower = $calculation.element('tbxLeaseObjectMotorPower').getValue(); const engineType = $calculation.element('selectEngineType').getValue(); const tbxMileage = $calculation.element('tbxMileage').getValue(); const brand = $calculation.element('selectBrand').getValue(); const model = $calculation.element('selectModel').getValue(); const selectLeaseObjectUseFor = $calculation.element('selectLeaseObjectUseFor').getValue(); const legalClientRegion = $calculation.element('selectLegalClientRegion').getValue(); const dealerPerson = $calculation.element('selectDealerPerson').getValue(); const cbxLeaseObjectUsed = $calculation.element('cbxLeaseObjectUsed').getValue(); let lead: CRMTypes.GetLeadQuery['lead'] = null; const leadid = $calculation.element('selectLead').getValue(); if (leadid) { const { data } = await apolloClient.query({ query: CRMTypes.GetLeadDocument, variables: { leadid }, }); ({ lead } = data); } const insuranceOPF = lead?.evo_inn?.length === 12 ? 100_000_001 : 100_000_000; const { data: { evo_insurance_ruleses }, } = await apolloClient.query({ query: CRMTypes.GetEltInsuranceRulesDocument, variables: { currentDate, }, }); const { data: { account: insuranceCompany }, } = await apolloClient.query({ query: CRMTypes.GetInsuranceCompanyDocument, variables: { accountId: row.key }, }); const filteredRules = evo_insurance_ruleses?.filter( (rule) => rule && rule.evo_insurer_accountid === insuranceCompany?.accountid && (riskType === 'kasko' ? rule.evo_risk === 100_000_000 : rule.evo_risk === 100_000_003) && leaseObjectCategory && rule.evo_category?.includes(leaseObjectCategory) && rule.evo_min_period !== null && rule.evo_max_period !== null && leasingPeriod !== null && rule.evo_min_period <= leasingPeriod && rule.evo_max_period >= leasingPeriod && (rule.evo_object_type === 100_000_000 || (rule.evo_object_type === 100_000_001 && cbxLeaseObjectUsed === false) || (rule.evo_object_type === 100_000_002 && cbxLeaseObjectUsed === true)) && selectLeaseObjectUseFor && rule.evo_use_for?.includes(selectLeaseObjectUseFor) && rule.evo_min_price !== null && rule.evo_max_price !== null && cost !== null && rule.evo_min_price <= cost && rule.evo_max_price >= cost && rule.evo_min_year !== null && rule.evo_max_year !== null && leaseObjectYear !== null && rule.evo_min_year <= leaseObjectYear && rule.evo_max_year >= leaseObjectYear && rule.evo_min_power !== null && rule.evo_max_power !== null && leaseObjectMotorPower !== null && rule.evo_min_power <= leaseObjectMotorPower && rule.evo_max_power >= leaseObjectMotorPower && engineType && rule.evo_enginie_type?.includes(engineType) && rule.evo_opf === insuranceOPF && rule.evo_min_mileage !== null && rule.evo_max_mileage !== null && tbxMileage !== null && rule.evo_min_mileage <= tbxMileage && rule.evo_max_mileage >= tbxMileage && (rule.evo_brand === 100_000_000 || (rule.evo_brand === 100_000_001 && rule.evo_brands?.some((x) => x?.evo_brandid === brand)) || (rule.evo_brand === 100_000_002 && !rule.evo_brands?.some((x) => x?.evo_brandid === brand))) && (rule.evo_model === 100_000_000 || (rule.evo_model === 100_000_001 && rule.evo_models?.some((x) => x?.evo_modelid === model)) || (rule.evo_model === 100_000_002 && !rule.evo_models?.some((x) => x?.evo_modelid === model))) && (rule.evo_region === 100_000_000 || (rule.evo_region === 100_000_001 && rule.evo_regions?.some((x) => x?.evo_regionid === legalClientRegion)) || (rule.evo_region === 100_000_002 && !rule.evo_regions?.some((x) => x?.evo_regionid === legalClientRegion))) && (rule.evo_dealer === 100_000_000 || (rule.evo_dealer === 100_000_001 && rule.accounts?.some((x) => x?.accountid === dealerPerson)) || (rule.evo_dealer === 100_000_002 && !rule.accounts?.some((x) => x?.accountid === dealerPerson))) ); // Обработка найденных записей по приоритету const sortedRules = filteredRules ? sort(filteredRules, (x) => dayjs(x?.evo_datefrom).date(), true) : []; const priorities = [100_000_000, 100_000_003, 100_000_001, 100_000_002]; return priorities.reduce( (found, priority) => found || sortedRules.find((rule) => rule?.evo_rules_type === priority), null as Rule ); }