From e0fa06fe964b8000ca539efa875ed6c322f6d57b Mon Sep 17 00:00:00 2001 From: Sebastian Rindom Date: Fri, 5 Nov 2021 16:48:29 +0100 Subject: [PATCH] fix: make it possible to update order shipping address (#668) --- .../admin/__snapshots__/order.js.snap | 22 +++++++ .../api/__tests__/admin/order.js | 57 +++++++++++++++++++ packages/medusa/src/services/order.js | 21 ++++--- 3 files changed, 93 insertions(+), 7 deletions(-) create mode 100644 integration-tests/api/__tests__/admin/__snapshots__/order.js.snap diff --git a/integration-tests/api/__tests__/admin/__snapshots__/order.js.snap b/integration-tests/api/__tests__/admin/__snapshots__/order.js.snap new file mode 100644 index 0000000000..7ab4c87e7c --- /dev/null +++ b/integration-tests/api/__tests__/admin/__snapshots__/order.js.snap @@ -0,0 +1,22 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`/admin/orders POST /admin/orders/:id updates a shipping adress 1`] = ` +Object { + "address_1": "Some Street", + "address_2": "", + "city": "losangeles", + "company": null, + "country_code": "us", + "created_at": Any, + "customer_id": null, + "deleted_at": null, + "first_name": "lebron", + "id": Any, + "last_name": null, + "metadata": null, + "phone": null, + "postal_code": "1235", + "province": "", + "updated_at": Any, +} +`; diff --git a/integration-tests/api/__tests__/admin/order.js b/integration-tests/api/__tests__/admin/order.js index 7d49dee62b..da5a7b07ec 100644 --- a/integration-tests/api/__tests__/admin/order.js +++ b/integration-tests/api/__tests__/admin/order.js @@ -74,6 +74,63 @@ describe("/admin/orders", () => { }) }) + describe("POST /admin/orders/:id", () => { + beforeEach(async () => { + try { + await adminSeeder(dbConnection) + await orderSeeder(dbConnection) + } catch (err) { + throw err + } + }) + + afterEach(async () => { + const db = useDb() + await db.teardown() + }) + + it("updates a shipping adress", async () => { + const api = useApi() + + const response = await api + .post( + "/admin/orders/test-order", + { + email: "test@test.com", + shipping_address: { + address_1: "Some Street", + address_2: "", + province: "", + postal_code: "1235", + city: "losangeles", + country_code: "us", + }, + }, + { + headers: { + authorization: "Bearer test_token", + }, + } + ) + .catch((err) => { + console.log(err.response.data) + }) + + expect(response.status).toEqual(200) + expect(response.data.order.shipping_address).toMatchSnapshot({ + id: expect.any(String), + address_1: "Some Street", + address_2: "", + province: "", + postal_code: "1235", + city: "losangeles", + country_code: "us", + created_at: expect.any(String), + updated_at: expect.any(String), + }) + }) + }) + describe("GET /admin/orders", () => { beforeEach(async () => { try { diff --git a/packages/medusa/src/services/order.js b/packages/medusa/src/services/order.js index 06c09f50d0..acb34b868b 100644 --- a/packages/medusa/src/services/order.js +++ b/packages/medusa/src/services/order.js @@ -784,7 +784,7 @@ class OrderService extends BaseService { await addrRepo.save({ ...addr, ...address }) } else { - const created = await addrRepo.create({ ...address }) + const created = addrRepo.create({ ...address }) await addrRepo.save(created) } } @@ -877,26 +877,33 @@ class OrderService extends BaseService { ) } - const { ...rest } = update + const { + metadata, + shipping_address, + billing_address, + no_notification, + items, + ...rest + } = update if ("metadata" in update) { - order.metadata = this.setMetadata_(order, update.metadata) + order.metadata = this.setMetadata_(order, metadata) } if ("shipping_address" in update) { - await this.updateShippingAddress_(order, update.shipping_address) + await this.updateShippingAddress_(order, shipping_address) } if ("billing_address" in update) { - await this.updateBillingAddress_(order, update.billing_address) + await this.updateBillingAddress_(order, billing_address) } if ("no_notification" in update) { - order.no_notification = update.no_notification + order.no_notification = no_notification } if ("items" in update) { - for (const item of update.items) { + for (const item of items) { await this.lineItemService_.withTransaction(manager).create({ ...item, order_id: orderId,