From ee538152b428d090028a9d4b27f92584db5f0e22 Mon Sep 17 00:00:00 2001 From: vchikalkin Date: Thu, 26 Jan 2023 20:07:47 +0300 Subject: [PATCH] =?UTF-8?q?process/configurator:=20=D0=BF=D0=BE=D0=B4?= =?UTF-8?q?=D0=B1=D0=BE=D1=80=20=D1=82=D0=B0=D1=80=D0=B8=D1=84=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/web/graphql/crm.types.ts | 21 +++ .../process/configurator/reactions/index.js | 2 +- .../process/configurator/reactions/values.ts | 142 ++++++++++++++++++ .../process/init/get-data/get-main-data.js | 22 +++ .../process/init/inject-reactions/default.js | 1 + 5 files changed, 187 insertions(+), 1 deletion(-) create mode 100644 apps/web/process/configurator/reactions/values.ts diff --git a/apps/web/graphql/crm.types.ts b/apps/web/graphql/crm.types.ts index 9add47e..ba45970 100644 --- a/apps/web/graphql/crm.types.ts +++ b/apps/web/graphql/crm.types.ts @@ -160,6 +160,20 @@ export type GetDealerPersons_ProcessConfiguratorQueryVariables = Exact<{ export type GetDealerPersons_ProcessConfiguratorQuery = { __typename?: 'Query', dealerPersons: Array<{ __typename?: 'account', accountid: string | null, label: string | null, value: string | null } | null> | null }; +export type GetTarifs_ProcessConfiguratorQueryVariables = Exact<{ + currentDate: InputMaybe; +}>; + + +export type GetTarifs_ProcessConfiguratorQuery = { __typename?: 'Query', evo_tarifs: Array<{ __typename?: 'evo_tarif', evo_tarifid: string | null, evo_baseproductid: string | null, evo_min_period: number | null, evo_max_period: number | null, evo_delivery_time: Array | null, evo_min_first_payment: number | null, evo_max_first_payment: number | null, evo_min_last_payment: number | null, evo_max_last_payment: number | null, evo_used: boolean | null } | null> | null }; + +export type GetTarif_ProcessConfiguratorQueryVariables = Exact<{ + tarifId: Scalars['Uuid']; +}>; + + +export type GetTarif_ProcessConfiguratorQuery = { __typename?: 'Query', evo_tarif: { __typename?: 'evo_tarif', evo_irr: number | null } | null }; + export type GetRisksDataFromQuoteQueryVariables = Exact<{ quoteId: Scalars['Uuid']; }>; @@ -215,6 +229,13 @@ export type GetAddproductTypesQueryVariables = Exact<{ [key: string]: never; }>; export type GetAddproductTypesQuery = { __typename?: 'Query', evo_addproduct_types: Array<{ __typename?: 'evo_addproduct_type', evo_graph_price: number | null, evo_product_type: number | null, label: string | null, value: string | null } | null> | null }; +export type GetTarifsQueryVariables = Exact<{ + currentDate: InputMaybe; +}>; + + +export type GetTarifsQuery = { __typename?: 'Query', selectTarif: Array<{ __typename?: 'evo_tarif', label: string | null, value: string | null } | null> | null }; + export type GetOwnerDataQueryVariables = Exact<{ domainname: InputMaybe; }>; diff --git a/apps/web/process/configurator/reactions/index.js b/apps/web/process/configurator/reactions/index.js index 536847a..0f93cdd 100644 --- a/apps/web/process/configurator/reactions/index.js +++ b/apps/web/process/configurator/reactions/index.js @@ -1,2 +1,2 @@ -/* eslint-disable import/prefer-default-export */ export { default as filters } from './filters'; +export { default as values } from './values'; diff --git a/apps/web/process/configurator/reactions/values.ts b/apps/web/process/configurator/reactions/values.ts new file mode 100644 index 0000000..0314735 --- /dev/null +++ b/apps/web/process/configurator/reactions/values.ts @@ -0,0 +1,142 @@ +/* eslint-disable @typescript-eslint/naming-convention */ +import { gql } from '@apollo/client'; +import dayjs from 'dayjs'; +import utc from 'dayjs/plugin/utc'; +import { autorun, reaction } from 'mobx'; + +import type * as CRMTypes from 'graphql/crm.types'; +import type { ReactionsContext } from 'process/types'; +import { makeDisposable } from 'tools'; + +dayjs.extend(utc); + +const QUERY_GET_TARIFS = gql` + query GetTarifs_ProcessConfigurator($currentDate: DateTime) { + evo_tarifs( + statecode: 0 + evo_datefrom_param: { lte: $currentDate } + evo_dateto_param: { gte: $currentDate } + ) { + evo_tarifid + evo_baseproductid + evo_min_period + evo_max_period + evo_delivery_time + evo_min_first_payment + evo_max_first_payment + evo_min_last_payment + evo_max_last_payment + evo_used + } + } +`; + +const QUERY_GET_TARIF = gql` + query GetTarif_ProcessConfigurator($tarifId: Uuid!) { + evo_tarif(evo_tarifid: $tarifId) { + evo_irr + } + } +`; + +export default function valuesReactions({ store, apolloClient }: ReactionsContext) { + const { $calculation, $process } = store; + + autorun( + async () => { + const { + product, + leasingPeriod, + leaseObjectUsed, + deliveryTime, + firstPaymentPerc, + lastPaymentPerc, + } = $calculation.$values.values; + + const currentDate = dayjs().utc(false).toISOString(); + + let { + data: { evo_tarifs = [] }, + } = await apolloClient.query< + CRMTypes.GetTarifs_ProcessConfiguratorQuery, + CRMTypes.GetTarifs_ProcessConfiguratorQueryVariables + >({ + query: QUERY_GET_TARIFS, + variables: { + currentDate, + }, + }); + + if (product && leasingPeriod && deliveryTime && evo_tarifs) { + evo_tarifs = evo_tarifs?.filter( + (tarif) => + tarif && + tarif.evo_baseproductid === product && + tarif.evo_min_period && + tarif.evo_min_period <= leasingPeriod && + tarif.evo_max_period && + tarif.evo_max_period >= leasingPeriod && + tarif.evo_delivery_time?.includes(deliveryTime) && + tarif.evo_min_first_payment && + tarif.evo_min_first_payment <= firstPaymentPerc && + tarif.evo_max_first_payment && + tarif.evo_max_first_payment >= firstPaymentPerc && + tarif.evo_min_last_payment && + tarif.evo_min_last_payment <= lastPaymentPerc && + tarif.evo_max_last_payment && + tarif.evo_max_last_payment >= lastPaymentPerc + ); + } else { + $calculation.element('selectTarif').resetValue(); + } + + if (leaseObjectUsed === true && evo_tarifs) { + evo_tarifs = evo_tarifs?.filter((tarif) => { + if (leaseObjectUsed === true) { + return tarif?.evo_used; + } + + return true; + }); + } + + $calculation.element('selectTarif').setValue(evo_tarifs?.at(0)?.evo_tarifid || null); + }, + { + delay: 10, + } + ); + + makeDisposable( + () => + reaction( + () => $calculation.element('selectTarif').getValue(), + async (tarifId) => { + if (!tarifId) { + $calculation.element('tbxIRR_Perc').resetValue(); + + return; + } + const { + data: { evo_tarif }, + } = await apolloClient.query< + CRMTypes.GetTarif_ProcessConfiguratorQuery, + CRMTypes.GetTarif_ProcessConfiguratorQueryVariables + >({ + query: QUERY_GET_TARIF, + variables: { + tarifId, + }, + }); + + if (evo_tarif?.evo_irr) { + $calculation.element('tbxIRR_Perc').setValue(evo_tarif?.evo_irr); + } else { + $calculation.element('tbxIRR_Perc').resetValue(); + } + } + ), + + () => $process.has('LoadKP') + ); +} diff --git a/apps/web/process/init/get-data/get-main-data.js b/apps/web/process/init/get-data/get-main-data.js index 3fb331b..3c990aa 100644 --- a/apps/web/process/init/get-data/get-main-data.js +++ b/apps/web/process/init/get-data/get-main-data.js @@ -89,6 +89,19 @@ const QUERY_GET_ADDPRODUCT_TYPES = gql` } `; +const QUERY_GET_TARIFS = gql` + query GetTarifs($currentDate: DateTime) { + selectTarif: evo_tarifs( + statecode: 0 + evo_datefrom_param: { lte: $currentDate } + evo_dateto_param: { gte: $currentDate } + ) { + label: evo_name + value: evo_tarifid + } + } +`; + const currentDate = dayjs().utc(false).toISOString(); function getMainData(query, onCompleted) { @@ -195,6 +208,15 @@ function getMainData(query, onCompleted) { selectLeasingWithoutKasko, }); }); + + query({ + query: QUERY_GET_TARIFS, + variables: { + currentDate, + }, + }).then(({ data }) => { + onCompleted(data); + }); } export function useMainData() { diff --git a/apps/web/process/init/inject-reactions/default.js b/apps/web/process/init/inject-reactions/default.js index 9a6fa74..821108a 100644 --- a/apps/web/process/init/inject-reactions/default.js +++ b/apps/web/process/init/inject-reactions/default.js @@ -35,4 +35,5 @@ export default function injectDefaultReactions(context) { leasingObject.common(context); leasingObject.validation(context); configurator.filters(context); + configurator.values(context); }