feat(MED-131): analysis package has list of medusa product IDs, not medipost IDs
This commit is contained in:
@@ -2,8 +2,8 @@ import { cache } from 'react';
|
||||
|
||||
import { listProductTypes, listProducts } from "@lib/data/products";
|
||||
import { listRegions } from '@lib/data/regions';
|
||||
import { AnalysisElement, getAnalysisElements } from '~/lib/services/analysis-element.service';
|
||||
import { getAnalysisElementOriginalIds } from '@/utils/medusa-product';
|
||||
import { getAnalysisElementMedusaProductIds } from '@/utils/medusa-product';
|
||||
import type { StoreProduct } from '@medusajs/types';
|
||||
|
||||
async function countryCodesLoader() {
|
||||
const countryCodes = await listRegions().then((regions) =>
|
||||
@@ -23,23 +23,32 @@ async function analysisPackagesLoader() {
|
||||
const [countryCodes, productTypes] = await Promise.all([loadCountryCodes(), loadProductTypes()]);
|
||||
const countryCode = countryCodes[0]!;
|
||||
|
||||
let analysisPackages: StoreProduct[] = [];
|
||||
let analysisPackageElements: StoreProduct[] = [];
|
||||
|
||||
const productType = productTypes.find(({ metadata }) => metadata?.handle === 'analysis-packages');
|
||||
if (!productType) {
|
||||
return { analysisElements: [], analysisPackages: [], countryCode };
|
||||
return { analysisPackageElements, analysisPackages, countryCode };
|
||||
}
|
||||
|
||||
const { response } = await listProducts({
|
||||
const analysisPackagesResponse = await listProducts({
|
||||
countryCode,
|
||||
queryParams: { limit: 100, "type_id[0]": productType.id },
|
||||
});
|
||||
const analysisPackages = response.products;
|
||||
let analysisElements: AnalysisElement[] = [];
|
||||
const analysisElementOriginalIds = getAnalysisElementOriginalIds(analysisPackages);
|
||||
analysisPackages = analysisPackagesResponse.response.products;
|
||||
|
||||
if (analysisElementOriginalIds.length) {
|
||||
analysisElements = await getAnalysisElements({ originalIds: analysisElementOriginalIds });
|
||||
const analysisElementMedusaProductIds = getAnalysisElementMedusaProductIds(analysisPackages);
|
||||
if (analysisElementMedusaProductIds.length > 0) {
|
||||
const { response: { products } } = await listProducts({
|
||||
countryCode,
|
||||
queryParams: {
|
||||
id: analysisElementMedusaProductIds,
|
||||
limit: 100,
|
||||
},
|
||||
});
|
||||
analysisPackageElements = products;
|
||||
}
|
||||
|
||||
return { analysisElements, analysisPackages, countryCode };
|
||||
return { analysisPackageElements, analysisPackages, countryCode };
|
||||
}
|
||||
export const loadAnalysisPackages = cache(analysisPackagesLoader);
|
||||
|
||||
Reference in New Issue
Block a user