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): match order group and order display ids in order group search (#1638)
Viktor Holik110e584e50c6
diff --git a/integration-tests/http/order/admin/order-list-filters.spec.ts b/integration-tests/http/order/admin/order-list-filters.spec.tsindex 0c2a7ac33..2c9c1765e 100644--- a/integration-tests/http/order/admin/order-list-filters.spec.ts+++ b/integration-tests/http/order/admin/order-list-filters.spec.ts@@ -513,6 +513,66 @@ medusaIntegrationTestRunner({})})+ describe("GET /admin/order-groups?q=...", () => {+ const detail = async (orderGroupId: string) => {+ const response = await api.get(+ `/admin/order-groups/${orderGroupId}?fields=+display_id,+orders.display_id`,+ adminHeaders+ )+ return response.data.order_group+ }++ it("finds a group by its own display_id, with or without the #G prefix", async () => {+ const { orderGroupId } = await completeCartCheckout(+ seller1Seed.offer.id+ )+ const { display_id } = await detail(orderGroupId)++ for (const q of [`${display_id}`, `#G${display_id}`, `g${display_id}`]) {+ const response = await api.get(+ `/admin/order-groups?q=${encodeURIComponent(q)}`,+ adminHeaders+ )+ expect(response.status).toEqual(200)+ const ids = response.data.order_groups.map((g: any) => g.id)+ expect(ids).toContain(orderGroupId)+ }+ })++ it("finds a group by the display_id of one of its orders", async () => {+ const { orderGroupId } = await completeMultiSellerCheckout([+ seller1Seed.offer.id,+ seller2Seed.offer.id,+ ])+ const group = await detail(orderGroupId)+ const orderDisplayId = group.orders[1].display_id++ const response = await api.get(+ `/admin/order-groups?q=${orderDisplayId}`,+ adminHeaders+ )++ expect(response.status).toEqual(200)+ const ids = response.data.order_groups.map((g: any) => g.id)+ expect(ids).toContain(orderGroupId)+ })++ it("still matches id fragments", async () => {+ const { orderGroupId } = await completeCartCheckout(+ seller1Seed.offer.id+ )++ const response = await api.get(+ `/admin/order-groups?q=${orderGroupId.slice(-8)}`,+ adminHeaders+ )++ expect(response.status).toEqual(200)+ const ids = response.data.order_groups.map((g: any) => g.id)+ expect(ids).toContain(orderGroupId)+ })+ })+describe("GET /admin/order-groups?status=...", () => {it("matches groups by child order status without shrinking seller_count or total", async () => {const { orderGroupId, orders } =diff --git a/packages/core/src/modules/seller/repositories/order-group.ts b/packages/core/src/modules/seller/repositories/order-group.tsindex 53b2d04f1..4f095a880 100644--- a/packages/core/src/modules/seller/repositories/order-group.ts+++ b/packages/core/src/modules/seller/repositories/order-group.ts@@ -18,6 +18,13 @@ const OPERATOR_MAP = {const SORTABLE_COLUMNS = ["display_id", "created_at", "updated_at"] as const+// Operators search by the numbers they see: the group shown as `#G258` in the+// list and the order `#264` quoted in customer and seller emails.+const parseDisplayIdSearch = (search: string): number | undefined => {+ const match = /^#?g?(\d{1,9})$/i.exec(search)+ return match ? Number(match[1]) : undefined+}+const normalizeDirection = (value: unknown): "ASC" | "DESC" | undefined => {if (typeof value !== "string") {return undefined@@ -145,9 +152,26 @@ export class OrderGroupRepository extends DALUtils.mikroOrmBaseRepositoryFactory}if (filters.q) {- whereClauses.push("(og.id ILIKE ? OR og.customer_id ILIKE ?)")- const searchPattern = `%${filters.q}%`+ const search = String(filters.q).trim()+ const searchClauses = ["og.id ILIKE ?", "og.customer_id ILIKE ?"]+ const searchPattern = `%${search}%`params.push(searchPattern, searchPattern)++ const displayId = parseDisplayIdSearch(search)+ if (displayId !== undefined) {+ searchClauses.push(+ "og.display_id = ?",+ `EXISTS (+ SELECT 1+ FROM order_group_order qogo+ JOIN "order" qo ON qo.id = qogo.order_id+ WHERE qogo.order_group_id = og.id AND qo.display_id = ?+ )`+ )+ params.push(displayId, displayId)+ }++ whereClauses.push(`(${searchClauses.join(" OR ")})`)}// Order-level filters only decide which groups match. The aggregates below