From c264fe7f1e6919e14c5d5eff9058d097628f714e Mon Sep 17 00:00:00 2001 From: vchikalkin Date: Thu, 21 Sep 2023 10:20:22 +0300 Subject: [PATCH] hooks/init: use Promise.all in getInitialData --- .../process/hooks/init/get-initial-data.ts | 79 ++++++++++--------- 1 file changed, 42 insertions(+), 37 deletions(-) diff --git a/apps/web/process/hooks/init/get-initial-data.ts b/apps/web/process/hooks/init/get-initial-data.ts index 2162c3d..ad75012 100644 --- a/apps/web/process/hooks/init/get-initial-data.ts +++ b/apps/web/process/hooks/init/get-initial-data.ts @@ -5,61 +5,66 @@ 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, }, }); - - const currentDate = dayjs().utc(false).toISOString(); - let { - data: { evo_baseproducts }, - } = await query({ - fetchPolicy: 'network-only', - query: CRMTypes.GetProductsDocument, - variables: { - currentDate, - }, - }); - - 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, + selectProduct: crmTools.evo_baseproducts(evo_baseproducts).filterBy.systemuser(systemuser), selectSupplierCurrency: transactioncurrencies, }, values: {