Skip to content
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

Merged
merged 16 commits into from
Mar 6, 2023

Conversation

riqwan
Copy link
Contributor

@riqwan riqwan commented Feb 28, 2023

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

@changeset-bot
Copy link

changeset-bot bot commented Feb 28, 2023

🦋 Changeset detected

Latest commit: 94a370e

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@medusajs/medusa Patch
@medusajs/inventory Patch

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

@riqwan riqwan marked this pull request as ready for review February 28, 2023 12:03
@riqwan riqwan requested a review from a team as a code owner February 28, 2023 12:03
Copy link
Contributor

@fPolic fPolic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM @riqwan 🎉

Copy link
Contributor

@olivermrbl olivermrbl left a 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
Copy link
Contributor

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.

Copy link
Member

@adrien2p adrien2p left a 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
Copy link
Member

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: [],
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member

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`,
{
Copy link
Member

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 () => {
Copy link
Member

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 () => {
Copy link
Member

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
Copy link
Member

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: {
Copy link
Member

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",
Copy link
Member

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
Copy link
Member

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
)
Copy link
Member

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

@riqwan
Copy link
Contributor Author

riqwan commented Mar 2, 2023

/snapshot-this

@github-actions
Copy link
Contributor

github-actions bot commented Mar 2, 2023

🚀 A snapshot release has been made for this PR

Test the snapshots by updating your package.json with the newly published versions:

yarn add @medusajs/[email protected]
yarn add @medusajs/[email protected]
yarn add @medusajs/[email protected]
yarn add @medusajs/[email protected]
yarn add @medusajs/[email protected]
yarn add @medusajs/[email protected]

Latest commit: 2507af6

@vercel
Copy link

vercel bot commented Mar 5, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Comments Updated
medusa-docs ⬜️ Ignored (Inspect) Mar 6, 2023 at 1:50PM (UTC)

@riqwan riqwan merged commit 0a6aa0e into develop Mar 6, 2023
@riqwan riqwan deleted the feat/ranking-categories branch March 6, 2023 14:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants