diff --git a/pages/index.jsx b/pages/index.jsx index 6abe8af..05269eb 100644 --- a/pages/index.jsx +++ b/pages/index.jsx @@ -5,7 +5,7 @@ import * as Calculation from 'Components/Calculation'; import { CRMError } from 'Components/Common/Error'; import Output from 'Components/Output'; import Head from 'next/head'; -import { useCRMData, useOwnerData } from 'process/init/get-data/hooks'; +import { useInsuranceData, useMainData, useOwnerData } from 'process/init/get-data'; import { useReactions } from 'process/init/inject-reactions/hooks'; import styled from 'styled-components'; import { Box } from 'UIKit/grid'; @@ -37,8 +37,9 @@ const Grid = styled(Box)` function Home({ user }) { const { error } = useOwnerData(user); + useMainData(); + useInsuranceData(); useReactions(); - useCRMData(user); if (error) return ; diff --git a/process/fingap/reactions/common.ts b/process/fingap/reactions/common.ts index 2a4a0e3..1519cc0 100644 --- a/process/fingap/reactions/common.ts +++ b/process/fingap/reactions/common.ts @@ -149,7 +149,7 @@ export default function commonReactions({ store, apolloClient, queryClient }: Re >({ query: QUERY_GET_FINGAP_ADDPRODUCT_TYPES, variables: { - currentDate: dayjs().utc().format('YYYY-MM-DD'), + currentDate: dayjs().utc(false).format('YYYY-MM-DD'), }, }); diff --git a/process/init/get-data/get-addproduct-types-data.ts b/process/init/get-data/get-addproduct-types-data.ts deleted file mode 100644 index 2c54901..0000000 --- a/process/init/get-data/get-addproduct-types-data.ts +++ /dev/null @@ -1,62 +0,0 @@ -import type { ApolloClient } from '@apollo/client'; -import { gql } from '@apollo/client'; -import type { GetAddproductTypesQuery } from 'graphql/crm.types'; - -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-data/get-brands.ts b/process/init/get-data/get-brands.ts deleted file mode 100644 index 099148c..0000000 --- a/process/init/get-data/get-brands.ts +++ /dev/null @@ -1,26 +0,0 @@ -import type { ApolloClient } from '@apollo/client'; -import { gql } from '@apollo/client'; -import type { GetBrandsQuery } from 'graphql/crm.types'; - -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/get-dealers.ts b/process/init/get-data/get-dealers.ts deleted file mode 100644 index 7e0b33c..0000000 --- a/process/init/get-data/get-dealers.ts +++ /dev/null @@ -1,26 +0,0 @@ -import type { ApolloClient } from '@apollo/client'; -import { gql } from '@apollo/client'; -import type { GetDealersQuery } from 'graphql/crm.types'; - -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-data/get-insurance-data.js b/process/init/get-data/get-insurance-data.js new file mode 100644 index 0000000..0c71fb8 --- /dev/null +++ b/process/init/get-data/get-insurance-data.js @@ -0,0 +1,67 @@ +/* eslint-disable import/prefer-default-export */ +import { gql, useQuery } from '@apollo/client'; +import { useStore } from 'stores/hooks'; + +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 function useInsuranceData() { + const { $tables } = useStore(); + + function handleOnCompleted(options) { + Object.keys(options).forEach((key) => { + const rowOptions = options[key]; + if (rowOptions !== undefined) { + Object.keys(rowOptions).forEach((valueName) => { + $tables.insurance.row(key).setOptions(valueName, rowOptions[valueName]); + }); + } + }); + } + + useQuery(QUERY_GET_INSURANCE_DATA, { + onCompleted: (insuranceData) => { + const insurance = { + osago: { + insuranceCompany: insuranceData.osago, + }, + kasko: { + insuranceCompany: insuranceData.kasko, + }, + fingap: { + insuranceCompany: insuranceData.fingap, + }, + }; + + handleOnCompleted(insurance); + }, + }); +} diff --git a/process/init/get-data/get-insurance-data.ts b/process/init/get-data/get-insurance-data.ts deleted file mode 100644 index fde9883..0000000 --- a/process/init/get-data/get-insurance-data.ts +++ /dev/null @@ -1,58 +0,0 @@ -import type { ApolloClient } from '@apollo/client'; -import { gql } from '@apollo/client'; -import type { GetInsuranceDataQuery } from 'graphql/crm.types'; - -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-data/get-main-data.js b/process/init/get-data/get-main-data.js new file mode 100644 index 0000000..f57c1b0 --- /dev/null +++ b/process/init/get-data/get-main-data.js @@ -0,0 +1,195 @@ +/* eslint-disable import/prefer-default-export */ +import { gql, useQuery } from '@apollo/client'; +import dayjs from 'dayjs'; +import utc from 'dayjs/plugin/utc'; +import { useStore } from 'stores/hooks'; + +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 + } + } +`; + +const QUERY_GET_BRANDS = gql` + query GetBrands { + selectBrand: evo_brands(statecode: 0) { + label: evo_name + value: evo_brandid + } + } +`; + +const QUERY_GET_DEALERS = gql` + query GetDealers { + selectDealer: accounts(evo_account_type: [100000001], statecode: 0, evo_legal_form: 100000001) { + label: name + value: accountid + } + } +`; + +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 + } + } +`; + +const currentDate = dayjs().utc(false).toISOString(); + +export function useMainData() { + const { $calculation } = useStore(); + + function handleOnCompleted(options) { + Object.keys(options).forEach((elementName) => { + const elementOptions = options[elementName]; + $calculation.element(elementName).setOptions(elementOptions); + }); + } + + useQuery(QUERY_GET_MAIN_OPTIONS, { + variables: { + currentDate, + }, + onCompleted: handleOnCompleted, + }); + + useQuery(QUERY_GET_SUBSIDIES, { + variables: { + currentDate, + }, + onCompleted: (subsidies) => { + 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) + ); + + handleOnCompleted({ + selectSubsidy, + selectImportProgram, + }); + }, + }); + + useQuery(QUERY_GET_REGIONS, { + onCompleted: (regions) => { + const selectRegionRegistration = regions?.evo_regions; + const selectObjectRegionRegistration = regions?.evo_regions; + const selectLegalClientRegion = regions?.evo_regions; + + handleOnCompleted({ + selectRegionRegistration, + selectObjectRegionRegistration, + selectLegalClientRegion, + }); + }, + }); + + useQuery(QUERY_GET_BRANDS, { + onCompleted: handleOnCompleted, + }); + + useQuery(QUERY_GET_DEALERS, { + onCompleted: handleOnCompleted, + }); + + useQuery(QUERY_GET_ADDPRODUCT_TYPES, { + onCompleted: (addproductTypes) => { + 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 + ); + + handleOnCompleted({ + selectRegistration, + selectTechnicalCard, + selectTelematic, + selectTracker, + selectInsNSIB, + }); + }, + }); +} diff --git a/process/init/get-data/get-main-data.ts b/process/init/get-data/get-main-data.ts deleted file mode 100644 index dc61dab..0000000 --- a/process/init/get-data/get-main-data.ts +++ /dev/null @@ -1,112 +0,0 @@ -/* 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 * as CRMTypes from 'graphql/crm.types'; - -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) { - const { data: mainOptions } = await apolloClient.query< - CRMTypes.GetMainOptionsQuery, - CRMTypes.GetMainOptionsQueryVariables - >({ - query: QUERY_GET_MAIN_OPTIONS, - variables: { - currentDate: dayjs().utc().toISOString(), - }, - }); - - const { data: subsidies } = await apolloClient.query< - CRMTypes.GetSubsidiesQuery, - CRMTypes.GetSubsidiesQueryVariables - >({ - 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< - CRMTypes.GetRegionsQuery, - CRMTypes.GetRegionsQueryVariables - >({ - 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-data/get-owner-data.ts b/process/init/get-data/get-owner-data.ts index 1fc5bd3..24b2cc8 100644 --- a/process/init/get-data/get-owner-data.ts +++ b/process/init/get-data/get-owner-data.ts @@ -1,9 +1,12 @@ -import type { ApolloClient, NormalizedCache } from '@apollo/client'; -import { gql } from '@apollo/client'; +/* eslint-disable import/prefer-default-export */ +import { gql, useQuery } from '@apollo/client'; + import type { User } from 'api/user/types'; import type { GetOwnerDataQuery, GetOwnerDataQueryVariables } from 'graphql/crm.types'; +import { useStore } from 'stores/hooks'; +import { normalizeOptions } from 'tools/entity'; -export const QUERY_GET_OWNER_DATA = gql` +const QUERY_GET_OWNER_DATA = gql` query GetOwnerData($domainname: String) { selectLead: leads(owner_domainname: $domainname) { label: fullname @@ -16,21 +19,38 @@ export const QUERY_GET_OWNER_DATA = gql` } `; -export default async function getOwnerData( - apolloClient: ApolloClient, - user: User -) { - const { data: ownerData } = await apolloClient.query< - GetOwnerDataQuery, - GetOwnerDataQueryVariables - >({ - query: QUERY_GET_OWNER_DATA, - variables: { - domainname: user.domainName, - }, - }); +export function useOwnerData(user: User) { + const { $calculation } = useStore(); + + function handleOnStartFetch() { + $calculation.$status.setStatus('selectLead', 'Loading'); + $calculation.$status.setStatus('selectOpportunity', 'Loading'); + } + + function handleOnCompleted(data: GetOwnerDataQuery) { + $calculation.element('selectLead').setOptions(normalizeOptions(data?.selectLead)); + $calculation.element('selectOpportunity').setOptions(normalizeOptions(data?.selectOpportunity)); + + $calculation.$status.setStatus('selectLead', 'Default'); + $calculation.$status.setStatus('selectOpportunity', 'Default'); + } + + const { loading, error } = useQuery( + QUERY_GET_OWNER_DATA, + { + variables: { + domainname: user?.domainName, + }, + + onCompleted: handleOnCompleted, + } + ); + + if (loading) { + handleOnStartFetch(); + } return { - options: ownerData, + error, }; } diff --git a/process/init/get-data/hooks.js b/process/init/get-data/hooks.js deleted file mode 100644 index fe359a4..0000000 --- a/process/init/get-data/hooks.js +++ /dev/null @@ -1,74 +0,0 @@ -/* eslint-disable import/prefer-default-export */ -import { useApolloClient, useQuery } from '@apollo/client'; -import { useStore } from 'stores/hooks'; -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'; -import { QUERY_GET_OWNER_DATA } from './get-owner-data'; - -export function useCRMData() { - const store = useStore(); - const apolloClient = useApolloClient(); - const { $calculation, $tables } = store; - - function setManyOptions(options) { - Object.keys(options).forEach((elementName) => { - const elementOptions = options[elementName]; - $calculation.element(elementName).setOptions(elementOptions); - }); - } - - getMainData(apolloClient).then(({ options }) => setManyOptions(options)); - getBrands(apolloClient).then(({ options }) => setManyOptions(options)); - getDealers(apolloClient).then(({ options }) => setManyOptions(options)); - getAddProductTypes(apolloClient).then(({ options }) => setManyOptions(options)); - - function setManyRowOptions(options) { - Object.keys(options).forEach((key) => { - const rowOptions = options[key]; - if (rowOptions !== undefined) { - Object.keys(rowOptions).forEach((valueName) => { - $tables.insurance.row(key).setOptions(valueName, rowOptions[valueName]); - }); - } - }); - } - - getInsuranceData(apolloClient).then(({ tables }) => setManyRowOptions(tables.insurance)); -} - -export function useOwnerData(user) { - const store = useStore(); - const { $calculation } = store; - - function handleOnStartFetch() { - $calculation.$status.setStatus('selectLead', 'Loading'); - $calculation.$status.setStatus('selectOpportunity', 'Loading'); - } - - function handleOnCompleted(data) { - $calculation.element('selectLead').setOptions(data?.selectLead); - $calculation.element('selectOpportunity').setOptions(data?.selectOpportunity); - - $calculation.$status.setStatus('selectLead', 'Default'); - $calculation.$status.setStatus('selectOpportunity', 'Default'); - } - - const { loading, error } = useQuery(QUERY_GET_OWNER_DATA, { - variables: { - domainname: user?.domainName, - }, - - onCompleted: handleOnCompleted, - }); - - if (loading) { - handleOnStartFetch(); - } - - return { - error, - }; -} diff --git a/process/init/get-data/index.js b/process/init/get-data/index.js new file mode 100644 index 0000000..6d65323 --- /dev/null +++ b/process/init/get-data/index.js @@ -0,0 +1,3 @@ +export { useInsuranceData } from './get-insurance-data'; +export { useMainData } from './get-main-data'; +export { useOwnerData } from './get-owner-data'; diff --git a/process/price/reactions/computed.ts b/process/price/reactions/computed.ts index 159c22d..a4bba6c 100644 --- a/process/price/reactions/computed.ts +++ b/process/price/reactions/computed.ts @@ -68,7 +68,7 @@ export default function computedReactions({ store, apolloClient }: ReactionsCont >({ query: QUERY_GET_EVO_CURRENCY_CHANGES, variables: { - currentDate: dayjs().utc().format('YYYY-MM-DD'), + currentDate: dayjs().utc(false).format('YYYY-MM-DD'), }, });