diff --git a/app/home/(user)/_lib/server/load-analysis-packages.ts b/app/home/(user)/_lib/server/load-analysis-packages.ts index f564e08..19f6043 100644 --- a/app/home/(user)/_lib/server/load-analysis-packages.ts +++ b/app/home/(user)/_lib/server/load-analysis-packages.ts @@ -3,7 +3,7 @@ import { cache } from 'react'; import { getAnalysisElementMedusaProductIds } from '@/utils/medusa-product'; import { listProductTypes, listProducts } from '@lib/data/products'; import { listRegions } from '@lib/data/regions'; -import type { StoreProduct } from '@medusajs/types'; +import type { StoreProduct, StoreProductType } from '@medusajs/types'; import type { AccountWithParams } from '@kit/accounts/types/accounts'; import type { AnalysisPackageWithVariant } from '@kit/shared/components/select-analysis-package'; @@ -118,17 +118,12 @@ async function analysisPackageElementsLoader({ async function analysisPackagesWithVariantLoader({ account, countryCode, + productType, }: { account: AccountWithParams; countryCode: string; + productType: StoreProductType; }) { - const productTypes = await loadProductTypes(); - const productType = productTypes.find( - ({ metadata }) => metadata?.handle === 'analysis-packages', - ); - if (!productType) { - return null; - } const analysisPackagesResponse = await listProducts({ countryCode, queryParams: { limit: 100, 'type_id[0]': productType.id }, @@ -171,12 +166,23 @@ async function analysisPackagesLoader() { throw new Error('Account not found'); } - const countryCodes = await loadCountryCodes(); + const [countryCodes, productTypes] = await Promise.all([ + loadCountryCodes(), + loadProductTypes(), + ]); const countryCode = countryCodes[0]!; + const productType = productTypes.find( + ({ metadata }) => metadata?.handle === 'analysis-packages', + ); + + if (!productType) { + return { analysisPackageElements: [], analysisPackages: [], countryCode }; + } const analysisPackagesWithVariant = await analysisPackagesWithVariantLoader({ account, countryCode, + productType, }); if (!analysisPackagesWithVariant) { return { analysisPackageElements: [], analysisPackages: [], countryCode };