From d013be319fef9bcaeae519c5f72407f76bbd7c08 Mon Sep 17 00:00:00 2001 From: vchikalkin Date: Wed, 18 Sep 2024 14:02:05 +0300 Subject: [PATCH] apps/web: migrate to 1c REST transtax request --- apps/web/api/1c/types.ts | 13 ++++---- apps/web/config/schema/env.js | 4 ++- apps/web/config/schema/runtime-config.js | 2 ++ apps/web/mocks/handlers.js | 16 ++++++---- apps/web/next.config.js | 8 ++--- apps/web/pages/api/1c/transtax.ts | 38 ++++++++++++++++++++++++ apps/web/process/gibdd/reactions.ts | 17 +++++------ apps/web/utils/axios.ts | 13 ++++++-- docker-compose.yml | 4 +++ 9 files changed, 85 insertions(+), 30 deletions(-) create mode 100644 apps/web/pages/api/1c/transtax.ts diff --git a/apps/web/api/1c/types.ts b/apps/web/api/1c/types.ts index 8bff49c..a7b14d8 100644 --- a/apps/web/api/1c/types.ts +++ b/apps/web/api/1c/types.ts @@ -1,11 +1,12 @@ export type RequestTransTax = { + CalcDate: string; + CarCategory: string; OKTMO: string; - calcDate: Date; - carCategory: string; - power: number; - year: number; + Power: number; + Year: number; }; export type ResponseTransTax = { - error: string; - tax: number; + Error: string; + Tax: number; + TaxRate: number; }; diff --git a/apps/web/config/schema/env.js b/apps/web/config/schema/env.js index c5c9dc3..e70a039 100644 --- a/apps/web/config/schema/env.js +++ b/apps/web/config/schema/env.js @@ -2,6 +2,8 @@ const { z } = require('zod'); const envSchema = z.object({ BASE_PATH: z.string().optional().default(''), + DEV_AUTH_TOKEN: z.string().optional(), + PASSWORD_1C_TRANSTAX: z.string(), PORT: z.string().optional(), SENTRY_AUTH_TOKEN: z.string(), SENTRY_DSN: z.string(), @@ -11,7 +13,6 @@ const envSchema = z.object({ URL_CACHE_GET_QUERIES_DIRECT: z.string().default('http://api:3001/proxy/get-queries'), URL_CACHE_GET_QUERY_DIRECT: z.string().default('http://api:3001/proxy/get-query'), URL_CACHE_RESET_QUERIES_DIRECT: z.string().default('http://api:3001/proxy/reset'), - DEV_AUTH_TOKEN: z.string().optional(), URL_CORE_CALCULATE_DIRECT: z.string(), URL_CORE_FINGAP_DIRECT: z.string(), URL_CRM_CREATEKP_DIRECT: z.string(), @@ -22,6 +23,7 @@ const envSchema = z.object({ URL_ELT_OSAGO_DIRECT: z.string(), URL_GET_USER_DIRECT: z.string(), USE_DEV_COLORS: z.unknown().optional().transform(Boolean), + USERNAME_1C_TRANSTAX: z.string(), }); module.exports = envSchema; diff --git a/apps/web/config/schema/runtime-config.js b/apps/web/config/schema/runtime-config.js index 6db9b2c..89ab41d 100644 --- a/apps/web/config/schema/runtime-config.js +++ b/apps/web/config/schema/runtime-config.js @@ -11,6 +11,7 @@ const publicRuntimeConfigSchema = envSchema.pick({ const serverRuntimeConfigSchema = envSchema.pick({ BASE_PATH: true, DEV_AUTH_TOKEN: true, + PASSWORD_1C_TRANSTAX: true, PORT: true, SENTRY_DSN: true, SENTRY_ENVIRONMENT: true, @@ -27,6 +28,7 @@ const serverRuntimeConfigSchema = envSchema.pick({ URL_ELT_KASKO_DIRECT: true, URL_ELT_OSAGO_DIRECT: true, URL_GET_USER_DIRECT: true, + USERNAME_1C_TRANSTAX: true, }); module.exports = { diff --git a/apps/web/mocks/handlers.js b/apps/web/mocks/handlers.js index f975dc1..79d9d67 100644 --- a/apps/web/mocks/handlers.js +++ b/apps/web/mocks/handlers.js @@ -39,20 +39,23 @@ export const handlers = [ rest.post(URL_1C_TRANSTAX, (req, res, ctx) => res( ctx.json({ - error: null, - tax: _.random(100000, 200000), + Error: null, + Tax: _.random(100000, 200000), }) ) ), - rest.post(URL_ELT_OSAGO, async (req, res, ctx) => res( + rest.post(URL_ELT_OSAGO, async (req, res, ctx) => + res( ctx.json({ numCalc: _.random(1000000, 3000000), skCalcId: _.random(50000000, 60000000).toString(), premiumSum: _.random(10000, 20000), message: 'OSAGO Message', }) - )), - rest.post(URL_ELT_KASKO, async (req, res, ctx) => res( + ) + ), + rest.post(URL_ELT_KASKO, async (req, res, ctx) => + res( ctx.json({ requestId: _.random(3000000, 4000000).toString(), skCalcId: _.random(50000000, 60000000).toString(), @@ -67,7 +70,8 @@ export const handlers = [ ], totalFranchise: _.random(20000, 40000), }) - )), + ) + ), // rest.post(URL_CRM_GRAPHQL, (req, res, ctx) => { // return res(ctx.status(503)); diff --git a/apps/web/next.config.js b/apps/web/next.config.js index 871834a..ff83b3b 100644 --- a/apps/web/next.config.js +++ b/apps/web/next.config.js @@ -66,10 +66,10 @@ module.exports = withSentryConfig( destination: env.URL_CORE_FINGAP_DIRECT, source: urls.URL_CORE_FINGAP_PROXY, }, - { - destination: env.URL_1C_TRANSTAX_DIRECT, - source: urls.URL_1C_TRANSTAX_PROXY, - }, + // { + // destination: env.URL_1C_TRANSTAX_DIRECT, + // source: urls.URL_1C_TRANSTAX_PROXY, + // }, { destination: env.URL_ELT_KASKO_DIRECT, source: urls.URL_ELT_KASKO_PROXY, diff --git a/apps/web/pages/api/1c/transtax.ts b/apps/web/pages/api/1c/transtax.ts new file mode 100644 index 0000000..fade2dc --- /dev/null +++ b/apps/web/pages/api/1c/transtax.ts @@ -0,0 +1,38 @@ +import type { ResponseTransTax } from '@/api/1c/types'; +import { serverRuntimeConfigSchema } from '@/config/schema/runtime-config'; +import { withHandleError } from '@/utils/axios'; +import type { HttpError } from '@/utils/error'; +import axios from 'axios'; +import type { NextApiRequest, NextApiResponse } from 'next'; +import getConfig from 'next/config'; + +const { serverRuntimeConfig } = getConfig(); +const { USERNAME_1C_TRANSTAX, PASSWORD_1C_TRANSTAX, URL_1C_TRANSTAX_DIRECT } = + serverRuntimeConfigSchema.parse(serverRuntimeConfig); + +export default function handler(req: NextApiRequest, res: NextApiResponse) { + const abortController = new AbortController(); + + req.on('close', () => { + abortController.abort(); + }); + + const params = req.body as ResponseTransTax; + + withHandleError( + axios.get(URL_1C_TRANSTAX_DIRECT, { + auth: { + password: PASSWORD_1C_TRANSTAX, + username: USERNAME_1C_TRANSTAX, + }, + params, + signal: abortController.signal, + }) + ) + .then(({ data }) => res.json(data)) + .catch((error) => { + const _err = error as HttpError; + + return res.json({ message: _err.message }); + }); +} diff --git a/apps/web/process/gibdd/reactions.ts b/apps/web/process/gibdd/reactions.ts index e59fd72..f5d8413 100644 --- a/apps/web/process/gibdd/reactions.ts +++ b/apps/web/process/gibdd/reactions.ts @@ -9,7 +9,6 @@ import * as CRMTypes from '@/graphql/crm.types'; import { getCurrentDateString } from '@/utils/date'; import { normalizeOptions } from '@/utils/entity'; import { disposableReaction } from '@/utils/mobx'; -import dayjs from 'dayjs'; import { reaction } from 'mobx'; export function common({ store, apolloClient, queryClient }: ProcessContext) { @@ -186,18 +185,16 @@ export function common({ store, apolloClient, queryClient }: ProcessContext) { const carCategory = getCarCategory(objectTypeTax); if (OKTMO) { - const currentDate = dayjs().utc(false).toDate(); - const response = await queryClient.fetchQuery( - ['1c', 'trans-tax', carCategory, leaseObjectMotorPower, leaseObjectYear], + ['1c', 'trans-tax', carCategory, leaseObjectMotorPower, leaseObjectYear, OKTMO], (context) => getTransTax( { + CalcDate: getCurrentDateString(), + CarCategory: carCategory, OKTMO, - calcDate: currentDate, - carCategory, - power: leaseObjectMotorPower, - year: leaseObjectYear, + Power: leaseObjectMotorPower, + Year: leaseObjectYear, }, context ), @@ -206,8 +203,8 @@ export function common({ store, apolloClient, queryClient }: ProcessContext) { } ); - if (response?.tax) { - $calculation.element('tbxVehicleTaxInYear').setValue(response.tax); + if (response?.Tax) { + $calculation.element('tbxVehicleTaxInYear').setValue(response.Tax); } else { $calculation.element('tbxVehicleTaxInYear').resetValue(); } diff --git a/apps/web/utils/axios.ts b/apps/web/utils/axios.ts index aefb72f..1e4c51f 100644 --- a/apps/web/utils/axios.ts +++ b/apps/web/utils/axios.ts @@ -4,10 +4,17 @@ import type { AxiosError } from 'axios'; import { isAxiosError } from 'axios'; import { pick } from 'radash'; -function getErrorMessage< - T extends { error?: string; errors?: string[]; fullMessage?: string; message?: string } ->(error: AxiosError) { +type ResponseError = { + Error?: string; + error?: string; + errors?: string[]; + fullMessage?: string; + message?: string; +}; + +function getErrorMessage(error: AxiosError) { return ( + error.response?.data?.Error || error.response?.data?.error || error.response?.data?.errors?.[0] || error.response?.data.fullMessage || diff --git a/docker-compose.yml b/docker-compose.yml index 8d974d0..a3b95ca 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -16,6 +16,8 @@ services: - URL_CORE_FINGAP_DIRECT=${URL_CORE_FINGAP_DIRECT} - URL_CORE_CALCULATE_DIRECT=${URL_CORE_CALCULATE_DIRECT} - URL_1C_TRANSTAX_DIRECT=${URL_1C_TRANSTAX_DIRECT} + - USERNAME_1C_TRANSTAX=${USERNAME_1C_TRANSTAX} + - PASSWORD_1C_TRANSTAX=${PASSWORD_1C_TRANSTAX} - URL_ELT_OSAGO_DIRECT=${URL_ELT_OSAGO_DIRECT} - URL_ELT_KASKO_DIRECT=${URL_ELT_KASKO_DIRECT} build: @@ -34,6 +36,8 @@ services: - URL_CORE_FINGAP_DIRECT=${URL_CORE_FINGAP_DIRECT} - URL_CORE_CALCULATE_DIRECT=${URL_CORE_CALCULATE_DIRECT} - URL_1C_TRANSTAX_DIRECT=${URL_1C_TRANSTAX_DIRECT} + - USERNAME_1C_TRANSTAX=${USERNAME_1C_TRANSTAX} + - PASSWORD_1C_TRANSTAX=${PASSWORD_1C_TRANSTAX} - URL_ELT_OSAGO_DIRECT=${URL_ELT_OSAGO_DIRECT} - URL_ELT_KASKO_DIRECT=${URL_ELT_KASKO_DIRECT} context: .