From d9d49be4febe68c8fe8b1c0c3a1889d7453f7887 Mon Sep 17 00:00:00 2001 From: Chika Date: Tue, 25 Oct 2022 19:18:31 +0300 Subject: [PATCH] process/supplier-agents: add base supplier reactions --- process/supplier-agent/lib/query.ts | 25 +- process/supplier-agent/reactions/agents.ts | 284 +++++++++++++++++++ process/supplier-agent/reactions/supplier.ts | 36 +-- 3 files changed, 314 insertions(+), 31 deletions(-) diff --git a/process/supplier-agent/lib/query.ts b/process/supplier-agent/lib/query.ts index 3736dfe..be9bcab 100644 --- a/process/supplier-agent/lib/query.ts +++ b/process/supplier-agent/lib/query.ts @@ -1,6 +1,5 @@ import { gql } from '@apollo/client'; -/* eslint-disable import/prefer-default-export */ export const QUERY_GET_AGENT = gql` query GetAgent($agentid: Uuid!) { agent: account(accountid: $agentid) { @@ -9,3 +8,27 @@ export const QUERY_GET_AGENT = gql` } } `; + +export const QUERY_GET_REWARD_CONDITIONS = gql` + query GetRewardConditions($agentid: Uuid!, $currentDate: DateTime) { + evo_reward_conditions( + evo_agent_accountid: $agentid + evo_datefrom_param: { lte: $currentDate } + evo_dateto_param: { gte: $currentDate } + statecode: 0 + evo_agency_agreementid_param: { has: true } + ) { + label: evo_name + value: evo_reward_conditionid + evo_reward_summ + } + } +`; + +export const QUERY_GET_REWARD_SUMM = gql` + query GetRewardSumm($conditionId: Uuid!) { + evo_reward_condition(evo_reward_conditionid: $conditionId) { + evo_reward_summ + } + } +`; diff --git a/process/supplier-agent/reactions/agents.ts b/process/supplier-agent/reactions/agents.ts index 1820f0b..63a5127 100644 --- a/process/supplier-agent/reactions/agents.ts +++ b/process/supplier-agent/reactions/agents.ts @@ -1,9 +1,17 @@ +/* eslint-disable @typescript-eslint/naming-convention */ /* eslint-disable implicit-arrow-linebreak */ 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 fillAgentsFromLead from '../lib/fill-agents-from-lead'; +import * as query from '../lib/query'; + +dayjs.extend(utc); export function commonReactions(store: RootStore, apolloClient: ApolloClient) { const { $calculation, $process } = store; @@ -24,6 +32,282 @@ export function commonReactions(store: RootStore, apolloClient: ApolloClient $process.has('LoadKP') ); + + // IndAgent + makeDisposable( + () => + reaction( + () => $calculation.getElementValue('selectIndAgent'), + async (indAgentId) => { + if (!indAgentId) { + $calculation.resetElement('selectIndAgentRewardCondition'); + + return; + } + + const { + data: { evo_reward_conditions }, + } = await apolloClient.query< + CRMTypes.GetRewardConditionsQuery, + CRMTypes.GetRewardConditionsQueryVariables + >({ + query: query.QUERY_GET_REWARD_CONDITIONS, + variables: { + agentid: indAgentId, + currentDate: dayjs().toISOString(), + }, + }); + + if (evo_reward_conditions?.length) { + $calculation.setElementOptions( + 'selectIndAgentRewardCondition', + normalizeOptions(evo_reward_conditions) + ); + } + } + ), + () => $process.has('LoadKP') + ); + + makeDisposable( + () => + reaction( + () => $calculation.getElementValue('selectIndAgentRewardCondition'), + async (rewardConditionId) => { + if (!rewardConditionId) { + $calculation.resetElement('tbxIndAgentRewardSumm'); + + return; + } + + const { + data: { evo_reward_condition }, + } = await apolloClient.query< + CRMTypes.GetRewardSummQuery, + CRMTypes.GetRewardSummQueryVariables + >({ + query: query.QUERY_GET_REWARD_SUMM, + variables: { + conditionId: rewardConditionId, + }, + }); + + $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') + ); } export function validationReactions(store: RootStore, apolloClient: ApolloClient) {} diff --git a/process/supplier-agent/reactions/supplier.ts b/process/supplier-agent/reactions/supplier.ts index 807cc1f..d7a4725 100644 --- a/process/supplier-agent/reactions/supplier.ts +++ b/process/supplier-agent/reactions/supplier.ts @@ -9,7 +9,7 @@ import { reaction } from 'mobx'; import type RootStore from 'stores/root'; import { normalizeOptions } from 'tools/entity'; import { makeDisposable } from 'tools/mobx'; -import { QUERY_GET_AGENT } from '../lib/query'; +import * as query from '../lib/query'; dayjs.extend(utc); @@ -102,7 +102,7 @@ export function commonReactions(store: RootStore, apolloClient: ApolloClient({ - query: QUERY_GET_AGENT, + query: query.QUERY_GET_AGENT, variables: { agentid: dealer?.evo_broker_accountid, }, @@ -127,22 +127,6 @@ export function commonReactions(store: RootStore, apolloClient: ApolloClient reaction( @@ -159,7 +143,7 @@ export function commonReactions(store: RootStore, apolloClient: ApolloClient({ - query: QUERY_GET_REWARD_CONDITIONS, + query: query.QUERY_GET_REWARD_CONDITIONS, variables: { agentid: dealerPersonId, currentDate: dayjs().toISOString(), @@ -177,14 +161,6 @@ export function commonReactions(store: RootStore, apolloClient: ApolloClient $process.has('LoadKP') ); - const QUERY_GET_REWARD_SUMM = gql` - query GetRewardSumm($conditionId: Uuid!) { - evo_reward_condition(evo_reward_conditionid: $conditionId) { - evo_reward_summ - } - } - `; - makeDisposable( () => reaction( @@ -202,7 +178,7 @@ export function commonReactions(store: RootStore, apolloClient: ApolloClient({ - query: QUERY_GET_REWARD_SUMM, + query: query.QUERY_GET_REWARD_SUMM, variables: { conditionId: rewardConditionId, }, @@ -238,7 +214,7 @@ export function commonReactions(store: RootStore, apolloClient: ApolloClient({ - query: QUERY_GET_REWARD_CONDITIONS, + query: query.QUERY_GET_REWARD_CONDITIONS, variables: { agentid: dealerBrokerId, currentDate: dayjs().toISOString(), @@ -273,7 +249,7 @@ export function commonReactions(store: RootStore, apolloClient: ApolloClient({ - query: QUERY_GET_REWARD_SUMM, + query: query.QUERY_GET_REWARD_SUMM, variables: { conditionId: rewardConditionId, },