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 }) => {