Skip to content

Commit

Permalink
chore: added filters to get endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
riqwan committed Jan 12, 2023
1 parent 80cd0de commit 3f27cf9
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 10 deletions.
45 changes: 43 additions & 2 deletions integration-tests/api/__tests__/store/product-category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ describe("/store/product-categories", () => {
let productCategoryChild = null
let productCategoryParent = null
let productCategoryChild2 = null
let productCategoryChild3 = null

beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", ".."))
Expand All @@ -38,24 +39,36 @@ describe("/store/product-categories", () => {
productCategoryParent = await simpleProductCategoryFactory(dbConnection, {
name: "category parent",
handle: "category-parent",
is_active: true,
})

productCategory = await simpleProductCategoryFactory(dbConnection, {
name: "category",
handle: "category",
parent_category: productCategoryParent,
is_active: true,
})

productCategoryChild = await simpleProductCategoryFactory(dbConnection, {
name: "category child",
handle: "category-child",
parent_category: productCategory,
is_active: true,
})

productCategoryChild2 = await simpleProductCategoryFactory(dbConnection, {
name: "category child 2",
handle: "category-child-2",
parent_category: productCategoryChild,
parent_category: productCategory,
is_internal: true,
is_active: true,
})

productCategoryChild3 = await simpleProductCategoryFactory(dbConnection, {
name: "category child 3",
handle: "category-child-3",
parent_category: productCategory,
is_active: false,
})
})

Expand Down Expand Up @@ -86,7 +99,7 @@ describe("/store/product-categories", () => {
id: productCategoryChild.id,
handle: productCategoryChild.handle,
name: productCategoryChild.name,
})
}),
]
})
)
Expand All @@ -105,5 +118,33 @@ describe("/store/product-categories", () => {
expect(error.response.data.type).toEqual('invalid_data')
expect(error.response.data.message).toEqual('Fields [mpath] are not valid')
})

it("throws error on querying for internal product category", async () => {
const api = useApi()

const error = await api.get(
`/store/product-categories/${productCategoryChild2.id}`,
).catch(e => e)

expect(error.response.status).toEqual(404)
expect(error.response.data.type).toEqual('not_found')
expect(error.response.data.message).toEqual(
`ProductCategory with id: ${productCategoryChild2.id} was not found`
)
})

it("throws error on querying for inactive product category", async () => {
const api = useApi()

const error = await api.get(
`/store/product-categories/${productCategoryChild3.id}`,
).catch(e => e)

expect(error.response.status).toEqual(404)
expect(error.response.data.type).toEqual('not_found')
expect(error.response.data.message).toEqual(
`ProductCategory with id: ${productCategoryChild3.id} was not found`
)
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,18 @@ export default async (req: Request, res: Response) => {
const { id } = req.params
const { retrieveConfig } = req

const scopes = {
is_internal: false,
is_active: true,
}
const productCategoryService: ProductCategoryService = req.scope.resolve(
"productCategoryService"
)

const productCategory = await productCategoryService.retrieve(
id,
retrieveConfig
retrieveConfig,
scopes,
)

res.status(200).json({
Expand All @@ -78,7 +83,8 @@ export default async (req: Request, res: Response) => {
// onto its children nodes. As an alternative, we are transforming the data post query.
product_category: transformTreeNodesWithConfig(
productCategory,
retrieveConfig
retrieveConfig,
scopes,
),
})
}
Expand Down
6 changes: 4 additions & 2 deletions packages/medusa/src/services/product-category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ class ProductCategoryService extends TransactionBaseService {
*/
async retrieve(
productCategoryId: string,
config: FindConfig<ProductCategory> = {}
config: FindConfig<ProductCategory> = {},
selector: Selector<ProductCategory> = {},
): Promise<ProductCategory> {
if (!isDefined(productCategoryId)) {
throw new MedusaError(
Expand All @@ -95,7 +96,8 @@ class ProductCategoryService extends TransactionBaseService {
)
}

const query = buildQuery({ id: productCategoryId }, config)
const selectors = Object.assign({id: productCategoryId }, selector)
const query = buildQuery(selectors, config)
const productCategoryRepo = this.manager_.getCustomRepository(
this.productCategoryRepo_
)
Expand Down
32 changes: 28 additions & 4 deletions packages/medusa/src/utils/transformers/tree.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,48 @@
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) {
export function transformTreeNodesWithConfig(
object,
config,
scopes = {},
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(scopes)) {
const modelValue = object[key]

if (isDefined(modelValue) && modelValue !== value) {
return null
}
}


if (object.parent_category) {
object.parent_category = transformTreeNodesWithConfig(
object.parent_category,
config
config,
scopes,
true
)
}

if ((object.category_children || []).length > 0) {
if (!isParentNode && (object.category_children || []).length > 0) {
object.category_children = object.category_children.map((child) => {
return transformTreeNodesWithConfig(child, config)
return transformTreeNodesWithConfig(child, config, scopes)
})

object.category_children = filter(
object.category_children,
el => !isNull(el)
)
}

return pick(object, selectsAndRelations)
Expand Down

0 comments on commit 3f27cf9

Please sign in to comment.