move function getInsuranceRule to new file

This commit is contained in:
vchikalkin 2024-07-30 14:02:35 +03:00
parent 5954a64941
commit 7dcae62f5f
6 changed files with 165 additions and 157 deletions

View File

@ -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';

View File

@ -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';

View File

@ -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,
};
}

View File

@ -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<ProcessContext, 'apolloClient' | 'store'>;
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<CRMTypes.GetEltInsuranceRulesQuery['evo_insurance_ruleses']>[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
);
}

View File

@ -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<CRMTypes.GetEltInsuranceRulesQuery['evo_insurance_ruleses']>[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
);
}

View File

@ -0,0 +1,7 @@
import type { Row } from '@/Components/Calculation/Form/ELT/types';
import type { ProcessContext } from '@/process/types';
export type BaseConvertResponseInput = {
context: Pick<ProcessContext, 'apolloClient' | 'store'>;
row: Row;
};