The complete open source platform to run your entire marketplace. Storefront, vendor panel, admin console, and integrations in one enterprise-grade platform you own.
TypeScript b2b-marketplacecomposable-commerceecommerceenterpriseheadless-commercemarketplacemedusamedusajsmulti-vendortypescript
fix(core): sort order groups by display_id (#1633)
Viktor Holikf0d58e780b50
diff --git a/integration-tests/http/order/admin/order-list-filters.spec.ts b/integration-tests/http/order/admin/order-list-filters.spec.tsindex 0102db2ba..0c2a7ac33 100644--- a/integration-tests/http/order/admin/order-list-filters.spec.ts+++ b/integration-tests/http/order/admin/order-list-filters.spec.ts@@ -573,6 +573,46 @@ medusaIntegrationTestRunner({expect(pendingGroup.total).toEqual(baseline.total)})})++ describe("GET /admin/order-groups?order=...", () => {+ it("sorts by display_id in both directions", async () => {+ const { orderGroupId: first } = await completeCartCheckout(+ seller1Seed.offer.id+ )+ const { orderGroupId: second } = await completeCartCheckout(+ seller2Seed.offer.id+ )+ const { orderGroupId: third } = await completeCartCheckout(+ seller1Seed.offer.id+ )++ const ascending = await api.get(+ `/admin/order-groups?order=display_id&fields=id,display_id`,+ adminHeaders+ )+ expect(ascending.status).toEqual(200)+ const ascIds = ascending.data.order_groups.map(+ (g: any) => g.id+ )+ expect(ascIds).toEqual([first, second, third])+ const ascDisplayIds = ascending.data.order_groups.map(+ (g: any) => g.display_id+ )+ expect(ascDisplayIds).toEqual(+ [...ascDisplayIds].sort((a, b) => a - b)+ )++ const descending = await api.get(+ `/admin/order-groups?order=-display_id&fields=id,display_id`,+ adminHeaders+ )+ expect(descending.status).toEqual(200)+ const descIds = descending.data.order_groups.map(+ (g: any) => g.id+ )+ expect(descIds).toEqual([third, second, first])+ })+ })})},})diff --git a/packages/core/src/modules/seller/repositories/order-group.ts b/packages/core/src/modules/seller/repositories/order-group.tsindex c626f2825..53b2d04f1 100644--- a/packages/core/src/modules/seller/repositories/order-group.ts+++ b/packages/core/src/modules/seller/repositories/order-group.ts@@ -16,6 +16,16 @@ const OPERATOR_MAP = {$ilike: "ILIKE",}+const SORTABLE_COLUMNS = ["display_id", "created_at", "updated_at"] as const++const normalizeDirection = (value: unknown): "ASC" | "DESC" | undefined => {+ if (typeof value !== "string") {+ return undefined+ }+ const upper = value.toUpperCase()+ return upper.startsWith("ASC") ? "ASC" : upper.startsWith("DESC") ? "DESC" : undefined+}+export class OrderGroupRepository extends DALUtils.mikroOrmBaseRepositoryFactory(OrderGroup) {@@ -155,11 +165,11 @@ export class OrderGroupRepository extends DALUtils.mikroOrmBaseRepositoryFactory}const orderByClauses: string[] = []- if (orderBy.created_at) {- orderByClauses.push(`og.created_at ${orderBy.created_at}`)- }- if (orderBy.updated_at) {- orderByClauses.push(`og.updated_at ${orderBy.updated_at}`)+ for (const column of SORTABLE_COLUMNS) {+ const direction = normalizeDirection(orderBy[column])+ if (direction) {+ orderByClauses.push(`og.${column} ${direction}`)+ }}if (orderByClauses.length === 0) {orderByClauses.push("og.created_at DESC")