From 6b3c01cd84b654d443388458b4073464a0d542c2 Mon Sep 17 00:00:00 2001 From: Chika Date: Wed, 2 Nov 2022 18:11:23 +0300 Subject: [PATCH] get sums from kp --- graphql/crm.types.ts | 23 +++-- .../supplier-agent/get-kp-values/get-sums.ts | 97 +++++++++++++++++++ .../index.ts} | 50 ++++++++-- trpc/routers/quote.ts | 8 +- 4 files changed, 157 insertions(+), 21 deletions(-) create mode 100644 process/supplier-agent/get-kp-values/get-sums.ts rename process/supplier-agent/{get-data-from-kp.ts => get-kp-values/index.ts} (55%) diff --git a/graphql/crm.types.ts b/graphql/crm.types.ts index 6354d29..8bde883 100644 --- a/graphql/crm.types.ts +++ b/graphql/crm.types.ts @@ -238,12 +238,19 @@ export type GetCurrencyIsoCodeQueryVariables = Exact<{ export type GetCurrencyIsoCodeQuery = { __typename?: 'Query', transactioncurrency?: { __typename?: 'transactioncurrency', isocurrencycode?: string | null } | null }; +export type GetRewardRulesQueryVariables = Exact<{ + conditionId: Scalars['Uuid']; +}>; + + +export type GetRewardRulesQuery = { __typename?: 'Query', evo_reward_condition?: { __typename?: 'evo_reward_condition', evo_calc_reward_rules?: number | null } | null }; + export type GetAgentsDataFromQuoteQueryVariables = Exact<{ quoteId: Scalars['Uuid']; }>; -export type GetAgentsDataFromQuoteQuery = { __typename?: 'Query', quote?: { __typename?: 'quote', evo_supplier_accountid?: any | null, evo_dealer_person_accountid?: any | null, evo_dealer_reward_conditionid?: any | null, evo_dealer_reward_total?: any | null, evo_dealer_broker_accountid?: any | null, evo_dealer_broker_reward_conditionid?: any | null, evo_dealer_broker_reward_total?: any | null, evo_agent_accountid?: any | null, evo_agent_reward_conditionid?: any | null, evo_agent_reward_total?: any | null, evo_double_agent_accountid?: any | null, evo_double_agent_reward_conditionid?: any | null, evo_double_agent_reward_total?: any | null, evo_broker_accountid?: any | null, evo_broker_reward_conditionid?: any | null, evo_broker_reward_total?: any | null, evo_fin_department_accountid?: any | null, evo_fin_department_reward_conditionid?: any | null, evo_fin_department_reward_total?: any | null } | null }; +export type GetAgentsDataFromQuoteQuery = { __typename?: 'Query', quote?: { __typename?: 'quote', evo_supplier_accountid?: any | null, evo_dealer_person_accountid?: any | null, evo_dealer_reward_conditionid?: any | null, evo_dealer_reward_total?: any | null, evo_dealer_reward_summ?: any | null, evo_dealer_broker_accountid?: any | null, evo_dealer_broker_reward_conditionid?: any | null, evo_dealer_broker_reward_total?: any | null, evo_dealer_broker_reward_summ?: any | null, evo_agent_accountid?: any | null, evo_agent_reward_conditionid?: any | null, evo_agent_reward_total?: any | null, evo_agent_reward_summ?: any | null, evo_double_agent_accountid?: any | null, evo_double_agent_reward_conditionid?: any | null, evo_double_agent_reward_total?: any | null, evo_double_agent_reward_summ?: any | null, evo_broker_accountid?: any | null, evo_broker_reward_conditionid?: any | null, evo_broker_reward_total?: any | null, evo_broker_reward_summ?: any | null, evo_fin_department_accountid?: any | null, evo_fin_department_reward_conditionid?: any | null, evo_fin_department_reward_total?: any | null, evo_fin_department_reward_summ?: any | null } | null }; export type GetRewardConditionsQueryVariables = Exact<{ agentid: Scalars['Uuid']; @@ -260,13 +267,6 @@ export type GetRewardSummQueryVariables = Exact<{ export type GetRewardSummQuery = { __typename?: 'Query', evo_reward_condition?: { __typename?: 'evo_reward_condition', evo_reward_summ?: any | null } | null }; -export type GetRewardConditionQueryVariables = Exact<{ - conditionId: Scalars['Uuid']; -}>; - - -export type GetRewardConditionQuery = { __typename?: 'Query', evo_reward_condition?: { __typename?: 'evo_reward_condition', evo_reward_summ?: any | null, evo_reduce_reward?: boolean | null, evo_min_reward_summ?: any | null } | null }; - export type GetAgentAccountIdFromLeadQueryVariables = Exact<{ leadid: Scalars['Uuid']; }>; @@ -302,6 +302,13 @@ export type GetAgentQueryVariables = Exact<{ export type GetAgentQuery = { __typename?: 'Query', agent?: { __typename?: 'account', label?: string | null, value?: any | null } | null }; +export type GetRewardConditionQueryVariables = Exact<{ + conditionId: Scalars['Uuid']; +}>; + + +export type GetRewardConditionQuery = { __typename?: 'Query', evo_reward_condition?: { __typename?: 'evo_reward_condition', evo_reward_summ?: any | null, evo_reduce_reward?: boolean | null, evo_min_reward_summ?: any | null, evo_calc_reward_rules?: number | null } | null }; + export type GetRewardWithoutOtherAgentQueryVariables = Exact<{ conditionId: Scalars['Uuid']; }>; diff --git a/process/supplier-agent/get-kp-values/get-sums.ts b/process/supplier-agent/get-kp-values/get-sums.ts new file mode 100644 index 0000000..929dd8b --- /dev/null +++ b/process/supplier-agent/get-kp-values/get-sums.ts @@ -0,0 +1,97 @@ +/* eslint-disable @typescript-eslint/naming-convention */ +import { gql } from '@apollo/client'; +import initializeApollo from 'apollo/client'; +import type * as CRMTypes from 'graphql/crm.types'; +import type { Quote } from './index'; + +const QUERY_GET_REWARD_RULES = gql` + query GetRewardRules($conditionId: Uuid!) { + evo_reward_condition(evo_reward_conditionid: $conditionId) { + evo_calc_reward_rules + } + } +`; + +async function getRewardSum( + conditionId: string | null | undefined, + quote: Quote, + quoteRewardSummField: keyof Quote, + quoteRewardTotalField: keyof Quote +) { + if (!conditionId) return 0; + + const apolloClient = initializeApollo(); + const { + data: { evo_reward_condition }, + } = await apolloClient.query( + { + query: QUERY_GET_REWARD_RULES, + variables: { + conditionId, + }, + } + ); + + if (evo_reward_condition?.evo_calc_reward_rules === 100_000_001) { + return quote[quoteRewardSummField]; + } + + return quote[quoteRewardTotalField]; +} + +export default async function getSums(quote: Quote) { + const [ + dealerRewardSumm, + dealerBrokerRewardSumm, + indAgentRewardSumm, + calcDoubleAgentRewardSumm, + calcBrokerRewardSum, + finDepartmentRewardSumm, + ] = await Promise.all([ + getRewardSum( + quote?.evo_dealer_reward_conditionid, + quote, + 'evo_dealer_reward_summ', + 'evo_dealer_reward_total' + ), + getRewardSum( + quote?.evo_dealer_broker_reward_conditionid, + quote, + 'evo_dealer_broker_reward_summ', + 'evo_dealer_broker_reward_total' + ), + getRewardSum( + quote?.evo_agent_reward_conditionid, + quote, + 'evo_agent_reward_summ', + 'evo_agent_reward_total' + ), + getRewardSum( + quote.evo_double_agent_reward_conditionid, + quote, + 'evo_double_agent_reward_summ', + 'evo_double_agent_reward_total' + ), + getRewardSum( + quote.evo_broker_reward_conditionid, + quote, + 'evo_broker_reward_summ', + 'evo_broker_reward_total' + ), + getRewardSum( + quote.evo_fin_department_reward_conditionid, + quote, + 'evo_fin_department_reward_summ', + 'evo_fin_department_reward_total' + ), + ]); + + return { + dealerRewardSumm, + dealerBrokerRewardSumm, + indAgentRewardSumm, + calcDoubleAgentRewardSumm, + calcBrokerRewardSum, + finDepartmentRewardSumm, + }; +} diff --git a/process/supplier-agent/get-data-from-kp.ts b/process/supplier-agent/get-kp-values/index.ts similarity index 55% rename from process/supplier-agent/get-data-from-kp.ts rename to process/supplier-agent/get-kp-values/index.ts index fa0a71b..09d2d84 100644 --- a/process/supplier-agent/get-data-from-kp.ts +++ b/process/supplier-agent/get-kp-values/index.ts @@ -1,10 +1,8 @@ import { gql } from '@apollo/client'; import initializeApollo from 'apollo/client'; -import type { - GetAgentsDataFromQuoteQuery, - GetAgentsDataFromQuoteQueryVariables, -} from 'graphql/crm.types'; +import type * as CRMTypes from 'graphql/crm.types'; import type { CalculationValues } from 'stores/calculation/values/types'; +import getSums from './get-sums'; const QUERY_GET_AGENTS_DATA_FROM_QUOTE = gql` query GetAgentsDataFromQuote($quoteId: Uuid!) { @@ -13,55 +11,93 @@ const QUERY_GET_AGENTS_DATA_FROM_QUOTE = gql` evo_dealer_person_accountid evo_dealer_reward_conditionid evo_dealer_reward_total + evo_dealer_reward_summ evo_dealer_broker_accountid evo_dealer_broker_reward_conditionid evo_dealer_broker_reward_total + evo_dealer_broker_reward_summ evo_agent_accountid evo_agent_reward_conditionid evo_agent_reward_total + evo_agent_reward_summ evo_double_agent_accountid evo_double_agent_reward_conditionid evo_double_agent_reward_total + evo_double_agent_reward_summ evo_broker_accountid evo_broker_reward_conditionid evo_broker_reward_total + evo_broker_reward_summ evo_fin_department_accountid evo_fin_department_reward_conditionid evo_fin_department_reward_total + evo_fin_department_reward_summ } } `; +export type Quote = NonNullable; + type InputData = { quoteId: NonNullable; }; -type SupplierData = { +type ResultData = { values: Partial; }; export default async function getSupplierAgentsDataFromKP({ quoteId, -}: InputData): Promise { +}: InputData): Promise { const apolloClient = initializeApollo(); const { data: { quote }, - } = await apolloClient.query({ + } = await apolloClient.query< + CRMTypes.GetAgentsDataFromQuoteQuery, + CRMTypes.GetAgentsDataFromQuoteQueryVariables + >({ query: QUERY_GET_AGENTS_DATA_FROM_QUOTE, variables: { quoteId, }, }); + if (!quote) { + throw new Error('Quote is empty'); + } + + const { + dealerRewardSumm, + dealerBrokerRewardSumm, + indAgentRewardSumm, + calcDoubleAgentRewardSumm, + calcBrokerRewardSum, + finDepartmentRewardSumm, + } = await getSums(quote); + return { values: { dealer: quote?.evo_supplier_accountid, + dealerPerson: quote?.evo_dealer_person_accountid, dealerRewardCondition: quote?.evo_dealer_reward_conditionid, + dealerRewardSumm, dealerBroker: quote?.evo_dealer_broker_accountid, dealerBrokerRewardCondition: quote?.evo_dealer_broker_reward_conditionid, + dealerBrokerRewardSumm, + + indAgent: quote?.evo_agent_accountid, + indAgentRewardCondition: quote?.evo_agent_reward_conditionid, + indAgentRewardSumm, calcDoubleAgent: quote?.evo_double_agent_accountid, calcDoubleAgentRewardCondition: quote?.evo_double_agent_reward_conditionid, + calcDoubleAgentRewardSumm, + calcBroker: quote?.evo_broker_accountid, + calcBrokerRewardCondition: quote?.evo_broker_reward_conditionid, + calcBrokerRewardSum, + calcFinDepartment: quote?.evo_fin_department_accountid, + finDepartmentRewardCondtion: quote?.evo_fin_department_reward_conditionid, + finDepartmentRewardSumm, }, }; } diff --git a/trpc/routers/quote.ts b/trpc/routers/quote.ts index b0d2be4..b85ff3e 100644 --- a/trpc/routers/quote.ts +++ b/trpc/routers/quote.ts @@ -1,6 +1,6 @@ import defaultValues from 'config/default-values'; import ValuesSchema from 'config/schema/values'; -import getSupplierAgentsDataFromKP from 'process/supplier-agent/get-data-from-kp'; +import getSupplierAgentsDataFromKP from 'process/supplier-agent/get-kp-values'; import { z } from 'zod'; import { t } from '../server'; @@ -24,11 +24,7 @@ const quoteRouter = t.router({ }); return { - values: { - ...defaultValues, - ...input.values, - ...values, - }, + values: Object.assign(defaultValues, values), }; }), });