/* eslint-disable canonical/sort-keys */ import { getUser } from '@/api/user/query'; import { STALE_TIME } from '@/constants/request'; import { crmTools } from '@/graphql/crm.tools'; import * as CRMTypes from '@/graphql/crm.types'; import { useStore } from '@/stores/hooks'; import { getCurrentISODate } from '@/utils/date'; import { normalizeOptions } from '@/utils/entity'; import { useApolloClient } from '@apollo/client'; import { useQuery } from '@tanstack/react-query'; import { useEffect } from 'react'; /** * * @param {import('@apollo/client').ApolloClient} apolloClient * @param {*} onCompleted */ function getMainData({ query }, onCompleted, user) { const currentDate = getCurrentISODate(); query({ query: CRMTypes.GetLeadsDocument, variables: { domainname: user.domainName }, }).then(({ data }) => { const systemuser = data?.systemusers?.[0]; if (systemuser) { const { leads } = systemuser; onCompleted({ selectLead: leads, }); } }); query({ query: CRMTypes.GetOpportunitiesDocument, variables: { domainname: user.domainName }, }).then(({ data }) => { onCompleted({ selectOpportunity: data?.opportunities, }); }); query({ query: CRMTypes.GetTransactionCurrenciesDocument }).then(({ data }) => { onCompleted({ selectSupplierCurrency: data?.transactioncurrencies, }); }); query({ fetchPolicy: 'network-only', query: CRMTypes.GetSystemUserDocument, variables: { domainname: user?.domainName }, }).then(({ data: { systemusers } }) => { const systemuser = systemusers?.[0]; if (systemuser) { query({ fetchPolicy: 'network-only', query: CRMTypes.GetProductsDocument, variables: { currentDate }, }).then(({ data: { evo_baseproducts } }) => { onCompleted({ selectProduct: crmTools .evo_baseproducts(evo_baseproducts) .filterBy.systemuser(systemuser), }); }); } }); // query({ // query: CRMTypes.GetLeaseObjectTypesDocument, // }).then(({ data }) => { // onCompleted({ // selectLeaseObjectType: data?.evo_leasingobject_types, // }); // }); query({ query: CRMTypes.GetGpsBrandsDocument, }).then(({ data }) => { onCompleted({ selectGPSBrand: data?.evo_gps_brands, }); }); query({ query: CRMTypes.GetSubsidiesDocument, variables: { currentDate, }, }).then(({ data }) => { const selectSubsidy = data?.evo_subsidies?.filter( (x) => x?.evo_subsidy_type && [100_000_000, 100_000_001].includes(x?.evo_subsidy_type) ); const selectImportProgram = data?.evo_subsidies?.filter( (x) => x?.evo_subsidy_type && [100_000_002].includes(x?.evo_subsidy_type) ); onCompleted({ selectSubsidy, selectImportProgram, }); }); query({ query: CRMTypes.GetRegionsDocument, }).then(({ data }) => { const selectRegionRegistration = data?.evo_regions; const selectObjectRegionRegistration = data?.evo_regions; const selectLegalClientRegion = data?.evo_regions; onCompleted({ selectRegionRegistration, selectObjectRegionRegistration, selectLegalClientRegion, }); }); // query({ // query: CRMTypes.GetBrandsDocument, // }).then(({ data }) => { // onCompleted({ // selectBrand: data?.evo_brands, // }); // }); // query({ // query: CRMTypes.GetDealersDocument, // }).then(({ data }) => { // onCompleted({ // selectDealer: data?.dealers, // }); // }); query({ query: CRMTypes.GetTelematicTypesDocument, variables: { currentDate, }, }).then(({ data }) => { onCompleted({ selectTelematic: data?.evo_addproduct_types }); }); query({ query: CRMTypes.GetTrackerTypesDocument, variables: { currentDate, }, }).then(({ data }) => { onCompleted({ selectTracker: data?.evo_addproduct_types }); }); query({ query: CRMTypes.GetInsNsibTypesDocument, variables: { currentDate, }, context: { disableModify: true, }, }).then(({ data }) => { onCompleted({ selectInsNSIB: data?.evo_addproduct_types }); }); // query({ // query: CRMTypes.GetTarifsDocument, // variables: { // currentDate, // }, // }).then(({ data }) => { // onCompleted({ // selectTarif: data?.evo_tarifs, // }); // }); } export function useMainData() { const { $calculation } = useStore(); const apolloClient = useApolloClient(); const { data: user } = useQuery(['user'], ({ signal }) => getUser({ signal }), { staleTime: STALE_TIME, }); function handleOnCompleted(options) { Object.keys(options).forEach((elementName) => { const elementOptions = options[elementName]; $calculation.element(elementName).setOptions(normalizeOptions(elementOptions)); }); } useEffect(() => { getMainData(apolloClient, handleOnCompleted, user); }, []); }