From c6c31d1f3e53b8274dd6b7ae0a6ac5af12ac30b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=BB=D0=B0=D0=B4=D0=B8=D1=81=D0=BB=D0=B0=D0=B2=20?= =?UTF-8?q?=D0=A7=D0=B8=D0=BA=D0=B0=D0=BB=D0=BA=D0=B8=D0=BD?= Date: Tue, 10 Nov 2020 12:59:47 +0300 Subject: [PATCH] move apollo to client | fix convert ents to opts --- package.json | 4 +- src/client/Containers/Calculation/index.jsx | 2 - src/client/common/constants.js | 1 + src/client/services/CalculationService.ts | 105 ++- .../services}/prepareData.ts | 0 .../CalculationStore/Effects/reaction.ts | 11 +- src/core/Data/initialOptions.ts | 16 +- src/core/constants/urls.ts | 3 +- src/core/fakeData/entityFakes.ts | 682 ------------------ src/core/tools/entities.ts | 6 +- src/core/types/Calculation/Requests.ts | 1 - src/core/types/Entities/query.ts | 1 + src/index.tsx | 7 +- .../controllers/CalculationController.ts | 18 + .../CalculationController/crmManager.test.ts | 33 - .../CalculationController/crmManager.ts | 33 - .../CalculationController/index.ts | 73 -- src/server/routes/calculation.ts | 3 +- src/server/routes/index.ts | 2 + src/server/routes/proxy.ts | 12 + 20 files changed, 166 insertions(+), 847 deletions(-) create mode 100644 src/client/common/constants.js rename src/{server/controllers/CalculationController => client/services}/prepareData.ts (100%) delete mode 100644 src/core/fakeData/entityFakes.ts create mode 100644 src/server/controllers/CalculationController.ts delete mode 100644 src/server/controllers/CalculationController/crmManager.test.ts delete mode 100644 src/server/controllers/CalculationController/crmManager.ts delete mode 100644 src/server/controllers/CalculationController/index.ts create mode 100644 src/server/routes/proxy.ts diff --git a/package.json b/package.json index 0910c1d..d13b844 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "craco-less": "^1.17.0", "cross-fetch": "^3.0.6", "express": "^4.17.1", + "express-http-proxy": "^1.6.2", "graphql": "^15.4.0", "helmet": "^4.1.0", "http-errors": "^1.8.0", @@ -54,12 +55,13 @@ "@types/cookie-parser": "^1.4.2", "@types/cors": "^2.8.7", "@types/express": "^4.17.7", + "@types/express-http-proxy": "^1.6.1", "@types/faker": "^5.1.0", "@types/jest": "^26.0.15", "@types/lodash": "^4.14.159", "@types/luxon": "^1.25.0", "@types/morgan": "^1.9.1", - "@types/node": "^14.6.0", + "@types/node": "^14.14.6", "@types/pluralize": "^0.0.29", "@types/react-router-dom": "^5.1.5", "@types/styled-components": "^5.1.2", diff --git a/src/client/Containers/Calculation/index.jsx b/src/client/Containers/Calculation/index.jsx index 7a7017d..680c2f0 100644 --- a/src/client/Containers/Calculation/index.jsx +++ b/src/client/Containers/Calculation/index.jsx @@ -19,7 +19,6 @@ const Calculation = () => { Promise.all([ CalculationService.getEntities({ queries: initialOptions, - toOptions: true, }), CalculationService.getEntities({ queries: staticEntitiesList, @@ -34,7 +33,6 @@ const Calculation = () => { many: true, }, ], - toOptions: true, }), ]) .then( diff --git a/src/client/common/constants.js b/src/client/common/constants.js new file mode 100644 index 0000000..71b4737 --- /dev/null +++ b/src/client/common/constants.js @@ -0,0 +1 @@ +export const API_HOSTNAME = window.location.hostname; diff --git a/src/client/services/CalculationService.ts b/src/client/services/CalculationService.ts index 69033bd..e2e4d60 100644 --- a/src/client/services/CalculationService.ts +++ b/src/client/services/CalculationService.ts @@ -1,22 +1,111 @@ -import axios from 'axios'; -import CalculationStore from 'client/stores/CalculationStore'; +import { ApolloClient, gql, HttpLink, InMemoryCache } from '@apollo/client'; +import { API_HOSTNAME } from 'client/common/constants'; +import { CRM_GRAPHQL_PROXY_URL, CRM_GRAPHQL_URL } from 'core/constants/urls'; +import { convertEntityToOption } from 'core/tools/entities'; +import { convertJSONToGQLQuery } from 'core/tools/query'; import { IGetEntitiesRequest } from 'core/types/Calculation/Requests'; import { IGetEntitiesResponse } from 'core/types/Calculation/Responses'; +import { IBaseOption } from 'core/types/Calculation/Store/options'; +import { TCRMEntity } from 'core/types/Entities/crmEntities'; +import { + CRMEntityAliases, + TEntities, + CRMEntityNames, +} from 'core/types/Entities/crmEntityNames'; +import { isPlural, singular } from 'pluralize'; + +const client = new ApolloClient({ + uri: CRM_GRAPHQL_URL, + 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, + ), + fetch, + }), + defaultOptions: { + query: { + fetchPolicy: 'no-cache', + errorPolicy: 'all', + }, + }, +}); class CalculationService { static getEntities = ({ queries, - toOptions, }: IGetEntitiesRequest): Promise => new Promise((resolve, reject) => { - axios - .post('/api/calculation/getCRMEntities', { queries, toOptions }) + // remove queries with invalid where + queries = queries.filter(query => { + return Object.values(query.where).some( + x => x !== null && x !== undefined, + ); + }); + + const convertedQuery = convertJSONToGQLQuery(queries); + client + .query({ + query: gql` + ${convertedQuery} + `, + }) .then(res => { - resolve(res.data); + const toOptions = queries + .filter(x => x.toOption) + .map(x => x.alias || x.entityName); + let resEntities: TEntities = res.data; + + Object.keys(resEntities).forEach(targetName => { + //@ts-ignore + const targetEnt: TCRMEntity | TCRMEntity[] = + //@ts-ignore + resEntities[targetName]; + + //@ts-ignore + if (toOptions.includes(targetName)) { + if (Array.isArray(targetEnt)) { + let optionatedEntities: (TCRMEntity & IBaseOption)[] = []; + for (let entity of targetEnt) { + const entityOption = convertEntityToOption( + entity, + entity.__typename, + ); + if (entityOption) { + optionatedEntities.push(entityOption); + } + } + //@ts-ignore + resEntities[ + //@ts-ignore + !CRMEntityAliases.includes(targetName) && isPlural(targetName) + ? singular(targetName) + : targetName + ] = optionatedEntities; + } else { + const entityOption = convertEntityToOption( + targetEnt, + targetEnt.__typename, + ); + //@ts-ignore + resEntities[ + //@ts-ignore + !CRMEntityAliases.includes(targetName) && isPlural(targetName) + ? singular(targetName) + : targetName + ] = entityOption; + } + } + }); + resolve({ entities: resEntities }); }) .catch(err => { - CalculationStore.showModal(err); - // reject(err); + reject(err); }); }); } diff --git a/src/server/controllers/CalculationController/prepareData.ts b/src/client/services/prepareData.ts similarity index 100% rename from src/server/controllers/CalculationController/prepareData.ts rename to src/client/services/prepareData.ts diff --git a/src/client/stores/CalculationStore/Effects/reaction.ts b/src/client/stores/CalculationStore/Effects/reaction.ts index ec7dc2c..b16e7ce 100644 --- a/src/client/stores/CalculationStore/Effects/reaction.ts +++ b/src/client/stores/CalculationStore/Effects/reaction.ts @@ -22,10 +22,6 @@ const reactionEffects: IReactionEffect[] = [ const lead = calculationStore.options.selectLead?.find( x => x.leadid === leadId, ); - console.log( - 'calculationStore.options.selectLead', - calculationStore.options.selectLead, - ); if (lead) { CalculationService.getEntities({ @@ -34,39 +30,44 @@ const reactionEffects: IReactionEffect[] = [ entityName: 'opportunity', where: { opportunityid: lead.evo_opportunityid }, fields: ['opportunityid', 'name'], + toOption: true, }, { entityName: 'quote', where: { evo_leadid: leadId }, fields: ['quoteid', 'name', 'quotenumber'], many: true, + toOption: true, }, { alias: 'agent', entityName: 'account', where: { accountid: lead.evo_agent_accountid }, fields: ['accountid', 'name'], + toOption: true, }, { alias: 'double_agent', entityName: 'account', where: { accountid: lead.evo_double_agent_accountid }, fields: ['accountid', 'name'], + toOption: true, }, { alias: 'broker', entityName: 'account', where: { accountid: lead.evo_broker_accountid }, fields: ['accountid', 'name'], + toOption: true, }, { alias: 'findepartment', entityName: 'account', where: { accountid: lead.evo_fin_department_accountid }, fields: ['accountid', 'name'], + toOption: true, }, ], - toOptions: true, }).then(({ entities }) => { if (entities.opportunity && !Array.isArray(entities.opportunity)) { calculationStore.setOptions('selectOpportunity', [ diff --git a/src/core/Data/initialOptions.ts b/src/core/Data/initialOptions.ts index aed7063..cc33a39 100644 --- a/src/core/Data/initialOptions.ts +++ b/src/core/Data/initialOptions.ts @@ -1,7 +1,7 @@ import { TEntityQuery } from 'core/types/Entities/query'; import { DateTime } from 'luxon'; -const currentDate = DateTime.local().toUTC().toJSDate(); +const currentDate = DateTime.local().toUTC().toJSDate().toDateString(); const initialOptions: TEntityQuery[] = [ { @@ -18,6 +18,7 @@ const initialOptions: TEntityQuery[] = [ ], where: { statecode: 0 }, many: true, + toOption: true, }, { alias: 'selectOpportunity', @@ -31,6 +32,7 @@ const initialOptions: TEntityQuery[] = [ ], where: { statecode: 0 }, many: true, + toOption: true, }, { alias: 'selectSupplier', @@ -42,6 +44,7 @@ const initialOptions: TEntityQuery[] = [ }, fields: ['accountid', 'name'], many: true, + toOption: true, }, { alias: 'selectSupplierCurrency', @@ -51,6 +54,7 @@ const initialOptions: TEntityQuery[] = [ }, fields: ['transactioncurrencyid', 'isocurrencycode'], many: true, + toOption: true, }, { alias: 'selectClientRisk', @@ -60,6 +64,7 @@ const initialOptions: TEntityQuery[] = [ }, fields: ['statecode', 'evo_name', 'evo_client_riskid'], many: true, + toOption: true, }, { alias: 'selectClientType', @@ -69,6 +74,7 @@ const initialOptions: TEntityQuery[] = [ }, fields: ['evo_name', 'evo_client_typeid'], many: true, + toOption: true, }, { alias: 'selectDealer', @@ -80,6 +86,7 @@ const initialOptions: TEntityQuery[] = [ }, fields: ['accountid', 'name'], many: true, + toOption: true, }, { alias: 'selectGPSBrand', @@ -89,6 +96,7 @@ const initialOptions: TEntityQuery[] = [ }, fields: ['evo_gps_brandid', 'evo_name'], many: true, + toOption: true, }, { alias: 'selectRegionRegistration', @@ -98,6 +106,7 @@ const initialOptions: TEntityQuery[] = [ }, fields: ['evo_name', 'evo_regionid'], many: true, + toOption: true, }, { @@ -109,6 +118,7 @@ const initialOptions: TEntityQuery[] = [ }, fields: ['accountid', 'name', 'evo_client_riskid'], many: true, + toOption: true, }, { alias: 'selectBrand', @@ -123,6 +133,7 @@ const initialOptions: TEntityQuery[] = [ 'evo_importer_reward_rub', ], many: true, + toOption: true, }, { alias: 'selectProduct', @@ -153,6 +164,7 @@ const initialOptions: TEntityQuery[] = [ }, ], many: true, + toOption: true, }, { alias: 'selectRegistration', @@ -171,6 +183,7 @@ const initialOptions: TEntityQuery[] = [ }, fields: ['evo_addproduct_typeid', 'evo_name'], many: true, + toOption: true, }, { alias: 'selectInsNSIB', @@ -189,6 +202,7 @@ const initialOptions: TEntityQuery[] = [ }, fields: ['evo_addproduct_typeid', 'evo_name'], many: true, + toOption: true, }, ]; diff --git a/src/core/constants/urls.ts b/src/core/constants/urls.ts index 79aad4e..0be9aec 100644 --- a/src/core/constants/urls.ts +++ b/src/core/constants/urls.ts @@ -1,2 +1,3 @@ export const API_PORT = 3001; -export const CRM_SERVICE_URL = 'http://crmgraphql-dev.evoleasing.ru'; +export const CRM_GRAPHQL_URL = 'http://crmgraphql-dev.evoleasing.ru'; +export const CRM_GRAPHQL_PROXY_URL = '/crmgraphql'; diff --git a/src/core/fakeData/entityFakes.ts b/src/core/fakeData/entityFakes.ts deleted file mode 100644 index 5473728..0000000 --- a/src/core/fakeData/entityFakes.ts +++ /dev/null @@ -1,682 +0,0 @@ -import { IOption } from 'core/types/Calculation/Store/options'; -import { TEntities } from 'core/types/Entities/crmEntityNames'; -import faker from 'faker'; - - -/** - * Fake Consts - */ -const ACCOUNT_1_ID = faker.random.uuid(); -const ACCOUNT_2_ID = faker.random.uuid(); -const ACCOUNT_3_ID = faker.random.uuid(); -const ACCOUNT_4_ID = faker.random.uuid(); -const ACCOUNT_5_ID = faker.random.uuid(); -const ACCOUNT_6_ID = faker.random.uuid(); -const ACCOUNT_7_ID = faker.random.uuid(); -const ACCOUNT_8_ID = faker.random.uuid(); -const ACCOUNT_9_ID = faker.random.uuid(); -const ACCOUNT_10_ID = faker.random.uuid(); -const ACCOUNT_11_ID = faker.random.uuid(); -const ACCOUNT_12_ID = faker.random.uuid(); -const ACCOUNT_13_ID = faker.random.uuid(); -const ACCOUNT_14_ID = faker.random.uuid(); -const ACCOUNT_15_ID = faker.random.uuid(); -const ACCOUNT_16_ID = faker.random.uuid(); - -const LEAD_1_ID = faker.random.uuid(); -const LEAD_2_ID = faker.random.uuid(); -const LEAD_3_ID = faker.random.uuid(); - -const OPPORTUNITY_1_ID = faker.random.uuid(); -const OPPORTUNITY_2_ID = faker.random.uuid(); -const OPPORTUNITY_3_ID = faker.random.uuid(); - -const QUOTE_1_ID = faker.random.uuid(); -const QUOTE_2_ID = faker.random.uuid(); -const QUOTE_3_ID = faker.random.uuid(); -const QUOTE_4_ID = faker.random.uuid(); - -const TRANSACTION_CURRENTCY_1_ID = faker.random.uuid(); -const TRANSACTION_CURRENTCY_2_ID = faker.random.uuid(); -const TRANSACTION_CURRENTCY_3_ID = faker.random.uuid(); - -const EVO_CLIENT_1_ID = faker.random.uuid(); -const EVO_CLIENT_2_ID = faker.random.uuid(); - -const EVO_CLIENT_RISK_1_ID = faker.random.uuid(); -const EVO_CLIENT_RISK_2_ID = faker.random.uuid(); - -const GPS_BRAND_1_ID = faker.random.uuid(); -const GPS_BRAND_2_ID = faker.random.uuid(); -const GPS_BRAND_3_ID = faker.random.uuid(); - -const GPS_MODEL_1_ID = faker.random.uuid(); -const GPS_MODEL_2_ID = faker.random.uuid(); -const GPS_MODEL_3_ID = faker.random.uuid(); -const GPS_MODEL_4_ID = faker.random.uuid(); - -const REGION_1_ID = faker.random.uuid(); -const REGION_2_ID = faker.random.uuid(); -const REGION_3_ID = faker.random.uuid(); -const REGION_4_ID = faker.random.uuid(); - -const TOWN_1_ID = faker.random.uuid(); -const TOWN_2_ID = faker.random.uuid(); -const TOWN_3_ID = faker.random.uuid(); -const TOWN_4_ID = faker.random.uuid(); - -const REWARD_CONDITION_1_ID = faker.random.uuid(); -const REWARD_CONDITION_2_ID = faker.random.uuid(); -const REWARD_CONDITION_3_ID = faker.random.uuid(); -const REWARD_CONDITION_4_ID = faker.random.uuid(); -const REWARD_CONDITION_5_ID = faker.random.uuid(); -const REWARD_CONDITION_6_ID = faker.random.uuid(); -const REWARD_CONDITION_7_ID = faker.random.uuid(); -const REWARD_CONDITION_8_ID = faker.random.uuid(); -const REWARD_CONDITION_9_ID = faker.random.uuid(); -const REWARD_CONDITION_10_ID = faker.random.uuid(); - -const EVO_CONNECTION_ROLE_1_ID = faker.random.uuid(); -const EVO_CONNECTION_ROLE_2_ID = faker.random.uuid(); -const EVO_CONNECTION_ROLE_3_ID = faker.random.uuid(); - -const CONNECTION_1_ID = faker.random.uuid(); -const CONNECTION_2_ID = faker.random.uuid(); -const CONNECTION_3_ID = faker.random.uuid(); -const CONNECTION_4_ID = faker.random.uuid(); -const CONNECTION_5_ID = faker.random.uuid(); -const CONNECTION_6_ID = faker.random.uuid(); - -const EVO_BRAND_ID = Array.from({ length: 10 }, () => faker.random.uuid()); -const EVO_MODEL_ID = Array.from({ length: 10 }, () => faker.random.uuid()); -const EVO_EQUIPMENT_ID = Array.from({ length: 10 }, () => faker.random.uuid()); -const EVO_IMPAIRMENT_GROUP_ID = Array.from({ length: 10 }, () => - faker.random.uuid(), -); -const EVO_CURRENCY_CHANGE_ID = Array.from({ length: 5 }, () => - faker.random.uuid(), -); -const EVO_STATUSCODE_ID = Array.from({ length: 5 }, () => faker.random.uuid()); - -/** - * Fake Consts - */ - -const entityFakeData: TEntities = { - account: [ - { - accountid: ACCOUNT_1_ID, - name: 'Салон 1', - evo_account_type: 100000001, - evo_supplier_type: 100000000, - statecode: 0, - evo_fin_department_accountid: ACCOUNT_4_ID, - evo_broker_accountid: ACCOUNT_11_ID, - }, - { - accountid: ACCOUNT_2_ID, - name: 'Салон 2', - evo_account_type: 100000001, - evo_supplier_type: 100000000, - statecode: 0, - evo_fin_department_accountid: ACCOUNT_5_ID, - evo_broker_accountid: ACCOUNT_12_ID, - }, - { - accountid: ACCOUNT_3_ID, - name: 'Салон 3', - evo_account_type: 100000001, - evo_supplier_type: 100000000, - statecode: 0, - // evo_broker_accountid: ACCOUNT_13_ID, - }, - { - accountid: ACCOUNT_4_ID, - name: 'Брокер', - evo_account_type: 100000005, - evo_supplier_type: 100000000, - statecode: 0, - }, - { - accountid: ACCOUNT_5_ID, - name: 'Финотдел2', - evo_account_type: 100000005, - evo_supplier_type: 100000000, - statecode: 0, - evo_client_riskid: EVO_CLIENT_RISK_1_ID, - }, - { - accountid: ACCOUNT_6_ID, - name: 'Агент1', - evo_account_type: 100000005, - evo_legal_form: 100000004, - statecode: 0, - }, - { - accountid: ACCOUNT_7_ID, - name: 'Агент2', - evo_account_type: 100000005, - evo_legal_form: 100000004, - statecode: 0, - }, - { - accountid: ACCOUNT_8_ID, - name: 'Агент3', - evo_account_type: 100000005, - evo_legal_form: 100000004, - statecode: 0, - }, - { - accountid: ACCOUNT_9_ID, - name: 'Агент4', - evo_account_type: 100000005, - evo_legal_form: 100000004, - statecode: 0, - }, - { - accountid: ACCOUNT_10_ID, - name: 'Клиент 1', - evo_account_type: 100000000, - evo_client_riskid: EVO_CLIENT_RISK_2_ID, - statecode: 0, - }, - { - name: 'ЮЛ поставщика 1', - accountid: ACCOUNT_11_ID, - evo_supplier_type: 100000001, - statecode: 0, - evo_broker_accountid: ACCOUNT_4_ID, - }, - { - name: 'ЮЛ поставщика 2', - accountid: ACCOUNT_12_ID, - evo_supplier_type: 100000001, - statecode: 0, - }, - { - name: 'ЮЛ поставщика 3', - accountid: ACCOUNT_13_ID, - evo_supplier_type: 100000001, - statecode: 0, - }, - { - name: 'ВСК', - accountid: ACCOUNT_14_ID, - evo_account_type: 100000002, - statecode: 0, - }, - { - name: 'РЕСО', - accountid: ACCOUNT_15_ID, - evo_account_type: 100000002, - statecode: 0, - }, - { - name: 'Ингосстрах', - accountid: ACCOUNT_16_ID, - evo_account_type: 100000002, - statecode: 0, - }, - ], - transactioncurrency: [ - { - transactioncurrencyid: TRANSACTION_CURRENTCY_1_ID, - isocurrencycode: 'RUB', - statecode: 0, - }, - { - transactioncurrencyid: TRANSACTION_CURRENTCY_2_ID, - isocurrencycode: 'USD', - statecode: 0, - }, - { - transactioncurrencyid: TRANSACTION_CURRENTCY_3_ID, - isocurrencycode: 'EUR', - statecode: 0, - }, - ], - evo_client_type: [ - { - evo_client_typeid: EVO_CLIENT_1_ID, - evo_name: 'Новый', - statecode: 0, - }, - { - evo_client_typeid: EVO_CLIENT_2_ID, - evo_name: 'Повторный', - statecode: 0, - }, - ], - evo_client_risk: [ - { - evo_client_riskid: EVO_CLIENT_RISK_1_ID, - evo_name: 'Низкий', - statecode: 0, - }, - { - evo_client_riskid: EVO_CLIENT_RISK_2_ID, - evo_name: 'Высокий', - statecode: 0, - }, - ], - lead: [ - { - fullname: '100_ООО "с агентами ФЛ', - leadid: LEAD_1_ID, - evo_opportunityid: OPPORTUNITY_1_ID, - evo_agent_accountid: ACCOUNT_6_ID, - evo_double_agent_accountid: ACCOUNT_7_ID, - }, - { - fullname: '150_ООО "с брокером', - leadid: LEAD_2_ID, - evo_opportunityid: OPPORTUNITY_2_ID, - evo_broker_accountid: ACCOUNT_4_ID, - }, - { - fullname: '176_ООО "С финотделом', - leadid: LEAD_3_ID, - // evo_opportunityid: OPPORTUNITY_3_ID, - evo_fin_department_accountid: ACCOUNT_5_ID, - account: ACCOUNT_5_ID, - }, - ], - opportunity: [ - { - name: '112345_ООО "с агентами ФЛ с риском', - opportunityid: OPPORTUNITY_1_ID, - evo_client_riskid: EVO_CLIENT_RISK_1_ID, - evo_leadid: LEAD_1_ID, - }, - { - name: '145678_ООО "с брокером и риском в клиенте', - opportunityid: OPPORTUNITY_2_ID, - evo_accountid: ACCOUNT_10_ID, - // evo_client_riskid: EVO_CLIENT_RISK_2_ID, - evo_leadid: LEAD_2_ID, - }, - { - name: '346343_ООО "с брокером и риском в интересе', - opportunityid: OPPORTUNITY_3_ID, - evo_accountid: ACCOUNT_9_ID, - evo_leadid: LEAD_3_ID, - }, - ], - quote: [ - { - name: '2345_ООО "с агентами ФЛ"', - quoteid: QUOTE_1_ID, - evo_leadid: LEAD_1_ID, - evo_recalc_limit: 2, - evo_statuscodeid: EVO_STATUSCODE_ID[0], - evo_approved_first_payment: 25, - }, - { - name: '6789_ООО "с брокером"', - quoteid: QUOTE_2_ID, - evo_leadid: LEAD_2_ID, - }, - { - name: '4567_ООО "с агентами ФЛ"', - quoteid: QUOTE_3_ID, - evo_leadid: LEAD_1_ID, - evo_recalc_limit: 1, - evo_statuscodeid: EVO_STATUSCODE_ID[1], - }, - { - name: '5678_ООО "с агентами ФЛ"', - quoteid: QUOTE_4_ID, - evo_leadid: LEAD_1_ID, - evo_broker_accountid: ACCOUNT_5_ID, - evo_recalc_limit: 0, - evo_statuscodeid: EVO_STATUSCODE_ID[0], - }, - ], - evo_gps_brand: [ - { - evo_name: 'Цезарь', - evo_gps_brandid: GPS_BRAND_1_ID, - statecode: 0, - }, - { - evo_name: 'Аркан', - evo_gps_brandid: GPS_BRAND_2_ID, - statecode: 0, - }, - { - evo_name: 'КОбраКоннекс', - evo_gps_brandid: GPS_BRAND_3_ID, - statecode: 0, - }, - ], - evo_gps_model: [ - { - evo_name: 'OmegaX', - evo_gps_modelid: GPS_MODEL_1_ID, - statecode: 0, - evo_gps_brandid: GPS_BRAND_1_ID, - }, - { - evo_name: 'Platinum', - evo_gps_modelid: GPS_MODEL_2_ID, - statecode: 0, - evo_gps_brandid: GPS_BRAND_1_ID, - }, - { - evo_name: 'Premium', - evo_gps_modelid: GPS_MODEL_3_ID, - statecode: 0, - evo_gps_brandid: GPS_BRAND_2_ID, - }, - { - evo_name: 'AutoConnex', - evo_gps_modelid: GPS_MODEL_4_ID, - statecode: 0, - evo_gps_brandid: GPS_BRAND_2_ID, - }, - ], - evo_region: [ - { - evo_name: 'Москвоская', - evo_regionid: REGION_1_ID, - statecode: 0, - }, - { - evo_name: 'Калининградская', - evo_regionid: REGION_2_ID, - statecode: 0, - }, - { - evo_name: 'Красноярская', - evo_regionid: REGION_3_ID, - statecode: 0, - }, - { - evo_name: 'Иркутская', - evo_regionid: REGION_4_ID, - statecode: 0, - }, - ], - evo_town: [ - { - evo_name: 'Одинцово', - evo_townid: TOWN_1_ID, - statecode: 0, - }, - { - evo_name: 'Калининград', - evo_townid: TOWN_2_ID, - statecode: 0, - }, - { - evo_name: 'Липецк', - evo_townid: TOWN_3_ID, - statecode: 0, - }, - { - evo_name: 'Серпухов', - evo_townid: TOWN_4_ID, - statecode: 0, - }, - ], - evo_reward_condition: [ - { - statecode: 0, - evo_agent_accountid: ACCOUNT_6_ID, - evo_reward_conditionid: REWARD_CONDITION_1_ID, - evo_name: '1.5%', - evo_reward_summ: 1.5, - evo_double_agent_accountid: ACCOUNT_7_ID, - }, - { - statecode: 0, - evo_agent_accountid: ACCOUNT_6_ID, - evo_reward_conditionid: REWARD_CONDITION_2_ID, - evo_name: 'Не более 2%', - evo_reward_summ: 2, - evo_reduce_reward: true, - }, - { - statecode: 0, - evo_agent_accountid: ACCOUNT_7_ID, - evo_reward_conditionid: REWARD_CONDITION_3_ID, - evo_name: '5%', - evo_reward_summ: 5, - }, - { - statecode: 0, - evo_agent_accountid: ACCOUNT_7_ID, - evo_reward_conditionid: REWARD_CONDITION_4_ID, - evo_name: 'Не более 10%', - evo_reward_summ: 10, - evo_reduce_reward: true, - }, - { - statecode: 0, - evo_agent_accountid: ACCOUNT_5_ID, - evo_reward_conditionid: REWARD_CONDITION_5_ID, - evo_name: '7%', - evo_reward_summ: 7, - }, - { - statecode: 0, - evo_agent_accountid: ACCOUNT_5_ID, - evo_reward_conditionid: REWARD_CONDITION_6_ID, - evo_name: 'Не более 8%', - evo_reward_summ: 8, - evo_reduce_reward: true, - }, - { - statecode: 0, - evo_agent_accountid: ACCOUNT_4_ID, - evo_reward_conditionid: REWARD_CONDITION_7_ID, - evo_name: '3%', - evo_reward_summ: 3, - }, - { - statecode: 0, - evo_agent_accountid: ACCOUNT_4_ID, - evo_reward_conditionid: REWARD_CONDITION_8_ID, - evo_name: 'Не более 4%', - evo_reward_summ: 4, - evo_reduce_reward: true, - }, - { - statecode: 0, - evo_agent_accountid: ACCOUNT_11_ID, - evo_reward_conditionid: REWARD_CONDITION_9_ID, - evo_name: '3%', - evo_reward_summ: 3, - }, - { - statecode: 0, - evo_agent_accountid: ACCOUNT_11_ID, - evo_reward_conditionid: REWARD_CONDITION_10_ID, - evo_name: 'Не более 6%', - evo_reward_summ: 6, - evo_reduce_reward: true, - }, - ], - connection: [ - { - connectionid: CONNECTION_1_ID, - record1id: ACCOUNT_1_ID, - record2roleid: EVO_CONNECTION_ROLE_1_ID, - record2id: ACCOUNT_11_ID, - statecode: 0, - }, - { - connectionid: CONNECTION_2_ID, - record1id: ACCOUNT_12_ID, - record2roleid: EVO_CONNECTION_ROLE_2_ID, - record2id: ACCOUNT_1_ID, - statecode: 0, - }, - { - connectionid: CONNECTION_3_ID, - record1id: ACCOUNT_2_ID, - record2roleid: EVO_CONNECTION_ROLE_1_ID, - record2id: ACCOUNT_13_ID, - statecode: 0, - }, - // { - // connectionid: CONNECTION_4_ID, - // statecode: 0, - // }, - // { - // connectionid: CONNECTION_5_ID, - // statecode: 0, - // }, - // { - // connectionid: CONNECTION_6_ID, - // statecode: 0, - // }, - ], - evo_connection_role: [ - { - evo_name: `ЮЛ Поставщика`, - statecode: 0, - evo_connection_roleid: EVO_CONNECTION_ROLE_1_ID, - }, - { - evo_name: `Салон`, - statecode: 0, - evo_connection_roleid: EVO_CONNECTION_ROLE_2_ID, - }, - { - evo_name: `Агент`, - statecode: 0, - evo_connection_roleid: EVO_CONNECTION_ROLE_3_ID, - }, - ], - evo_brand: [ - { - evo_brandid: EVO_BRAND_ID[0], - evo_name: 'AUDI', - evo_importer_reward_perc: 2, - evo_importer_reward_rub: 5000, - statecode: 0, - }, - { - evo_brandid: EVO_BRAND_ID[1], - evo_name: 'BMW', - statecode: 0, - }, - ], - evo_model: [ - { - evo_modelid: EVO_MODEL_ID[0], - evo_name: 'A1', - evo_brandid: EVO_BRAND_ID[0], - statecode: 0, - }, - { - evo_modelid: EVO_MODEL_ID[1], - evo_name: 'A5', - evo_brandid: EVO_BRAND_ID[0], - statecode: 0, - }, - { - evo_modelid: EVO_MODEL_ID[2], - evo_name: 'Q7', - evo_importer_reward_perc: 5, - evo_importer_reward_rub: 10000, - evo_brandid: EVO_BRAND_ID[0], - statecode: 0, - }, - { - evo_modelid: EVO_MODEL_ID[3], - evo_name: '320i', - evo_brandid: EVO_BRAND_ID[1], - statecode: 0, - }, - { - evo_modelid: EVO_MODEL_ID[4], - evo_name: 'X1', - evo_brandid: EVO_BRAND_ID[1], - statecode: 0, - }, - { - evo_modelid: EVO_MODEL_ID[5], - evo_name: 'X5', - evo_brandid: EVO_BRAND_ID[1], - statecode: 0, - evo_impairment_groupid: EVO_IMPAIRMENT_GROUP_ID[0], - evo_leasingobject_risk: 100000000, - }, - ], - evo_equipment: [ - { - evo_equipmentid: EVO_EQUIPMENT_ID[0], - evo_modelid: EVO_MODEL_ID[2], - evo_name: 'Exclusive', - statecode: 0, - }, - { - evo_equipmentid: EVO_EQUIPMENT_ID[1], - evo_modelid: EVO_MODEL_ID[2], - evo_name: 'Business', - statecode: 0, - }, - { - evo_equipmentid: EVO_EQUIPMENT_ID[2], - evo_modelid: EVO_MODEL_ID[5], - evo_name: 'Super', - statecode: 0, - evo_impairment_groupid: EVO_IMPAIRMENT_GROUP_ID[1], - evo_leasingobject_risk: 100000001, - }, - { - evo_equipmentid: EVO_EQUIPMENT_ID[3], - evo_modelid: EVO_MODEL_ID[5], - evo_name: 'Basic', - statecode: 0, - }, - ], - evo_impairment_group: [ - { - evo_name: 'Группа #1', - evo_impairment_groupid: EVO_IMPAIRMENT_GROUP_ID[0], - statecode: 0, - }, - { - evo_name: 'Группа #2', - evo_impairment_groupid: EVO_IMPAIRMENT_GROUP_ID[1], - statecode: 0, - }, - ], - evo_currencychange: [ - { - evo_currencychangeid: EVO_CURRENCY_CHANGE_ID[0], - evo_name: 'Доллар на сегодня', - evo_ref_transactioncurrency: TRANSACTION_CURRENTCY_2_ID, - evo_currencychange: 100, - // evo_coursedate: new Date(), - statecode: 0, - }, - { - evo_currencychangeid: EVO_CURRENCY_CHANGE_ID[1], - evo_name: 'Евро на сегодня', - evo_ref_transactioncurrency: TRANSACTION_CURRENTCY_3_ID, - evo_currencychange: 200, - // evo_coursedate: new Date(), - statecode: 0, - }, - ], - evo_statuscode: [ - { - evo_statuscodeid: EVO_STATUSCODE_ID[0], - evo_id: '2.3', - evo_name: 'Положительное решение', - statecode: 0, - }, - { - evo_statuscodeid: EVO_STATUSCODE_ID[1], - evo_id: '2.2', - evo_name: 'Одобрено клиентом', - statecode: 0, - }, - ], -}; - -export default entityFakeData; diff --git a/src/core/tools/entities.ts b/src/core/tools/entities.ts index e757e5c..2f56b99 100644 --- a/src/core/tools/entities.ts +++ b/src/core/tools/entities.ts @@ -5,8 +5,12 @@ import { CRMEntityNames } from '../types/Entities/crmEntityNames'; export function convertEntityToOption( entity: TCRMEntity, - entityName: CRMEntityNames, + entityName?: CRMEntityNames, ): (TCRMEntity & IBaseOption) | undefined { + if (!entityName) { + throw new Error('entityName is missing'); + } + if (!entity || !propsMap || !propsMap[entityName]) { throw new Error(`${entityName} not found in propsMap!`); } diff --git a/src/core/types/Calculation/Requests.ts b/src/core/types/Calculation/Requests.ts index c6617a6..d148702 100644 --- a/src/core/types/Calculation/Requests.ts +++ b/src/core/types/Calculation/Requests.ts @@ -4,7 +4,6 @@ import { TValue, TValues } from './Store/values'; export interface IGetEntitiesRequest { queries: TEntityQuery[]; - toOptions?: boolean; } export interface IGetCalculationRequest { diff --git a/src/core/types/Entities/query.ts b/src/core/types/Entities/query.ts index 94bfb58..f9bd681 100644 --- a/src/core/types/Entities/query.ts +++ b/src/core/types/Entities/query.ts @@ -26,4 +26,5 @@ export type TBaseEntityQuery = { export type TEntityQuery = TBaseEntityQuery & { where: TWhere; whereCmp?: TWhere; + toOption?: boolean; }; diff --git a/src/index.tsx b/src/index.tsx index 560af9c..86e9663 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -5,11 +5,10 @@ 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'; -const isDevelopmentMode = process.env.NODE_ENV === 'development'; - -if (isDevelopmentMode) { - axios.defaults.baseURL = `http://${window.location.hostname}:${API_PORT}`; +if (process.env.NODE_ENV === 'development') { + axios.defaults.baseURL = `http://${API_HOSTNAME}:${API_PORT}`; } ReactDOM.render( diff --git a/src/server/controllers/CalculationController.ts b/src/server/controllers/CalculationController.ts new file mode 100644 index 0000000..0042bf3 --- /dev/null +++ b/src/server/controllers/CalculationController.ts @@ -0,0 +1,18 @@ +import { Response } from 'express'; +import { IGetCalculationResponse } from '../../core/types/Calculation/Responses'; +import { IGetCalculationRequest } from '../../core/types/Calculation/Requests'; + +class CalculationController { + static calculate = async ( + req: { body: IGetCalculationRequest }, + res: Response, + ): Promise => { + /** + * prepare Data + * send to core + * send to client + */ + }; +} + +export default CalculationController; diff --git a/src/server/controllers/CalculationController/crmManager.test.ts b/src/server/controllers/CalculationController/crmManager.test.ts deleted file mode 100644 index b6a22ff..0000000 --- a/src/server/controllers/CalculationController/crmManager.test.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { getEntities } from './crmManager'; - -test('getEntities', async () => { - const entities = await getEntities([ - { - entityName: 'evo_baseproduct', - where: { statecode: 0, evo_account_type: [123123132] }, - whereCmp: { - evo_datefrom: { - gte: new Date().toString(), - lte: 55, - }, - }, - fields: ['evo_id', 'evo_name'], - relatedEntities: [ - { - entityName: 'evo_leasingobject_type', - fields: ['evo_id', 'evo_name'], - }, - ], - }, - { - entityName: 'account', - where: { - accountid: 'fdf1db27-a0e3-ea11-af4b-00155d020202', - }, - fields: ['accountid', 'name'], - }, - ]); - - console.log(entities); - expect(entities).not.toBeUndefined(); -}); diff --git a/src/server/controllers/CalculationController/crmManager.ts b/src/server/controllers/CalculationController/crmManager.ts deleted file mode 100644 index 2234d54..0000000 --- a/src/server/controllers/CalculationController/crmManager.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { ApolloClient, gql, HttpLink, InMemoryCache } from '@apollo/client'; -import { TEntities } from 'core/types/Entities/crmEntityNames'; -import fetch from 'cross-fetch'; -import { CRM_SERVICE_URL } from '../../../core/constants/urls'; -import { convertJSONToGQLQuery } from '../../../core/tools/query'; -import { TEntityQuery } from '../../../core/types/Entities/query'; -import { TCRMEntity } from '../../../core/types/Entities/crmEntities'; - -const client = new ApolloClient({ - uri: CRM_SERVICE_URL, - cache: new InMemoryCache(), - link: new HttpLink({ uri: CRM_SERVICE_URL, fetch }), -}); - -export const getEntities = ( - queries: TEntityQuery[], -): Promise> => { - const convertedQuery = convertJSONToGQLQuery(queries); - return client - .query({ - query: gql` - ${convertedQuery} - `, - }) - .then(res => { - return res.data; - }) - .catch(err => { - throw err; - }); - // Convert - // Request to GQL -}; diff --git a/src/server/controllers/CalculationController/index.ts b/src/server/controllers/CalculationController/index.ts deleted file mode 100644 index 0b7cf98..0000000 --- a/src/server/controllers/CalculationController/index.ts +++ /dev/null @@ -1,73 +0,0 @@ -import { TCRMEntity } from './../../../core/types/Entities/crmEntities'; -import { IBaseOption } from 'core/types/Calculation/Store/options'; -import { NextFunction, Request, Response } from 'express'; -import { convertEntityToOption } from '../../../core/tools/entities'; -import { - IGetCalculationResponse, - IGetEntitiesResponse, -} from '../../../core/types/Calculation/Responses'; -import { - IGetCalculationRequest, - IGetEntitiesRequest, -} from './../../../core/types/Calculation/Requests'; -import { - CRMEntityNames, - TEntities, - CRMEntityAliases, -} from '../../../core/types/Entities/crmEntityNames'; -import { getEntities } from './crmManager'; -import { singular, isPlural } from 'pluralize'; - -class CalculationController { - static getCRMEntities = async ( - req: Request, - res: Response, - next: NextFunction, - ): Promise => { - let { queries, toOptions } = req.body as IGetEntitiesRequest; - - queries = queries.filter(query => { - return Object.values(query.where).some( - x => x !== null && x !== undefined, - ); - }); - let resEntities = await getEntities(queries); - if (toOptions) { - let entitiesWithOptions: TEntities = {}; - Object.keys(resEntities).forEach(name => { - const targetEntities: TCRMEntity[] = resEntities[name]; - - let optionatedOptions = []; - for (let entity of targetEntities) { - optionatedOptions.push( - //@ts-ignore - //TODO - convertEntityToOption(entity, entity.__typename), - ); - } - entitiesWithOptions[ - //@ts-ignore - !CRMEntityAliases.includes(name) && isPlural(name) - ? singular(name) - : name - ] = optionatedOptions; - }); - res.send({ entities: entitiesWithOptions }); - return; - } - res.send({ entities: resEntities }); - }; - - static calculate = async ( - req: { body: IGetCalculationRequest }, - res: Response, - ): Promise => { - /** - * prepare Data - * send to core - * send to client - */ - }; -} - -export default CalculationController; diff --git a/src/server/routes/calculation.ts b/src/server/routes/calculation.ts index fc1f6a2..9cb6ad0 100644 --- a/src/server/routes/calculation.ts +++ b/src/server/routes/calculation.ts @@ -1,9 +1,8 @@ -import CalculationController from '../controllers/CalculationController'; import { Router } from 'express'; +import CalculationController from '../controllers/CalculationController'; const router = Router(); -router.post('/getCRMEntities', CalculationController.getCRMEntities); router.post('/calculate', CalculationController.calculate); export default router; diff --git a/src/server/routes/index.ts b/src/server/routes/index.ts index 91c43b7..8b4c68b 100644 --- a/src/server/routes/index.ts +++ b/src/server/routes/index.ts @@ -2,11 +2,13 @@ import { Router } from 'express'; import values from './values'; import users from './users'; import calculation from './calculation'; +import proxy from './proxy'; const routes = Router(); routes.use('/api/values', values); routes.use('/api/users', users); routes.use('/api/calculation', calculation); +routes.use('/proxy', proxy); export default routes; diff --git a/src/server/routes/proxy.ts b/src/server/routes/proxy.ts new file mode 100644 index 0000000..c83d8b6 --- /dev/null +++ b/src/server/routes/proxy.ts @@ -0,0 +1,12 @@ +import { Router } from 'express'; +import proxy from 'express-http-proxy'; +import { + CRM_GRAPHQL_PROXY_URL, + CRM_GRAPHQL_URL, +} from '../../core/constants/urls'; + +const router = Router(); + +router.use(CRM_GRAPHQL_PROXY_URL, proxy(CRM_GRAPHQL_URL)); + +export default router;