diff --git a/pages/index.tsx b/pages/index.tsx index 0f0c26f..35de6bb 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -8,6 +8,7 @@ import * as InsuranceTableConfig from 'config/tables/insurance-table'; import { merge } from 'lodash-es'; import type { GetServerSideProps } from 'next'; import Head from 'next/head'; +import * as agentsReactions from 'process/agents/reactions'; import * as calculateReactions from 'process/calculate/reactions'; import { getCRMData } from 'process/init/get-data'; import * as leadOpportunityReactions from 'process/lead-opportunity/reactions'; @@ -55,6 +56,7 @@ function Home() { leadOpportunityReactions.urls(store, apolloClient); paymentsReactions(store, apolloClient); calculateReactions.validation(store, apolloClient); + agentsReactions.common(store, apolloClient); }); return ( diff --git a/process/agents/lib/__generated__/GetAgent.ts b/process/agents/lib/__generated__/GetAgent.ts new file mode 100644 index 0000000..b57e5d1 --- /dev/null +++ b/process/agents/lib/__generated__/GetAgent.ts @@ -0,0 +1,22 @@ +/* tslint:disable */ +/* eslint-disable */ +// @generated +// This file was automatically generated and should not be edited. + +// ==================================================== +// GraphQL query operation: GetAgent +// ==================================================== + +export interface GetAgent_agent { + __typename: "account"; + label: string | null; + value: any | null; +} + +export interface GetAgent { + agent: GetAgent_agent | null; +} + +export interface GetAgentVariables { + evo_agent_accountid: any; +} diff --git a/process/agents/lib/__generated__/GetAgentAccountId.ts b/process/agents/lib/__generated__/GetAgentAccountId.ts new file mode 100644 index 0000000..2ddade7 --- /dev/null +++ b/process/agents/lib/__generated__/GetAgentAccountId.ts @@ -0,0 +1,24 @@ +/* tslint:disable */ +/* eslint-disable */ +// @generated +// This file was automatically generated and should not be edited. + +// ==================================================== +// GraphQL query operation: GetAgentAccountId +// ==================================================== + +export interface GetAgentAccountId_lead { + __typename: "lead"; + evo_agent_accountid: any | null; +} + +export interface GetAgentAccountId { + /** + * Лизинговые сделки. statecode по умолчанию 0, можно отфильтровать по ownerid и/или domainName + */ + lead: GetAgentAccountId_lead | null; +} + +export interface GetAgentAccountIdVariables { + leadid: any; +} diff --git a/process/agents/lib/fill-agents-from-lead.ts b/process/agents/lib/fill-agents-from-lead.ts new file mode 100644 index 0000000..a717ea8 --- /dev/null +++ b/process/agents/lib/fill-agents-from-lead.ts @@ -0,0 +1,63 @@ +/* eslint-disable import/prefer-default-export */ +import type { ApolloClient } from '@apollo/client'; +import { gql } from '@apollo/client'; +import type RootStore from 'stores/root'; +import { normalizeOptions } from 'tools/entity'; +import type { GetAgent } from './__generated__/GetAgent'; +import type { GetAgentAccountId } from './__generated__/GetAgentAccountId'; + +const QUERY_GET_AGENT_ACCOUNTID = gql` + query GetAgentAccountId($leadid: Uuid!) { + lead(leadid: $leadid) { + evo_agent_accountid + } + } +`; + +const QUERY_GET_AGENT = gql` + query GetAgent($evo_agent_accountid: Uuid!) { + agent: account(accountid: $evo_agent_accountid) { + 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'); + } +} diff --git a/process/agents/reactions/common.ts b/process/agents/reactions/common.ts new file mode 100644 index 0000000..d968d48 --- /dev/null +++ b/process/agents/reactions/common.ts @@ -0,0 +1,14 @@ +import type { ApolloClient } from '@apollo/client'; +import { reaction } from 'mobx'; +import type RootStore from 'stores/root'; +import { fillIndAgent } from '../lib/fill-agents-from-lead'; + +export default function commonReactions(store: RootStore, apolloClient: ApolloClient) { + const { $calculation } = store; + reaction( + () => $calculation.getElementValue('selectLead'), + (leadid) => { + fillIndAgent(store, apolloClient, leadid); + } + ); +} diff --git a/process/agents/reactions/index.js b/process/agents/reactions/index.js new file mode 100644 index 0000000..1e2e8e6 --- /dev/null +++ b/process/agents/reactions/index.js @@ -0,0 +1,2 @@ +/* eslint-disable import/prefer-default-export */ +export { default as common } from './common';