From 373a8d9881d95fbac1a8a1a8d8150e5e758dc861 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: Wed, 23 Sep 2020 17:04:09 +0300 Subject: [PATCH] add getinitial options && types --- src/client/Containers/Calculation/index.jsx | 22 +++++++--- src/client/services/CalculationService.ts | 6 +-- src/core/Data/initialOptionsMap.ts | 16 +++++++ src/core/fakeData/entityFakes.ts | 6 +++ src/core/types/Requests/Calculation.ts | 2 +- .../controllers/CalculationController.ts | 44 ++++++++++++------- src/server/routes/calculation.ts | 2 +- 7 files changed, 71 insertions(+), 27 deletions(-) create mode 100644 src/core/Data/initialOptionsMap.ts diff --git a/src/client/Containers/Calculation/index.jsx b/src/client/Containers/Calculation/index.jsx index 52b9e56..4294a1a 100644 --- a/src/client/Containers/Calculation/index.jsx +++ b/src/client/Containers/Calculation/index.jsx @@ -4,22 +4,34 @@ import { Box, Flex } from 'client/UIKit/grid'; import React, { useEffect } from 'react'; import Results from './Results'; import Sections from './Sections'; +import initialOptionsMap from 'core/Data/initialOptionsMap'; const Calculation = () => { const { calculationStore } = useStores(); useEffect(() => { - CalculationService.getEntityOptions({ - entityName: 'lead', - fields: undefined, - where: undefined, + CalculationService.getInitialOptions({ + elementsList: initialOptionsMap, }) .then(options => { - calculationStore.applyOptions({ selectLead: options }); + calculationStore.applyOptions({ ...options }); }) .catch(err => { throw err; }); }, []); + + CalculationService.getEntityOptions({ + entityName: 'lead', + fields: undefined, + where: undefined, + }) + .then(leadOptions => { + calculationStore.applyOptions({ selectLead: leadOptions }); + }) + .catch(err => { + throw err; + }); + return ( diff --git a/src/client/services/CalculationService.ts b/src/client/services/CalculationService.ts index 527a05c..4888e8e 100644 --- a/src/client/services/CalculationService.ts +++ b/src/client/services/CalculationService.ts @@ -7,9 +7,9 @@ import { } from 'core/types/Requests/Calculation'; class CalculationService { - static getInitialOptions = ( - elementsList: TGetInitialOptions, - ): Promise => + static getInitialOptions = ({ + elementsList, + }: TGetInitialOptions): Promise => new Promise((resolve, reject) => { axios .post('/api/calculation/getInitialOptions', { elementsList }) diff --git a/src/core/Data/initialOptionsMap.ts b/src/core/Data/initialOptionsMap.ts new file mode 100644 index 0000000..01e24f4 --- /dev/null +++ b/src/core/Data/initialOptionsMap.ts @@ -0,0 +1,16 @@ +import { TGetInitialOptions } from 'core/types/Requests/Calculation'; + +const initialOptionMap: TGetInitialOptions = { + selectSupplier: { + entityName: 'account', + where: { + evo_account_type: 100000001, + statecode: 0, + }, + whereIn: { + evo_supplier_type: 100000000, + }, + }, +}; + +export default initialOptionMap; diff --git a/src/core/fakeData/entityFakes.ts b/src/core/fakeData/entityFakes.ts index 8d07c77..3d8cf2d 100644 --- a/src/core/fakeData/entityFakes.ts +++ b/src/core/fakeData/entityFakes.ts @@ -29,6 +29,12 @@ const entityFakeData: { accountid: ACCOUNT_1_ID, name: 'Account10101010', }, + { + accountid: '123123123123', + evo_account_type: 100000001, + statecode: 0, + evo_supplier_type: 100000000, + }, ], lead: [ { diff --git a/src/core/types/Requests/Calculation.ts b/src/core/types/Requests/Calculation.ts index a40f26f..7194384 100644 --- a/src/core/types/Requests/Calculation.ts +++ b/src/core/types/Requests/Calculation.ts @@ -3,7 +3,7 @@ import { EntityNames } from 'core/types/Entities/entityNames'; import { ElementsNames } from 'core/types/elements'; export type TGetInitialOptions = { - [elementName in ElementsNames]?: IGetEntity; + elementsList: { [elementName in ElementsNames]?: IGetEntity }; }; export type TGetInitialOptionsResponse = { diff --git a/src/server/controllers/CalculationController.ts b/src/server/controllers/CalculationController.ts index c0b81e2..5cc03ce 100644 --- a/src/server/controllers/CalculationController.ts +++ b/src/server/controllers/CalculationController.ts @@ -1,22 +1,27 @@ import { objectToOption } from '../../core/tools/data'; import { IGetEntity, - IGetInitialData, - IGetInitialDataResponse, + TGetInitialOptions, + TGetInitialOptionsResponse, } from 'core/types/Requests/Calculation'; import { Request, Response } from 'express'; import entityFakeData from '../../core/fakeData/entityFakes'; import { EntityNames } from 'core/types/Entities/entityNames'; +import { IOption } from 'core/types/Calculation/options'; +import { ElementsNames } from 'core/types/elements'; -function _getFakeEntities(entityName, fields, where) { +function _getFakeEntities(entityName, fields?, where?, wherein?): IOption[] { + console.log('where', where); let entities = entityFakeData[entityName]; + let totalWhere = Object.assign({}, where, wherein); if (entities) - if (where) + if (Object.keys(totalWhere).length > 0) entities = entities.filter(entity => { - for (let w in where) { - return entity[w] === where[w]; + for (let w in totalWhere) { + return entity[w] === totalWhere[w]; } + return false; }); return entities; @@ -24,20 +29,25 @@ function _getFakeEntities(entityName, fields, where) { //TODO: move logic from controller to service class CalculationController { - static getInitialData = async ( + static getInitialOptions = async ( req: Request, - res: Response, + res: Response, ): Promise => { - const { username }: IGetInitialData = req.body; - // TODO: get session: options, values, filters - - let leads = _getFakeEntities('lead', undefined, undefined); - console.log('CalculationController -> leads', leads); - // TODO: check leads exist - leads = leads.map(lead => objectToOption(lead, 'lead')); - res.send({ - leads, + const { elementsList }: { elementsList: TGetInitialOptions } = req.body; + console.log('CalculationController -> elementsList', elementsList); + let options = {}; + Object.keys(elementsList).forEach(elementName => { + const element: IGetEntity = elementsList[elementName]; + let opts = _getFakeEntities( + element.entityName, + element.fields, + element.where, + element.whereIn, + ); + opts = opts.map(opt => objectToOption(opt, element.entityName)); + options[elementName] = opts; }); + res.send(options); }; static getEntityOptions = async ( diff --git a/src/server/routes/calculation.ts b/src/server/routes/calculation.ts index 1040e09..c6c2d90 100644 --- a/src/server/routes/calculation.ts +++ b/src/server/routes/calculation.ts @@ -3,7 +3,7 @@ import { Router } from 'express'; const router = Router(); -router.post('/getInitialData', CalculationController.getInitialData); +router.post('/getInitialOptions', CalculationController.getInitialOptions); router.post('/getEntityOptions', CalculationController.getEntityOptions); export default router;