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:
1 parent
3992fc15a0
commit
0076332e0e
4 files changed
+85
-16
No files matched your search
@@ -76,6 +76,46 @@ describe("/admin/draft-orders", () => {
|
|||||||
expect(response.status).toEqual(200);
|
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 () => {
|
it("fails to create a draft order with option requirement", async () => {
|
||||||
const api = useApi();
|
const api = useApi();
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ const { useApi } = require("../../../helpers/use-api");
|
|||||||
const { initDb, useDb } = require("../../../helpers/use-db");
|
const { initDb, useDb } = require("../../../helpers/use-db");
|
||||||
|
|
||||||
const draftOrderSeeder = require("../../helpers/draft-order-seeder");
|
const draftOrderSeeder = require("../../helpers/draft-order-seeder");
|
||||||
const { create } = require("domain");
|
|
||||||
|
|
||||||
jest.setTimeout(30000);
|
jest.setTimeout(30000);
|
||||||
|
|
||||||
|
|||||||
@@ -202,11 +202,23 @@ describe("CartService", () => {
|
|||||||
|
|
||||||
const addressRepository = MockRepository({ create: c => c })
|
const addressRepository = MockRepository({ create: c => c })
|
||||||
const cartRepository = MockRepository()
|
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({
|
const cartService = new CartService({
|
||||||
manager: MockManager,
|
manager: MockManager,
|
||||||
addressRepository,
|
addressRepository,
|
||||||
totalsService,
|
totalsService,
|
||||||
cartRepository,
|
cartRepository,
|
||||||
|
customerService,
|
||||||
regionService,
|
regionService,
|
||||||
eventBusService,
|
eventBusService,
|
||||||
})
|
})
|
||||||
@@ -218,6 +230,7 @@ describe("CartService", () => {
|
|||||||
it("successfully creates a cart", async () => {
|
it("successfully creates a cart", async () => {
|
||||||
await cartService.create({
|
await cartService.create({
|
||||||
region_id: IdMap.getId("testRegion"),
|
region_id: IdMap.getId("testRegion"),
|
||||||
|
email: "email@test.com",
|
||||||
})
|
})
|
||||||
|
|
||||||
expect(eventBusService.emit).toHaveBeenCalledTimes(1)
|
expect(eventBusService.emit).toHaveBeenCalledTimes(1)
|
||||||
@@ -237,6 +250,9 @@ describe("CartService", () => {
|
|||||||
shipping_address: {
|
shipping_address: {
|
||||||
country_code: "us",
|
country_code: "us",
|
||||||
},
|
},
|
||||||
|
customer_id: IdMap.getId("customer"),
|
||||||
|
email: "email@test.com",
|
||||||
|
customer: expect.any(Object),
|
||||||
})
|
})
|
||||||
|
|
||||||
expect(cartRepository.save).toHaveBeenCalledTimes(1)
|
expect(cartRepository.save).toHaveBeenCalledTimes(1)
|
||||||
@@ -734,11 +750,17 @@ describe("CartService", () => {
|
|||||||
if (email === "no@mail.com") {
|
if (email === "no@mail.com") {
|
||||||
return Promise.reject()
|
return Promise.reject()
|
||||||
}
|
}
|
||||||
return Promise.resolve({ id: IdMap.getId("existing") })
|
return Promise.resolve({
|
||||||
|
id: IdMap.getId("existing"),
|
||||||
|
email,
|
||||||
|
})
|
||||||
}),
|
}),
|
||||||
create: jest
|
create: jest.fn().mockImplementation(data =>
|
||||||
.fn()
|
Promise.resolve({
|
||||||
.mockReturnValue(Promise.resolve({ id: IdMap.getId("newCus") })),
|
id: IdMap.getId("newCus"),
|
||||||
|
email: data.email,
|
||||||
|
})
|
||||||
|
),
|
||||||
withTransaction: function() {
|
withTransaction: function() {
|
||||||
return this
|
return this
|
||||||
},
|
},
|
||||||
@@ -774,6 +796,7 @@ describe("CartService", () => {
|
|||||||
customer_id: IdMap.getId("existing"),
|
customer_id: IdMap.getId("existing"),
|
||||||
customer: {
|
customer: {
|
||||||
id: IdMap.getId("existing"),
|
id: IdMap.getId("existing"),
|
||||||
|
email: "test@testdom.com",
|
||||||
},
|
},
|
||||||
email: "test@testdom.com",
|
email: "test@testdom.com",
|
||||||
discount_total: 0,
|
discount_total: 0,
|
||||||
@@ -798,7 +821,7 @@ describe("CartService", () => {
|
|||||||
expect(cartRepository.save).toHaveBeenCalledTimes(1)
|
expect(cartRepository.save).toHaveBeenCalledTimes(1)
|
||||||
expect(cartRepository.save).toHaveBeenCalledWith({
|
expect(cartRepository.save).toHaveBeenCalledWith({
|
||||||
customer_id: IdMap.getId("newCus"),
|
customer_id: IdMap.getId("newCus"),
|
||||||
customer: { id: IdMap.getId("newCus") },
|
customer: { id: IdMap.getId("newCus"), email: "no@mail.com" },
|
||||||
email: "no@mail.com",
|
email: "no@mail.com",
|
||||||
discount_total: 0,
|
discount_total: 0,
|
||||||
shipping_total: 0,
|
shipping_total: 0,
|
||||||
|
|||||||
@@ -290,6 +290,13 @@ class CartService extends BaseService {
|
|||||||
|
|
||||||
const regCountries = region.countries.map(({ iso_2 }) => iso_2)
|
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) {
|
if (data.shipping_address_id) {
|
||||||
const addr = await addressRepo.findOne(data.shipping_address_id)
|
const addr = await addressRepo.findOne(data.shipping_address_id)
|
||||||
data.shipping_address = addr
|
data.shipping_address = addr
|
||||||
@@ -611,7 +618,10 @@ class CartService extends BaseService {
|
|||||||
await this.updateCustomerId_(cart, update.customer_id)
|
await this.updateCustomerId_(cart, update.customer_id)
|
||||||
} else {
|
} else {
|
||||||
if ("email" in update) {
|
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
|
* Creates or fetches a user based on an email.
|
||||||
* @param {string} cartId - the id of the cart to add email to
|
* @param {string} email - the email to use
|
||||||
* @param {string} email - the email to add to cart
|
* @return {Promise} the resultign customer object
|
||||||
* @return {Promise} the result of the update operation
|
|
||||||
*/
|
*/
|
||||||
async updateEmail_(cart, email) {
|
async createOrFetchUserFromEmail_(email) {
|
||||||
const schema = Validator.string()
|
const schema = Validator.string()
|
||||||
.email()
|
.email()
|
||||||
.required()
|
.required()
|
||||||
@@ -730,12 +739,10 @@ class CartService extends BaseService {
|
|||||||
if (!customer) {
|
if (!customer) {
|
||||||
customer = await this.customerService_
|
customer = await this.customerService_
|
||||||
.withTransaction(this.transactionManager_)
|
.withTransaction(this.transactionManager_)
|
||||||
.create({ email })
|
.create({ email: value })
|
||||||
}
|
}
|
||||||
|
|
||||||
cart.email = value
|
return customer
|
||||||
cart.customer = customer
|
|
||||||
cart.customer_id = customer.id
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in new issue
Block a user