From 8fbef8a66769704737c6826111c85ffffbfb2923 Mon Sep 17 00:00:00 2001 From: "Carlos R. L. Rodrigues" <37986729+carlos-r-l-rodrigues@users.noreply.github.com> Date: Mon, 7 Oct 2024 14:45:20 -0300 Subject: [PATCH] fix(order): searchable fields (#9493) FIXES: TRI-353 --- .../http/__tests__/order/admin/order.spec.ts | 17 +++++++++++++++++ .../src/dal/mikro-orm/__fixtures__/utils.ts | 12 ++++++------ .../mikro-orm-free-text-search-filter.ts | 13 +++++++++---- packages/modules/order/src/models/address.ts | 10 ++++++++++ packages/modules/order/src/models/order.ts | 3 +++ .../modules/order/src/models/return-reason.ts | 1 - 6 files changed, 45 insertions(+), 11 deletions(-) diff --git a/integration-tests/http/__tests__/order/admin/order.spec.ts b/integration-tests/http/__tests__/order/admin/order.spec.ts index 509ab8efed..6bf99aa0c6 100644 --- a/integration-tests/http/__tests__/order/admin/order.spec.ts +++ b/integration-tests/http/__tests__/order/admin/order.spec.ts @@ -140,6 +140,23 @@ medusaIntegrationTestRunner({ .order }) + it("should find the order querying it by number", async () => { + const userEmail = "tony@stark-industries.com" + + const response = ( + await api.get(`/admin/orders/?q=non-existing`, adminHeaders) + ).data + + expect(response.orders).toHaveLength(0) + + const response2 = ( + await api.get(`/admin/orders/?fields=+email&q=@stark`, adminHeaders) + ).data + + expect(response2.orders).toHaveLength(1) + expect(response2.orders[0].email).toEqual(userEmail) + }) + it("should only create fulfillments grouped by shipping requirement", async () => { const { response: { data }, diff --git a/packages/core/utils/src/dal/mikro-orm/__fixtures__/utils.ts b/packages/core/utils/src/dal/mikro-orm/__fixtures__/utils.ts index 8e64311ab6..d3dec6ef44 100644 --- a/packages/core/utils/src/dal/mikro-orm/__fixtures__/utils.ts +++ b/packages/core/utils/src/dal/mikro-orm/__fixtures__/utils.ts @@ -402,7 +402,7 @@ class SearchableEntity1 { deleted_at: Date | null @Searchable() - @Property() + @Property({ columnType: "text" }) searchableField: string @Searchable() @@ -430,7 +430,7 @@ class SearchableEntity2 { deleted_at: Date | null @Searchable() - @Property() + @Property({ columnType: "text" }) searchableField: string @ManyToOne(() => SearchableEntity1, { mapToPk: true }) @@ -450,12 +450,12 @@ export { Entity2, Entity2WithUnDecoratedProp, InternalCircularDependencyEntity1, - RecursiveEntity1, - RecursiveEntity2, - SearchableEntity1, - SearchableEntity2, Product, ProductOption, ProductOptionValue, ProductVariant, + RecursiveEntity1, + RecursiveEntity2, + SearchableEntity1, + SearchableEntity2, } diff --git a/packages/core/utils/src/dal/mikro-orm/mikro-orm-free-text-search-filter.ts b/packages/core/utils/src/dal/mikro-orm/mikro-orm-free-text-search-filter.ts index cddc0a9891..4b50830b06 100644 --- a/packages/core/utils/src/dal/mikro-orm/mikro-orm-free-text-search-filter.ts +++ b/packages/core/utils/src/dal/mikro-orm/mikro-orm-free-text-search-filter.ts @@ -1,11 +1,11 @@ -import { EntityMetadata, EntitySchema, ReferenceType } from "@mikro-orm/core" -import { SqlEntityManager } from "@mikro-orm/postgresql" import type { - FindOptions, EntityClass, EntityProperty, FindOneOptions, + FindOptions, } from "@mikro-orm/core" +import { EntityMetadata, EntitySchema, ReferenceType } from "@mikro-orm/core" +import { SqlEntityManager } from "@mikro-orm/postgresql" export const FreeTextSearchFilterKey = "freeTextSearch" @@ -57,8 +57,13 @@ function retrieveRelationsConstraints( continue } + const isText = propertyConfiguration?.columnTypes?.includes("text") + const columnName = isText + ? propertyConfiguration.name + : `${propertyConfiguration.name}::text` + relationFreeTextSearchWhere.push({ - [propertyConfiguration.name]: { + [columnName]: { $ilike: `%${searchValue}%`, }, }) diff --git a/packages/modules/order/src/models/address.ts b/packages/modules/order/src/models/address.ts index 83d7d0e7c1..a4cbc3bdf4 100644 --- a/packages/modules/order/src/models/address.ts +++ b/packages/modules/order/src/models/address.ts @@ -2,6 +2,7 @@ import { DAL } from "@medusajs/framework/types" import { createPsqlIndexStatementHelper, generateEntityId, + Searchable, } from "@medusajs/framework/utils" import { BeforeCreate, @@ -30,33 +31,42 @@ export default class OrderAddress { @CustomerIdIndex.MikroORMIndex() customer_id: string | null = null + @Searchable() @Property({ columnType: "text", nullable: true }) company: string | null = null + @Searchable() @Property({ columnType: "text", nullable: true }) first_name: string | null = null + @Searchable() @Property({ columnType: "text", nullable: true }) last_name: string | null = null + @Searchable() @Property({ columnType: "text", nullable: true }) address_1: string | null = null + @Searchable() @Property({ columnType: "text", nullable: true }) address_2: string | null = null + @Searchable() @Property({ columnType: "text", nullable: true }) city: string | null = null @Property({ columnType: "text", nullable: true }) country_code: string | null = null + @Searchable() @Property({ columnType: "text", nullable: true }) province: string | null = null + @Searchable() @Property({ columnType: "text", nullable: true }) postal_code: string | null = null + @Searchable() @Property({ columnType: "text", nullable: true }) phone: string | null = null diff --git a/packages/modules/order/src/models/order.ts b/packages/modules/order/src/models/order.ts index 302f1ddc01..283e60fcec 100644 --- a/packages/modules/order/src/models/order.ts +++ b/packages/modules/order/src/models/order.ts @@ -1,6 +1,7 @@ import { DAL } from "@medusajs/framework/types" import { OrderStatus, + Searchable, createPsqlIndexStatementHelper, generateEntityId, } from "@medusajs/framework/utils" @@ -90,6 +91,7 @@ export default class Order { @PrimaryKey({ columnType: "text" }) id: string + @Searchable() @Property({ autoincrement: true, primary: false }) @DisplayIdIndex.MikroORMIndex() display_id: number @@ -130,6 +132,7 @@ export default class Order { @IsDraftOrderIndex.MikroORMIndex() is_draft_order: boolean = false + @Searchable() @Property({ columnType: "text", nullable: true }) email: string | null = null diff --git a/packages/modules/order/src/models/return-reason.ts b/packages/modules/order/src/models/return-reason.ts index 5ace1f0823..9bd52a28a0 100644 --- a/packages/modules/order/src/models/return-reason.ts +++ b/packages/modules/order/src/models/return-reason.ts @@ -70,7 +70,6 @@ export default class ReturnReason { cascade: [Cascade.PERSIST], }) parent_return_reason?: Rel | null - Searchable @OneToMany( () => ReturnReason, (return_reason) => return_reason.parent_return_reason,