From 9c72acda408fea994da3828163f9e6d436846721 Mon Sep 17 00:00:00 2001 From: Sebastian Rindom Date: Fri, 15 Oct 2021 19:14:44 +0200 Subject: [PATCH 1/2] fix: failing integration tests --- .../store/__snapshots__/product-variants.js.snap | 8 ++++---- .../api/__tests__/store/product-variants.js | 12 ++++++++++++ .../api/routes/store/customers/update-customer.js | 4 ++-- packages/medusa/src/services/customer.js | 15 ++++++++++----- 4 files changed, 28 insertions(+), 11 deletions(-) diff --git a/integration-tests/api/__tests__/store/__snapshots__/product-variants.js.snap b/integration-tests/api/__tests__/store/__snapshots__/product-variants.js.snap index b6850cd492..3aa71856e9 100644 --- a/integration-tests/api/__tests__/store/__snapshots__/product-variants.js.snap +++ b/integration-tests/api/__tests__/store/__snapshots__/product-variants.js.snap @@ -19,12 +19,12 @@ Object { "mid_code": null, "options": Array [ Object { - "created_at": "2021-10-13T08:02:41.668Z", + "created_at": Any, "deleted_at": null, "id": "test-variant-option", "metadata": null, "option_id": "test-option", - "updated_at": "2021-10-13T08:02:41.668Z", + "updated_at": Any, "value": "Default variant", "variant_id": "test-variant", }, @@ -75,12 +75,12 @@ Object { "mid_code": null, "options": Array [ Object { - "created_at": "2021-10-13T08:02:41.493Z", + "created_at": Any, "deleted_at": null, "id": "test-variant-option", "metadata": null, "option_id": "test-option", - "updated_at": "2021-10-13T08:02:41.493Z", + "updated_at": Any, "value": "Default variant", "variant_id": "test-variant", }, diff --git a/integration-tests/api/__tests__/store/product-variants.js b/integration-tests/api/__tests__/store/product-variants.js index e7e7ab1960..c0f9c1d355 100644 --- a/integration-tests/api/__tests__/store/product-variants.js +++ b/integration-tests/api/__tests__/store/product-variants.js @@ -65,6 +65,12 @@ describe("/store/variants", () => { updated_at: expect.any(String), weight: null, width: null, + options: [ + { + created_at: expect.any(String), + updated_at: expect.any(String), + }, + ], prices: [ { created_at: expect.any(String), @@ -113,6 +119,12 @@ describe("/store/variants", () => { updated_at: expect.any(String), weight: null, width: null, + options: [ + { + created_at: expect.any(String), + updated_at: expect.any(String), + }, + ], prices: [ { created_at: expect.any(String), 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 8f862b3551..0de93b539e 100644 --- a/packages/medusa/src/api/routes/store/customers/update-customer.js +++ b/packages/medusa/src/api/routes/store/customers/update-customer.js @@ -66,9 +66,9 @@ export default async (req, res) => { try { const customerService = req.scope.resolve("customerService") - let customer = await customerService.update(id, value) + await customerService.update(id, value) - customer = await customerService.retrieve(customer.id, { + const customer = await customerService.retrieve(id, { relations: defaultRelations, select: defaultFields, }) diff --git a/packages/medusa/src/services/customer.js b/packages/medusa/src/services/customer.js index 8955cb49a8..1af5a256f0 100644 --- a/packages/medusa/src/services/customer.js +++ b/packages/medusa/src/services/customer.js @@ -224,7 +224,6 @@ class CustomerService extends BaseService { const query = this.buildQuery_({ id: validatedId }, config) const customer = await customerRepo.findOne(query) - if (!customer) { throw new MedusaError( MedusaError.Types.NOT_FOUND, @@ -286,7 +285,7 @@ class CustomerService extends BaseService { /** * Hashes a password * @param {string} password - the value to hash - * @return {string} hashed password + * @return {Promise} hashed password */ async hashPassword_(password) { const buf = await Scrypt.kdf(password, { logN: 1, r: 1, p: 1 }) @@ -371,7 +370,14 @@ class CustomerService extends BaseService { const customer = await this.retrieve(customerId) - const { email, password, metadata, ...rest } = update + const { + email, + password, + metadata, + billing_address, + billing_address_id, + ...rest + } = update if (metadata) { customer.metadata = this.setMetadata_(customer, metadata) @@ -381,7 +387,7 @@ class CustomerService extends BaseService { customer.email = this.validateEmail_(email) } - if ("billing_address_id" in update || "billing_address" in update) { + if (billing_address_id || billing_address) { const address = update.billing_address_id || update.billing_address await this.updateBillingAddress_(customer, address, addrRepo) } @@ -421,7 +427,6 @@ class CustomerService extends BaseService { if (addressOrId.id) { customer.billing_address_id = addressOrId.id - customer.billing_address = addressOrId } else { if (customer.billing_address_id) { const addr = await addrRepo.findOne({ From dc7ecc959ae6792351a733fa9f24c95bb6e9e893 Mon Sep 17 00:00:00 2001 From: Sebastian Rindom Date: Fri, 15 Oct 2021 19:24:44 +0200 Subject: [PATCH 2/2] 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 },