From c0f825a5874e1f60beb3324a1e518e216c16f78c Mon Sep 17 00:00:00 2001 From: vchikalkin Date: Sat, 20 Apr 2024 10:26:00 +0300 Subject: [PATCH] merge branch release/dyn-4331_fingap-rat --- apps/web/process/add-product/reactions.ts | 55 ++++++++++++++++++++-- apps/web/process/fingap/reactions/index.ts | 1 + apps/web/process/fingap/reactions/init.ts | 34 +++++++++++++ 3 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 apps/web/process/fingap/reactions/init.ts diff --git a/apps/web/process/add-product/reactions.ts b/apps/web/process/add-product/reactions.ts index 3e4b48b..60bc810 100644 --- a/apps/web/process/add-product/reactions.ts +++ b/apps/web/process/add-product/reactions.ts @@ -8,9 +8,10 @@ import { getCurrentISODate } from '@/utils/date'; import { normalizeOptions } from '@/utils/entity'; import { debouncedReaction } from '@/utils/mobx'; import { reaction, toJS } from 'mobx'; +import { max } from 'radash'; export default function reactions({ store, apolloClient }: ProcessContext) { - const { $calculation, $tables } = store; + const { $calculation, $tables, $process } = store; reaction( () => $calculation.$values.getValues(['leasingPeriod', 'leaseObjectType', 'maxMass']), @@ -47,9 +48,9 @@ export default function reactions({ store, apolloClient }: ProcessContext) { ) ); - $calculation.element('selectTechnicalCard').setOptions(normalizeOptions(options)); - const currentTechnicalCardId = $calculation.element('selectTechnicalCard').getValue(); + + $calculation.element('selectTechnicalCard').setOptions(normalizeOptions(options)); if (currentTechnicalCardId) { const { data: { evo_addproduct_type }, @@ -74,6 +75,54 @@ export default function reactions({ store, apolloClient }: ProcessContext) { } ); + /** + * Устанавливаем самую дорогую техническую карточку по умолчанию первый раз при формировании списка карт + */ + { + const dispose = reaction( + () => { + const technicalCards = $calculation.element('selectTechnicalCard').getOptions(); + const isLoadKP = $process.has('LoadKP'); + + return { isLoadKP, technicalCards }; + }, + async ({ technicalCards, isLoadKP }) => { + if (isLoadKP) { + dispose(); + + return; + } + + const currentTechnicalCard = $calculation.element('selectTechnicalCard').getValue(); + + if (technicalCards.length && !currentTechnicalCard) { + const evo_addproduct_types = await Promise.all( + technicalCards.map(async (x) => { + const { + data: { evo_addproduct_type }, + } = await apolloClient.query({ + query: CRMTypes.GetAddProductTypeDocument, + variables: { addproductTypeId: x.value }, + }); + + return evo_addproduct_type; + }) + ); + + const maxPriceTechnicalCard = max(evo_addproduct_types, (x) => x?.evo_graph_price ?? 0); + + if (maxPriceTechnicalCard) { + $calculation + .element('selectTechnicalCard') + .setValue(maxPriceTechnicalCard?.evo_addproduct_typeid); + } + + dispose(); + } + } + ); + } + reaction( () => $calculation.$values.getValues(['leasingPeriod', 'leaseObjectType']), async ({ leasingPeriod, leaseObjectType }) => { diff --git a/apps/web/process/fingap/reactions/index.ts b/apps/web/process/fingap/reactions/index.ts index c8077ba..af23c4e 100644 --- a/apps/web/process/fingap/reactions/index.ts +++ b/apps/web/process/fingap/reactions/index.ts @@ -1 +1,2 @@ export { default as common } from './common'; +export { default as init } from './init'; diff --git a/apps/web/process/fingap/reactions/init.ts b/apps/web/process/fingap/reactions/init.ts new file mode 100644 index 0000000..cb576d0 --- /dev/null +++ b/apps/web/process/fingap/reactions/init.ts @@ -0,0 +1,34 @@ +import type { ProcessContext } from '@/process/types'; +import { when } from 'mobx'; + +export default function reactions({ store }: ProcessContext) { + const { $tables } = store; + + /** + * Устанавливаем СК фингап по умолчанию при загрузке страницы + */ + when(() => { + const finGAPInsuranceCompanies = $tables.insurance.row('fingap').getOptions('insuranceCompany'); + if (finGAPInsuranceCompanies.length) { + $tables.insurance + .row('fingap') + .setValue('insuranceCompany', finGAPInsuranceCompanies[0]?.value) + .setValue('insured', 100_000_001); + } + + return Boolean(finGAPInsuranceCompanies.length); + }); + + /** + * Устанавливаем все риски ФинГАП по умолчанию при загрузке страницы + */ + when(() => { + const { risks } = $tables.fingap; + + if (risks.length) { + $tables.fingap.setSelectedKeys(risks.map((x) => x.key)); + } + + return Boolean(risks.length); + }); +}