Skip to content

Commit 3826bde

Browse files
authored
fix(medusa): Query Config update Order By filter (#12781)
**What** Fixed a bug in the prepareListQuery function where nested field ordering was not properly building the expected nested object structure. The function was returning flat objects like { "employee.first_name": "ASC" } instead of the correct nested structure { "employee": { "first_name": "ASC" } }. **Why** The buildOrder function is designed to create nested objects from dot-notation field paths, which is essential for proper query building in the Medusa framework. When this functionality was broken, it prevented correct ordering of related fields and caused queries to fail or return unexpected results. **How** - Root cause: The `prepareListQuery` function was not properly utilizing the `buildOrder` utility function to transform dot-notation field paths into nested objects - Before: order = "employee.first_name" → { "employee.first_name": "ASC" } - After: order = "employee.first_name" → { "employee": { "first_name": "ASC" } } - Added comprehensive tests: Created detailed unit tests for the prepareListQuery function focusing on buildOrder functionality, covering various scenarios including: - Simple ascending/descending order - Nested field ordering (e.g., product.title) - Deeply nested ordering (e.g., product.variants.prices.amount) - Multiple nesting levels (up to 5 levels deep) - Added integration tests: Created integration tests in `product.spec.ts` to verify the full end-to-end functionality of nested ordering with variant titles The fix ensures that the buildOrder function properly transforms dot-notation field paths into the expected nested object structure, enabling correct query building for related field ordering throughout the Medusa framework. Resolves SUP-1868
1 parent 490695a commit 3826bde

6 files changed

Lines changed: 613 additions & 20 deletions

File tree

integration-tests/http/__tests__/product/store/product.spec.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -788,31 +788,33 @@ medusaIntegrationTestRunner({
788788

789789
// TODO: This doesn't work currently, but worked in v1
790790
it.skip("returns a list of ordered products by variants title DESC", async () => {
791+
})
792+
793+
it("returns a list of ordered products by variant title ASC", async () => {
791794
const response = await api.get(
792-
"/store/products?order=-variants.title",
795+
"/store/products?order=variants.title",
793796
storeHeaders
794797
)
795798

796799
expect(response.status).toEqual(200)
797-
expect(response.data.products).toEqual([
798-
expect.objectContaining({ id: product3.id }),
799-
expect.objectContaining({ id: product2.id }),
800-
expect.objectContaining({ id: product.id }),
800+
expect(response.data.products.map((p) => p.id)).toEqual([
801+
product.id,
802+
product2.id,
803+
product3.id,
801804
])
802805
})
803806

804-
// TODO: This doesn't work currently, but worked in v1
805-
it.skip("returns a list of ordered products by variants title ASC", async () => {
807+
it("returns a list of ordered products by variant title DESC", async () => {
806808
const response = await api.get(
807-
"/store/products?order=variants.title",
809+
"/store/products?order=-variants.title",
808810
storeHeaders
809811
)
810812

811813
expect(response.status).toEqual(200)
812-
expect(response.data.products).toEqual([
813-
expect.objectContaining({ id: product3.id }),
814-
expect.objectContaining({ id: product2.id }),
815-
expect.objectContaining({ id: product.id }),
814+
expect(response.data.products.map((p) => p.id)).toEqual([
815+
product3.id,
816+
product2.id,
817+
product.id,
816818
])
817819
})
818820

0 commit comments

Comments
 (0)