2022-11-14 21:33:18 +03:00

102 lines
3.3 KiB
TypeScript

import { gql } from '@apollo/client';
import initializeApollo from 'apollo/client';
import type * as CRMTypes from 'graphql/crm.types';
import { z } from 'zod';
import type { GetQuoteDataInput } from '../../load-kp/types';
import { GetQuoteDataOutputSchema } from '../../load-kp/types';
import getSums from './get-sums';
const QUERY_GET_AGENTS_DATA_FROM_QUOTE = gql`
query GetAgentsDataFromQuote($quoteId: Uuid!) {
quote(quoteId: $quoteId) {
evo_supplier_accountid
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<CRMTypes.GetAgentsDataFromQuoteQuery['quote']>;
const QuoteSupplierAgentProcessDataSchema = z.object({
values: GetQuoteDataOutputSchema.shape.values.partial(),
});
type QuoteSupplierAgentProcessData = z.infer<typeof QuoteSupplierAgentProcessDataSchema>;
export default async function getSupplierAgentsDataFromKP({
values: { quote: quoteId },
}: GetQuoteDataInput): Promise<QuoteSupplierAgentProcessData> {
const apolloClient = initializeApollo();
const {
data: { quote },
} = 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,
},
};
}