fix(order): searchable fields (#9493)

FIXES: TRI-353
This commit is contained in:
Carlos R. L. Rodrigues
2024-10-07 17:45:20 +00:00
committed by GitHub
parent dc9c23be34
commit 8fbef8a667
6 changed files with 45 additions and 11 deletions
@@ -140,6 +140,23 @@ medusaIntegrationTestRunner({
.order .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 () => { it("should only create fulfillments grouped by shipping requirement", async () => {
const { const {
response: { data }, response: { data },
@@ -402,7 +402,7 @@ class SearchableEntity1 {
deleted_at: Date | null deleted_at: Date | null
@Searchable() @Searchable()
@Property() @Property({ columnType: "text" })
searchableField: string searchableField: string
@Searchable() @Searchable()
@@ -430,7 +430,7 @@ class SearchableEntity2 {
deleted_at: Date | null deleted_at: Date | null
@Searchable() @Searchable()
@Property() @Property({ columnType: "text" })
searchableField: string searchableField: string
@ManyToOne(() => SearchableEntity1, { mapToPk: true }) @ManyToOne(() => SearchableEntity1, { mapToPk: true })
@@ -450,12 +450,12 @@ export {
Entity2, Entity2,
Entity2WithUnDecoratedProp, Entity2WithUnDecoratedProp,
InternalCircularDependencyEntity1, InternalCircularDependencyEntity1,
RecursiveEntity1,
RecursiveEntity2,
SearchableEntity1,
SearchableEntity2,
Product, Product,
ProductOption, ProductOption,
ProductOptionValue, ProductOptionValue,
ProductVariant, ProductVariant,
RecursiveEntity1,
RecursiveEntity2,
SearchableEntity1,
SearchableEntity2,
} }
@@ -1,11 +1,11 @@
import { EntityMetadata, EntitySchema, ReferenceType } from "@mikro-orm/core"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import type { import type {
FindOptions,
EntityClass, EntityClass,
EntityProperty, EntityProperty,
FindOneOptions, FindOneOptions,
FindOptions,
} from "@mikro-orm/core" } from "@mikro-orm/core"
import { EntityMetadata, EntitySchema, ReferenceType } from "@mikro-orm/core"
import { SqlEntityManager } from "@mikro-orm/postgresql"
export const FreeTextSearchFilterKey = "freeTextSearch" export const FreeTextSearchFilterKey = "freeTextSearch"
@@ -57,8 +57,13 @@ function retrieveRelationsConstraints(
continue continue
} }
const isText = propertyConfiguration?.columnTypes?.includes("text")
const columnName = isText
? propertyConfiguration.name
: `${propertyConfiguration.name}::text`
relationFreeTextSearchWhere.push({ relationFreeTextSearchWhere.push({
[propertyConfiguration.name]: { [columnName]: {
$ilike: `%${searchValue}%`, $ilike: `%${searchValue}%`,
}, },
}) })
@@ -2,6 +2,7 @@ import { DAL } from "@medusajs/framework/types"
import { import {
createPsqlIndexStatementHelper, createPsqlIndexStatementHelper,
generateEntityId, generateEntityId,
Searchable,
} from "@medusajs/framework/utils" } from "@medusajs/framework/utils"
import { import {
BeforeCreate, BeforeCreate,
@@ -30,33 +31,42 @@ export default class OrderAddress {
@CustomerIdIndex.MikroORMIndex() @CustomerIdIndex.MikroORMIndex()
customer_id: string | null = null customer_id: string | null = null
@Searchable()
@Property({ columnType: "text", nullable: true }) @Property({ columnType: "text", nullable: true })
company: string | null = null company: string | null = null
@Searchable()
@Property({ columnType: "text", nullable: true }) @Property({ columnType: "text", nullable: true })
first_name: string | null = null first_name: string | null = null
@Searchable()
@Property({ columnType: "text", nullable: true }) @Property({ columnType: "text", nullable: true })
last_name: string | null = null last_name: string | null = null
@Searchable()
@Property({ columnType: "text", nullable: true }) @Property({ columnType: "text", nullable: true })
address_1: string | null = null address_1: string | null = null
@Searchable()
@Property({ columnType: "text", nullable: true }) @Property({ columnType: "text", nullable: true })
address_2: string | null = null address_2: string | null = null
@Searchable()
@Property({ columnType: "text", nullable: true }) @Property({ columnType: "text", nullable: true })
city: string | null = null city: string | null = null
@Property({ columnType: "text", nullable: true }) @Property({ columnType: "text", nullable: true })
country_code: string | null = null country_code: string | null = null
@Searchable()
@Property({ columnType: "text", nullable: true }) @Property({ columnType: "text", nullable: true })
province: string | null = null province: string | null = null
@Searchable()
@Property({ columnType: "text", nullable: true }) @Property({ columnType: "text", nullable: true })
postal_code: string | null = null postal_code: string | null = null
@Searchable()
@Property({ columnType: "text", nullable: true }) @Property({ columnType: "text", nullable: true })
phone: string | null = null phone: string | null = null
@@ -1,6 +1,7 @@
import { DAL } from "@medusajs/framework/types" import { DAL } from "@medusajs/framework/types"
import { import {
OrderStatus, OrderStatus,
Searchable,
createPsqlIndexStatementHelper, createPsqlIndexStatementHelper,
generateEntityId, generateEntityId,
} from "@medusajs/framework/utils" } from "@medusajs/framework/utils"
@@ -90,6 +91,7 @@ export default class Order {
@PrimaryKey({ columnType: "text" }) @PrimaryKey({ columnType: "text" })
id: string id: string
@Searchable()
@Property({ autoincrement: true, primary: false }) @Property({ autoincrement: true, primary: false })
@DisplayIdIndex.MikroORMIndex() @DisplayIdIndex.MikroORMIndex()
display_id: number display_id: number
@@ -130,6 +132,7 @@ export default class Order {
@IsDraftOrderIndex.MikroORMIndex() @IsDraftOrderIndex.MikroORMIndex()
is_draft_order: boolean = false is_draft_order: boolean = false
@Searchable()
@Property({ columnType: "text", nullable: true }) @Property({ columnType: "text", nullable: true })
email: string | null = null email: string | null = null
@@ -70,7 +70,6 @@ export default class ReturnReason {
cascade: [Cascade.PERSIST], cascade: [Cascade.PERSIST],
}) })
parent_return_reason?: Rel<ReturnReason> | null parent_return_reason?: Rel<ReturnReason> | null
Searchable
@OneToMany( @OneToMany(
() => ReturnReason, () => ReturnReason,
(return_reason) => return_reason.parent_return_reason, (return_reason) => return_reason.parent_return_reason,