From 378d1b408d1991e8e79a4a1422cd02bff5af2855 Mon Sep 17 00:00:00 2001 From: Chika Date: Thu, 14 Jul 2022 16:18:40 +0300 Subject: [PATCH] process/agents: generic function fillAgent --- process/agents/lib/__generated__/GetAgent.ts | 2 +- .../lib/__generated__/GetAgentAccountId.ts | 2 +- .../lib/__generated__/GetDoubleAgent.ts | 6 +- .../__generated__/GetDoubleAgentAccountId.ts | 2 +- process/agents/lib/fill-agents-from-lead.ts | 172 ++++++++---------- 5 files changed, 77 insertions(+), 107 deletions(-) diff --git a/process/agents/lib/__generated__/GetAgent.ts b/process/agents/lib/__generated__/GetAgent.ts index b57e5d1..dd8cf7a 100644 --- a/process/agents/lib/__generated__/GetAgent.ts +++ b/process/agents/lib/__generated__/GetAgent.ts @@ -18,5 +18,5 @@ export interface GetAgent { } export interface GetAgentVariables { - evo_agent_accountid: any; + agentid: any; } diff --git a/process/agents/lib/__generated__/GetAgentAccountId.ts b/process/agents/lib/__generated__/GetAgentAccountId.ts index 2ddade7..f97a2f7 100644 --- a/process/agents/lib/__generated__/GetAgentAccountId.ts +++ b/process/agents/lib/__generated__/GetAgentAccountId.ts @@ -9,7 +9,7 @@ export interface GetAgentAccountId_lead { __typename: "lead"; - evo_agent_accountid: any | null; + agentid: any | null; } export interface GetAgentAccountId { diff --git a/process/agents/lib/__generated__/GetDoubleAgent.ts b/process/agents/lib/__generated__/GetDoubleAgent.ts index 7fb5278..7f5f1c6 100644 --- a/process/agents/lib/__generated__/GetDoubleAgent.ts +++ b/process/agents/lib/__generated__/GetDoubleAgent.ts @@ -7,16 +7,16 @@ // GraphQL query operation: GetDoubleAgent // ==================================================== -export interface GetDoubleAgent_doubleAgent { +export interface GetDoubleAgent_agent { __typename: "account"; label: string | null; value: any | null; } export interface GetDoubleAgent { - doubleAgent: GetDoubleAgent_doubleAgent | null; + agent: GetDoubleAgent_agent | null; } export interface GetDoubleAgentVariables { - evo_double_agent_accountid: any; + agentid: any; } diff --git a/process/agents/lib/__generated__/GetDoubleAgentAccountId.ts b/process/agents/lib/__generated__/GetDoubleAgentAccountId.ts index 95c9b76..dd4d913 100644 --- a/process/agents/lib/__generated__/GetDoubleAgentAccountId.ts +++ b/process/agents/lib/__generated__/GetDoubleAgentAccountId.ts @@ -9,7 +9,7 @@ export interface GetDoubleAgentAccountId_lead { __typename: "lead"; - evo_double_agent_accountid: any | null; + agentid: any | null; } export interface GetDoubleAgentAccountId { diff --git a/process/agents/lib/fill-agents-from-lead.ts b/process/agents/lib/fill-agents-from-lead.ts index d81cd4d..35f4693 100644 --- a/process/agents/lib/fill-agents-from-lead.ts +++ b/process/agents/lib/fill-agents-from-lead.ts @@ -1,12 +1,54 @@ /* eslint-disable import/prefer-default-export */ -import type { ApolloClient } from '@apollo/client'; +import type { ApolloClient, DocumentNode } from '@apollo/client'; import { gql } from '@apollo/client'; +import type { Elements } from 'Components/Calculation/config/map/values'; import type RootStore from 'stores/root'; import { normalizeOptions } from 'tools/entity'; -import type { GetAgent } from './__generated__/GetAgent'; -import type { GetAgentAccountId } from './__generated__/GetAgentAccountId'; -import type { GetDoubleAgent } from './__generated__/GetDoubleAgent'; -import type { GetDoubleAgentAccountId } from './__generated__/GetDoubleAgentAccountId'; + +function fillAgent( + elementName: Elements, + queryGetAgentId: DocumentNode, + queryGetAgent: DocumentNode +) { + return async function ( + { $calculation }: RootStore, + apolloClient: ApolloClient, + leadid: string | null + ) { + if (!leadid) { + $calculation.resetElement(elementName); + + return; + } + + const { + data: { lead }, + } = await apolloClient.query({ + query: queryGetAgentId, + variables: { + leadid, + }, + }); + + if (lead?.agentid) { + const { + data: { agent }, + } = await apolloClient.query({ + query: queryGetAgent, + variables: { + agentid: lead.agentid, + }, + }); + + if (agent) { + $calculation.$options.setElementOptions(elementName, normalizeOptions([agent])); + $calculation.setElementValue(elementName, agent.value); + } + } else { + $calculation.resetElement(elementName); + } + }; +} /** * Если lead содержит данные, @@ -17,75 +59,21 @@ import type { GetDoubleAgentAccountId } from './__generated__/GetDoubleAgentAcco const QUERY_GET_AGENT_ACCOUNTID = gql` query GetAgentAccountId($leadid: Uuid!) { lead(leadid: $leadid) { - evo_agent_accountid + agentid: evo_agent_accountid } } `; const QUERY_GET_AGENT = gql` - query GetAgent($evo_agent_accountid: Uuid!) { - agent: account(accountid: $evo_agent_accountid) { + query GetAgent($agentid: Uuid!) { + agent: account(accountid: $agentid) { label: name value: accountid } } `; -export async function fillIndAgent( - { $calculation }: RootStore, - apolloClient: ApolloClient, - leadid: string | null -) { - if (!leadid) { - $calculation.resetElement('selectIndAgent'); - - return; - } - - const { - data: { lead }, - } = await apolloClient.query({ - query: QUERY_GET_AGENT_ACCOUNTID, - variables: { - leadid, - }, - }); - - if (lead?.evo_agent_accountid) { - const { - data: { agent }, - } = await apolloClient.query({ - query: QUERY_GET_AGENT, - variables: { - evo_agent_accountid: lead.evo_agent_accountid, - }, - }); - - if (agent) { - $calculation.$options.setElementOptions('selectIndAgent', normalizeOptions([agent])); - $calculation.setElementValue('selectIndAgent', agent.value); - } - } else { - $calculation.resetElement('selectIndAgent'); - } -} - -const QUERY_GET_DOUBLE_AGENT_ACCOUNTID = gql` - query GetDoubleAgentAccountId($leadid: Uuid!) { - lead(leadid: $leadid) { - evo_double_agent_accountid - } - } -`; - -const QUERY_GET_DOUBLE_AGENT = gql` - query GetDoubleAgent($evo_double_agent_accountid: Uuid!) { - doubleAgent: account(accountid: $evo_double_agent_accountid) { - label: name - value: accountid - } - } -`; +export const fillIndAgent = fillAgent('selectIndAgent', QUERY_GET_AGENT_ACCOUNTID, QUERY_GET_AGENT); /** * Если lead содержит данные, @@ -93,44 +81,26 @@ const QUERY_GET_DOUBLE_AGENT = gql` * записанную в поле Интереса "Двойной агент" (lead.evo_double_agent_accountid → account) * иначе очищать поле калькулятора calcDoubleAgent */ -export async function fillCalcDoubleAgent( - { $calculation }: RootStore, - apolloClient: ApolloClient, - leadid: string | null -) { - if (!leadid) { - $calculation.resetElement('selectCalcDoubleAgent'); - return; - } - - const { - data: { lead }, - } = await apolloClient.query({ - query: QUERY_GET_DOUBLE_AGENT_ACCOUNTID, - variables: { - leadid, - }, - }); - - if (lead?.evo_double_agent_accountid) { - const { - data: { doubleAgent }, - } = await apolloClient.query({ - query: QUERY_GET_DOUBLE_AGENT, - variables: { - evo_double_agent_accountid: lead.evo_double_agent_accountid, - }, - }); - - if (doubleAgent) { - $calculation.$options.setElementOptions( - 'selectCalcDoubleAgent', - normalizeOptions([doubleAgent]) - ); - $calculation.setElementValue('selectCalcDoubleAgent', doubleAgent.value); +const QUERY_GET_DOUBLE_AGENT_ACCOUNTID = gql` + query GetDoubleAgentAccountId($leadid: Uuid!) { + lead(leadid: $leadid) { + agentid: evo_double_agent_accountid } - } else { - $calculation.resetElement('selectCalcDoubleAgent'); } -} +`; + +const QUERY_GET_DOUBLE_AGENT = gql` + query GetDoubleAgent($agentid: Uuid!) { + agent: account(accountid: $agentid) { + label: name + value: accountid + } + } +`; + +export const fillCalcDoubleAgent = fillAgent( + 'selectCalcDoubleAgent', + QUERY_GET_DOUBLE_AGENT_ACCOUNTID, + QUERY_GET_DOUBLE_AGENT +);