From 6fe0fa1faae73c1973fb43ad0b79b77f61d44937 Mon Sep 17 00:00:00 2001 From: Chika Date: Fri, 13 Nov 2020 11:37:41 +0300 Subject: [PATCH] refactor after first successful connection --- src/client/common/constants.js | 1 - src/client/common/urls.js | 11 ++++++ src/client/services/CalculationService.ts | 45 ++++++++--------------- src/client/services/prepareData.ts | 36 +++++++++--------- src/core/constants/{urls.ts => urls.js} | 2 +- src/core/types/Calculation/Prepare.ts | 2 +- src/core/types/Calculation/Requests.ts | 5 +-- src/index.tsx | 7 ++-- src/server/index.ts | 8 ++-- 9 files changed, 55 insertions(+), 62 deletions(-) create mode 100644 src/client/common/urls.js rename src/core/constants/{urls.ts => urls.js} (86%) diff --git a/src/client/common/constants.js b/src/client/common/constants.js index 8f99dc4..67a018d 100644 --- a/src/client/common/constants.js +++ b/src/client/common/constants.js @@ -1,4 +1,3 @@ import { DateTime } from 'luxon'; -export const API_HOSTNAME = window.location.hostname; export const currentDate = DateTime.local().toUTC().toJSDate().toDateString(); diff --git a/src/client/common/urls.js b/src/client/common/urls.js new file mode 100644 index 0000000..c2ddc0e --- /dev/null +++ b/src/client/common/urls.js @@ -0,0 +1,11 @@ +import { SERVER_PORT } from 'core/constants/urls'; + +export const API_HOSTNAME = window.location.hostname; + +export const getServerUrl = url => + String.prototype.concat( + (process.env.NODE_ENV === 'development' && + `http://${API_HOSTNAME}:${SERVER_PORT}`) || + '', + url || '', + ); diff --git a/src/client/services/CalculationService.ts b/src/client/services/CalculationService.ts index 667a913..9091e14 100644 --- a/src/client/services/CalculationService.ts +++ b/src/client/services/CalculationService.ts @@ -1,22 +1,22 @@ import { ApolloClient, gql, HttpLink, InMemoryCache } from '@apollo/client'; import axios from 'axios'; -import { API_HOSTNAME } from 'client/common/constants'; +import { getServerUrl } from 'client/common/urls'; import { CORE_PROXY_URL, CRM_GRAPHQL_PROXY_URL, - CRM_GRAPHQL_URL, + CRM_GRAPHQL_URL } from 'core/constants/urls'; import { convertEntityToOption } from 'core/tools/entities'; import { convertJSONToGQLQuery } from 'core/tools/query'; import { - IGetEntitiesRequest, - IRequestToCRMGQL, IGetCalculationRequest, + IGetEntitiesRequest, + IRequestToCRMGQL } from 'core/types/Calculation/Requests'; import { IGetCalculationResponse, IGetEntitiesResponse, - IGetUserResponse, + IGetUserResponse } from 'core/types/Calculation/Responses'; import { IBaseOption } from 'core/types/Calculation/Store/options'; import { TCRMEntity } from 'core/types/Entities/crmEntities'; @@ -28,14 +28,7 @@ const client = new ApolloClient({ cache: new InMemoryCache(), link: new HttpLink({ - uri: - process.env.NODE_ENV !== 'development' - ? String.prototype.concat('/proxy', CRM_GRAPHQL_PROXY_URL) - : String.prototype.concat( - `http://${API_HOSTNAME}:3001`, - '/proxy', - CRM_GRAPHQL_PROXY_URL, - ), + uri: getServerUrl(String.prototype.concat('/proxy', CRM_GRAPHQL_PROXY_URL)), }), defaultOptions: { @@ -61,23 +54,17 @@ class CalculationService { new Promise((resolve, reject) => { axios .post( - String.prototype.concat( - (process.env.NODE_ENV !== 'development' && - `http://${API_HOSTNAME}:3001`) || - '', - '/proxy', - CORE_PROXY_URL, - '/api', - '/v1', - '/calculation', - '/calculate', + getServerUrl( + String.prototype.concat( + '/proxy', + CORE_PROXY_URL, + '/api', + '/v1', + '/calculation', + '/calculate', + ), ), - { - preparedValues: preparedData.preparedValues, - preparedPayments: { - Rows: preparedData.preparedPayments, - }, - }, + preparedData, ) .then(res => { console.log(res); diff --git a/src/client/services/prepareData.ts b/src/client/services/prepareData.ts index 191aac8..ed76e30 100644 --- a/src/client/services/prepareData.ts +++ b/src/client/services/prepareData.ts @@ -1,24 +1,22 @@ import { calcPrice } from 'client/stores/CalculationStore/Effects/lib/tools'; import valuesConstants from 'core/constants/values'; -import { - PreparedPayments, - PreparedValues, -} from 'core/types/Calculation/Prepare'; +import { PreparedValues } from 'core/types/Calculation/Prepare'; import { IGetCalculationRequest } from 'core/types/Calculation/Requests'; -import { DateTime } from 'luxon'; import { ICalculationStore } from 'core/types/Calculation/Store'; +import { DateTime } from 'luxon'; +import { PaymentRow } from './../../core/types/Calculation/Prepare'; export const prepareCalculationData = ({ calculationStore, }: { calculationStore: ICalculationStore; -}): any => { +}): IGetCalculationRequest => { const currentDate = DateTime.local().toUTC().toJSDate(); const { values, options, tables } = calculationStore; //@ts-ignore - let preparedPayments: PreparedPayments = Array.from( + let preparedPaymentsRows: PaymentRow[] = Array.from( { length: values.leasingPeriod }, () => ({ numberPayment: 0, @@ -37,10 +35,10 @@ export const prepareCalculationData = ({ ); for (let i = 0; i < values.leasingPeriod; i++) { - preparedPayments[i].numberPayment = i + 1; - preparedPayments[i].percentPayment = + preparedPaymentsRows[i].numberPayment = i + 1; + preparedPaymentsRows[i].percentPayment = i === 0 ? 0 : tables.tablePayments.rows[i].paymentRelation?.value; - preparedPayments[i].sumPayment = 0; + preparedPaymentsRows[i].sumPayment = 0; if (tracker) { if (tracker.evo_planpayments && tracker.evo_planpayments.length > 0) { @@ -48,11 +46,11 @@ export const prepareCalculationData = ({ x => x.evo_name === (i + 1).toString(), ); if (evo_planpayment) { - preparedPayments[i].gpsBasePayment = + preparedPaymentsRows[i].gpsBasePayment = (evo_planpayment.evo_cost_price_telematics_withoutnds || 0) + (evo_planpayment.evo_cost_equipment_withoutnds || 0); - preparedPayments[i].gpsCostPayment = + preparedPaymentsRows[i].gpsCostPayment = evo_planpayment.evo_cost_telematics_withoutnds || 0; } } @@ -64,11 +62,11 @@ export const prepareCalculationData = ({ x => x.evo_name === (i + 1).toString(), ); if (evo_planpayment) { - preparedPayments[i].tlmBasePayment = + preparedPaymentsRows[i].tlmBasePayment = (evo_planpayment.evo_cost_price_telematics_withoutnds || 0) + (evo_planpayment.evo_cost_equipment_withoutnds || 0); - preparedPayments[i].tlmCostPayment = + preparedPaymentsRows[i].tlmCostPayment = evo_planpayment.evo_cost_telematics_withoutnds || 0; } } @@ -450,10 +448,10 @@ export const prepareCalculationData = ({ (preparedValues.trackerCost || 0) + (preparedValues.tLMCost || 0) + preparedValues.transportTaxGr + - preparedPayments + preparedPaymentsRows .map(x => x.tlmCostPayment || 0) .reduce((a, b) => a + b, 0) + - preparedPayments + preparedPaymentsRows .map(x => x.gpsCostPayment || 0) .reduce((a, b) => a + b, 0)) * preparedValues.leasing0K - @@ -676,10 +674,10 @@ export const prepareCalculationData = ({ plPrice + trackerCost + tLMCost + - preparedPayments + preparedPaymentsRows .map(x => x.tlmCostPayment || 0) .reduce((a, b) => a + b, 0) + - preparedPayments + preparedPaymentsRows .map(x => x.gpsCostPayment || 0) .reduce((a, b) => a + b, 0) + registration + @@ -733,7 +731,7 @@ export const prepareCalculationData = ({ firstPaymentSum; return { - preparedPayments, + preparedPayments: { Rows: preparedPaymentsRows }, preparedValues, }; }; diff --git a/src/core/constants/urls.ts b/src/core/constants/urls.js similarity index 86% rename from src/core/constants/urls.ts rename to src/core/constants/urls.js index 5ac1179..a051bd1 100644 --- a/src/core/constants/urls.ts +++ b/src/core/constants/urls.js @@ -1,4 +1,4 @@ -export const API_PORT = 3001; +export const SERVER_PORT = 3001; export const CRM_GRAPHQL_URL = 'http://crmgraphql-dev.evoleasing.ru'; export const CRM_GRAPHQL_PROXY_URL = '/crmgraphql'; export const CORE_URL = 'http://localhost:5000'; diff --git a/src/core/types/Calculation/Prepare.ts b/src/core/types/Calculation/Prepare.ts index 8f6ff6c..68a160a 100644 --- a/src/core/types/Calculation/Prepare.ts +++ b/src/core/types/Calculation/Prepare.ts @@ -101,4 +101,4 @@ export interface PaymentRow { tlmCostPayment?: number; } -export type PreparedPayments = PaymentRow[]; +export type PreparedPayments = { Rows: PaymentRow[] }; diff --git a/src/core/types/Calculation/Requests.ts b/src/core/types/Calculation/Requests.ts index 8c9fb21..26bcb98 100644 --- a/src/core/types/Calculation/Requests.ts +++ b/src/core/types/Calculation/Requests.ts @@ -1,10 +1,9 @@ -import { ICalculationStore } from 'core/types/Calculation/Store'; -import { CRMEntityNames } from 'core/types/Entities/crmEntityNames'; -import { TEntityQuery } from '../Entities/query'; import { PreparedPayments, PreparedValues, } from 'core/types/Calculation/Prepare'; +import { CRMEntityNames } from 'core/types/Entities/crmEntityNames'; +import { TEntityQuery } from '../Entities/query'; export interface IGetEntitiesRequest { queries: TEntityQuery[]; diff --git a/src/index.tsx b/src/index.tsx index 86e9663..07eacb3 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -2,13 +2,12 @@ import axios from 'axios'; import React from 'react'; import ReactDOM from 'react-dom'; import App from './client/App'; -import './index.css'; import * as serviceWorker from './client/serviceWorker'; -import { API_PORT } from './core/constants/urls'; -import { API_HOSTNAME } from 'client/common/constants'; +import './index.css'; +import { getServerUrl } from 'client/common/urls'; if (process.env.NODE_ENV === 'development') { - axios.defaults.baseURL = `http://${API_HOSTNAME}:${API_PORT}`; + axios.defaults.baseURL = getServerUrl(); } ReactDOM.render( diff --git a/src/server/index.ts b/src/server/index.ts index ab1f1e3..9911071 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -7,9 +7,9 @@ import helmet from 'helmet'; import morgan from 'morgan'; import path from 'path'; import 'reflect-metadata'; -import { API_PORT } from '../core/constants/urls'; import routes from './routes'; import ntlm from 'express-ntlm'; +import { SERVER_PORT } from './../core/constants/urls'; const isDevelopmentMode = process.env.NODE_ENV === 'development'; @@ -56,7 +56,7 @@ app.get('*', function (req, res) { }); /**CLIENT */ -console.log('checking port', API_PORT); -app.listen(API_PORT, () => { - console.log(`Server now listening on port: ${API_PORT}`); +console.log('checking port', SERVER_PORT); +app.listen(SERVER_PORT, () => { + console.log(`Server now listening on port: ${SERVER_PORT}`); });