diff --git a/apps/web/graphql/crm.tools.ts b/apps/web/graphql/crm.tools.ts new file mode 100644 index 0000000..c3435e4 --- /dev/null +++ b/apps/web/graphql/crm.tools.ts @@ -0,0 +1,25 @@ +import type * as CRMTypes from '@/graphql/crm.types'; + +function evo_baseproducts(evo_baseproducts: CRMTypes.GetProductsQuery['evo_baseproducts']) { + return { + filterBy: { + systemuser(systemuser: CRMTypes.GetSystemUserQuery['systemuser']) { + if (!evo_baseproducts?.length || !systemuser) return []; + + return evo_baseproducts?.filter( + (evo_baseproduct) => + !evo_baseproduct?.systemusers?.length || + evo_baseproduct?.systemusers?.some( + (evo_baseproduct_systemuser) => + systemuser?.systemuserid && + evo_baseproduct_systemuser?.systemuserid === systemuser?.systemuserid + ) + ); + }, + }, + }; +} + +export const crmTools = { + evo_baseproducts, +}; diff --git a/apps/web/process/configurator/reactions/filters.ts b/apps/web/process/configurator/reactions/filters.ts index 5e3c7cc..050f4d9 100644 --- a/apps/web/process/configurator/reactions/filters.ts +++ b/apps/web/process/configurator/reactions/filters.ts @@ -1,5 +1,6 @@ /* eslint-disable sonarjs/cognitive-complexity */ import { radioCalcType, radioGraphType, selectSeasonType } from '@/config/default-options'; +import { crmTools } from '@/graphql/crm.tools'; import * as CRMTypes from '@/graphql/crm.types'; import { SEASON_TYPES } from '@/process/payments/lib/seasons-constants'; import type { ProcessContext } from '@/process/types'; @@ -380,15 +381,9 @@ export default function reactions({ store, apolloClient, user }: ProcessContext) }, }); - evo_baseproducts = evo_baseproducts?.filter( - (x) => - !x?.systemusers?.length || - x?.systemusers?.some( - (evo_baseproduct_systemuser) => - systemuser?.systemuserid && - evo_baseproduct_systemuser?.systemuserid === systemuser?.systemuserid - ) - ); + evo_baseproducts = crmTools + .evo_baseproducts(evo_baseproducts) + .filterBy.systemuser(systemuser); } if (!$calculation.element('cbxRecalcWithRevision').getValue()) { @@ -434,9 +429,6 @@ export default function reactions({ store, apolloClient, user }: ProcessContext) } else { $calculation.element('selectProduct').setOptions(normalizeOptions(evo_baseproducts)); } - }, - { - fireImmediately: true, } ); } diff --git a/apps/web/process/hooks/init/get-initial-data.ts b/apps/web/process/hooks/init/get-initial-data.ts index d97a2bf..ad75012 100644 --- a/apps/web/process/hooks/init/get-initial-data.ts +++ b/apps/web/process/hooks/init/get-initial-data.ts @@ -1,39 +1,70 @@ import type { User } from '@/api/user/types'; +import { crmTools } from '@/graphql/crm.tools'; import * as CRMTypes from '@/graphql/crm.types'; import type { ApolloClient } from '@apollo/client'; +import dayjs from 'dayjs'; export async function getInitialData({ query }: ApolloClient, user: User) { - const { - data: { leads }, - } = await query({ query: CRMTypes.GetLeadsDocument, variables: { domainname: user.domainName } }); - - const { - data: { opportunities }, - } = await query({ + const leadsQuery = query({ + query: CRMTypes.GetLeadsDocument, + variables: { domainname: user.domainName }, + }); + const opportunitiesQuery = query({ query: CRMTypes.GetOpportunitiesDocument, variables: { domainname: user.domainName }, }); - - const { - data: { transactioncurrencies }, - } = await query({ - query: CRMTypes.GetTransactionCurrenciesDocument, + const transactionCurrenciesQuery = query({ query: CRMTypes.GetTransactionCurrenciesDocument }); + const productsQuery = query({ + fetchPolicy: 'network-only', + query: CRMTypes.GetProductsDocument, + variables: { currentDate: dayjs().utc(false).toISOString() }, }); + const systemUserQuery = query({ + fetchPolicy: 'network-only', + query: CRMTypes.GetSystemUserDocument, + variables: { domainname: user?.domainName }, + }); + + const [ + { + data: { leads }, + }, + { + data: { opportunities }, + }, + { + data: { transactioncurrencies }, + }, + { + data: { evo_baseproducts }, + }, + { + data: { systemuser }, + }, + ] = await Promise.all([ + leadsQuery, + opportunitiesQuery, + transactionCurrenciesQuery, + productsQuery, + systemUserQuery, + ]); const transactioncurrency_rub = transactioncurrencies?.find((x) => x?.isocurrencycode === 'RUB'); - if (transactioncurrency_rub?.transactioncurrencyid) + if (transactioncurrency_rub?.transactioncurrencyid) { await query({ query: CRMTypes.GetTransactionCurrencyDocument, variables: { currencyid: transactioncurrency_rub?.transactioncurrencyid, }, }); + } return { options: { selectLead: leads, selectOpportunity: opportunities, + selectProduct: crmTools.evo_baseproducts(evo_baseproducts).filterBy.systemuser(systemuser), selectSupplierCurrency: transactioncurrencies, }, values: { diff --git a/apps/web/process/hooks/init/get-main-data.js b/apps/web/process/hooks/init/get-main-data.js index 126a289..4737406 100644 --- a/apps/web/process/hooks/init/get-main-data.js +++ b/apps/web/process/hooks/init/get-main-data.js @@ -33,17 +33,6 @@ function getMainData({ query }, onCompleted) { }); }); - query({ - query: CRMTypes.GetProductsDocument, - variables: { - currentDate, - }, - }).then(({ data }) => { - onCompleted({ - selectProduct: data?.evo_baseproducts, - }); - }); - query({ query: CRMTypes.GetSubsidiesDocument, variables: {