From 0076332e0e72014c0dec382ec3f9b400e11427ef Mon Sep 17 00:00:00 2001 From: Sebastian Mateos Nicolajsen <80953876+sebastiannicolajsen@users.noreply.github.com> Date: Tue, 7 Sep 2021 11:15:43 +0200 Subject: [PATCH] Fix/create customer in draft order (#285) * draft order now creates customer when creating cart * corrected to update instead of create * corrected tests * added creation of customer to create of cart * removed update function from mock * added test * corrected update to update email properly * corrected tests * removed unused func * updated core properly * removed manual-payment service --- .../api/__tests__/admin/draft-order.js | 40 +++++++++++++++++++ .../api/__tests__/store/draft-order.js | 1 - .../medusa/src/services/__tests__/cart.js | 33 ++++++++++++--- packages/medusa/src/services/cart.js | 27 ++++++++----- 4 files changed, 85 insertions(+), 16 deletions(-) diff --git a/integration-tests/api/__tests__/admin/draft-order.js b/integration-tests/api/__tests__/admin/draft-order.js index 89898b10a2..bbe1174b08 100644 --- a/integration-tests/api/__tests__/admin/draft-order.js +++ b/integration-tests/api/__tests__/admin/draft-order.js @@ -76,6 +76,46 @@ describe("/admin/draft-orders", () => { expect(response.status).toEqual(200); }); + it("creates a draft order cart and creates new user", async () => { + const api = useApi(); + + const payload = { + email: "non-existing@test.dk", + customer_id: "non-existing", + shipping_address: "oli-shipping", + items: [ + { + variant_id: "test-variant", + quantity: 2, + metadata: {}, + }, + ], + region_id: "test-region", + shipping_methods: [ + { + option_id: "test-option", + }, + ], + }; + + const response = await api + .post("/admin/draft-orders", payload, { + headers: { + Authorization: "Bearer test_token", + }, + }) + .catch((err) => { + console.log(err); + }); + + expect(response.status).toEqual(200); + + const draftOrder = response.data.draft_order; + + expect(draftOrder.cart.customer_id).toBeDefined(); + expect(draftOrder.cart.email).toEqual("non-existing@test.dk"); + }); + it("fails to create a draft order with option requirement", async () => { const api = useApi(); diff --git a/integration-tests/api/__tests__/store/draft-order.js b/integration-tests/api/__tests__/store/draft-order.js index 4039b81b84..6821163f8d 100644 --- a/integration-tests/api/__tests__/store/draft-order.js +++ b/integration-tests/api/__tests__/store/draft-order.js @@ -5,7 +5,6 @@ const { useApi } = require("../../../helpers/use-api"); const { initDb, useDb } = require("../../../helpers/use-db"); const draftOrderSeeder = require("../../helpers/draft-order-seeder"); -const { create } = require("domain"); jest.setTimeout(30000); diff --git a/packages/medusa/src/services/__tests__/cart.js b/packages/medusa/src/services/__tests__/cart.js index eeadb861fd..18919d6b29 100644 --- a/packages/medusa/src/services/__tests__/cart.js +++ b/packages/medusa/src/services/__tests__/cart.js @@ -202,11 +202,23 @@ describe("CartService", () => { const addressRepository = MockRepository({ create: c => c }) const cartRepository = MockRepository() + const customerService = { + retrieveByEmail: jest.fn().mockReturnValue( + Promise.resolve({ + id: IdMap.getId("customer"), + email: "email@test.com", + }) + ), + withTransaction: function() { + return this + }, + } const cartService = new CartService({ manager: MockManager, addressRepository, totalsService, cartRepository, + customerService, regionService, eventBusService, }) @@ -218,6 +230,7 @@ describe("CartService", () => { it("successfully creates a cart", async () => { await cartService.create({ region_id: IdMap.getId("testRegion"), + email: "email@test.com", }) expect(eventBusService.emit).toHaveBeenCalledTimes(1) @@ -237,6 +250,9 @@ describe("CartService", () => { shipping_address: { country_code: "us", }, + customer_id: IdMap.getId("customer"), + email: "email@test.com", + customer: expect.any(Object), }) expect(cartRepository.save).toHaveBeenCalledTimes(1) @@ -734,11 +750,17 @@ describe("CartService", () => { if (email === "no@mail.com") { return Promise.reject() } - return Promise.resolve({ id: IdMap.getId("existing") }) + return Promise.resolve({ + id: IdMap.getId("existing"), + email, + }) }), - create: jest - .fn() - .mockReturnValue(Promise.resolve({ id: IdMap.getId("newCus") })), + create: jest.fn().mockImplementation(data => + Promise.resolve({ + id: IdMap.getId("newCus"), + email: data.email, + }) + ), withTransaction: function() { return this }, @@ -774,6 +796,7 @@ describe("CartService", () => { customer_id: IdMap.getId("existing"), customer: { id: IdMap.getId("existing"), + email: "test@testdom.com", }, email: "test@testdom.com", discount_total: 0, @@ -798,7 +821,7 @@ describe("CartService", () => { expect(cartRepository.save).toHaveBeenCalledTimes(1) expect(cartRepository.save).toHaveBeenCalledWith({ customer_id: IdMap.getId("newCus"), - customer: { id: IdMap.getId("newCus") }, + customer: { id: IdMap.getId("newCus"), email: "no@mail.com" }, email: "no@mail.com", discount_total: 0, shipping_total: 0, diff --git a/packages/medusa/src/services/cart.js b/packages/medusa/src/services/cart.js index bc39376708..5052d57cce 100644 --- a/packages/medusa/src/services/cart.js +++ b/packages/medusa/src/services/cart.js @@ -290,6 +290,13 @@ class CartService extends BaseService { const regCountries = region.countries.map(({ iso_2 }) => iso_2) + if (data.email) { + const customer = await this.createOrFetchUserFromEmail_(data.email) + data.customer = customer + data.customer_id = customer.id + data.email = customer.email + } + if (data.shipping_address_id) { const addr = await addressRepo.findOne(data.shipping_address_id) data.shipping_address = addr @@ -611,7 +618,10 @@ class CartService extends BaseService { await this.updateCustomerId_(cart, update.customer_id) } else { if ("email" in update) { - await this.updateEmail_(cart, update.email) + const customer = await this.createOrFetchUserFromEmail_(update.email) + cart.customer = customer + cart.customer_id = customer.id + cart.email = customer.email } } @@ -705,12 +715,11 @@ class CartService extends BaseService { } /** - * Sets the email of a cart - * @param {string} cartId - the id of the cart to add email to - * @param {string} email - the email to add to cart - * @return {Promise} the result of the update operation + * Creates or fetches a user based on an email. + * @param {string} email - the email to use + * @return {Promise} the resultign customer object */ - async updateEmail_(cart, email) { + async createOrFetchUserFromEmail_(email) { const schema = Validator.string() .email() .required() @@ -730,12 +739,10 @@ class CartService extends BaseService { if (!customer) { customer = await this.customerService_ .withTransaction(this.transactionManager_) - .create({ email }) + .create({ email: value }) } - cart.email = value - cart.customer = customer - cart.customer_id = customer.id + return customer } /**