diff --git a/dump.rdb b/dump.rdb index 5c4a869..99ef1e9 100644 Binary files a/dump.rdb and b/dump.rdb differ diff --git a/medusa-config.js b/medusa-config.js index db6cfe5..0f57a30 100644 --- a/medusa-config.js +++ b/medusa-config.js @@ -77,7 +77,7 @@ const plugins = [ const { id, title, description, thumbnail, handle } = product; const prices = {}; - product.variants[0].prices.forEach((price) => { + product.variants?.[0]?.prices.forEach((price) => { prices[price.currency_code] = price.amount; }); diff --git a/src/services/category.ts b/src/services/category.ts index 484a727..6b3d62e 100644 --- a/src/services/category.ts +++ b/src/services/category.ts @@ -1,5 +1,5 @@ import { ProductCategory, ProductCategoryService } from "@medusajs/medusa"; -import { buildLocationTree, getLastLocationPath, getLocationPath, getLocationPathWithLastID } from "../utils/convertIntoNestedLocations"; + class CategoryService extends ProductCategoryService { constructor(container) { @@ -15,17 +15,30 @@ class CategoryService extends ProductCategoryService { } async retrieveAllLastLocationsName() { - const retrieveAllLocations = ` - SELECT name, id, parent_category_id FROM "product_category" WHERE handle LIKE 'loc:%' - ` + const categories = await this.listAndCount({}) + const res = await Promise.all(categories[0].map((c) => { + return this.productCategoryRepo_.findDescendantsTree(c) + })) + + const childCategories = res.filter(r => !r.category_children?.length && r.handle.startsWith('loc:')); + + const categoriesWithParent = await Promise.all(childCategories.map((c) => this.productCategoryRepo_.findAncestorsTree(c))) - const result = await this.activeManager_.query(retrieveAllLocations) as { name: string, id: string, parent_category_id: string }[] - // Convert the location tree to slash-separated strings + return categoriesWithParent.map((category) => { + const list = []; - const nestedLocations = buildLocationTree(result); - const locationStrings: string[] = getLocationPath(nestedLocations) - return locationStrings + const getParent = (pcat: ProductCategory) => { + if (pcat.parent_category) { + list.unshift(pcat.name) + getParent(pcat.parent_category) + } + } + + getParent(category); + + return list.join(' / '); + }) } @@ -43,7 +56,6 @@ class CategoryService extends ProductCategoryService { const getLocation = async (locationId: string) => { const location = await this.retrieve(locationId); - if (location.parent_category_id) { locations.unshift(location) await getLocation(location.parent_category_id) diff --git a/src/services/customer.ts b/src/services/customer.ts index 997f84d..d10b3e0 100644 --- a/src/services/customer.ts +++ b/src/services/customer.ts @@ -25,7 +25,6 @@ export default class CustomerService extends BaseCustomerService { const customerResult = await this.activeManager_.query(customerByEmailQuery, [email]) as Customer[] if (!customerResult.length) { - console.log(customerResult); throw new Error('Customer not found with this email!') }