From be06339a7e8af0e2afd52b9cbf38886171d31f90 Mon Sep 17 00:00:00 2001 From: Chika Date: Sun, 27 Sep 2020 21:32:25 +0300 Subject: [PATCH] effects: brand, model, configuration, risk --- .../Calculation/Sections/sectionsList.ts | 2 +- src/client/Containers/Calculation/index.jsx | 7 +- src/client/services/CalculationService.ts | 20 +++ .../CalculationStore/Data/staticEntities.js | 12 ++ .../CalculationStore/Effects/computed.js | 31 +++++ .../CalculationStore/Effects/reaction.ts | 115 ++++++++++++++++++ src/client/stores/CalculationStore/index.ts | 3 + src/core/Data/initialOptionsMap.ts | 6 + src/core/Data/propsMap.ts | 8 +- src/core/Data/staticEntitiesList.ts | 9 ++ src/core/config/initialValues.ts | 3 + src/core/fakeData/entityFakes.ts | 97 +++++++++++++++ src/core/types/Entities/entityNames.ts | 3 +- src/core/types/Entities/index.ts | 13 +- src/core/types/staticData.ts | 9 ++ src/core/types/stores.ts | 5 + src/core/types/values.ts | 5 +- .../controllers/CalculationController.ts | 24 ++++ src/server/routes/calculation.ts | 1 + 19 files changed, 366 insertions(+), 7 deletions(-) create mode 100644 src/client/stores/CalculationStore/Data/staticEntities.js create mode 100644 src/core/Data/staticEntitiesList.ts create mode 100644 src/core/types/staticData.ts diff --git a/src/client/Containers/Calculation/Sections/sectionsList.ts b/src/client/Containers/Calculation/Sections/sectionsList.ts index 9b513fb..8da5cc4 100644 --- a/src/client/Containers/Calculation/Sections/sectionsList.ts +++ b/src/client/Containers/Calculation/Sections/sectionsList.ts @@ -673,7 +673,7 @@ const sections: ISections[] = [ Component: Label, props: { name: 'labelLeaseObjectRisk', - valueName: 'leaseObjectRisk', + computedValue: 'leaseObjectRiskName', }, }, diff --git a/src/client/Containers/Calculation/index.jsx b/src/client/Containers/Calculation/index.jsx index 0c8d06c..a54da2c 100644 --- a/src/client/Containers/Calculation/index.jsx +++ b/src/client/Containers/Calculation/index.jsx @@ -6,6 +6,7 @@ import { useStores } from 'client/hooks/useStores'; import CalculationService from 'client/services/CalculationService'; import { Box, Flex } from 'client/UIKit/grid'; import initialOptionsMap from 'core/Data/initialOptionsMap'; +import staticEntitiesList from 'core/Data/staticEntitiesList'; import React, { useEffect, useState } from 'react'; import Results from './Results'; import Sections from './Sections'; @@ -19,6 +20,9 @@ const Calculation = () => { CalculationService.getInitialOptions({ elementsList: initialOptionsMap, }), + CalculationService.getStaticData({ + staticEntitiesList, + }), CalculationService.getEntityOptions({ entityName: 'lead', fields: undefined, @@ -28,8 +32,9 @@ const Calculation = () => { entityName: 'opportunity', }), ]) - .then(([initialOptions, leadOptions, opportunities]) => { + .then(([initialOptions, staticEntities, leadOptions, opportunities]) => { calculationStore.applyOptions({ ...initialOptions }); + calculationStore.setStaticData(staticEntities); calculationStore.applyOptions({ selectLead: leadOptions }); calculationStore.applyOptions({ selectOpportunity: opportunities }); setReady(true); diff --git a/src/client/services/CalculationService.ts b/src/client/services/CalculationService.ts index 4888e8e..33a1e2f 100644 --- a/src/client/services/CalculationService.ts +++ b/src/client/services/CalculationService.ts @@ -1,3 +1,4 @@ +import { IOption } from 'core/types/Calculation/options'; import axios from 'axios'; import { IGetEntity, @@ -41,6 +42,25 @@ class CalculationService { reject(err); }); }); + + static getStaticData = ({ + staticEntitiesList, + }: { + staticEntitiesList: IGetEntity[]; + }): Promise => + new Promise((resolve, reject) => { + axios + .post('/api/calculation/getStaticEntities', { + staticEntitiesList, + }) + .then(res => { + const { staticEntities } = res.data; + resolve(staticEntities); + }) + .catch(err => { + reject(err); + }); + }); } export default CalculationService; diff --git a/src/client/stores/CalculationStore/Data/staticEntities.js b/src/client/stores/CalculationStore/Data/staticEntities.js new file mode 100644 index 0000000..06332bc --- /dev/null +++ b/src/client/stores/CalculationStore/Data/staticEntities.js @@ -0,0 +1,12 @@ +const staticData = {}; + +const staticDataAction = { + getStaticData(dataName) { + return this.staticData[dataName]; + }, + setStaticData(data) { + this.staticData = { ...this.staticData, ...data }; + }, +}; + +export { staticData, staticDataAction }; diff --git a/src/client/stores/CalculationStore/Effects/computed.js b/src/client/stores/CalculationStore/Effects/computed.js index d7b63b1..5d28c79 100644 --- a/src/client/stores/CalculationStore/Effects/computed.js +++ b/src/client/stores/CalculationStore/Effects/computed.js @@ -1,3 +1,9 @@ +const LEASE_OBJECT_RISK = { + 100000000: 'Низкий', + 100000001: 'Средний', + 100000002: 'Высокий', +}; + const computedEffects = { leadName() { const leadId = this.values.lead; @@ -22,6 +28,31 @@ const computedEffects = { } } }, + leaseObjectRiskName() { + const configurationId = this.values.configuration; + if (configurationId) { + const configuration = this.options.selectConfiguration?.find( + x => x.evo_equipmentid === configurationId, + ); + if (configuration) { + if (configuration.evo_leasingobject_risk) { + const res = LEASE_OBJECT_RISK[configuration.evo_leasingobject_risk]; + return res; + } + } + } + + const modelId = this.values.model; + if (modelId) { + const model = this.options.selectModel?.find( + x => x.evo_modelid === modelId, + ); + if (model) { + const evo_leasingobject_risk = model.evo_leasingobject_risk; + return LEASE_OBJECT_RISK[evo_leasingobject_risk]; + } + } + }, }; export default computedEffects; diff --git a/src/client/stores/CalculationStore/Effects/reaction.ts b/src/client/stores/CalculationStore/Effects/reaction.ts index 21b4a6b..b7b4670 100644 --- a/src/client/stores/CalculationStore/Effects/reaction.ts +++ b/src/client/stores/CalculationStore/Effects/reaction.ts @@ -1300,6 +1300,121 @@ const reactionEffects: IReactionEffect[] = [ } }, }), + + calculationStore => ({ + expression: () => { + const { brand } = calculationStore.values; + return brand; + }, + effect: brandId => { + if (brandId) { + const brand = calculationStore.options.selectBrand?.find( + x => x.evo_brandid === brandId, + ); + if (brand) { + CalculationService.getEntityOptions({ + entityName: 'evo_model', + where: { + statecode: 0, + evo_brandid: brandId, + }, + }).then(models => { + if (models && models.length > 0) { + calculationStore.setStatus('selectModel', Status.Default); + calculationStore.setOptions('selectModel', models); + calculationStore.setValue('model', null); + } + }); + } + } else { + calculationStore.setStatus('selectModel', Status.Disabled); + calculationStore.setOptions('selectModel', []); + calculationStore.setValue('model', null); + } + }, + }), + + calculationStore => ({ + expression: () => { + const { model } = calculationStore.values; + return model; + }, + effect: modelId => { + if (modelId) { + const model = calculationStore.options.selectModel?.find( + x => x.evo_modelid === modelId, + ); + if (model) { + CalculationService.getEntityOptions({ + entityName: 'evo_equipment', + where: { + statecode: 0, + evo_modelid: modelId, + }, + }).then(equipments => { + if (equipments && equipments.length > 0) { + calculationStore.setStatus('selectConfiguration', Status.Default); + calculationStore.setOptions('selectConfiguration', equipments); + calculationStore.setValue('configuration', null); + } + }); + } + } else { + calculationStore.setStatus('selectConfiguration', Status.Disabled); + calculationStore.setOptions('selectConfiguration', []); + calculationStore.setValue('configuration', null); + } + }, + }), + + calculationStore => ({ + expression: () => { + const { model, configuration } = calculationStore.values; + return [model, configuration]; + }, + effect: ([modelId, configurationId]) => { + if (configurationId) { + const configuration = calculationStore.options.selectConfiguration?.find( + x => x.evo_equipmentid === configurationId, + ); + if (configuration) { + if (configuration.evo_impairment_groupid) { + const evo_impairment_groups = calculationStore.getStaticData( + 'evo_impairment_group', + ); + const evo_impairment_group = evo_impairment_groups.find( + x => + x.evo_impairment_groupid === + configuration.evo_impairment_groupid, + ); + calculationStore.setValue( + 'depreciationGroup', + evo_impairment_group ? evo_impairment_group.evo_name : '', + ); + return; + } + } + } + const model = calculationStore.options.selectModel?.find( + x => x.evo_modelid === modelId, + ); + if (model) + if (model.evo_impairment_groupid) { + const evo_impairment_groups = calculationStore.getStaticData( + 'evo_impairment_group', + ); + const evo_impairment_group = evo_impairment_groups.find( + x => x.evo_impairment_groupid === model.evo_impairment_groupid, + ); + calculationStore.setValue( + 'depreciationGroup', + evo_impairment_group ? evo_impairment_group.evo_name : '', + ); + return; + } + calculationStore.setValue('depreciationGroup', null); + }, + }), ]; export default reactionEffects; diff --git a/src/client/stores/CalculationStore/index.ts b/src/client/stores/CalculationStore/index.ts index 46d4dad..02ef7a7 100644 --- a/src/client/stores/CalculationStore/index.ts +++ b/src/client/stores/CalculationStore/index.ts @@ -13,6 +13,7 @@ import { valuesActions, valuesData, } from './Data/index'; +import { staticData, staticDataAction } from './Data/staticEntities'; import autorunEffects from './Effects/autorun'; import computedEffects from './Effects/computed'; import reactionEffects from './Effects/reaction'; @@ -21,6 +22,8 @@ import whenEffects from './Effects/when'; const CalculationStore: ICalculationStore = observable( assignProperties( {}, + staticData, + staticDataAction, valuesData, valuesActions, tablesData, diff --git a/src/core/Data/initialOptionsMap.ts b/src/core/Data/initialOptionsMap.ts index 233d1b7..595a0c3 100644 --- a/src/core/Data/initialOptionsMap.ts +++ b/src/core/Data/initialOptionsMap.ts @@ -55,6 +55,12 @@ const initialOptionMap: TInitialElementsOptions = { selectAccount: { entityName: 'account', }, + selectBrand: { + entityName: 'evo_brand', + where: { + statecode: 0, + }, + }, }; export default initialOptionMap; diff --git a/src/core/Data/propsMap.ts b/src/core/Data/propsMap.ts index 9d967d7..d506033 100644 --- a/src/core/Data/propsMap.ts +++ b/src/core/Data/propsMap.ts @@ -44,11 +44,11 @@ const propsMap: { value: 'evo_leasingobject_typeid', }, evo_brand: { - name: 'evo_brand', + name: 'evo_name', value: 'evo_brandid', }, evo_model: { - name: 'evo_model', + name: 'evo_name', value: 'evo_modelid', }, evo_equipment: { @@ -83,6 +83,10 @@ const propsMap: { name: 'evo_name', value: 'evo_connection_roleid', }, + evo_impairment_group: { + name: 'evo_name', + value: 'evo_impairment_groupid', + }, }; export default propsMap; diff --git a/src/core/Data/staticEntitiesList.ts b/src/core/Data/staticEntitiesList.ts new file mode 100644 index 0000000..3fe308a --- /dev/null +++ b/src/core/Data/staticEntitiesList.ts @@ -0,0 +1,9 @@ +import { IGetEntity } from '../types/Requests/Calculation'; + +const staticEntitiesList: IGetEntity[] = [ + { + entityName: 'evo_impairment_group', + }, +]; + +export default staticEntitiesList; diff --git a/src/core/config/initialValues.ts b/src/core/config/initialValues.ts index f5135e4..bc3f6ed 100644 --- a/src/core/config/initialValues.ts +++ b/src/core/config/initialValues.ts @@ -24,6 +24,9 @@ const initialValues: TValues = { comissionRub: 0, saleBonus: 1.1, IRR_Perc: 20, + brand: null, + model: null, + configuration: null, deliveryTime: 100000000, leaseObjectCount: 1, withTrailer: 100000000, diff --git a/src/core/fakeData/entityFakes.ts b/src/core/fakeData/entityFakes.ts index 32038d4..c646b54 100644 --- a/src/core/fakeData/entityFakes.ts +++ b/src/core/fakeData/entityFakes.ts @@ -83,6 +83,13 @@ 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(), +); + /** * Fake Consts */ @@ -506,6 +513,96 @@ const entityFakeData: { evo_connection_roleid: EVO_CONNECTION_ROLE_3_ID, }, ], + evo_brand: [ + { + evo_brandid: EVO_BRAND_ID[0], + evo_name: 'AUDI', + 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_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], + }, + { + evo_name: 'Группа #2', + evo_impairment_groupid: EVO_IMPAIRMENT_GROUP_ID[1], + }, + ], }; export default entityFakeData; diff --git a/src/core/types/Entities/entityNames.ts b/src/core/types/Entities/entityNames.ts index e225238..c039010 100644 --- a/src/core/types/Entities/entityNames.ts +++ b/src/core/types/Entities/entityNames.ts @@ -18,4 +18,5 @@ export type EntityNames = | 'evo_region' | 'evo_town' | 'connection' - | 'evo_connection_role'; + | 'evo_connection_role' + | 'evo_impairment_group'; diff --git a/src/core/types/Entities/index.ts b/src/core/types/Entities/index.ts index 4a77b1e..3506f4e 100644 --- a/src/core/types/Entities/index.ts +++ b/src/core/types/Entities/index.ts @@ -71,7 +71,10 @@ export interface IEvoBrand extends IBaseOption { export interface IEvoModel extends IBaseOption { evo_name?: string; + evo_brandid?: string; evo_modelid?: string; + evo_impairment_groupid?: string; + evo_leasingobject_risk?: number; statecode?: number; } @@ -79,6 +82,8 @@ export interface IEvoEquipment extends IBaseOption { evo_equipmentid?: string; evo_name?: string; evo_modelid?: string; + evo_impairment_groupid?: string; + evo_leasingobject_risk?: number; statecode?: number; } @@ -135,6 +140,11 @@ export interface IEvoConnectionRole extends IBaseOption { evo_connection_roleid?: string; } +export interface IEvoImpairmentGroup extends IBaseOption { + evo_name?: string; + evo_impairment_groupid?: string; +} + export type TEntity = IAccount & ILead & IOpportunity & @@ -154,4 +164,5 @@ export type TEntity = IAccount & IEvoTown & IEvoContact & IConnection & - IEvoConnectionRole; + IEvoConnectionRole & + IEvoImpairmentGroup; diff --git a/src/core/types/staticData.ts b/src/core/types/staticData.ts new file mode 100644 index 0000000..5c0d26f --- /dev/null +++ b/src/core/types/staticData.ts @@ -0,0 +1,9 @@ +import { EntityNames } from 'core/types/Entities/entityNames'; +import { IOption } from './Calculation/options'; +import { TValue } from './values'; + +export type StaticDataNames = 'evo_impairment_group'; + +export type TStaticData = { + [dataName in StaticDataNames | EntityNames]?: IOption[] | TValue; +}; diff --git a/src/core/types/stores.ts b/src/core/types/stores.ts index 2e900b9..63abd59 100644 --- a/src/core/types/stores.ts +++ b/src/core/types/stores.ts @@ -1,11 +1,16 @@ import { TElementFilter } from 'core/types/Calculation/filters'; import { IBaseOption, IOption } from 'core/types/Calculation/options'; import { ElementsNames, TElements } from './elements'; +import { StaticDataNames, TStaticData } from './staticData'; import { Status } from './statuses'; import { IStoreTable, TableNames, TableValuesNames } from './tables'; import { TValue, TValues, ValuesNames } from './values'; export interface ICalculationStore { + staticData: TStaticData; + getStaticData: (dataName: StaticDataNames) => IOption[]; + setStaticData: (data: TStaticData) => void; + options: TElements; getOptions: (elementName: ElementsNames) => IOption[]; setOptions: (elementName: ElementsNames, options: IOption[]) => void; diff --git a/src/core/types/values.ts b/src/core/types/values.ts index edc6261..7201b83 100644 --- a/src/core/types/values.ts +++ b/src/core/types/values.ts @@ -147,7 +147,10 @@ export type ValuesNames = | 'insuranceTermNS' | 'insuranceTermAddEquipment'; -export type ComputedValuesNames = 'leadName' | 'opportunityName'; +export type ComputedValuesNames = + | 'leadName' + | 'opportunityName' + | 'leaseObjectRiskName'; export type TValues = { [valueName in ValuesNames]?: T; diff --git a/src/server/controllers/CalculationController.ts b/src/server/controllers/CalculationController.ts index 82b910a..49702ab 100644 --- a/src/server/controllers/CalculationController.ts +++ b/src/server/controllers/CalculationController.ts @@ -65,6 +65,30 @@ class CalculationController { entityOptions, }); }; + + static getStaticEntities = async ( + req: Request, + res: Response, + ): Promise => { + const { + staticEntitiesList, + }: { staticEntitiesList: IGetEntity[] } = req.body; + let staticEntities = {}; + staticEntitiesList.forEach(entityQuery => { + const entityList = _getFakeEntities( + entityQuery.entityName, + entityQuery.fields, + entityQuery.where, + entityQuery.whereIn, + ); + if (entityList) { + staticEntities[entityQuery.entityName] = entityList; + } + }); + res.send({ + staticEntities, + }); + }; } export default CalculationController; diff --git a/src/server/routes/calculation.ts b/src/server/routes/calculation.ts index c6c2d90..d6ce261 100644 --- a/src/server/routes/calculation.ts +++ b/src/server/routes/calculation.ts @@ -5,5 +5,6 @@ const router = Router(); router.post('/getInitialOptions', CalculationController.getInitialOptions); router.post('/getEntityOptions', CalculationController.getEntityOptions); +router.post('/getStaticEntities', CalculationController.getStaticEntities); export default router;