passthrough apolloClient & queryClient to get-kp-data's
This commit is contained in:
parent
f63a6ac96b
commit
95b6dec872
@ -1,14 +1,12 @@
|
||||
import type { GetQuoteInputData, GetQuoteProcessData } from '../load-kp/types';
|
||||
import initializeApollo from '@/apollo/client';
|
||||
import defaultValues from '@/config/default-values';
|
||||
import * as CRMTypes from '@/graphql/crm.types';
|
||||
|
||||
export async function getKPData({
|
||||
values: { recalcWithRevision },
|
||||
quote,
|
||||
ctx: { apolloClient },
|
||||
}: GetQuoteInputData): Promise<GetQuoteProcessData> {
|
||||
const apolloClient = initializeApollo();
|
||||
|
||||
const registration =
|
||||
quote?.evo_addproduct_types?.find((x) => x?.evo_product_type === 100_000_001)
|
||||
?.evo_addproduct_typeid ?? defaultValues.registration;
|
||||
|
||||
@ -2,7 +2,6 @@
|
||||
/* eslint-disable complexity */
|
||||
import type { GetQuoteInputData, GetQuoteProcessData } from '../load-kp/types';
|
||||
import helper from './lib/helper';
|
||||
import initializeApollo from '@/apollo/client';
|
||||
import defaultValues from '@/config/default-values';
|
||||
import calculateHelper from '@/process/calculate/lib/helper';
|
||||
import { getKPData as getKPDataPrice } from '@/process/price/get-kp-data';
|
||||
@ -13,9 +12,10 @@ import { normalizeOptions } from '@/utils/entity';
|
||||
export async function getKPData({
|
||||
values,
|
||||
quote,
|
||||
ctx,
|
||||
}: GetQuoteInputData): Promise<GetQuoteProcessData> {
|
||||
const { apolloClient } = ctx;
|
||||
const { recalcWithRevision } = values;
|
||||
const apolloClient = initializeApollo();
|
||||
|
||||
const leasingPeriod = recalcWithRevision
|
||||
? Math.min(quote?.evo_period ?? 0, quote?.evo_accept_period ?? 0)
|
||||
@ -61,8 +61,8 @@ export async function getKPData({
|
||||
let minPriceChange = quote?.evo_min_change_price ?? defaultValues.minPriceChange;
|
||||
|
||||
if (!recalcWithRevision) {
|
||||
const kpDataPrice = await getKPDataPrice({ quote, values });
|
||||
const kpDataSubsidy = await getKPDataSubsidy({ quote, values });
|
||||
const kpDataPrice = await getKPDataPrice({ ctx, quote, values });
|
||||
const kpDataSubsidy = await getKPDataSubsidy({ ctx, quote, values });
|
||||
|
||||
const supplierCurrency = kpDataPrice.values?.supplierCurrency ?? defaultValues.supplierCurrency;
|
||||
const leaseObjectPrice = kpDataPrice.values?.leaseObjectPrice ?? defaultValues.leaseObjectPrice;
|
||||
|
||||
@ -1,17 +1,18 @@
|
||||
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
||||
import type { GetQuoteInputData, GetQuoteProcessData } from '../load-kp/types';
|
||||
import helper from './lib/helper';
|
||||
import initializeApollo from '@/apollo/client';
|
||||
import defaultValues from '@/config/default-values';
|
||||
import { getKPData as getKPDataPayments } from '@/process/payments/get-kp-data';
|
||||
import { getKPData as getKPDataPrice } from '@/process/price/get-kp-data';
|
||||
import { createCurrencyUtility } from '@/utils/currency';
|
||||
import { QueryClient } from '@tanstack/react-query';
|
||||
|
||||
export async function getKPData({
|
||||
quote,
|
||||
values,
|
||||
ctx,
|
||||
}: GetQuoteInputData): Promise<GetQuoteProcessData> {
|
||||
const { apolloClient, queryClient } = ctx;
|
||||
|
||||
if (!quote?.evo_fingap_accountid) {
|
||||
return {
|
||||
fingap: {
|
||||
@ -21,13 +22,10 @@ export async function getKPData({
|
||||
};
|
||||
}
|
||||
|
||||
const apolloClient = initializeApollo();
|
||||
const queryClient = new QueryClient();
|
||||
|
||||
const { getFingapRisks } = helper({ apolloClient, queryClient });
|
||||
|
||||
const kpDataPrice = await getKPDataPrice({ quote, values });
|
||||
const kpDataPayments = await getKPDataPayments({ quote, values });
|
||||
const kpDataPrice = await getKPDataPrice({ ctx, quote, values });
|
||||
const kpDataPayments = await getKPDataPayments({ ctx, quote, values });
|
||||
|
||||
const leaseObjectPrice = kpDataPrice.values?.leaseObjectPrice ?? defaultValues.leaseObjectPrice;
|
||||
const firstPaymentRub = kpDataPrice.values?.firstPaymentRub ?? defaultValues.firstPaymentRub;
|
||||
|
||||
@ -1,17 +1,15 @@
|
||||
/* eslint-disable sonarjs/cognitive-complexity */
|
||||
import type { GetQuoteInputData, GetQuoteProcessData } from '../load-kp/types';
|
||||
import helper from './lib/helper';
|
||||
import initializeApollo from '@/apollo/client';
|
||||
import defaultValues from '@/config/default-values';
|
||||
|
||||
export async function getKPData({
|
||||
values,
|
||||
quote,
|
||||
ctx: { apolloClient },
|
||||
}: GetQuoteInputData): Promise<GetQuoteProcessData> {
|
||||
const { recalcWithRevision, lead: leadId, opportunity: opportunityId } = values;
|
||||
|
||||
const apolloClient = initializeApollo();
|
||||
|
||||
const { getData } = helper({
|
||||
apolloClient,
|
||||
});
|
||||
|
||||
@ -11,8 +11,9 @@ import { sum } from 'radash';
|
||||
export async function getKPData({
|
||||
values,
|
||||
quote,
|
||||
ctx,
|
||||
}: GetQuoteInputData): Promise<GetQuoteProcessData> {
|
||||
const kpDataFingap = await getKPDataFingap({ quote, values });
|
||||
const kpDataFingap = await getKPDataFingap({ ctx, quote, values });
|
||||
|
||||
const risks = kpDataFingap.fingap?.risks;
|
||||
|
||||
|
||||
@ -1,14 +1,12 @@
|
||||
import type { GetQuoteInputData, GetQuoteProcessData } from '../load-kp/types';
|
||||
import helper from './lib/helper';
|
||||
import initializeApollo from '@/apollo/client';
|
||||
import defaultValues from '@/config/default-values';
|
||||
|
||||
export async function getKPData({
|
||||
values: { recalcWithRevision },
|
||||
quote,
|
||||
ctx: { apolloClient },
|
||||
}: GetQuoteInputData): Promise<GetQuoteProcessData> {
|
||||
const apolloClient = initializeApollo();
|
||||
|
||||
const brand = quote?.evo_brandid ?? defaultValues.brand;
|
||||
const model = quote?.evo_modelid ?? defaultValues.model;
|
||||
const leaseObjectUsed = quote?.evo_leasingobject_used ?? defaultValues.leaseObjectUsed;
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
import type { GetQuoteInputData, GetQuoteProcessData } from '../load-kp/types';
|
||||
import helper from './lib/helper';
|
||||
import initializeApollo from '@/apollo/client';
|
||||
import defaultValues from '@/config/default-values';
|
||||
import { getKPData as getKPDataPrice } from '@/process/price/get-kp-data';
|
||||
import { createCurrencyUtility } from '@/utils/currency';
|
||||
@ -8,10 +7,11 @@ import { createCurrencyUtility } from '@/utils/currency';
|
||||
export async function getKPData({
|
||||
values,
|
||||
quote,
|
||||
ctx,
|
||||
}: GetQuoteInputData): Promise<GetQuoteProcessData> {
|
||||
const apolloClient = initializeApollo();
|
||||
const { apolloClient } = ctx;
|
||||
|
||||
const kpDataPrice = await getKPDataPrice({ quote, values });
|
||||
const kpDataPrice = await getKPDataPrice({ ctx, quote, values });
|
||||
const { RUB } = createCurrencyUtility({ apolloClient });
|
||||
|
||||
const supplierCurrency = kpDataPrice.values?.supplierCurrency ?? defaultValues.supplierCurrency;
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
import initializeApollo from '@/apollo/client';
|
||||
import * as CRMTypes from '@/graphql/crm.types';
|
||||
import type { GetQuoteContext } from '@/server/routers/quote/types';
|
||||
|
||||
type Quote = NonNullable<CRMTypes.GetQuoteDataQuery['quote']>;
|
||||
|
||||
@ -19,39 +19,42 @@ type QuoteTotalFields =
|
||||
| 'evo_double_agent_reward_total'
|
||||
| 'evo_fin_department_reward_total';
|
||||
|
||||
async function getRewardSum(
|
||||
conditionId: string | null | undefined,
|
||||
quote: Quote,
|
||||
quoteRewardSummField: keyof Pick<Quote, QuoteSumFields>,
|
||||
quoteRewardTotalField: keyof Pick<Quote, QuoteTotalFields>
|
||||
) {
|
||||
if (!conditionId) return 0;
|
||||
function makeGetRewardSum({ apolloClient }: GetQuoteContext) {
|
||||
return async function (
|
||||
conditionId: string | null | undefined,
|
||||
quote: Quote,
|
||||
quoteRewardSummField: keyof Pick<Quote, QuoteSumFields>,
|
||||
quoteRewardTotalField: keyof Pick<Quote, QuoteTotalFields>
|
||||
) {
|
||||
if (!conditionId) return 0;
|
||||
|
||||
const apolloClient = initializeApollo();
|
||||
const {
|
||||
data: { evo_reward_condition },
|
||||
} = await apolloClient.query<
|
||||
CRMTypes.GetRewardConditionQuery,
|
||||
CRMTypes.GetRewardConditionQueryVariables
|
||||
>({
|
||||
query: CRMTypes.GetRewardConditionDocument,
|
||||
variables: {
|
||||
conditionId,
|
||||
},
|
||||
});
|
||||
const {
|
||||
data: { evo_reward_condition },
|
||||
} = await apolloClient.query<
|
||||
CRMTypes.GetRewardConditionQuery,
|
||||
CRMTypes.GetRewardConditionQueryVariables
|
||||
>({
|
||||
query: CRMTypes.GetRewardConditionDocument,
|
||||
variables: {
|
||||
conditionId,
|
||||
},
|
||||
});
|
||||
|
||||
if (evo_reward_condition?.evo_calc_reward_rules === 100_000_001) {
|
||||
return quote[quoteRewardSummField];
|
||||
}
|
||||
if (evo_reward_condition?.evo_calc_reward_rules === 100_000_001) {
|
||||
return quote[quoteRewardSummField];
|
||||
}
|
||||
|
||||
return quote[quoteRewardTotalField];
|
||||
return quote[quoteRewardTotalField];
|
||||
};
|
||||
}
|
||||
|
||||
export default async function getSums(quote: Quote | null) {
|
||||
export default async function getSums(quote: Quote | null, ctx: GetQuoteContext) {
|
||||
if (!quote) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const getRewardSum = makeGetRewardSum(ctx);
|
||||
|
||||
const [
|
||||
dealerRewardSumm,
|
||||
dealerBrokerRewardSumm,
|
||||
|
||||
@ -3,8 +3,8 @@ import type { GetQuoteInputData, GetQuoteProcessData } from '../../load-kp/types
|
||||
import getSums from './get-sums';
|
||||
import defaultValues from '@/config/default-values';
|
||||
|
||||
export async function getKPData({ quote }: GetQuoteInputData): Promise<GetQuoteProcessData> {
|
||||
const sums = await getSums(quote);
|
||||
export async function getKPData({ quote, ctx }: GetQuoteInputData): Promise<GetQuoteProcessData> {
|
||||
const sums = await getSums(quote, ctx);
|
||||
|
||||
return {
|
||||
values: {
|
||||
|
||||
@ -16,7 +16,7 @@ export const calculateRouter = router({
|
||||
.input(CalculateInputSchema)
|
||||
.output(CalculateOutputSchema)
|
||||
.mutation(async ({ input, ctx }) => {
|
||||
const apolloClient = initializeApollo();
|
||||
const apolloClient = initializeApollo(null, ctx.headers);
|
||||
const queryClient = new QueryClient();
|
||||
|
||||
try {
|
||||
|
||||
@ -59,10 +59,12 @@ export const quoteRouter = router({
|
||||
getQuote: protectedProcedure
|
||||
.input(GetQuoteInputDataSchema)
|
||||
.output(GetQuoteOutputDataSchema)
|
||||
.query(async ({ input }) => {
|
||||
.query(async ({ input, ctx }) => {
|
||||
const { quote: quoteId } = input.values;
|
||||
|
||||
const apolloClient = initializeApollo();
|
||||
const apolloClient = initializeApollo(null, ctx.headers);
|
||||
const queryClient = new QueryClient();
|
||||
|
||||
const {
|
||||
data: { quote },
|
||||
} = await apolloClient.query({
|
||||
@ -87,7 +89,16 @@ export const quoteRouter = router({
|
||||
addProduct,
|
||||
createKpProcess,
|
||||
eltProcess,
|
||||
].map(({ getKPData }) => getKPData({ ...input, quote }))
|
||||
].map(({ getKPData }) =>
|
||||
getKPData({
|
||||
...input,
|
||||
quote,
|
||||
ctx: {
|
||||
apolloClient,
|
||||
queryClient,
|
||||
},
|
||||
})
|
||||
)
|
||||
);
|
||||
|
||||
const values = processData.reduce((obj, data) => ({ ...obj, ...data.values }), defaultValues);
|
||||
@ -111,7 +122,7 @@ export const quoteRouter = router({
|
||||
.input(CreateQuoteInputDataSchema)
|
||||
.output(CreateQuoteOutputDataSchema)
|
||||
.mutation(async ({ input, ctx }) => {
|
||||
const apolloClient = initializeApollo();
|
||||
const apolloClient = initializeApollo(null, ctx.headers);
|
||||
const queryClient = new QueryClient();
|
||||
|
||||
try {
|
||||
|
||||
@ -5,6 +5,8 @@ import { KeysSchema, RowSchema } from '@/config/schema/insurance';
|
||||
import PaymentsSchema from '@/config/schema/payments';
|
||||
import ValuesSchema from '@/config/schema/values';
|
||||
import type * as CRMTypes from '@/graphql/crm.types';
|
||||
import type { ApolloClient, NormalizedCacheObject } from '@apollo/client';
|
||||
import type { QueryClient } from '@tanstack/react-query';
|
||||
import { z } from 'zod';
|
||||
|
||||
const { quote, recalcWithRevision, lead, opportunity } = ValuesSchema.shape;
|
||||
@ -22,7 +24,13 @@ export const GetQuoteInputDataSchema = z
|
||||
})
|
||||
.strict();
|
||||
|
||||
export type GetQuoteContext = {
|
||||
apolloClient: ApolloClient<NormalizedCacheObject>;
|
||||
queryClient: QueryClient;
|
||||
};
|
||||
|
||||
export type GetQuoteInputData = z.infer<typeof GetQuoteInputDataSchema> & {
|
||||
ctx: GetQuoteContext;
|
||||
quote: CRMTypes.GetQuoteDataQuery['quote'];
|
||||
};
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user