253 lines
7.3 KiB
TypeScript
253 lines
7.3 KiB
TypeScript
/* eslint-disable canonical/sort-keys */
|
|
import { protectedProcedure } from '../../procedure';
|
|
import { router } from '../../trpc';
|
|
import { validateResults } from '../calculate/lib/post-validation';
|
|
import { createRequestData } from '../calculate/lib/request';
|
|
import { transformCalculateResults } from '../calculate/lib/transform';
|
|
import { validate } from '../calculate/lib/validation';
|
|
import type { Context } from '../calculate/types';
|
|
import {
|
|
CreateQuoteInputDataSchema,
|
|
CreateQuoteOutputDataSchema,
|
|
GetQuoteInputDataSchema,
|
|
GetQuoteOutputDataSchema,
|
|
} from './types';
|
|
import { calculate } from '@/api/core/query';
|
|
import { createKP } from '@/api/crm/query';
|
|
import type { RequestCreateKP } from '@/api/crm/types';
|
|
import type { User } from '@/api/user/types';
|
|
import initializeApollo from '@/apollo/client';
|
|
import defaultValues from '@/config/default-values';
|
|
import {
|
|
DEFAULT_FINGAP_ROW,
|
|
DEFAULT_KASKO_ROW,
|
|
DEFAULT_OSAGO_ROW,
|
|
} from '@/config/tables/insurance-table';
|
|
import getUrls from '@/config/urls';
|
|
import * as CRMTypes from '@/graphql/crm.types';
|
|
import * as addProduct from '@/process/add-product';
|
|
import * as bonuses from '@/process/bonuses';
|
|
import * as configurator from '@/process/configurator';
|
|
import * as createKpProcess from '@/process/create-kp';
|
|
import * as eltProcess from '@/process/elt';
|
|
import * as fingapProcess from '@/process/fingap';
|
|
import * as gibdd from '@/process/gibdd';
|
|
import * as insuranceProcess from '@/process/insurance';
|
|
import * as leasingObject from '@/process/leasing-object';
|
|
import * as paymentsProcess from '@/process/payments';
|
|
import * as price from '@/process/price';
|
|
import * as subsidy from '@/process/subsidy';
|
|
import * as supplierAgent from '@/process/supplier-agent';
|
|
import type { CalculationValues } from '@/stores/calculation/values/types';
|
|
import { HttpError } from '@/utils/error';
|
|
import { createTRPCError } from '@/utils/trpc';
|
|
import { QueryClient } from '@tanstack/react-query';
|
|
|
|
const defaultInsurance = {
|
|
values: {
|
|
fingap: DEFAULT_FINGAP_ROW,
|
|
kasko: DEFAULT_KASKO_ROW,
|
|
osago: DEFAULT_OSAGO_ROW,
|
|
},
|
|
};
|
|
|
|
const defaultFingap = { keys: [], risks: [] };
|
|
const defaultPayments = { values: [], sums: [] };
|
|
|
|
const { URL_CRM_DOWNLOADKP } = getUrls();
|
|
|
|
export const quoteRouter = router({
|
|
getQuote: protectedProcedure
|
|
.input(GetQuoteInputDataSchema)
|
|
.output(GetQuoteOutputDataSchema)
|
|
.query(async ({ input, ctx }) => {
|
|
const { quote: quoteId } = input.values;
|
|
|
|
const apolloClient = initializeApollo(null, ctx.headers);
|
|
const queryClient = new QueryClient();
|
|
|
|
const {
|
|
data: { quote },
|
|
} = await apolloClient.query({
|
|
query: CRMTypes.GetQuoteDataDocument,
|
|
variables: {
|
|
quoteId,
|
|
},
|
|
});
|
|
|
|
const processData = await Promise.all(
|
|
[
|
|
configurator,
|
|
supplierAgent,
|
|
paymentsProcess,
|
|
price,
|
|
bonuses,
|
|
leasingObject,
|
|
fingapProcess,
|
|
gibdd,
|
|
subsidy,
|
|
insuranceProcess,
|
|
addProduct,
|
|
createKpProcess,
|
|
eltProcess,
|
|
].map(({ getKPData }) =>
|
|
getKPData({
|
|
...input,
|
|
quote,
|
|
ctx: {
|
|
apolloClient,
|
|
queryClient,
|
|
},
|
|
})
|
|
)
|
|
);
|
|
|
|
const values = processData.reduce((obj, data) => ({ ...obj, ...data.values }), defaultValues);
|
|
const payments = processData.find((x) => x.payments)?.payments ?? defaultPayments;
|
|
const insurance = processData.find((x) => x.insurance)?.insurance ?? defaultInsurance;
|
|
const fingap = processData.find((x) => x.fingap)?.fingap ?? defaultFingap;
|
|
const elt = processData.find((x) => x.elt)?.elt;
|
|
const options = processData[0].options;
|
|
|
|
return {
|
|
values,
|
|
payments,
|
|
insurance,
|
|
fingap,
|
|
elt,
|
|
options,
|
|
};
|
|
}),
|
|
|
|
createQuote: protectedProcedure
|
|
.input(CreateQuoteInputDataSchema)
|
|
.output(CreateQuoteOutputDataSchema)
|
|
.mutation(async ({ input, ctx }) => {
|
|
const apolloClient = initializeApollo(null, ctx.headers);
|
|
const queryClient = new QueryClient();
|
|
|
|
try {
|
|
if (!ctx.unlimited) {
|
|
const validationResult = await validate({
|
|
context: {
|
|
apolloClient,
|
|
queryClient,
|
|
user: ctx.user,
|
|
},
|
|
input,
|
|
});
|
|
|
|
if (validationResult.success === false) {
|
|
throw new HttpError(validationResult.message, 400);
|
|
}
|
|
}
|
|
|
|
let user: Pick<User, 'domainName'> = { domainName: ctx.user.domainName };
|
|
if (ctx.unlimited && input.values.user) {
|
|
user = { domainName: input.values.user };
|
|
}
|
|
|
|
const requestData = await createRequestData({
|
|
context: {
|
|
apolloClient,
|
|
queryClient,
|
|
...ctx,
|
|
user,
|
|
},
|
|
input,
|
|
});
|
|
|
|
const calculateResult = await calculate(requestData);
|
|
|
|
const postValidationResult = await validateResults({
|
|
calculateResult,
|
|
context: {
|
|
apolloClient,
|
|
queryClient,
|
|
user: ctx.user,
|
|
},
|
|
input,
|
|
});
|
|
|
|
if (postValidationResult.success === false) {
|
|
throw new HttpError(postValidationResult.message, 400);
|
|
}
|
|
|
|
const requestCreateKP = compatRequestCreateKP({
|
|
domainName: user.domainName,
|
|
finGAP: input.fingap,
|
|
insurance: Object.values(input.insurance.values),
|
|
calculation: {
|
|
calculationValues: await compatValues(input.values, { apolloClient }),
|
|
...calculateResult,
|
|
preparedPayments: requestData.preparedPayments,
|
|
additionalData: requestData.additionalData,
|
|
},
|
|
elt: input.elt,
|
|
});
|
|
|
|
const createKPResult = await createKP(requestCreateKP);
|
|
|
|
const result = transformCalculateResults({
|
|
calculateInput: input,
|
|
requestCalculate: requestData,
|
|
responseCalculate: calculateResult,
|
|
});
|
|
|
|
if (URL_CRM_DOWNLOADKP) {
|
|
result.values.downloadKp = URL_CRM_DOWNLOADKP?.concat(createKPResult.offerprintformapi);
|
|
}
|
|
|
|
return {
|
|
data: result,
|
|
success: true,
|
|
};
|
|
} catch (error) {
|
|
throw createTRPCError(error);
|
|
}
|
|
}),
|
|
});
|
|
|
|
function compatRequestCreateKP(request: RequestCreateKP) {
|
|
const fingapIndex = request.insurance.findIndex((x) => x.key === 'fingap');
|
|
if (fingapIndex >= 0) {
|
|
request.insurance[fingapIndex].key = 'finGAP';
|
|
}
|
|
|
|
return request;
|
|
}
|
|
|
|
async function compatValues(
|
|
values: CalculationValues,
|
|
{ apolloClient }: Pick<Context, 'apolloClient'>
|
|
) {
|
|
let product = null;
|
|
if (values.product) {
|
|
const {
|
|
data: { evo_baseproduct },
|
|
} = await apolloClient.query({
|
|
query: CRMTypes.GetProductDocument,
|
|
variables: {
|
|
productId: values.product,
|
|
},
|
|
});
|
|
if (evo_baseproduct?.evo_id) product = evo_baseproduct?.evo_id;
|
|
}
|
|
|
|
let rate = null;
|
|
if (values.rate) {
|
|
const {
|
|
data: { evo_rate },
|
|
} = await apolloClient.query({
|
|
fetchPolicy: 'network-only',
|
|
query: CRMTypes.GetRateDocument,
|
|
variables: {
|
|
rateId: values.rate,
|
|
},
|
|
});
|
|
if (evo_rate?.evo_id) rate = evo_rate?.evo_id;
|
|
}
|
|
|
|
return { ...values, product, rate };
|
|
}
|