diff --git a/src/client/common/urls.js b/src/client/common/urls.js index c2ddc0e..1ed11b4 100644 --- a/src/client/common/urls.js +++ b/src/client/common/urls.js @@ -2,10 +2,10 @@ import { SERVER_PORT } from 'core/constants/urls'; export const API_HOSTNAME = window.location.hostname; -export const getServerUrl = url => +export const getServerUrl = (...url) => String.prototype.concat( (process.env.NODE_ENV === 'development' && `http://${API_HOSTNAME}:${SERVER_PORT}`) || '', - url || '', + ...url, ); diff --git a/src/client/services/CalculationService.ts b/src/client/services/CalculationService.ts index b395c30..96e160b 100644 --- a/src/client/services/CalculationService.ts +++ b/src/client/services/CalculationService.ts @@ -1,12 +1,7 @@ -import { IMutateToCRMGQL } from './../../core/types/Calculation/Requests'; import { ApolloClient, gql, HttpLink, InMemoryCache } from '@apollo/client'; import axios from 'axios'; import { getServerUrl } from 'client/common/urls'; -import { - CORE_PROXY_URL, - CRM_GRAPHQL_PROXY_URL, - CRM_GRAPHQL_URL, -} from 'core/constants/urls'; +import { CORE_PROXY_URL, CRM_PROXY_URL, CRM_URL } from 'core/constants/urls'; import { convertEntityToOption } from 'core/tools/entities'; import { convertJSONToGQLQuery } from 'core/tools/query'; import { @@ -20,16 +15,20 @@ import { IGetUserResponse, } from 'core/types/Calculation/Responses'; import { IBaseOption } from 'core/types/Calculation/Store/options'; -import { TCRMEntity } from 'core/types/Entities/crmEntities'; +import { IQuote, TCRMEntity } from 'core/types/Entities/crmEntities'; import { TEntities } from 'core/types/Entities/crmEntityNames'; import { isPlural, singular } from 'pluralize'; +import { + ICreateKpRequest, + IMutateToCRMGQL, +} from './../../core/types/Calculation/Requests'; const client = new ApolloClient({ - uri: CRM_GRAPHQL_URL, + uri: CRM_URL, cache: new InMemoryCache(), link: new HttpLink({ - uri: getServerUrl(String.prototype.concat('/proxy', CRM_GRAPHQL_PROXY_URL)), + uri: getServerUrl('/proxy', CRM_PROXY_URL), }), defaultOptions: { @@ -43,9 +42,14 @@ const client = new ApolloClient({ class CalculationService { static getUser = (): Promise => new Promise((resolve, reject) => { - axios.get('/api/users/getUser').then(res => { - resolve(res.data); - }); + axios + .get('/api/users/getUser') + .then(res => { + resolve(res.data); + }) + .catch(err => { + throw err; + }); }); static calculate = ( @@ -55,14 +59,12 @@ class CalculationService { axios .post( getServerUrl( - String.prototype.concat( - '/proxy', - CORE_PROXY_URL, - '/api', - '/v1', - '/calculation', - '/calculate', - ), + '/proxy', + CORE_PROXY_URL, + '/api', + '/v1', + '/calculation', + '/calculate', ), preparedData, ) @@ -98,8 +100,8 @@ class CalculationService { resEntities[targetName]; if (toOptions) - //@ts-ignore if (toOptions.includes(targetName)) { + //@ts-ignore //@ts-ignore if (Array.isArray(targetEnt)) { let optionatedEntities: (TCRMEntity & IBaseOption)[] = []; @@ -160,6 +162,21 @@ class CalculationService { }); }); + static createKp = (data: ICreateKpRequest): Promise => + new Promise(async (resolve, reject) => { + axios + .post( + getServerUrl('/proxy', CRM_PROXY_URL, '/offer', '/leasingcalculator'), + data, + ) + .then(res => { + resolve(res.data); + }) + .catch(err => { + reject(err); + }); + }); + /** * @deprecated */ diff --git a/src/client/stores/CalculationStore/Effects/action.ts b/src/client/stores/CalculationStore/Effects/action.ts index 6208420..01ecf39 100644 --- a/src/client/stores/CalculationStore/Effects/action.ts +++ b/src/client/stores/CalculationStore/Effects/action.ts @@ -174,50 +174,37 @@ const actions: TAction = { console.log('domainName', domainName); // } - CalculationService.crmgqlmutaion({ - mutation: gql` - mutation CreateOffer( - $postValues: PostValuesInput - $columns: CalculationColumnsInput - $preparedValues: PreparedValuesInput - $preparedPayments: PreparedPaymentsInput - $values: CalculationValuesInput - $domainName: String - ) { - createBy(domainName: $domainName) { - createOfferLeasingCalculator( - calculation: { - calculationColumns: $columns - postValues: $postValues - preparedValues: $preparedValues - preparedPayments: $preparedPayments - calculationValues: $values - } - ) { - offerprintform - evo_quotename - } - } - } - `, - variables: { - values, - columns, - postValues, - preparedValues, - preparedPayments, - domainName, - }, + CalculationService.createKp({ + domainName, + preparedValues, + preparedPayments, + calculationColumns: columns, + postValues, + calculationValues: values, }) - .then(({ entities: { quote } }) => { - message.success({ - content: `КП ${quote.evo_quotename} создано!`, - duration: 5, - style: { - marginTop: '7vh', + .then(({ quoteid }) => { + CalculationService.crmgqlquery({ + query: gql` + query($quoteid: String) { + quote(quoteId: $quoteid) { + offerprintform + evo_quotename + } + } + `, + variables: { + quoteid, }, + }).then(({ entities: { quote } }) => { + message.success({ + content: `КП ${quote.evo_quotename} создано!`, + duration: 5, + style: { + marginTop: '7vh', + }, + }); + if (quote.offerprintform) window.open(quote.offerprintform, '_blank'); }); - if (quote.offerprintform) window.open(quote.offerprintform, '_blank'); }) .catch(err => { throw err; diff --git a/src/core/constants/urls.js b/src/core/constants/urls.js index b6d1165..7fa2458 100644 --- a/src/core/constants/urls.js +++ b/src/core/constants/urls.js @@ -1,9 +1,9 @@ export const SERVER_PORT = process.env.NODE_ENV === 'development' ? 3001 : 80; -export const CRM_GRAPHQL_URL = +export const CRM_URL = process.env.crm !== 'prod' ? 'http://crmgraphql-dev.evoleasing.ru' : 'http://crm-graphql.evoleasing.ru'; -export const CRM_GRAPHQL_PROXY_URL = '/crmgraphql'; +export const CRM_PROXY_URL = '/crmgraphql'; export const CORE_URL = process.env.NODE_ENV === 'development' ? 'http://localhost:5000' diff --git a/src/core/types/Calculation/Requests.ts b/src/core/types/Calculation/Requests.ts index a8fe95d..aa8bba8 100644 --- a/src/core/types/Calculation/Requests.ts +++ b/src/core/types/Calculation/Requests.ts @@ -4,6 +4,8 @@ import { } from 'core/types/Calculation/Prepare'; import { CRMEntityNames } from 'core/types/Entities/crmEntityNames'; import { TEntityQuery } from '../Entities/query'; +import { ColumnsNames, IColumn, PostValues } from 'core/types/Calculation/Core'; +import { TValues } from 'core/types/Calculation/Store/values'; export interface IGetEntitiesRequest { queries: TEntityQuery[]; @@ -24,3 +26,12 @@ export interface IGetCalculationRequest { preparedPayments: PreparedPayments; preparedValues: PreparedValues; } + +export interface ICreateKpRequest { + domainName: string; + preparedValues: PreparedValues; + preparedPayments: PreparedPayments; + calculationColumns: { [column in ColumnsNames]?: IColumn }; + postValues: PostValues; + calculationValues: TValues; +} diff --git a/src/server/routes/proxy.ts b/src/server/routes/proxy.ts index 98b7f5d..061db90 100644 --- a/src/server/routes/proxy.ts +++ b/src/server/routes/proxy.ts @@ -1,15 +1,15 @@ import { Router } from 'express'; import proxy from 'express-http-proxy'; import { - CRM_GRAPHQL_PROXY_URL, - CRM_GRAPHQL_URL, + CRM_PROXY_URL, + CRM_URL, CORE_URL, CORE_PROXY_URL, } from '../../core/constants/urls'; const router = Router(); -router.use(CRM_GRAPHQL_PROXY_URL, proxy(CRM_GRAPHQL_URL)); +router.use(CRM_PROXY_URL, proxy(CRM_URL)); router.use(CORE_PROXY_URL, proxy(CORE_URL)); export default router;