feat(medusa): Claim customer orders (#2710)

This commit is contained in:
Philip Korsholm
2022-12-08 17:48:49 +01:00
committed by GitHub
parent 86f9455d00
commit a6243618fe
36 changed files with 872 additions and 107 deletions
@@ -1,4 +1,3 @@
const jwt = require("jsonwebtoken")
const path = require("path")
const setupServer = require("../../../helpers/setup-server")
@@ -27,7 +27,7 @@ describe("[MEDUSA_FF_ORDER_EDITING] /admin/payment-collections", () => {
const cwd = path.resolve(path.join(__dirname, "..", ".."))
const [process, connection] = await startServerWithEnvironment({
cwd,
env: { MEDUSA_FF_ORDER_EDITING: true }
env: { MEDUSA_FF_ORDER_EDITING: true },
})
dbConnection = connection
medusaProcess = process
@@ -1799,6 +1799,56 @@ describe("/store/carts", () => {
expect(res.data.cart.payment_authorized_at).not.toBe(null)
expect(res.data.cart.completed_at).not.toBe(null)
})
it("completes cart with a non-customer and for a customer with the same email created later the order doesn't show up", async () => {
const api = useApi()
const customerEmail = "test-email-for-non-existent-customer@test.com"
const product = await simpleProductFactory(dbConnection)
const region = await simpleRegionFactory(dbConnection, { tax_rate: 10 })
const cart = await simpleCartFactory(dbConnection, {
customer: {
email: customerEmail,
has_account: false,
},
region: region.id,
line_items: [
{
variant_id: product.variants[0].id,
quantity: 1,
unit_price: 1000,
},
],
})
await api.post(`/store/carts/${cart.id}/payment-sessions`)
const completeRes = await api.post(`/store/carts/${cart.id}/complete`)
expect(completeRes.status).toEqual(200)
const customerResponse = await api.post("/store/customers", {
first_name: "John",
last_name: "Doe",
email: customerEmail,
password: "test",
})
const [authCookie] = customerResponse.headers["set-cookie"][0].split(";")
const customerOrdersResponse = await api
.get("/store/customers/me/orders?status[]=completed", {
headers: {
Cookie: authCookie,
},
})
.catch((err) => {
return err.response
})
expect(customerOrdersResponse.status).toEqual(200)
expect(customerOrdersResponse.data.orders.length).toEqual(0)
})
})
describe("POST /store/carts/:id/shipping-methods", () => {
@@ -1,9 +1,11 @@
const jwt = require("jsonwebtoken")
const path = require("path")
const { Address, Customer, Order, Region } = require("@medusajs/medusa")
const setupServer = require("../../../helpers/setup-server")
const { useApi } = require("../../../helpers/use-api")
const { initDb, useDb } = require("../../../helpers/use-db")
const { simpleOrderFactory } = require("../../factories")
jest.setTimeout(30000)
@@ -28,6 +30,78 @@ describe("/store/customers", () => {
medusaProcess.kill()
})
describe("POST /store/customers/confirm-claim", () => {
let orderId
beforeEach(async () => {
const manager = dbConnection.manager
await manager.insert(Customer, {
id: "test_customer",
first_name: "John",
last_name: "Deere",
email: "john@deere.com",
password_hash:
"c2NyeXB0AAEAAAABAAAAAVMdaddoGjwU1TafDLLlBKnOTQga7P2dbrfgf3fB+rCD/cJOMuGzAvRdKutbYkVpuJWTU39P7OpuWNkUVoEETOVLMJafbI8qs8Qx/7jMQXkN", // password matching "test"
has_account: true,
})
await manager.insert(Customer, {
id: "test_customer-1",
first_name: "John",
last_name: "Deere",
email: "john@deere.com",
})
const order = await simpleOrderFactory(dbConnection, {
customer: {
id: "test_customer-1",
},
})
orderId = order.id
})
afterEach(async () => {
await doAfterEach()
})
it("Successfully confirms a claim ", async () => {
const api = useApi()
const token = jwt.sign(
{
claimingCustomerId: "test_customer",
orders: [orderId],
},
"test"
)
const authResponse = await api.post("/store/auth", {
email: "john@deere.com",
password: "test",
})
const [authCookie] = authResponse.headers["set-cookie"][0].split(";")
const authHeader = {
headers: {
Cookie: authCookie,
},
}
const ordersRes1 = await api.get(`/store/customers/me/orders`, authHeader)
expect(ordersRes1.data.orders.length).toEqual(0)
const response = await api.post("/store/orders/customer/confirm", {
token,
})
expect(response.status).toBe(200)
const ordersRes2 = await api.get(`/store/customers/me/orders`, authHeader)
expect(ordersRes2.data.orders.length).toEqual(1)
})
})
describe("POST /store/customers", () => {
beforeEach(async () => {
const manager = dbConnection.manager
@@ -1,9 +1,9 @@
import { Customer } from "@medusajs/medusa"
import faker from "faker"
import { Customer } from "@medusajs/medusa"
import { Connection } from "typeorm"
import {
CustomerGroupFactoryData,
simpleCustomerGroupFactory
simpleCustomerGroupFactory,
} from "./simple-customer-group-factory"
export type CustomerFactoryData = {
@@ -28,7 +28,7 @@ export const simpleCustomerFactory = async (
const customerId = data.id || `simple-customer-${Math.random() * 1000}`
const c = manager.create(Customer, {
id: customerId,
email: data.email,
email: data.email ?? faker.internet.email(),
password_hash:
data.password_hash ??
"c2NyeXB0AAEAAAABAAAAAVMdaddoGjwU1TafDLLlBKnOTQga7P2dbrfgf3fB+rCD/cJOMuGzAvRdKutbYkVpuJWTU39P7OpuWNkUVoEETOVLMJafbI8qs8Qx/7jMQXkN", // password matching "test"
@@ -27,6 +27,10 @@ import {
SalesChannelFactoryData,
simpleSalesChannelFactory,
} from "./simple-sales-channel-factory"
import {
CustomerFactoryData,
simpleCustomerFactory,
} from "./simple-customer-factory"
export type OrderFactoryData = {
id?: string
@@ -34,6 +38,7 @@ export type OrderFactoryData = {
fulfillment_status?: FulfillmentStatus
region?: RegionFactoryData | string
email?: string | null
customer?: CustomerFactoryData | null
currency_code?: string
tax_rate?: number | null
line_items?: LineItemFactoryData[]
@@ -70,11 +75,10 @@ export const simpleOrderFactory = async (
}
const address = await simpleAddressFactory(connection, data.shipping_address)
const customerToSave = manager.create(Customer, {
email:
typeof data.email !== "undefined" ? data.email : faker.internet.email(),
const customer = await simpleCustomerFactory(connection, {
...data.customer,
email: data.email ?? undefined,
})
const customer = await manager.save(customerToSave)
let discounts = []
if (typeof data.discounts !== "undefined") {