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
This commit is contained in:
Sebastian Mateos Nicolajsen
2021-09-07 11:15:43 +02:00
committed by GitHub
parent 3992fc15a0
commit 0076332e0e
4 changed files with 85 additions and 16 deletions
+28 -5
View File
@@ -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,
+17 -10
View File
@@ -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
}
/**