-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(medusa): categories can be ranked based on position #3341
Conversation
🦋 Changeset detectedLatest commit: 94a370e The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
ba9a574
to
6ec9963
Compare
de71f7e
to
0d55773
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM @riqwan 🎉
packages/medusa/src/api/routes/admin/product-categories/update-product-category.ts
Outdated
Show resolved
Hide resolved
7d7b709
to
c0baf45
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tricky to wrap my head around the repositioning, but your comments helped a lot. Also, props for the implementation. This was prob. not an easy one 💪
const targetPosition = input.position | ||
const shouldChangeParent = | ||
targetParentId !== undefined && targetParentId !== originalParentId | ||
const shouldIncrementPosition = false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion(non-blocking): It seems, we don't need a const for this one. Return false directly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very great work man
}) | ||
|
||
productCategory = await simpleProductCategoryFactory(dbConnection, { | ||
name: "sweater", | ||
parent_category: productCategoryParent, | ||
is_internal: true, | ||
position: 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Should we call it rank
to be coherent with the variant ranking as well?
@@ -275,15 +340,31 @@ describe("/admin/product-categories", () => { | |||
category_children: [ | |||
expect.objectContaining({ | |||
id: productCategoryChild2.id, | |||
category_children: [] | |||
category_children: [], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Should we also check the name for the different object?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we only care about position in this one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah but since the handle is part of the order, to avoid regression it should be checked as well right? Otherwise someone can remove the order by handle and it will still be green even though the result is different
|
||
const response = await api.post( | ||
`/admin/product-categories`, | ||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion(nit): Move that to a const payload and then use it here plus line 461/4462 you can check payload.name and payload.handle. The idea is that if we need to change the values later for any reason, there is less things to change
) | ||
}) | ||
|
||
it("when position is greater than list count, position is updated to last element", async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: ... is updated to elements count + 1
) | ||
}) | ||
|
||
it("when parent is updated with position, position accurately updated", async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: ..., position should be updated
@@ -16,6 +16,7 @@ describe("/store/product-categories", () => { | |||
let productCategoryParent = null | |||
let productCategoryChild2 = null | |||
let productCategoryChild3 = null | |||
let productCategoryChild4 = null |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: same changes as the admin file
expect(productCategoryRepository.findDescendantsTree).toHaveBeenCalledTimes(1) | ||
expect(productCategoryRepository.getFreeTextSearchResultsAndCount).toHaveBeenCalledWith( | ||
{ | ||
order: { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Since we are ordering by default, does it make sense to have this one?
@@ -59,14 +62,15 @@ describe("ProductCategoryService", () => { | |||
expect(productCategoryRepository.getFreeTextSearchResultsAndCount).toHaveBeenCalledWith( | |||
{ | |||
order: { | |||
created_at: "DESC", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Since we are ordering by default, does it make sense to have this one?
}, | ||
treeSelector: QuerySelector<ProductCategory> = {} | ||
): Promise<[ProductCategory[], number]> { | ||
const includeDescendantsTree = selector.include_descendants_tree | ||
const includeDescendantsTree = selector.include_descendants_tree || false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion(nit): !!selector.include_descendants_tree
here if it is false it will go to the || condition
q, | ||
treeSelector, | ||
includeDescendantsTree | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
todo: update jsdoc for the retrieve method
/snapshot-this |
The latest updates on your projects. Learn more about Vercel for Git ↗︎ 1 Ignored Deployment
|
What:
Introduces ranking into product categories.
Why:
For categories to be displayed in a particular way that isn't based on the existing sorting algos we provide.
How:
Introduces a position column that is unique by tree node and position number that cannot go negative.
It also introduces some heavy logic in the services that manages ranking information on create/update/delete.
RESOLVES CORE-1174
RESOLVES CORE-1183