process/agents: generic function fillAgent

This commit is contained in:
Chika 2022-07-14 16:18:40 +03:00
parent efa9002c8b
commit 378d1b408d
5 changed files with 77 additions and 107 deletions

View File

@ -18,5 +18,5 @@ export interface GetAgent {
}
export interface GetAgentVariables {
evo_agent_accountid: any;
agentid: any;
}

View File

@ -9,7 +9,7 @@
export interface GetAgentAccountId_lead {
__typename: "lead";
evo_agent_accountid: any | null;
agentid: any | null;
}
export interface GetAgentAccountId {

View File

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

View File

@ -9,7 +9,7 @@
export interface GetDoubleAgentAccountId_lead {
__typename: "lead";
evo_double_agent_accountid: any | null;
agentid: any | null;
}
export interface GetDoubleAgentAccountId {

View File

@ -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<object>,
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<object>,
leadid: string | null
) {
if (!leadid) {
$calculation.resetElement('selectIndAgent');
return;
}
const {
data: { lead },
} = await apolloClient.query<GetAgentAccountId>({
query: QUERY_GET_AGENT_ACCOUNTID,
variables: {
leadid,
},
});
if (lead?.evo_agent_accountid) {
const {
data: { agent },
} = await apolloClient.query<GetAgent>({
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<object>,
leadid: string | null
) {
if (!leadid) {
$calculation.resetElement('selectCalcDoubleAgent');
return;
}
const {
data: { lead },
} = await apolloClient.query<GetDoubleAgentAccountId>({
query: QUERY_GET_DOUBLE_AGENT_ACCOUNTID,
variables: {
leadid,
},
});
if (lead?.evo_double_agent_accountid) {
const {
data: { doubleAgent },
} = await apolloClient.query<GetDoubleAgent>({
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
);