feat(medusa): Retrieve (service + controller) a product category (#3004)

What:

Introduces a store endpoint to retrieve a product category

Why:

This is part of a greater goal of allowing products to be added to multiple categories.

How:

- Creates an endpoint in store routes

RESOLVES CORE-967
This commit is contained in:
Riqwan Thamir
2023-01-12 17:19:06 +01:00
committed by GitHub
parent b80124d32d
commit b2839e2e4d
9 changed files with 458 additions and 4 deletions
@@ -0,0 +1,47 @@
import { pick } from "lodash"
import { isDefined } from "medusa-core-utils"
import { filter, isNull } from "lodash"
// TODO: When we implement custom queries for tree paths in medusa, remove the transformer
// Adding this here since typeorm tree repo doesn't allow configs to be passed
// onto its children nodes. As an alternative, we are transforming the data post query.
export function transformTreeNodesWithConfig(
object,
config,
scope = {},
isParentNode = false
) {
const selects = (config.select || []) as string[]
const relations = (config.relations || []) as string[]
const selectsAndRelations = selects.concat(relations)
for (const [key, value] of Object.entries(scope)) {
const modelValue = object[key]
if (isDefined(modelValue) && modelValue !== value) {
return null
}
}
if (object.parent_category) {
object.parent_category = transformTreeNodesWithConfig(
object.parent_category,
config,
scope,
true
)
}
if (!isParentNode && (object.category_children || []).length > 0) {
object.category_children = object.category_children.map((child) => {
return transformTreeNodesWithConfig(child, config, scope)
})
object.category_children = filter(
object.category_children,
(el) => !isNull(el)
)
}
return pick(object, selectsAndRelations)
}