add getinitial options && types

This commit is contained in:
Владислав Чикалкин 2020-09-23 17:04:09 +03:00
parent a9a0b0885c
commit 373a8d9881
7 changed files with 71 additions and 27 deletions

View File

@ -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 (
<Box mx={['0', '1%', '1%', '1.5%', '2%', '10%']}>
<Flex flexWrap="wrap" mb="50px">

View File

@ -7,9 +7,9 @@ import {
} from 'core/types/Requests/Calculation';
class CalculationService {
static getInitialOptions = (
elementsList: TGetInitialOptions,
): Promise<TGetInitialOptionsResponse> =>
static getInitialOptions = ({
elementsList,
}: TGetInitialOptions): Promise<TGetInitialOptionsResponse> =>
new Promise((resolve, reject) => {
axios
.post('/api/calculation/getInitialOptions', { elementsList })

View File

@ -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;

View File

@ -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: [
{

View File

@ -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 = {

View File

@ -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<IGetInitialDataResponse>,
res: Response,
): Promise<any> => {
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 (

View File

@ -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;