clean code
This commit is contained in:
parent
6fe0fa1faa
commit
38793c28d2
@ -1,151 +1,24 @@
|
||||
import { LoadingStatus } from 'client/common/loadingStatus';
|
||||
import Spinner from 'client/Components/Spinner';
|
||||
import { withStoreModal } from 'client/hocs/withStore';
|
||||
import { useStores } from 'client/hooks/useStores';
|
||||
import CalculationService from 'client/services/CalculationService';
|
||||
import { Box, Flex } from 'client/UIKit/grid';
|
||||
import initialOptions from 'core/Data/initialOptions';
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import Result from 'client/Components/Result';
|
||||
import Spinner from 'client/Components/Spinner';
|
||||
import Modal from 'client/Elements/Modal';
|
||||
import { withStoreModal } from 'client/hocs/withStore';
|
||||
import { Box, Flex } from 'client/UIKit/grid';
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import fetchData from './lib/fetchData';
|
||||
import Results from './Results';
|
||||
import Sections from './Sections';
|
||||
import { gql } from '@apollo/client';
|
||||
import { currentDate } from 'client/common/constants';
|
||||
import { getUser } from 'client/tools/user';
|
||||
|
||||
const Calculation = () => {
|
||||
const [status, setStatus] = useState(LoadingStatus.loading);
|
||||
const { calculationStore } = useStores();
|
||||
//TODO: move to external file
|
||||
useEffect(() => {
|
||||
getUser().then(({ UserName, DomainName }) => {
|
||||
Promise.all([
|
||||
CalculationService.getEntities({
|
||||
queries: initialOptions,
|
||||
}),
|
||||
CalculationService.crmgqlquery({
|
||||
query: gql`
|
||||
query($statecode: Int, $currentDate: DateTime) {
|
||||
evo_impairment_group: evo_impairment_groups(
|
||||
statecode: $statecode
|
||||
) {
|
||||
evo_impairment_groupid
|
||||
evo_name
|
||||
}
|
||||
evo_currencychange: evo_currencychanges(
|
||||
statecode: $statecode
|
||||
evo_coursedate_param: { eq: $currentDate }
|
||||
) {
|
||||
evo_currencychange
|
||||
evo_ref_transactioncurrency
|
||||
}
|
||||
evo_coefficient: evo_coefficients(
|
||||
statecode: $statecode
|
||||
evo_datefrom_param: { lte: $currentDate }
|
||||
evo_dateto_param: { gte: $currentDate }
|
||||
) {
|
||||
evo_correction_coefficient
|
||||
evo_graph_type
|
||||
evo_season_type
|
||||
evo_job_titleid
|
||||
evo_sot_coefficient_typeid
|
||||
evo_sot_coefficient
|
||||
evo_corfficient_type
|
||||
evo_min_period
|
||||
evo_max_period
|
||||
evo_graph_type
|
||||
evo_season_type
|
||||
evo_correction_coefficient
|
||||
evo_client_riskid
|
||||
evo_client_typeid
|
||||
evo_risk_delta
|
||||
evo_leasingobject_types {
|
||||
evo_name
|
||||
evo_id
|
||||
evo_leasingobject_typeid
|
||||
}
|
||||
}
|
||||
evo_sot_coefficient_type: evo_sot_coefficient_types(
|
||||
statecode: $statecode
|
||||
) {
|
||||
evo_id
|
||||
evo_name
|
||||
evo_sot_coefficient_typeid
|
||||
}
|
||||
evo_job_title: evo_job_titles(statecode: $statecode) {
|
||||
evo_id
|
||||
evo_name
|
||||
evo_job_titleid
|
||||
}
|
||||
}
|
||||
`,
|
||||
variables: {
|
||||
statecode: 0,
|
||||
currentDate,
|
||||
},
|
||||
}),
|
||||
CalculationService.crmgqlquery({
|
||||
query: gql`
|
||||
query($username: String) {
|
||||
systemuser(domainname: $username) {
|
||||
evo_job_titleid
|
||||
businessunitid
|
||||
firstname
|
||||
lastname
|
||||
fullname
|
||||
}
|
||||
}
|
||||
`,
|
||||
variables: { username: `${DomainName}\\${UserName}` },
|
||||
}),
|
||||
CalculationService.getEntities({
|
||||
queries: [
|
||||
{
|
||||
alias: 'insuranceCompany',
|
||||
entityName: 'account',
|
||||
where: { evo_account_type: 100000002, statecode: 0 },
|
||||
fields: ['accountid', 'name', 'evo_type_ins_policy'],
|
||||
many: true,
|
||||
},
|
||||
],
|
||||
}),
|
||||
])
|
||||
.then(
|
||||
([
|
||||
{ entities: initialOptions },
|
||||
{ entities: staticEntities },
|
||||
{
|
||||
entities: { systemuser },
|
||||
},
|
||||
{ entities: insuranceCompanies },
|
||||
]) => {
|
||||
console.log('Calculation -> staticEntities', staticEntities);
|
||||
calculationStore.applyOptions(initialOptions);
|
||||
calculationStore.applyStaticData(staticEntities);
|
||||
calculationStore.applyStaticData({ systemuser: [systemuser] });
|
||||
calculationStore.setTableColumns('tableInsurance')({
|
||||
options: { ...insuranceCompanies },
|
||||
});
|
||||
|
||||
//TODO: move to external file
|
||||
var supplierCurrency = calculationStore.options.selectSupplierCurrency.find(
|
||||
x => x.isocurrencycode === 'RUB',
|
||||
);
|
||||
calculationStore.setValue(
|
||||
'supplierCurrency',
|
||||
supplierCurrency.transactioncurrencyid,
|
||||
);
|
||||
|
||||
setStatus(LoadingStatus.ready);
|
||||
},
|
||||
)
|
||||
.catch(err => {
|
||||
setStatus(LoadingStatus.error);
|
||||
throw err;
|
||||
});
|
||||
});
|
||||
//TODO: makes 2 requests, need 1
|
||||
fetchData()
|
||||
.then(() => {
|
||||
setStatus(LoadingStatus.ready);
|
||||
})
|
||||
.catch(() => {
|
||||
setStatus(LoadingStatus.error);
|
||||
});
|
||||
}, []);
|
||||
|
||||
if (status === LoadingStatus.loading) {
|
||||
|
||||
136
src/client/Containers/Calculation/lib/fetchData.js
Normal file
136
src/client/Containers/Calculation/lib/fetchData.js
Normal file
@ -0,0 +1,136 @@
|
||||
import { gql } from '@apollo/client';
|
||||
import { currentDate } from 'client/common/constants';
|
||||
import CalculationService from 'client/services/CalculationService';
|
||||
import CalculationStore from 'client/stores/CalculationStore';
|
||||
import { getUser } from 'client/tools/user';
|
||||
import initialOptions from 'core/Data/initialOptions';
|
||||
|
||||
export default () =>
|
||||
new Promise((resolve, reject) => {
|
||||
getUser().then(({ UserName, DomainName }) => {
|
||||
Promise.all([
|
||||
CalculationService.getEntities({
|
||||
queries: initialOptions,
|
||||
}),
|
||||
CalculationService.crmgqlquery({
|
||||
query: gql`
|
||||
query($statecode: Int, $currentDate: DateTime) {
|
||||
evo_impairment_group: evo_impairment_groups(
|
||||
statecode: $statecode
|
||||
) {
|
||||
evo_impairment_groupid
|
||||
evo_name
|
||||
}
|
||||
evo_currencychange: evo_currencychanges(
|
||||
statecode: $statecode
|
||||
evo_coursedate_param: { eq: $currentDate }
|
||||
) {
|
||||
evo_currencychange
|
||||
evo_ref_transactioncurrency
|
||||
}
|
||||
evo_coefficient: evo_coefficients(
|
||||
statecode: $statecode
|
||||
evo_datefrom_param: { lte: $currentDate }
|
||||
evo_dateto_param: { gte: $currentDate }
|
||||
) {
|
||||
evo_correction_coefficient
|
||||
evo_graph_type
|
||||
evo_season_type
|
||||
evo_job_titleid
|
||||
evo_sot_coefficient_typeid
|
||||
evo_sot_coefficient
|
||||
evo_corfficient_type
|
||||
evo_min_period
|
||||
evo_max_period
|
||||
evo_graph_type
|
||||
evo_season_type
|
||||
evo_correction_coefficient
|
||||
evo_client_riskid
|
||||
evo_client_typeid
|
||||
evo_risk_delta
|
||||
evo_leasingobject_types {
|
||||
evo_name
|
||||
evo_id
|
||||
evo_leasingobject_typeid
|
||||
}
|
||||
}
|
||||
evo_sot_coefficient_type: evo_sot_coefficient_types(
|
||||
statecode: $statecode
|
||||
) {
|
||||
evo_id
|
||||
evo_name
|
||||
evo_sot_coefficient_typeid
|
||||
}
|
||||
evo_job_title: evo_job_titles(statecode: $statecode) {
|
||||
evo_id
|
||||
evo_name
|
||||
evo_job_titleid
|
||||
}
|
||||
}
|
||||
`,
|
||||
variables: {
|
||||
statecode: 0,
|
||||
currentDate,
|
||||
},
|
||||
}),
|
||||
CalculationService.crmgqlquery({
|
||||
query: gql`
|
||||
query($username: String) {
|
||||
systemuser(domainname: $username) {
|
||||
evo_job_titleid
|
||||
businessunitid
|
||||
firstname
|
||||
lastname
|
||||
fullname
|
||||
}
|
||||
}
|
||||
`,
|
||||
variables: { username: `${DomainName}\\${UserName}` },
|
||||
}),
|
||||
CalculationService.getEntities({
|
||||
queries: [
|
||||
{
|
||||
//@ts-ignore
|
||||
alias: 'insuranceCompany',
|
||||
entityName: 'account',
|
||||
where: { evo_account_type: 100000002, statecode: 0 },
|
||||
fields: ['accountid', 'name', 'evo_type_ins_policy'],
|
||||
many: true,
|
||||
},
|
||||
],
|
||||
}),
|
||||
])
|
||||
.then(
|
||||
([
|
||||
{ entities: initialOptions },
|
||||
{ entities: staticEntities },
|
||||
{
|
||||
entities: { systemuser },
|
||||
},
|
||||
{ entities: insuranceCompanies },
|
||||
]) => {
|
||||
CalculationStore.applyOptions(initialOptions);
|
||||
CalculationStore.applyStaticData(staticEntities);
|
||||
CalculationStore.applyStaticData({ systemuser: [systemuser] });
|
||||
CalculationStore.setTableColumns('tableInsurance')({
|
||||
options: { ...insuranceCompanies },
|
||||
});
|
||||
|
||||
var supplierCurrency = CalculationStore.options.selectSupplierCurrency?.find(
|
||||
x => x.isocurrencycode === 'RUB',
|
||||
);
|
||||
if (supplierCurrency)
|
||||
CalculationStore.setValue(
|
||||
'supplierCurrency',
|
||||
supplierCurrency.transactioncurrencyid,
|
||||
);
|
||||
|
||||
resolve();
|
||||
},
|
||||
)
|
||||
.catch(err => {
|
||||
reject(err);
|
||||
throw err;
|
||||
});
|
||||
});
|
||||
});
|
||||
@ -4,19 +4,19 @@ import { getServerUrl } from 'client/common/urls';
|
||||
import {
|
||||
CORE_PROXY_URL,
|
||||
CRM_GRAPHQL_PROXY_URL,
|
||||
CRM_GRAPHQL_URL
|
||||
CRM_GRAPHQL_URL,
|
||||
} from 'core/constants/urls';
|
||||
import { convertEntityToOption } from 'core/tools/entities';
|
||||
import { convertJSONToGQLQuery } from 'core/tools/query';
|
||||
import {
|
||||
IGetCalculationRequest,
|
||||
IGetEntitiesRequest,
|
||||
IRequestToCRMGQL
|
||||
IRequestToCRMGQL,
|
||||
} from 'core/types/Calculation/Requests';
|
||||
import {
|
||||
IGetCalculationResponse,
|
||||
IGetEntitiesResponse,
|
||||
IGetUserResponse
|
||||
IGetUserResponse,
|
||||
} from 'core/types/Calculation/Responses';
|
||||
import { IBaseOption } from 'core/types/Calculation/Store/options';
|
||||
import { TCRMEntity } from 'core/types/Entities/crmEntities';
|
||||
@ -43,7 +43,6 @@ class CalculationService {
|
||||
static getUser = (): Promise<IGetUserResponse> =>
|
||||
new Promise((resolve, reject) => {
|
||||
axios.get('/api/users/getUser').then(res => {
|
||||
console.log('User', res.data);
|
||||
resolve(res.data);
|
||||
});
|
||||
});
|
||||
@ -67,7 +66,6 @@ class CalculationService {
|
||||
preparedData,
|
||||
)
|
||||
.then(res => {
|
||||
console.log(res);
|
||||
resolve(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
@ -99,8 +97,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)[] = [];
|
||||
|
||||
@ -1,20 +0,0 @@
|
||||
import { convertJSONToGQL } from './query';
|
||||
|
||||
it('query converter works', () => {
|
||||
const res = convertJSONToGQL([
|
||||
{
|
||||
entityName: 'evo_baseproducts',
|
||||
where: { statecode: 0 },
|
||||
fields: ['evo_id', 'evo_name'],
|
||||
relatedEntities: [
|
||||
{
|
||||
entityName: 'evo_leasingobject_types',
|
||||
fields: ['evo_id', 'evo_name'],
|
||||
},
|
||||
],
|
||||
},
|
||||
]);
|
||||
|
||||
console.log(res);
|
||||
expect(res).not.toBeUndefined();
|
||||
});
|
||||
@ -1,3 +0,0 @@
|
||||
class CalculationController {}
|
||||
|
||||
export default CalculationController;
|
||||
@ -1,12 +0,0 @@
|
||||
import { Request, Response } from "express";
|
||||
|
||||
class ValuesController {
|
||||
static getValue = async (req: Request, res: Response): Promise<void> => {
|
||||
res.send({
|
||||
a: 1,
|
||||
b: 2,
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
export default ValuesController;
|
||||
@ -13,14 +13,10 @@ import { SERVER_PORT } from './../core/constants/urls';
|
||||
|
||||
const isDevelopmentMode = process.env.NODE_ENV === 'development';
|
||||
|
||||
const buildDir = path.join(
|
||||
process.cwd(),
|
||||
process.env.NODE_ENV === 'development' ? '/build' : '',
|
||||
);
|
||||
const buildDir = path.join(process.cwd(), isDevelopmentMode ? '/build' : '');
|
||||
|
||||
const app = express();
|
||||
|
||||
app.use(cors({ origin: isDevelopmentMode && '*', credentials: false }));
|
||||
/** AUTHENTICATION */
|
||||
if (!isDevelopmentMode)
|
||||
app.use(
|
||||
@ -32,14 +28,14 @@ if (!isDevelopmentMode)
|
||||
/** AUTHENTICATION */
|
||||
|
||||
/**EXTENTIONS */
|
||||
app.use(cors({ origin: isDevelopmentMode && '*', credentials: true }));
|
||||
app.use(helmet({ contentSecurityPolicy: { reportOnly: true } }));
|
||||
app.use(bodyParser.json());
|
||||
app.use(
|
||||
bodyParser.urlencoded({
|
||||
extended: true,
|
||||
}),
|
||||
);
|
||||
app.use(helmet({ contentSecurityPolicy: { reportOnly: true } }));
|
||||
|
||||
// app.use(cookieParser());
|
||||
app.use(compression());
|
||||
app.use(morgan(isDevelopmentMode ? 'dev' : 'tiny'));
|
||||
|
||||
@ -1,6 +0,0 @@
|
||||
import { Router } from 'express';
|
||||
import CalculationController from '../controllers/CalculationController';
|
||||
|
||||
const router = Router();
|
||||
|
||||
export default router;
|
||||
@ -1,14 +1,10 @@
|
||||
import { Router } from 'express';
|
||||
import values from './values';
|
||||
import users from './users';
|
||||
import calculation from './calculation';
|
||||
import proxy from './proxy';
|
||||
|
||||
const routes = Router();
|
||||
|
||||
routes.use('/api/values', values);
|
||||
routes.use('/api/users', users);
|
||||
routes.use('/api/calculation', calculation);
|
||||
routes.use('/proxy', proxy);
|
||||
|
||||
export default routes;
|
||||
|
||||
@ -1,8 +0,0 @@
|
||||
import ValuesController from "../controllers/ValuesController";
|
||||
import { Router } from "express";
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.get("/", ValuesController.getValue);
|
||||
|
||||
export default router;
|
||||
Reference in New Issue
Block a user