From a766546dfe0ae93f42cab7f871f2c5f39eee398c Mon Sep 17 00:00:00 2001 From: Chika Date: Tue, 25 Oct 2022 20:53:12 +0300 Subject: [PATCH] process/supplier-agent: auto generate similar reactions --- .eslintrc.json | 3 +- process/init/inject-reactions/default.js | 1 + .../supplier-agent/lib/create-reactions.ts | 102 ++++++ process/supplier-agent/reactions/agents.ts | 328 +++--------------- process/supplier-agent/reactions/supplier.ts | 161 ++------- 5 files changed, 182 insertions(+), 413 deletions(-) create mode 100644 process/supplier-agent/lib/create-reactions.ts diff --git a/.eslintrc.json b/.eslintrc.json index 052062c..d0b495f 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -74,7 +74,8 @@ "ignore": ["^.*.(jsx|tsx)$"] } ], - "import/no-unresolved": "warn" + "import/no-unresolved": "warn", + "implicit-arrow-linebreak": "warn" }, "overrides": [ // Only uses Testing Library lint rules in test files diff --git a/process/init/inject-reactions/default.js b/process/init/inject-reactions/default.js index c05f968..31b0b3d 100644 --- a/process/init/inject-reactions/default.js +++ b/process/init/inject-reactions/default.js @@ -14,6 +14,7 @@ export default function injectDefaultReactions(store, apolloClient, queryClient) calculateReactions.validation(store, apolloClient, queryClient); supplierReactions.commonReactions(store, apolloClient, queryClient); supplierReactions.validationReactions(store, apolloClient, queryClient); + agentsReactions.fillReactions(store, apolloClient, queryClient); agentsReactions.commonReactions(store, apolloClient, queryClient); agentsReactions.validationReactions(store, apolloClient, queryClient); priceReactions.computed(store, apolloClient, queryClient); diff --git a/process/supplier-agent/lib/create-reactions.ts b/process/supplier-agent/lib/create-reactions.ts new file mode 100644 index 0000000..94c209d --- /dev/null +++ b/process/supplier-agent/lib/create-reactions.ts @@ -0,0 +1,102 @@ +/* eslint-disable @typescript-eslint/naming-convention */ +import type { ApolloClient } from '@apollo/client'; +import type * as Values from 'Components/Calculation/config/map/values'; +import dayjs from 'dayjs'; +import utc from 'dayjs/plugin/utc'; +import type * as CRMTypes from 'graphql/crm.types'; +import { reaction } from 'mobx'; +import type RootStore from 'stores/root'; +import { normalizeOptions } from 'tools/entity'; +import { makeDisposable } from 'tools/mobx'; +import * as query from './query'; + +dayjs.extend(utc); + +export function fillAgentRewardReaction( + store: RootStore, + apolloClient: ApolloClient, + agentParams: { + agentField: Values.Elements; + rewardConditionField: Values.Elements; + } +) { + const { $calculation, $process } = store; + const { agentField, rewardConditionField } = agentParams; + + makeDisposable( + () => + reaction( + () => $calculation.getElementValue(agentField), + async (agentId) => { + if (!agentId) { + $calculation.resetElement(rewardConditionField); + + return; + } + const { + data: { evo_reward_conditions }, + } = await apolloClient.query< + CRMTypes.GetRewardConditionsQuery, + CRMTypes.GetRewardConditionsQueryVariables + >({ + query: query.QUERY_GET_REWARD_CONDITIONS, + variables: { + agentid: agentId, + currentDate: dayjs().toISOString(), + }, + }); + + if (evo_reward_conditions?.length) { + $calculation.setElementOptions( + rewardConditionField, + normalizeOptions(evo_reward_conditions) + ); + } + } + ), + () => $process.has('LoadKP') + ); +} + +export function fillAgentRewardSummReaction( + store: RootStore, + apolloClient: ApolloClient, + agentParams: { + rewardConditionField: Values.Elements; + rewardSummField: Values.Elements; + } +) { + const { $calculation, $process } = store; + const { rewardConditionField, rewardSummField } = agentParams; + + makeDisposable( + () => + reaction( + () => $calculation.getElementValue(rewardConditionField), + async (rewardConditionId) => { + if (!rewardConditionId) { + $calculation.resetElement(rewardSummField); + + return; + } + + const { + data: { evo_reward_condition }, + } = await apolloClient.query< + CRMTypes.GetRewardSummQuery, + CRMTypes.GetRewardSummQueryVariables + >({ + query: query.QUERY_GET_REWARD_SUMM, + variables: { + conditionId: rewardConditionId, + }, + }); + + $calculation.setElementValue(rewardSummField, evo_reward_condition?.evo_reward_summ); + + $calculation.unblockElement(rewardSummField); + } + ), + () => $process.has('LoadKP') + ); +} diff --git a/process/supplier-agent/reactions/agents.ts b/process/supplier-agent/reactions/agents.ts index 63a5127..70e8999 100644 --- a/process/supplier-agent/reactions/agents.ts +++ b/process/supplier-agent/reactions/agents.ts @@ -3,17 +3,15 @@ import type { ApolloClient } from '@apollo/client'; import dayjs from 'dayjs'; import utc from 'dayjs/plugin/utc'; -import type * as CRMTypes from 'graphql/crm.types'; import { reaction } from 'mobx'; import type RootStore from 'stores/root'; -import { normalizeOptions } from 'tools/entity'; import { makeDisposable } from 'tools/mobx'; +import * as createReactions from '../lib/create-reactions'; import * as fillAgentsFromLead from '../lib/fill-agents-from-lead'; -import * as query from '../lib/query'; dayjs.extend(utc); -export function commonReactions(store: RootStore, apolloClient: ApolloClient) { +export function fillReactions(store: RootStore, apolloClient: ApolloClient) { const { $calculation, $process } = store; /** @@ -33,281 +31,67 @@ export function commonReactions(store: RootStore, apolloClient: ApolloClient $process.has('LoadKP') ); - // IndAgent - makeDisposable( - () => - reaction( - () => $calculation.getElementValue('selectIndAgent'), - async (indAgentId) => { - if (!indAgentId) { - $calculation.resetElement('selectIndAgentRewardCondition'); + /** + * IndAgent + */ + // Заполняем selectIndAgentRewardCondition + createReactions.fillAgentRewardReaction(store, apolloClient, { + agentField: 'selectIndAgent', + rewardConditionField: 'selectIndAgentRewardCondition', + }); - return; - } + // Заполняем tbxIndAgentRewardSumm + createReactions.fillAgentRewardSummReaction(store, apolloClient, { + rewardConditionField: 'selectIndAgentRewardCondition', + rewardSummField: 'tbxIndAgentRewardSumm', + }); - const { - data: { evo_reward_conditions }, - } = await apolloClient.query< - CRMTypes.GetRewardConditionsQuery, - CRMTypes.GetRewardConditionsQueryVariables - >({ - query: query.QUERY_GET_REWARD_CONDITIONS, - variables: { - agentid: indAgentId, - currentDate: dayjs().toISOString(), - }, - }); + /** + * CalcDoubleAgent + */ + // Заполняем selectCalcDoubleAgentRewardCondition + createReactions.fillAgentRewardReaction(store, apolloClient, { + agentField: 'selectCalcDoubleAgent', + rewardConditionField: 'selectCalcDoubleAgentRewardCondition', + }); - if (evo_reward_conditions?.length) { - $calculation.setElementOptions( - 'selectIndAgentRewardCondition', - normalizeOptions(evo_reward_conditions) - ); - } - } - ), - () => $process.has('LoadKP') - ); + // Заполняем tbxCalcDoubleAgentRewardSumm + createReactions.fillAgentRewardSummReaction(store, apolloClient, { + rewardConditionField: 'selectCalcDoubleAgentRewardCondition', + rewardSummField: 'tbxCalcDoubleAgentRewardSumm', + }); - makeDisposable( - () => - reaction( - () => $calculation.getElementValue('selectIndAgentRewardCondition'), - async (rewardConditionId) => { - if (!rewardConditionId) { - $calculation.resetElement('tbxIndAgentRewardSumm'); + /** + * CalcBroker + */ + // Заполняем selectCalcBrokerRewardCondition + createReactions.fillAgentRewardReaction(store, apolloClient, { + agentField: 'selectCalcBroker', + rewardConditionField: 'selectCalcBrokerRewardCondition', + }); - return; - } + // Заполняем tbxCalcBrokerRewardSum + createReactions.fillAgentRewardSummReaction(store, apolloClient, { + rewardConditionField: 'selectCalcBrokerRewardCondition', + rewardSummField: 'tbxCalcBrokerRewardSum', + }); - const { - data: { evo_reward_condition }, - } = await apolloClient.query< - CRMTypes.GetRewardSummQuery, - CRMTypes.GetRewardSummQueryVariables - >({ - query: query.QUERY_GET_REWARD_SUMM, - variables: { - conditionId: rewardConditionId, - }, - }); + /** + * CalcFinDepartment + */ + // Заполняем selectFinDepartmentRewardCondtion + createReactions.fillAgentRewardReaction(store, apolloClient, { + agentField: 'selectCalcFinDepartment', + rewardConditionField: 'selectFinDepartmentRewardCondtion', + }); - $calculation.setElementValue( - 'tbxIndAgentRewardSumm', - evo_reward_condition?.evo_reward_summ - ); - $calculation.unblockElement('tbxIndAgentRewardSumm'); - } - ), - () => $process.has('LoadKP') - ); - - // CalcDoubleAgent - makeDisposable( - () => - reaction( - () => $calculation.getElementValue('selectCalcDoubleAgent'), - async (calcDoubleAgentId) => { - if (!calcDoubleAgentId) { - $calculation.resetElement('selectCalcDoubleAgentRewardCondition'); - - return; - } - - const { - data: { evo_reward_conditions }, - } = await apolloClient.query< - CRMTypes.GetRewardConditionsQuery, - CRMTypes.GetRewardConditionsQueryVariables - >({ - query: query.QUERY_GET_REWARD_CONDITIONS, - variables: { - agentid: calcDoubleAgentId, - currentDate: dayjs().toISOString(), - }, - }); - - if (evo_reward_conditions?.length) { - $calculation.setElementOptions( - 'selectCalcDoubleAgentRewardCondition', - normalizeOptions(evo_reward_conditions) - ); - } - } - ), - () => $process.has('LoadKP') - ); - - makeDisposable( - () => - reaction( - () => $calculation.getElementValue('selectCalcDoubleAgentRewardCondition'), - async (rewardConditionId) => { - if (!rewardConditionId) { - $calculation.resetElement('tbxCalcDoubleAgentRewardSumm'); - - return; - } - - const { - data: { evo_reward_condition }, - } = await apolloClient.query< - CRMTypes.GetRewardSummQuery, - CRMTypes.GetRewardSummQueryVariables - >({ - query: query.QUERY_GET_REWARD_SUMM, - variables: { - conditionId: rewardConditionId, - }, - }); - - $calculation.setElementValue( - 'tbxCalcDoubleAgentRewardSumm', - evo_reward_condition?.evo_reward_summ - ); - $calculation.unblockElement('tbxCalcDoubleAgentRewardSumm'); - } - ), - () => $process.has('LoadKP') - ); - - // CalcBroker - makeDisposable( - () => - reaction( - () => $calculation.getElementValue('selectCalcBroker'), - async (calcBrokerId) => { - if (!calcBrokerId) { - $calculation.resetElement('selectCalcBrokerRewardCondition'); - - return; - } - - const { - data: { evo_reward_conditions }, - } = await apolloClient.query< - CRMTypes.GetRewardConditionsQuery, - CRMTypes.GetRewardConditionsQueryVariables - >({ - query: query.QUERY_GET_REWARD_CONDITIONS, - variables: { - agentid: calcBrokerId, - currentDate: dayjs().toISOString(), - }, - }); - - if (evo_reward_conditions?.length) { - $calculation.setElementOptions( - 'selectCalcBrokerRewardCondition', - normalizeOptions(evo_reward_conditions) - ); - } - } - ), - () => $process.has('LoadKP') - ); - - makeDisposable( - () => - reaction( - () => $calculation.getElementValue('selectCalcBrokerRewardCondition'), - async (rewardConditionId) => { - if (!rewardConditionId) { - $calculation.resetElement('tbxCalcBrokerRewardSum'); - - return; - } - - const { - data: { evo_reward_condition }, - } = await apolloClient.query< - CRMTypes.GetRewardSummQuery, - CRMTypes.GetRewardSummQueryVariables - >({ - query: query.QUERY_GET_REWARD_SUMM, - variables: { - conditionId: rewardConditionId, - }, - }); - - $calculation.setElementValue( - 'tbxCalcBrokerRewardSum', - evo_reward_condition?.evo_reward_summ - ); - $calculation.unblockElement('tbxCalcBrokerRewardSum'); - } - ), - () => $process.has('LoadKP') - ); - - // CalcFinDepartment - makeDisposable( - () => - reaction( - () => $calculation.getElementValue('selectCalcFinDepartment'), - async (calcFinDepartmentId) => { - if (!calcFinDepartmentId) { - $calculation.resetElement('selectFinDepartmentRewardCondtion'); - - return; - } - - const { - data: { evo_reward_conditions }, - } = await apolloClient.query< - CRMTypes.GetRewardConditionsQuery, - CRMTypes.GetRewardConditionsQueryVariables - >({ - query: query.QUERY_GET_REWARD_CONDITIONS, - variables: { - agentid: calcFinDepartmentId, - currentDate: dayjs().toISOString(), - }, - }); - - if (evo_reward_conditions?.length) { - $calculation.setElementOptions( - 'selectFinDepartmentRewardCondtion', - normalizeOptions(evo_reward_conditions) - ); - } - } - ), - () => $process.has('LoadKP') - ); - - makeDisposable( - () => - reaction( - () => $calculation.getElementValue('selectFinDepartmentRewardCondtion'), - async (rewardConditionId) => { - if (!rewardConditionId) { - $calculation.resetElement('tbxFinDepartmentRewardSumm'); - - return; - } - - const { - data: { evo_reward_condition }, - } = await apolloClient.query< - CRMTypes.GetRewardSummQuery, - CRMTypes.GetRewardSummQueryVariables - >({ - query: query.QUERY_GET_REWARD_SUMM, - variables: { - conditionId: rewardConditionId, - }, - }); - - $calculation.setElementValue( - 'tbxFinDepartmentRewardSumm', - evo_reward_condition?.evo_reward_summ - ); - $calculation.unblockElement('tbxFinDepartmentRewardSumm'); - } - ), - () => $process.has('LoadKP') - ); + // Заполняем tbxCalcBrokerRewardSum + createReactions.fillAgentRewardSummReaction(store, apolloClient, { + rewardConditionField: 'selectFinDepartmentRewardCondtion', + rewardSummField: 'tbxFinDepartmentRewardSumm', + }); } +export function commonReactions(store: RootStore, apolloClient: ApolloClient) {} + export function validationReactions(store: RootStore, apolloClient: ApolloClient) {} diff --git a/process/supplier-agent/reactions/supplier.ts b/process/supplier-agent/reactions/supplier.ts index d7a4725..18e2cc7 100644 --- a/process/supplier-agent/reactions/supplier.ts +++ b/process/supplier-agent/reactions/supplier.ts @@ -1,4 +1,3 @@ -/* eslint-disable implicit-arrow-linebreak */ /* eslint-disable @typescript-eslint/naming-convention */ import type { ApolloClient } from '@apollo/client'; import { gql } from '@apollo/client'; @@ -9,6 +8,7 @@ import { reaction } from 'mobx'; import type RootStore from 'stores/root'; import { normalizeOptions } from 'tools/entity'; import { makeDisposable } from 'tools/mobx'; +import * as createReactions from '../lib/create-reactions'; import * as query from '../lib/query'; dayjs.extend(utc); @@ -123,148 +123,29 @@ export function commonReactions(store: RootStore, apolloClient: ApolloClient $process.has('LoadKP') ); - /** - * Заполняем selectDealerRewardCondition - */ + // Заполняем selectDealerRewardCondition + createReactions.fillAgentRewardReaction(store, apolloClient, { + agentField: 'selectDealerPerson', + rewardConditionField: 'selectDealerRewardCondition', + }); - makeDisposable( - () => - reaction( - () => $calculation.getElementValue('selectDealerPerson'), - async (dealerPersonId) => { - if (!dealerPersonId) { - $calculation.resetElement('selectDealerRewardCondition'); + // Заполняем tbxDealerRewardSumm + createReactions.fillAgentRewardSummReaction(store, apolloClient, { + rewardConditionField: 'selectDealerRewardCondition', + rewardSummField: 'tbxDealerRewardSumm', + }); - return; - } - const { - data: { evo_reward_conditions }, - } = await apolloClient.query< - CRMTypes.GetRewardConditionsQuery, - CRMTypes.GetRewardConditionsQueryVariables - >({ - query: query.QUERY_GET_REWARD_CONDITIONS, - variables: { - agentid: dealerPersonId, - currentDate: dayjs().toISOString(), - }, - }); + // Заполняем selectDealerBrokerRewardCondition + createReactions.fillAgentRewardReaction(store, apolloClient, { + agentField: 'selectDealerBroker', + rewardConditionField: 'selectDealerBrokerRewardCondition', + }); - if (evo_reward_conditions?.length) { - $calculation.setElementOptions( - 'selectDealerRewardCondition', - normalizeOptions(evo_reward_conditions) - ); - } - } - ), - () => $process.has('LoadKP') - ); - - makeDisposable( - () => - reaction( - () => $calculation.getElementValue('selectDealerRewardCondition'), - async (rewardConditionId) => { - if (!rewardConditionId) { - $calculation.resetElement('tbxDealerRewardSumm'); - - return; - } - - const { - data: { evo_reward_condition }, - } = await apolloClient.query< - CRMTypes.GetRewardSummQuery, - CRMTypes.GetRewardSummQueryVariables - >({ - query: query.QUERY_GET_REWARD_SUMM, - variables: { - conditionId: rewardConditionId, - }, - }); - - $calculation.setElementValue( - 'tbxDealerRewardSumm', - evo_reward_condition?.evo_reward_summ - ); - $calculation.unblockElement('tbxDealerRewardSumm'); - } - ), - () => $process.has('LoadKP') - ); - - /** - * Заполняем selectDealerBrokerRewardCondition - */ - - makeDisposable( - () => - reaction( - () => $calculation.getElementValue('selectDealerBroker'), - async (dealerBrokerId) => { - if (!dealerBrokerId) { - $calculation.resetElement('selectDealerBrokerRewardCondition'); - - return; - } - const { - data: { evo_reward_conditions }, - } = await apolloClient.query< - CRMTypes.GetRewardConditionsQuery, - CRMTypes.GetRewardConditionsQueryVariables - >({ - query: query.QUERY_GET_REWARD_CONDITIONS, - variables: { - agentid: dealerBrokerId, - currentDate: dayjs().toISOString(), - }, - }); - - if (evo_reward_conditions?.length) { - $calculation.setElementOptions( - 'selectDealerBrokerRewardCondition', - normalizeOptions(evo_reward_conditions) - ); - } - } - ), - () => $process.has('LoadKP') - ); - - makeDisposable( - () => - reaction( - () => $calculation.getElementValue('selectDealerBrokerRewardCondition'), - async (rewardConditionId) => { - if (!rewardConditionId) { - $calculation.resetElement('tbxDealerBrokerRewardSumm'); - - return; - } - - const { - data: { evo_reward_condition }, - } = await apolloClient.query< - CRMTypes.GetRewardSummQuery, - CRMTypes.GetRewardSummQueryVariables - >({ - query: query.QUERY_GET_REWARD_SUMM, - variables: { - conditionId: rewardConditionId, - }, - }); - - $calculation.setElementValue( - 'tbxDealerBrokerRewardSumm', - evo_reward_condition?.evo_reward_summ - ); - - $calculation.unblockElement('tbxDealerBrokerRewardSumm'); - } - ), - () => $process.has('LoadKP') - ); + // Заполняем tbxDealerBrokerRewardSumm + createReactions.fillAgentRewardSummReaction(store, apolloClient, { + rewardConditionField: 'selectDealerBrokerRewardCondition', + rewardSummField: 'tbxDealerBrokerRewardSumm', + }); reaction( () => $calculation.getElementValue('selectDealerRewardCondition'),