From 645e0d0ec5e2e49048887c62db662427c8a39cdf Mon Sep 17 00:00:00 2001 From: Philip Korsholm <88927411+pKorsholm@users.noreply.github.com> Date: Thu, 29 Dec 2022 15:20:18 +0100 Subject: [PATCH] Feat(medusa): search orders by customer phone and name (#2913) * add support for customer first- and last names and phone * test for querying customer first_name, last_name and phone * add customer as prop for querying orders * polishing before pr * changeset --- .changeset/light-cars-smile.md | 5 ++ .../api/__tests__/admin/order/order.js | 78 +++++++++++++++++++ .../api/factories/simple-customer-factory.ts | 6 ++ packages/medusa/src/services/order.ts | 4 + 4 files changed, 93 insertions(+) create mode 100644 .changeset/light-cars-smile.md diff --git a/.changeset/light-cars-smile.md b/.changeset/light-cars-smile.md new file mode 100644 index 0000000000..91a9dc038d --- /dev/null +++ b/.changeset/light-cars-smile.md @@ -0,0 +1,5 @@ +--- +"@medusajs/medusa": patch +--- + +Make orders queryable by customer fields diff --git a/integration-tests/api/__tests__/admin/order/order.js b/integration-tests/api/__tests__/admin/order/order.js index 9f7479ea9e..710467b307 100644 --- a/integration-tests/api/__tests__/admin/order/order.js +++ b/integration-tests/api/__tests__/admin/order/order.js @@ -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() diff --git a/integration-tests/api/factories/simple-customer-factory.ts b/integration-tests/api/factories/simple-customer-factory.ts index 9e01859d70..7c06e5d7e7 100644 --- a/integration-tests/api/factories/simple-customer-factory.ts +++ b/integration-tests/api/factories/simple-customer-factory.ts @@ -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 @@ -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" diff --git a/packages/medusa/src/services/order.ts b/packages/medusa/src/services/order.ts index 48e6faa12f..037934d967 100644 --- a/packages/medusa/src/services/order.ts +++ b/packages/medusa/src/services/order.ts @@ -220,6 +220,7 @@ class OrderService extends TransactionBaseService { alias: "order", innerJoin: { shipping_address: "order.shipping_address", + customer: "order.customer", }, } @@ -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}%` }) }) ) }