From 98da9e42e18746cf77c9ad7583cd7e2d0f82a35a Mon Sep 17 00:00:00 2001 From: Philip Korsholm Date: Thu, 29 Dec 2022 11:34:02 +0100 Subject: [PATCH 1/5] add support for customer first- and last names and phone --- integration-tests/api/factories/simple-customer-factory.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/integration-tests/api/factories/simple-customer-factory.ts b/integration-tests/api/factories/simple-customer-factory.ts index 9e01859d7009b..7c06e5d7e798e 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" From e783e8ec8643eb2b02fd7a2bb1200805d062182b Mon Sep 17 00:00:00 2001 From: Philip Korsholm Date: Thu, 29 Dec 2022 11:34:25 +0100 Subject: [PATCH 2/5] test for querying customer first_name, last_name and phone --- .../api/__tests__/admin/order/order.js | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/integration-tests/api/__tests__/admin/order/order.js b/integration-tests/api/__tests__/admin/order/order.js index 9f7479ea9e771..cdfe1e571169e 100644 --- a/integration-tests/api/__tests__/admin/order/order.js +++ b/integration-tests/api/__tests__/admin/order/order.js @@ -1369,6 +1369,86 @@ 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", + }, + }) + + console.log(order.customer) + + 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() From 790fb3cb18c56756cf536d704d0189bc05588d0a Mon Sep 17 00:00:00 2001 From: Philip Korsholm Date: Thu, 29 Dec 2022 11:34:40 +0100 Subject: [PATCH 3/5] add customer as prop for querying orders --- packages/medusa/src/services/order.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/medusa/src/services/order.ts b/packages/medusa/src/services/order.ts index 48e6faa12fadd..d1e7172ae850b 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,11 @@ class OrderService extends TransactionBaseService { }) .orWhere(`order.email ILIKE :q`, { q: `%${q}%` }) .orWhere(`display_id::varchar(255) ILIKE :dId`, { dId: `${q}` }) + + // Customer + .orWhere(`customer.first_name ILIKE :q`, { q: `%${q}%` }) + .orWhere(`customer.last_name ILIKE :q`, { q: `%${q}%` }) + .orWhere(`customer.phone ILIKE :q`, { q: `%${q}%` }) }) ) } From ad0d9e4b9e756ac9961996e7bfe2c071208e0414 Mon Sep 17 00:00:00 2001 From: Philip Korsholm Date: Thu, 29 Dec 2022 11:36:37 +0100 Subject: [PATCH 4/5] polishing before pr --- integration-tests/api/__tests__/admin/order/order.js | 2 -- packages/medusa/src/services/order.ts | 2 -- 2 files changed, 4 deletions(-) diff --git a/integration-tests/api/__tests__/admin/order/order.js b/integration-tests/api/__tests__/admin/order/order.js index cdfe1e571169e..710467b307671 100644 --- a/integration-tests/api/__tests__/admin/order/order.js +++ b/integration-tests/api/__tests__/admin/order/order.js @@ -1428,8 +1428,6 @@ describe("/admin/orders", () => { }, }) - console.log(order.customer) - const api = useApi() const response = await api.get("/admin/orders?q=Doe", adminReqConfig) diff --git a/packages/medusa/src/services/order.ts b/packages/medusa/src/services/order.ts index d1e7172ae850b..037934d9675e8 100644 --- a/packages/medusa/src/services/order.ts +++ b/packages/medusa/src/services/order.ts @@ -234,8 +234,6 @@ class OrderService extends TransactionBaseService { }) .orWhere(`order.email ILIKE :q`, { q: `%${q}%` }) .orWhere(`display_id::varchar(255) ILIKE :dId`, { dId: `${q}` }) - - // Customer .orWhere(`customer.first_name ILIKE :q`, { q: `%${q}%` }) .orWhere(`customer.last_name ILIKE :q`, { q: `%${q}%` }) .orWhere(`customer.phone ILIKE :q`, { q: `%${q}%` }) From 745d80efd86b6e9d7ba42bd7d668088b13570c7d Mon Sep 17 00:00:00 2001 From: Philip Korsholm Date: Thu, 29 Dec 2022 11:55:54 +0100 Subject: [PATCH 5/5] changeset --- .changeset/light-cars-smile.md | 5 +++++ 1 file changed, 5 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 0000000000000..91a9dc038db7a --- /dev/null +++ b/.changeset/light-cars-smile.md @@ -0,0 +1,5 @@ +--- +"@medusajs/medusa": patch +--- + +Make orders queryable by customer fields