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
@@ -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();
@@ -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);
+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
}
/**