api/core, api/crm: try/catch improvements
This commit is contained in:
parent
d9d30ac45d
commit
8c137229b8
@ -1,21 +1,23 @@
|
||||
import type { RequestCalculate, RequestFinGAP, ResponseCalculate, ResponseFinGAP } from './types';
|
||||
import getUrls from '@/config/urls';
|
||||
import { withHandleError } from '@/utils/axios';
|
||||
import type { QueryFunctionContext } from '@tanstack/react-query';
|
||||
import axios from 'axios';
|
||||
|
||||
const { URL_CORE_FINGAP, URL_CORE_CALCULATE } = getUrls();
|
||||
|
||||
export async function calculateFinGAP(payload: RequestFinGAP, { signal }: QueryFunctionContext) {
|
||||
const { data } = await axios.post<ResponseFinGAP>(URL_CORE_FINGAP, payload, {
|
||||
signal,
|
||||
});
|
||||
|
||||
return data;
|
||||
return withHandleError(
|
||||
axios
|
||||
.post<ResponseFinGAP>(URL_CORE_FINGAP, payload, {
|
||||
signal,
|
||||
})
|
||||
.then(({ data }) => data)
|
||||
);
|
||||
}
|
||||
|
||||
export async function calculate(payload: RequestCalculate): Promise<ResponseCalculate> {
|
||||
return await axios
|
||||
.post<ResponseCalculate>(URL_CORE_CALCULATE, payload)
|
||||
.then((response) => response.data)
|
||||
.catch((error) => error.response.data);
|
||||
export async function calculate(payload: RequestCalculate) {
|
||||
return withHandleError(
|
||||
axios.post<ResponseCalculate>(URL_CORE_CALCULATE, payload).then(({ data }) => data)
|
||||
);
|
||||
}
|
||||
|
||||
@ -1,12 +1,12 @@
|
||||
import type { RequestCreateKP, ResponseCreateKP } from './types';
|
||||
import getUrls from '@/config/urls';
|
||||
import { withHandleError } from '@/utils/axios';
|
||||
import axios from 'axios';
|
||||
|
||||
const { URL_CRM_CREATEKP } = getUrls();
|
||||
|
||||
export async function createKP(payload: RequestCreateKP): Promise<ResponseCreateKP> {
|
||||
return await axios
|
||||
.post(URL_CRM_CREATEKP, payload)
|
||||
.then((response) => ({ ...response.data, success: true }))
|
||||
.catch((error) => ({ ...error.response.data, success: false }));
|
||||
export async function createKP(payload: RequestCreateKP) {
|
||||
return withHandleError(
|
||||
axios.post<ResponseCreateKP>(URL_CRM_CREATEKP, payload).then(({ data }) => data)
|
||||
);
|
||||
}
|
||||
|
||||
@ -31,21 +31,13 @@ export const RequestCreateKPSchema = z.object({
|
||||
|
||||
export type RequestCreateKP = z.infer<typeof RequestCreateKPSchema>;
|
||||
|
||||
export const ResponseCreateKPSchema = z.union([
|
||||
z.object({
|
||||
evo_quotename: z.string(),
|
||||
link: z.string(),
|
||||
name: z.string(),
|
||||
offerprintform: z.string(),
|
||||
offerprintformapi: z.string(),
|
||||
quoteid: z.string(),
|
||||
success: z.literal(true),
|
||||
}),
|
||||
z.object({
|
||||
fullMessage: z.string(),
|
||||
message: z.string(),
|
||||
success: z.literal(false),
|
||||
}),
|
||||
]);
|
||||
export const ResponseCreateKPSchema = z.object({
|
||||
evo_quotename: z.string(),
|
||||
link: z.string(),
|
||||
name: z.string(),
|
||||
offerprintform: z.string(),
|
||||
offerprintformapi: z.string(),
|
||||
quoteid: z.string(),
|
||||
});
|
||||
|
||||
export type ResponseCreateKP = z.infer<typeof ResponseCreateKPSchema>;
|
||||
|
||||
@ -1,19 +1,12 @@
|
||||
import { love } from './tools';
|
||||
import type { User } from './types';
|
||||
import getUrls from '@/config/urls';
|
||||
import type { AxiosError, AxiosRequestConfig } from 'axios';
|
||||
import { withHandleError } from '@/utils/axios';
|
||||
import type { AxiosRequestConfig } from 'axios';
|
||||
import axios from 'axios';
|
||||
|
||||
const { URL_GET_USER } = getUrls();
|
||||
|
||||
export async function getUser(config?: AxiosRequestConfig) {
|
||||
return axios
|
||||
.get<User>(URL_GET_USER, config)
|
||||
.then((res) => love(res.data))
|
||||
.catch((error: AxiosError | Error) => {
|
||||
if (axios.isAxiosError(error)) {
|
||||
// TODO: track error
|
||||
throw new Error(error.message);
|
||||
}
|
||||
});
|
||||
return withHandleError(axios.get<User>(URL_GET_USER, config).then((res) => love(res.data)));
|
||||
}
|
||||
|
||||
@ -205,15 +205,19 @@ export default function reactions({ store, apolloClient, queryClient }: ProcessC
|
||||
risks
|
||||
.map((risk) => getFingapRequestDataFromRisk(risk))
|
||||
.map((data) => makeRequestGetFinGAP(data))
|
||||
).then((results) => {
|
||||
const newRisks = risks.map((risk, i) => ({
|
||||
...risk,
|
||||
premium: results.at(i)?.premium || 0,
|
||||
sum: results.at(i)?.sum || 0,
|
||||
}));
|
||||
)
|
||||
.then((results) => {
|
||||
const newRisks = risks.map((risk, i) => ({
|
||||
...risk,
|
||||
premium: results.at(i)?.premium || 0,
|
||||
sum: results.at(i)?.sum || 0,
|
||||
}));
|
||||
|
||||
$tables.fingap.setRisks(newRisks);
|
||||
});
|
||||
$tables.fingap.setRisks(newRisks);
|
||||
})
|
||||
.catch(() => {
|
||||
$tables.fingap.clear();
|
||||
});
|
||||
},
|
||||
{
|
||||
// Important: delay prohibits multiple reaction invocation
|
||||
|
||||
@ -8,7 +8,7 @@ import { TRPCError } from '@trpc/server';
|
||||
* @see https://trpc.io/docs/v10/middlewares
|
||||
*/
|
||||
export const userMiddleware = t.middleware(async ({ ctx, next }) => {
|
||||
if (process.env.NODE_ENV !== 'development' && !ctx.user) {
|
||||
if (!ctx.user) {
|
||||
throw new TRPCError({
|
||||
code: 'UNAUTHORIZED',
|
||||
});
|
||||
|
||||
@ -50,29 +50,31 @@ export const calculateRouter = router({
|
||||
input,
|
||||
});
|
||||
|
||||
const calculateResult = await calculate(requestData);
|
||||
try {
|
||||
const calculateResult = await calculate(requestData);
|
||||
|
||||
const result = transformCalculateResults({
|
||||
calculateInput: input,
|
||||
requestCalculate: requestData,
|
||||
responseCalculate: calculateResult,
|
||||
});
|
||||
|
||||
// TEMP
|
||||
if (ctx.unlimited) {
|
||||
result.__calculateResult = calculateResult;
|
||||
}
|
||||
|
||||
if (calculateResult.errors?.length > 0) {
|
||||
return {
|
||||
error: calculateResult.errors[0],
|
||||
data: result,
|
||||
success: true,
|
||||
};
|
||||
} catch (error_) {
|
||||
const error = (error_ as Error).message;
|
||||
|
||||
return {
|
||||
error,
|
||||
success: false,
|
||||
};
|
||||
}
|
||||
|
||||
const result = transformCalculateResults({
|
||||
calculateInput: input,
|
||||
requestCalculate: requestData,
|
||||
responseCalculate: calculateResult,
|
||||
});
|
||||
|
||||
// TEMP
|
||||
if (ctx.unlimited) {
|
||||
result.__calculateResult = calculateResult;
|
||||
}
|
||||
|
||||
return {
|
||||
data: result,
|
||||
success: true,
|
||||
};
|
||||
}),
|
||||
});
|
||||
|
||||
@ -129,51 +129,46 @@ export const quoteRouter = router({
|
||||
input,
|
||||
});
|
||||
|
||||
const calculateResult = await calculate(requestData);
|
||||
try {
|
||||
const calculateResult = await calculate(requestData);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
if (calculateResult.errors?.length > 0) {
|
||||
return {
|
||||
error: calculateResult.errors[0],
|
||||
data: result,
|
||||
success: true,
|
||||
};
|
||||
} catch (error_) {
|
||||
const error = (error_ as Error).message;
|
||||
|
||||
return {
|
||||
error,
|
||||
success: false,
|
||||
};
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
if (createKPResult.success === false) {
|
||||
return {
|
||||
success: false,
|
||||
error: createKPResult.message || createKPResult.fullMessage,
|
||||
};
|
||||
}
|
||||
|
||||
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,
|
||||
};
|
||||
}),
|
||||
});
|
||||
|
||||
|
||||
25
apps/web/utils/axios.ts
Normal file
25
apps/web/utils/axios.ts
Normal file
@ -0,0 +1,25 @@
|
||||
import type { AxiosError } from 'axios';
|
||||
import { isAxiosError } from 'axios';
|
||||
|
||||
function getErrorMessage<T extends { error?: string; errors?: string[]; message?: string }>(
|
||||
error: AxiosError<T>
|
||||
) {
|
||||
return (
|
||||
error.response?.data?.error ||
|
||||
error.response?.data?.errors?.[0] ||
|
||||
error.response?.data?.message ||
|
||||
error.message
|
||||
);
|
||||
}
|
||||
|
||||
export async function withHandleError<T>(fn: Promise<T>) {
|
||||
return fn.catch((error_: AxiosError | Error) => {
|
||||
if (isAxiosError(error_)) {
|
||||
// TODO: track error
|
||||
const errorMessage = getErrorMessage(error_);
|
||||
throw new Error(errorMessage);
|
||||
}
|
||||
|
||||
return null as unknown as T;
|
||||
});
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user