process/supplier-agent: auto generate similar reactions

This commit is contained in:
Chika 2022-10-25 20:53:12 +03:00
parent d9d49be4fe
commit a766546dfe
5 changed files with 182 additions and 413 deletions

View File

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

View File

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

View File

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

View File

@ -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<object>) {
export function fillReactions(store: RootStore, apolloClient: ApolloClient<object>) {
const { $calculation, $process } = store;
/**
@ -33,281 +31,67 @@ export function commonReactions(store: RootStore, apolloClient: ApolloClient<obj
() => $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<object>) {}
export function validationReactions(store: RootStore, apolloClient: ApolloClient<object>) {}

View File

@ -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<obj
() => $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'),