From ea3f4bb52e1feef256d234ceab2b98ad6429e994 Mon Sep 17 00:00:00 2001 From: Chika Date: Mon, 18 Jul 2022 13:26:45 +0300 Subject: [PATCH] process/init: split requests to files --- pages/index.jsx | 29 +-- process/init/get-addproduct-types-data.ts | 62 +++++ process/init/get-brands.ts | 26 ++ process/init/get-data.ts | 299 ---------------------- process/init/get-dealers.ts | 26 ++ process/init/get-insurance-data.ts | 58 +++++ process/init/get-main-data.ts | 106 ++++++++ process/init/get-owner-data.ts | 34 +++ process/init/index.js | 29 +++ 9 files changed, 343 insertions(+), 326 deletions(-) create mode 100644 process/init/get-addproduct-types-data.ts create mode 100644 process/init/get-brands.ts delete mode 100644 process/init/get-data.ts create mode 100644 process/init/get-dealers.ts create mode 100644 process/init/get-insurance-data.ts create mode 100644 process/init/get-main-data.ts create mode 100644 process/init/get-owner-data.ts create mode 100644 process/init/index.js diff --git a/pages/index.jsx b/pages/index.jsx index 89c42e8..c05b548 100644 --- a/pages/index.jsx +++ b/pages/index.jsx @@ -6,14 +6,7 @@ import Output from 'Components/Output'; import Head from 'next/head'; import * as agentsReactions from 'process/agents/reactions'; import * as calculateReactions from 'process/calculate/reactions'; -import { - getAddProductTypes, - getBrands, - getDealers, - getInsuranceData, - getMainData, - getOwnerData, -} from 'process/init/get-data'; +import getData, { getOwnerData } from 'process/init'; import * as leadOpportunityReactions from 'process/lead-opportunity/reactions'; import paymentsReactions from 'process/payments/reactions'; import { useEffect } from 'react'; @@ -60,25 +53,7 @@ function Home() { const apolloClient = useApolloClient(); useEffect(() => { - getMainData(apolloClient).then(({ options }) => { - store.$calculation.$options.setManyOptions(options); - }); - - getBrands(apolloClient).then(({ options }) => { - store.$calculation.$options.setManyOptions(options); - }); - - getDealers(apolloClient).then(({ options }) => { - store.$calculation.$options.setManyOptions(options); - }); - - getAddProductTypes(apolloClient).then(({ options }) => { - store.$calculation.$options.setManyOptions(options); - }); - - getInsuranceData(apolloClient).then(({ tables }) => { - store.$tables.insurance.setManyRowOptions(tables.insurance); - }); + getData(apolloClient, store); injectReactions(store, apolloClient); }, []); diff --git a/process/init/get-addproduct-types-data.ts b/process/init/get-addproduct-types-data.ts new file mode 100644 index 0000000..6f0bcd3 --- /dev/null +++ b/process/init/get-addproduct-types-data.ts @@ -0,0 +1,62 @@ +import type { ApolloClient } from '@apollo/client'; +import { gql } from '@apollo/client'; +import type { GetAddproductTypes } from './__generated__/GetAddproductTypes'; + +const QUERY_GET_ADDPRODUCT_TYPES = gql` + query GetAddproductTypes { + evo_addproduct_types(statecode: 0) { + label: evo_name + value: evo_addproduct_typeid + evo_graph_price + evo_product_type + } + } +`; + +export default async function getAddProductTypes(apolloClient: ApolloClient) { + const { data: addproductTypes } = await apolloClient.query({ + query: QUERY_GET_ADDPRODUCT_TYPES, + }); + + const selectRegistration = addproductTypes.evo_addproduct_types + ?.filter((x) => x?.evo_product_type === 100_000_001) + .map((x) => ({ + ...x, + label: `${x?.label} (${x?.evo_graph_price} руб.)`, + })); + + const selectTechnicalCard = addproductTypes.evo_addproduct_types + ?.filter((x) => x?.evo_product_type === 100_000_000) + .map((x) => ({ + ...x, + label: `${x?.label} (${x?.evo_graph_price} руб.)`, + })); + + const selectTelematic = addproductTypes.evo_addproduct_types + ?.filter((x) => x?.evo_product_type === 100_000_004) + .map((x) => ({ + ...x, + label: `${x?.label} (${x?.evo_graph_price} руб.)`, + })); + + const selectTracker = addproductTypes.evo_addproduct_types + ?.filter((x) => x?.evo_product_type === 100_000_003) + .map((x) => ({ + ...x, + label: `${x?.label} (${x?.evo_graph_price} руб.)`, + })); + + const selectInsNSIB = addproductTypes.evo_addproduct_types?.filter( + (x) => x?.evo_product_type === 100_000_002 + ); + + return { + options: { + selectRegistration, + selectTechnicalCard, + selectTelematic, + selectTracker, + selectInsNSIB, + }, + }; +} diff --git a/process/init/get-brands.ts b/process/init/get-brands.ts new file mode 100644 index 0000000..841225c --- /dev/null +++ b/process/init/get-brands.ts @@ -0,0 +1,26 @@ +import type { ApolloClient } from '@apollo/client'; +import { gql } from '@apollo/client'; +import type { GetBrands } from './__generated__/GetBrands'; + +const QUERY_GET_BRANDS = gql` + query GetBrands { + selectBrand: evo_brands(statecode: 0) { + label: evo_name + value: evo_brandid + } + } +`; + +export default async function getBrands(apolloClient: ApolloClient) { + const { + data: { selectBrand }, + } = await apolloClient.query({ + query: QUERY_GET_BRANDS, + }); + + return { + options: { + selectBrand, + }, + }; +} diff --git a/process/init/get-data.ts b/process/init/get-data.ts deleted file mode 100644 index f4acf2c..0000000 --- a/process/init/get-data.ts +++ /dev/null @@ -1,299 +0,0 @@ -/* eslint-disable import/prefer-default-export */ -import type { ApolloClient, NormalizedCache } from '@apollo/client'; -import { gql } from '@apollo/client'; -import dayjs from 'dayjs'; -import utc from 'dayjs/plugin/utc'; -import { getDomainName } from 'services/user/tools'; -import type { User } from 'services/user/types'; -import type { GetAddproductTypes } from './__generated__/GetAddproductTypes'; -import type { GetBrands } from './__generated__/GetBrands'; -import type { GetDealers } from './__generated__/GetDealers'; -import type { GetInsuranceData } from './__generated__/GetInsuranceData'; -import type { GetMainOptions } from './__generated__/GetMainOptions'; -import type { GetOwnerData, GetOwnerDataVariables } from './__generated__/GetOwnerData'; -import type { GetRegions } from './__generated__/GetRegions'; -import type { GetSubsidies } from './__generated__/GetSubsidies'; - -dayjs.extend(utc); - -const QUERY_GET_OWNER_DATA = gql` - query GetOwnerData($domainname: String) { - selectLead: leads(owner_domainname: $domainname) { - label: fullname - value: leadid - } - selectOpportunity: opportunities(owner_domainname: $domainname) { - label: name - value: opportunityid - } - } -`; - -export async function getOwnerData(apolloClient: ApolloClient, user: User) { - const { data: ownerData } = await apolloClient.query({ - query: QUERY_GET_OWNER_DATA, - variables: { - domainname: getDomainName(user), - }, - }); - - return { - options: ownerData, - }; -} - -const QUERY_GET_MAIN_OPTIONS = gql` - query GetMainOptions($currentDate: DateTime) { - selectSupplierCurrency: transactioncurrencies { - label: currencyname - currencysymbol - value: transactioncurrencyid - } - - selectProduct: evo_baseproducts( - statecode: 0 - evo_relation: [100000000] - evo_datefrom_param: { lte: $currentDate } - evo_dateto_param: { gte: $currentDate } - ) { - label: evo_name - value: evo_baseproductid - } - - selectLeaseObjectType: evo_leasingobject_types(statecode: 0) { - label: evo_name - value: evo_leasingobject_typeid - } - - selectGPSBrand: evo_gps_brands(statecode: 0) { - label: evo_name - value: evo_gps_brandid - } - } -`; - -const QUERY_GET_SUBSIDIES = gql` - query GetSubsidies($currentDate: DateTime) { - evo_subsidies( - statecode: 0 - evo_datefrom_param: { lte: $currentDate } - evo_dateto_param: { gte: $currentDate } - ) { - label: evo_name - value: evo_subsidyid - evo_subsidy_type - } - } -`; - -const QUERY_GET_REGIONS = gql` - query GetRegions { - evo_regions { - label: evo_name - value: evo_regionid - } - } -`; - -export async function getMainData(apolloClient: ApolloClient) { - // prettier-ignore - const { data: mainOptions } = await apolloClient.query({ - query: QUERY_GET_MAIN_OPTIONS, - variables: { - currentDate: dayjs().utc().toISOString() - } - }); - - const { data: subsidies } = await apolloClient.query({ - query: QUERY_GET_SUBSIDIES, - variables: { - currentDate: dayjs().utc().toISOString(), - }, - }); - const selectSubsidy = subsidies.evo_subsidies?.filter( - (x) => x?.evo_subsidy_type && [100_000_000, 100_000_001].includes(x?.evo_subsidy_type) - ); - - const selectImportProgram = subsidies.evo_subsidies?.filter( - (x) => x?.evo_subsidy_type && [100_000_002].includes(x?.evo_subsidy_type) - ); - - const { data: regions } = await apolloClient.query({ - query: QUERY_GET_REGIONS, - }); - - const selectRegionRegistration = regions.evo_regions; - const selectObjectRegionRegistration = regions.evo_regions; - const selectLegalClientRegion = regions.evo_regions; - - return { - options: { - ...mainOptions, - selectSubsidy, - selectImportProgram, - selectRegionRegistration, - selectObjectRegionRegistration, - selectLegalClientRegion, - }, - }; -} - -const QUERY_GET_BRANDS = gql` - query GetBrands { - selectBrand: evo_brands(statecode: 0) { - label: evo_name - value: evo_brandid - } - } -`; - -export async function getBrands(apolloClient: ApolloClient) { - const { - data: { selectBrand }, - } = await apolloClient.query({ - query: QUERY_GET_BRANDS, - }); - - return { - options: { - selectBrand, - }, - }; -} - -const QUERY_GET_DEALERS = gql` - query GetDealers { - selectDealer: accounts(evo_account_type: [100000001], statecode: 0, evo_legal_form: 100000001) { - label: name - value: accountid - } - } -`; - -export async function getDealers(apolloClient: ApolloClient) { - const { - data: { selectDealer }, - } = await apolloClient.query({ - query: QUERY_GET_DEALERS, - }); - - return { - options: { - selectDealer, - }, - }; -} - -const QUERY_GET_ADDPRODUCT_TYPES = gql` - query GetAddproductTypes { - evo_addproduct_types(statecode: 0) { - label: evo_name - value: evo_addproduct_typeid - evo_graph_price - evo_product_type - } - } -`; - -export async function getAddProductTypes(apolloClient: ApolloClient) { - const { data: addproductTypes } = await apolloClient.query({ - query: QUERY_GET_ADDPRODUCT_TYPES, - }); - - const selectRegistration = addproductTypes.evo_addproduct_types - ?.filter((x) => x?.evo_product_type === 100_000_001) - .map((x) => ({ - ...x, - label: `${x?.label} (${x?.evo_graph_price} руб.)`, - })); - - const selectTechnicalCard = addproductTypes.evo_addproduct_types - ?.filter((x) => x?.evo_product_type === 100_000_000) - .map((x) => ({ - ...x, - label: `${x?.label} (${x?.evo_graph_price} руб.)`, - })); - - const selectTelematic = addproductTypes.evo_addproduct_types - ?.filter((x) => x?.evo_product_type === 100_000_004) - .map((x) => ({ - ...x, - label: `${x?.label} (${x?.evo_graph_price} руб.)`, - })); - - const selectTracker = addproductTypes.evo_addproduct_types - ?.filter((x) => x?.evo_product_type === 100_000_003) - .map((x) => ({ - ...x, - label: `${x?.label} (${x?.evo_graph_price} руб.)`, - })); - - const selectInsNSIB = addproductTypes.evo_addproduct_types?.filter( - (x) => x?.evo_product_type === 100_000_002 - ); - - return { - options: { - selectRegistration, - selectTechnicalCard, - selectTelematic, - selectTracker, - selectInsNSIB, - }, - }; -} - -const QUERY_GET_INSURANCE_DATA = gql` - query GetInsuranceData($evo_account_type: [Int!]) { - osago: accounts( - evo_account_type: $evo_account_type - evo_type_ins_policy: [100000001] - statecode: 0 - ) { - value: accountid - label: name - } - - kasko: accounts( - evo_account_type: $evo_account_type - evo_type_ins_policy: [100000000] - statecode: 0 - ) { - value: accountid - label: name - } - - fingap: accounts( - evo_account_type: $evo_account_type - evo_type_ins_policy: [100000002] - statecode: 0 - ) { - value: accountid - label: name - } - } -`; - -export async function getInsuranceData(apolloClient: ApolloClient) { - const { data: insuranceData } = await apolloClient.query({ - query: QUERY_GET_INSURANCE_DATA, - }); - - const insurance = { - osago: { - insuranceCompany: insuranceData.osago, - }, - kasko: { - insuranceCompany: insuranceData.kasko, - }, - fingap: { - insuranceCompany: insuranceData.fingap, - }, - }; - - return { - tables: { - insurance, - }, - }; -} diff --git a/process/init/get-dealers.ts b/process/init/get-dealers.ts new file mode 100644 index 0000000..ba2c958 --- /dev/null +++ b/process/init/get-dealers.ts @@ -0,0 +1,26 @@ +import type { ApolloClient } from '@apollo/client'; +import { gql } from '@apollo/client'; +import type { GetDealers } from './__generated__/GetDealers'; + +const QUERY_GET_DEALERS = gql` + query GetDealers { + selectDealer: accounts(evo_account_type: [100000001], statecode: 0, evo_legal_form: 100000001) { + label: name + value: accountid + } + } +`; + +export default async function getDealers(apolloClient: ApolloClient) { + const { + data: { selectDealer }, + } = await apolloClient.query({ + query: QUERY_GET_DEALERS, + }); + + return { + options: { + selectDealer, + }, + }; +} diff --git a/process/init/get-insurance-data.ts b/process/init/get-insurance-data.ts new file mode 100644 index 0000000..f467569 --- /dev/null +++ b/process/init/get-insurance-data.ts @@ -0,0 +1,58 @@ +import type { ApolloClient } from '@apollo/client'; +import { gql } from '@apollo/client'; +import type { GetInsuranceData } from './__generated__/GetInsuranceData'; + +const QUERY_GET_INSURANCE_DATA = gql` + query GetInsuranceData($evo_account_type: [Int!]) { + osago: accounts( + evo_account_type: $evo_account_type + evo_type_ins_policy: [100000001] + statecode: 0 + ) { + value: accountid + label: name + } + + kasko: accounts( + evo_account_type: $evo_account_type + evo_type_ins_policy: [100000000] + statecode: 0 + ) { + value: accountid + label: name + } + + fingap: accounts( + evo_account_type: $evo_account_type + evo_type_ins_policy: [100000002] + statecode: 0 + ) { + value: accountid + label: name + } + } +`; + +export default async function getInsuranceData(apolloClient: ApolloClient) { + const { data: insuranceData } = await apolloClient.query({ + query: QUERY_GET_INSURANCE_DATA, + }); + + const insurance = { + osago: { + insuranceCompany: insuranceData.osago, + }, + kasko: { + insuranceCompany: insuranceData.kasko, + }, + fingap: { + insuranceCompany: insuranceData.fingap, + }, + }; + + return { + tables: { + insurance, + }, + }; +} diff --git a/process/init/get-main-data.ts b/process/init/get-main-data.ts new file mode 100644 index 0000000..8e56d69 --- /dev/null +++ b/process/init/get-main-data.ts @@ -0,0 +1,106 @@ +/* eslint-disable import/prefer-default-export */ +import type { ApolloClient } from '@apollo/client'; +import { gql } from '@apollo/client'; +import dayjs from 'dayjs'; +import utc from 'dayjs/plugin/utc'; +import type { GetMainOptions } from './__generated__/GetMainOptions'; +import type { GetRegions } from './__generated__/GetRegions'; +import type { GetSubsidies } from './__generated__/GetSubsidies'; + +dayjs.extend(utc); + +const QUERY_GET_MAIN_OPTIONS = gql` + query GetMainOptions($currentDate: DateTime) { + selectSupplierCurrency: transactioncurrencies { + label: currencyname + currencysymbol + value: transactioncurrencyid + } + + selectProduct: evo_baseproducts( + statecode: 0 + evo_relation: [100000000] + evo_datefrom_param: { lte: $currentDate } + evo_dateto_param: { gte: $currentDate } + ) { + label: evo_name + value: evo_baseproductid + } + + selectLeaseObjectType: evo_leasingobject_types(statecode: 0) { + label: evo_name + value: evo_leasingobject_typeid + } + + selectGPSBrand: evo_gps_brands(statecode: 0) { + label: evo_name + value: evo_gps_brandid + } + } +`; + +const QUERY_GET_SUBSIDIES = gql` + query GetSubsidies($currentDate: DateTime) { + evo_subsidies( + statecode: 0 + evo_datefrom_param: { lte: $currentDate } + evo_dateto_param: { gte: $currentDate } + ) { + label: evo_name + value: evo_subsidyid + evo_subsidy_type + } + } +`; + +const QUERY_GET_REGIONS = gql` + query GetRegions { + evo_regions { + label: evo_name + value: evo_regionid + } + } +`; + +export default async function getMainData(apolloClient: ApolloClient) { + // prettier-ignore + const { data: mainOptions } = await apolloClient.query({ + query: QUERY_GET_MAIN_OPTIONS, + variables: { + currentDate: dayjs().utc().toISOString() + } + }); + + const { data: subsidies } = await apolloClient.query({ + query: QUERY_GET_SUBSIDIES, + variables: { + currentDate: dayjs().utc().toISOString(), + }, + }); + const selectSubsidy = subsidies.evo_subsidies?.filter( + (x) => x?.evo_subsidy_type && [100_000_000, 100_000_001].includes(x?.evo_subsidy_type) + ); + + const selectImportProgram = subsidies.evo_subsidies?.filter( + (x) => x?.evo_subsidy_type && [100_000_002].includes(x?.evo_subsidy_type) + ); + + const { data: regions } = await apolloClient.query({ + query: QUERY_GET_REGIONS, + }); + + const selectRegionRegistration = regions.evo_regions; + const selectObjectRegionRegistration = regions.evo_regions; + const selectLegalClientRegion = regions.evo_regions; + + return { + options: { + ...mainOptions, + selectSubsidy, + selectImportProgram, + selectRegionRegistration, + selectObjectRegionRegistration, + selectLegalClientRegion, + }, + }; +} diff --git a/process/init/get-owner-data.ts b/process/init/get-owner-data.ts new file mode 100644 index 0000000..e6e52e3 --- /dev/null +++ b/process/init/get-owner-data.ts @@ -0,0 +1,34 @@ +import type { ApolloClient, NormalizedCache } from '@apollo/client'; +import { gql } from '@apollo/client'; +import { getDomainName } from 'services/user/tools'; +import type { User } from 'services/user/types'; +import type { GetOwnerData, GetOwnerDataVariables } from './__generated__/GetOwnerData'; + +const QUERY_GET_OWNER_DATA = gql` + query GetOwnerData($domainname: String) { + selectLead: leads(owner_domainname: $domainname) { + label: fullname + value: leadid + } + selectOpportunity: opportunities(owner_domainname: $domainname) { + label: name + value: opportunityid + } + } +`; + +export default async function getOwnerData( + apolloClient: ApolloClient, + user: User +) { + const { data: ownerData } = await apolloClient.query({ + query: QUERY_GET_OWNER_DATA, + variables: { + domainname: getDomainName(user), + }, + }); + + return { + options: ownerData, + }; +} diff --git a/process/init/index.js b/process/init/index.js new file mode 100644 index 0000000..7240f5e --- /dev/null +++ b/process/init/index.js @@ -0,0 +1,29 @@ +import getAddProductTypes from './get-addproduct-types-data'; +import getBrands from './get-brands'; +import getDealers from './get-dealers'; +import getInsuranceData from './get-insurance-data'; +import getMainData from './get-main-data'; + +export default function getData(apolloClient, store) { + getMainData(apolloClient).then(({ options }) => { + store.$calculation.$options.setManyOptions(options); + }); + + getBrands(apolloClient).then(({ options }) => { + store.$calculation.$options.setManyOptions(options); + }); + + getDealers(apolloClient).then(({ options }) => { + store.$calculation.$options.setManyOptions(options); + }); + + getAddProductTypes(apolloClient).then(({ options }) => { + store.$calculation.$options.setManyOptions(options); + }); + + getInsuranceData(apolloClient).then(({ tables }) => { + store.$tables.insurance.setManyRowOptions(tables.insurance); + }); +} + +export { default as getOwnerData } from './get-owner-data';