From 7dcae62f5f10a78b94fb49cb89bc62b6e41d3977 Mon Sep 17 00:00:00 2001 From: vchikalkin Date: Tue, 30 Jul 2024 14:02:35 +0300 Subject: [PATCH] move function getInsuranceRule to new file --- .../Components/Calculation/Form/ELT/Kasko.tsx | 2 +- .../Components/Calculation/Form/ELT/Osago.tsx | 2 +- .../Calculation/Form/ELT/lib/tools.ts | 12 ++ apps/web/process/elt/lib/response.ts | 149 +---------------- apps/web/process/elt/lib/tools.ts | 150 ++++++++++++++++-- apps/web/process/elt/lib/types.ts | 7 + 6 files changed, 165 insertions(+), 157 deletions(-) create mode 100644 apps/web/Components/Calculation/Form/ELT/lib/tools.ts create mode 100644 apps/web/process/elt/lib/types.ts diff --git a/apps/web/Components/Calculation/Form/ELT/Kasko.tsx b/apps/web/Components/Calculation/Form/ELT/Kasko.tsx index 8455f43..91858ac 100644 --- a/apps/web/Components/Calculation/Form/ELT/Kasko.tsx +++ b/apps/web/Components/Calculation/Form/ELT/Kasko.tsx @@ -1,8 +1,8 @@ /* eslint-disable sonarjs/cognitive-complexity */ import { PolicyTable, ReloadButton, Validation } from './Components'; import { columns } from './lib/config'; +import { resetRow } from './lib/tools'; import type { Row, StoreSelector } from './types'; -import { resetRow } from '@/process/elt/lib/tools'; import { useStore } from '@/stores/hooks'; import { trpcClient } from '@/trpc/client'; import { observer } from 'mobx-react-lite'; diff --git a/apps/web/Components/Calculation/Form/ELT/Osago.tsx b/apps/web/Components/Calculation/Form/ELT/Osago.tsx index 23a4e53..91a03ac 100644 --- a/apps/web/Components/Calculation/Form/ELT/Osago.tsx +++ b/apps/web/Components/Calculation/Form/ELT/Osago.tsx @@ -2,8 +2,8 @@ /* eslint-disable sonarjs/cognitive-complexity */ import { PolicyTable, ReloadButton, Validation } from './Components'; import { columns } from './lib/config'; +import { resetRow } from './lib/tools'; import type { Row, StoreSelector } from './types'; -import { resetRow } from '@/process/elt/lib/tools'; import { useStore } from '@/stores/hooks'; import { trpcClient } from '@/trpc/client'; import { observer } from 'mobx-react-lite'; diff --git a/apps/web/Components/Calculation/Form/ELT/lib/tools.ts b/apps/web/Components/Calculation/Form/ELT/lib/tools.ts new file mode 100644 index 0000000..fda71ef --- /dev/null +++ b/apps/web/Components/Calculation/Form/ELT/lib/tools.ts @@ -0,0 +1,12 @@ +import type { Row } from '@/Components/Calculation/Form/ELT/types'; +import { defaultRow } from '@/stores/tables/elt/default-values'; + +export function resetRow(row: Row): Row { + return { + ...defaultRow, + id: row.id, + key: row.key, + metodCalc: row.metodCalc, + name: row.name, + }; +} diff --git a/apps/web/process/elt/lib/response.ts b/apps/web/process/elt/lib/response.ts index 873a7a9..e6d9a1d 100644 --- a/apps/web/process/elt/lib/response.ts +++ b/apps/web/process/elt/lib/response.ts @@ -1,22 +1,15 @@ /* eslint-disable sonarjs/cognitive-complexity */ /* eslint-disable complexity */ import type { ownOsagoRequest } from './request'; +import { getInsuranceRule } from './tools'; +import type { BaseConvertResponseInput } from './types'; import type * as ELT from '@/api/elt/types'; import type { Row } from '@/Components/Calculation/Form/ELT/types'; import { MAX_FRANCHISE, MAX_INSURANCE, MIN_INSURANCE } from '@/constants/values'; import * as CRMTypes from '@/graphql/crm.types'; -import type { ProcessContext } from '@/process/types'; import { defaultRow } from '@/stores/tables/elt/default-values'; -import { getCurrentDateString } from '@/utils/date'; -import dayjs from 'dayjs'; -import { sort } from 'radash'; import { round } from 'tools'; -type BaseConvertResponseInput = { - context: Pick; - row: Row; -}; - type ConvertEltOsagoResponseInput = BaseConvertResponseInput & { response: ELT.ResponseEltOsago; }; @@ -56,7 +49,7 @@ export async function convertEltOsagoResponse(input: ConvertEltOsagoResponseInpu $calculation.$values.getValue('importProgramSum') + $calculation.$values.getValue('addEquipmentPrice'); - const selectedRule = await getInsuranceCondition(input, { cost, riskType: 'osago' }); + const selectedRule = await getInsuranceRule(input, { cost, riskType: 'osago' }); const insuranceCondition = selectedRule?.evo_id ?? null; @@ -224,7 +217,7 @@ export async function convertEltKaskoResponse(input: ConvertEltKaskoResponseInpu $calculation.$values.getValue('importProgramSum') + $calculation.$values.getValue('addEquipmentPrice'); - const selectedRule = await getInsuranceCondition(input, { cost, riskType: 'kasko' }); + const selectedRule = await getInsuranceRule(input, { cost, riskType: 'kasko' }); const insuranceCondition = selectedRule?.evo_id ?? null; @@ -289,137 +282,3 @@ export async function convertEltKaskoResponse(input: ConvertEltKaskoResponseInpu totalFranchise, }; } - -type GetInsuranceConditionParams = { - cost: number; - riskType: 'osago' | 'kasko'; -}; - -async function getInsuranceCondition( - 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?.includes(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) - : []; - - type Rule = - | NonNullable[number] - | undefined; - 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 - ); -} diff --git a/apps/web/process/elt/lib/tools.ts b/apps/web/process/elt/lib/tools.ts index fda71ef..f7c69d0 100644 --- a/apps/web/process/elt/lib/tools.ts +++ b/apps/web/process/elt/lib/tools.ts @@ -1,12 +1,142 @@ -import type { Row } from '@/Components/Calculation/Form/ELT/types'; -import { defaultRow } from '@/stores/tables/elt/default-values'; +/* 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'; -export function resetRow(row: Row): Row { - return { - ...defaultRow, - id: row.id, - key: row.key, - metodCalc: row.metodCalc, - name: row.name, - }; +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?.includes(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 + ); } diff --git a/apps/web/process/elt/lib/types.ts b/apps/web/process/elt/lib/types.ts new file mode 100644 index 0000000..c949697 --- /dev/null +++ b/apps/web/process/elt/lib/types.ts @@ -0,0 +1,7 @@ +import type { Row } from '@/Components/Calculation/Form/ELT/types'; +import type { ProcessContext } from '@/process/types'; + +export type BaseConvertResponseInput = { + context: Pick; + row: Row; +};