From c3c73998235232e378712115622b4483e9380a0a Mon Sep 17 00:00:00 2001 From: vchikalkin Date: Wed, 20 Sep 2023 17:16:47 +0300 Subject: [PATCH] filter evo_baseproducts on page load --- apps/web/graphql/crm.tools.ts | 25 +++++++++++++++++++ .../process/configurator/reactions/filters.ts | 16 +++--------- .../process/hooks/init/get-initial-data.ts | 20 +++++++++++++++ apps/web/process/hooks/init/get-main-data.js | 11 -------- 4 files changed, 49 insertions(+), 23 deletions(-) create mode 100644 apps/web/graphql/crm.tools.ts 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..fd72054 100644 --- a/apps/web/process/hooks/init/get-initial-data.ts +++ b/apps/web/process/hooks/init/get-initial-data.ts @@ -1,4 +1,5 @@ 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'; @@ -30,10 +31,29 @@ export async function getInitialData({ query }: ApolloClient, user: User }, }); + let { + data: { evo_baseproducts }, + } = await query({ + query: CRMTypes.GetProductsDocument, + }); + + const { + data: { systemuser }, + } = await query({ + fetchPolicy: 'network-only', + query: CRMTypes.GetSystemUserDocument, + variables: { + domainname: user?.domainName, + }, + }); + + evo_baseproducts = crmTools.evo_baseproducts(evo_baseproducts).filterBy.systemuser(systemuser); + return { options: { selectLead: leads, selectOpportunity: opportunities, + selectProduct: evo_baseproducts, 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: {