effects: brand, model, configuration, risk

This commit is contained in:
Chika 2020-09-27 21:32:25 +03:00
parent 99cce9a97d
commit be06339a7e
19 changed files with 366 additions and 7 deletions

View File

@ -673,7 +673,7 @@ const sections: ISections[] = [
Component: Label,
props: {
name: 'labelLeaseObjectRisk',
valueName: 'leaseObjectRisk',
computedValue: 'leaseObjectRiskName',
},
},

View File

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

View File

@ -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<IOption[]> =>
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;

View File

@ -0,0 +1,12 @@
const staticData = {};
const staticDataAction = {
getStaticData(dataName) {
return this.staticData[dataName];
},
setStaticData(data) {
this.staticData = { ...this.staticData, ...data };
},
};
export { staticData, staticDataAction };

View File

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

View File

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

View File

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

View File

@ -55,6 +55,12 @@ const initialOptionMap: TInitialElementsOptions = {
selectAccount: {
entityName: 'account',
},
selectBrand: {
entityName: 'evo_brand',
where: {
statecode: 0,
},
},
};
export default initialOptionMap;

View File

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

View File

@ -0,0 +1,9 @@
import { IGetEntity } from '../types/Requests/Calculation';
const staticEntitiesList: IGetEntity[] = [
{
entityName: 'evo_impairment_group',
},
];
export default staticEntitiesList;

View File

@ -24,6 +24,9 @@ const initialValues: TValues<TValue> = {
comissionRub: 0,
saleBonus: 1.1,
IRR_Perc: 20,
brand: null,
model: null,
configuration: null,
deliveryTime: 100000000,
leaseObjectCount: 1,
withTrailer: 100000000,

View File

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

View File

@ -18,4 +18,5 @@ export type EntityNames =
| 'evo_region'
| 'evo_town'
| 'connection'
| 'evo_connection_role';
| 'evo_connection_role'
| 'evo_impairment_group';

View File

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

View File

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

View File

@ -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<IOption[]>;
getOptions: (elementName: ElementsNames) => IOption[];
setOptions: (elementName: ElementsNames, options: IOption[]) => void;

View File

@ -147,7 +147,10 @@ export type ValuesNames =
| 'insuranceTermNS'
| 'insuranceTermAddEquipment';
export type ComputedValuesNames = 'leadName' | 'opportunityName';
export type ComputedValuesNames =
| 'leadName'
| 'opportunityName'
| 'leaseObjectRiskName';
export type TValues<T> = {
[valueName in ValuesNames]?: T;

View File

@ -65,6 +65,30 @@ class CalculationController {
entityOptions,
});
};
static getStaticEntities = async (
req: Request,
res: Response,
): Promise<any> => {
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;

View File

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