Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat(medusa): search orders by customer phone and name #2913

Merged
merged 5 commits into from
Dec 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/light-cars-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/medusa": patch
---

Make orders queryable by customer fields
78 changes: 78 additions & 0 deletions integration-tests/api/__tests__/admin/order/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -1369,6 +1369,84 @@ describe("/admin/orders", () => {
)
})

it("list all orders with matching customer phone", async () => {
const order = await simpleOrderFactory(dbConnection, {
customer: {
phone: "1234567890",
},
})

const api = useApi()

const response = await api.get("/admin/orders?q=123456", adminReqConfig)

expect(response.status).toEqual(200)
expect(response.data.count).toEqual(1)
expect(response.data.orders).toHaveLength(1)
expect(response.data.orders).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: order.id,
customer: expect.objectContaining({
phone: "1234567890",
}),
}),
])
)
})

it("list all orders with matching customer first_name", async () => {
const order = await simpleOrderFactory(dbConnection, {
customer: {
first_name: "john",
},
})

const api = useApi()

const response = await api.get("/admin/orders?q=john", adminReqConfig)

expect(response.status).toEqual(200)
expect(response.data.count).toEqual(1)
expect(response.data.orders).toHaveLength(1)
expect(response.data.orders).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: order.id,
customer: expect.objectContaining({
first_name: "john",
}),
}),
])
)
})

it("list all orders with matching customer last_name", async () => {
const order = await simpleOrderFactory(dbConnection, {
customer: {
last_name: "Doe",
},
})

const api = useApi()

const response = await api.get("/admin/orders?q=Doe", adminReqConfig)

expect(response.status).toEqual(200)
expect(response.data.count).toEqual(1)
expect(response.data.orders).toHaveLength(1)
expect(response.data.orders).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: order.id,
customer: expect.objectContaining({
last_name: "Doe",
}),
}),
])
)
})

it("list all orders with matching shipping_address first name", async () => {
const api = useApi()

Expand Down
6 changes: 6 additions & 0 deletions integration-tests/api/factories/simple-customer-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import {
export type CustomerFactoryData = {
id?: string
email?: string
phone?: string
first_name?: string
last_name?: string
groups?: CustomerGroupFactoryData[]
password_hash?: string
has_account?: boolean
Expand All @@ -29,6 +32,9 @@ export const simpleCustomerFactory = async (
const c = manager.create(Customer, {
id: customerId,
email: data.email ?? faker.internet.email(),
phone: data.phone ?? faker.phone.phoneNumber(),
first_name: data.first_name ?? faker.name.firstName(),
last_name: data.last_name ?? faker.name.lastName(),
password_hash:
data.password_hash ??
"c2NyeXB0AAEAAAABAAAAAVMdaddoGjwU1TafDLLlBKnOTQga7P2dbrfgf3fB+rCD/cJOMuGzAvRdKutbYkVpuJWTU39P7OpuWNkUVoEETOVLMJafbI8qs8Qx/7jMQXkN", // password matching "test"
Expand Down
4 changes: 4 additions & 0 deletions packages/medusa/src/services/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ class OrderService extends TransactionBaseService {
alias: "order",
innerJoin: {
shipping_address: "order.shipping_address",
customer: "order.customer",
},
}

Expand All @@ -233,6 +234,9 @@ class OrderService extends TransactionBaseService {
})
.orWhere(`order.email ILIKE :q`, { q: `%${q}%` })
.orWhere(`display_id::varchar(255) ILIKE :dId`, { dId: `${q}` })
.orWhere(`customer.first_name ILIKE :q`, { q: `%${q}%` })
.orWhere(`customer.last_name ILIKE :q`, { q: `%${q}%` })
.orWhere(`customer.phone ILIKE :q`, { q: `%${q}%` })
})
)
}
Expand Down