From dc7ecc959ae6792351a733fa9f24c95bb6e9e893 Mon Sep 17 00:00:00 2001 From: Sebastian Rindom Date: Fri, 15 Oct 2021 19:24:44 +0200 Subject: [PATCH] fix: add possiblity to unset billing address --- .../api/__tests__/store/customer.js | 42 +++++++++++++++++++ .../routes/store/customers/update-customer.js | 2 +- packages/medusa/src/services/customer.js | 9 +++- 3 files changed, 50 insertions(+), 3 deletions(-) diff --git a/integration-tests/api/__tests__/store/customer.js b/integration-tests/api/__tests__/store/customer.js index 52189ca8c0..0d59738059 100644 --- a/integration-tests/api/__tests__/store/customer.js +++ b/integration-tests/api/__tests__/store/customer.js @@ -221,5 +221,47 @@ describe("/store/customers", () => { }) ) }) + + it("unsets customer billing address", async () => { + const api = useApi() + + const authResponse = await api.post("/store/auth", { + email: "john@deere.com", + password: "test", + }) + + const customerId = authResponse.data.customer.id + const [authCookie] = authResponse.headers["set-cookie"][0].split(";") + + const check = await api.post( + `/store/customers/me`, + { + billing_address: "addr_test", + }, + { + headers: { + Cookie: authCookie, + }, + } + ) + + expect(check.status).toEqual(200) + expect(check.data.customer.billing_address_id).toEqual("addr_test") + + const response = await api.post( + `/store/customers/me`, + { + billing_address: null, + }, + { + headers: { + Cookie: authCookie, + }, + } + ) + + expect(response.status).toEqual(200) + expect(response.data.customer.billing_address_id).toEqual(null) + }) }) }) diff --git a/packages/medusa/src/api/routes/store/customers/update-customer.js b/packages/medusa/src/api/routes/store/customers/update-customer.js index 0de93b539e..562d54a575 100644 --- a/packages/medusa/src/api/routes/store/customers/update-customer.js +++ b/packages/medusa/src/api/routes/store/customers/update-customer.js @@ -50,7 +50,7 @@ export default async (req, res) => { const id = req.user.customer_id const schema = Validator.object().keys({ - billing_address: Validator.address().optional(), + billing_address: Validator.address().optional().allow(null), first_name: Validator.string().optional(), last_name: Validator.string().optional(), password: Validator.string().optional(), diff --git a/packages/medusa/src/services/customer.js b/packages/medusa/src/services/customer.js index 1af5a256f0..bbe07131d7 100644 --- a/packages/medusa/src/services/customer.js +++ b/packages/medusa/src/services/customer.js @@ -387,8 +387,8 @@ class CustomerService extends BaseService { customer.email = this.validateEmail_(email) } - if (billing_address_id || billing_address) { - const address = update.billing_address_id || update.billing_address + if ("billing_address_id" in update || "billing_address" in update) { + const address = billing_address_id || billing_address await this.updateBillingAddress_(customer, address, addrRepo) } @@ -417,6 +417,11 @@ class CustomerService extends BaseService { * @return {Promise} the result of the update operation */ async updateBillingAddress_(customer, addressOrId, addrRepo) { + if (addressOrId === null) { + customer.billing_address_id = null + return + } + if (typeof addressOrId === `string`) { addressOrId = await addrRepo.findOne({ where: { id: addressOrId },