Skip to content

Commit

Permalink
feat(core-flows,medusa): remove cart customer validation + rename wor…
Browse files Browse the repository at this point in the history
…kflow (medusajs#10207)

what:

- removes cart customer takeover validation
- change workflow from update to transfer
  • Loading branch information
riqwan authored Nov 21, 2024
1 parent 5f9029c commit 6486f2b
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 150 deletions.
6 changes: 6 additions & 0 deletions .changeset/odd-dolls-tie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@medusajs/core-flows": patch
"@medusajs/medusa": patch
---

feat(core-flows,medusa): remove cart customer validation + rename workflow
49 changes: 0 additions & 49 deletions integration-tests/http/__tests__/cart/store/cart.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1300,55 +1300,6 @@ medusaIntegrationTestRunner({
)
})

it("should throw error when trying to update a cart that belongs to a customer that has an account", async () => {
const customerUpdate1 = await api.post(
`/store/carts/${cart.id}/customer`,
{},
storeHeadersWithCustomer
)

expect(customerUpdate1.status).toEqual(200)
expect(customerUpdate1.data.cart).toEqual(
expect.objectContaining({
email: customer.email,
customer: expect.objectContaining({
id: customer.id,
email: customer.email,
}),
})
)

const { jwt: jwt2 } = await createAuthenticatedCustomer(
api,
storeHeaders,
{
first_name: "tony2",
last_name: "stark",
email: "[email protected]",
}
)

const storeHeadersWithCustomer2 = {
headers: {
...storeHeaders.headers,
authorization: `Bearer ${jwt2}`,
},
}

const { response } = await api
.post(
`/store/carts/${cart.id}/customer`,
{},
storeHeadersWithCustomer2
)
.catch((e) => e)

expect(response.status).toEqual(400)
expect(response.data.message).toEqual(
"Cannot update cart customer when its assigned to a different customer"
)
})

it("should successfully update cart customer when cart is without customer", async () => {
const updated = await api.post(
`/store/carts/${cart.id}/customer`,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/core-flows/src/cart/workflows/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ export * from "./create-carts"
export * from "./create-payment-collection-for-cart"
export * from "./list-shipping-options-for-cart"
export * from "./refresh-payment-collection"
export * from "./transfer-cart-customer"
export * from "./update-cart"
export * from "./update-cart-customer"
export * from "./update-cart-promotions"
export * from "./update-line-item-in-cart"
export * from "./update-tax-lines"
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import {
createWorkflow,
transform,
when,
WorkflowData,
} from "@medusajs/framework/workflows-sdk"
import { useQueryGraphStep } from "../../common"
import { updateCartsStep } from "../steps"

export const transferCartCustomerWorkflowId = "transfer-cart-customer"
/**
* This workflow transfers cart's customer.
*/
export const transferCartCustomerWorkflow = createWorkflow(
transferCartCustomerWorkflowId,
(input: WorkflowData<{ id: string; customer_id: string }>) => {
const cartQuery = useQueryGraphStep({
entity: "cart",
filters: { id: input.id },
fields: [
"id",
"email",
"customer_id",
"customer.has_account",
"shipping_address.*",
"region.*",
"region.countries.*",
],
options: { throwIfKeyNotFound: true },
}).config({ name: "get-cart" })

const cart = transform({ cartQuery }, ({ cartQuery }) => cartQuery.data[0])

const customerQuery = useQueryGraphStep({
entity: "customer",
filters: { id: input.customer_id },
fields: ["id", "email"],
options: { throwIfKeyNotFound: true },
}).config({ name: "get-customer" })

const customer = transform(
{ customerQuery },
({ customerQuery }) => customerQuery.data[0]
)

// If its the same customer, we don't want the email to be overridden, so we skip the
// update entirely. When the customer is different, we also override the email.
// The customer will have an opportunity to edit email again through update cart endpoint.
const shouldTransfer = transform(
{ cart, customer },
({ cart, customer }) => cart.customer?.id !== customer.id
)

when({ shouldTransfer }, ({ shouldTransfer }) => shouldTransfer).then(
() => {
const cartInput = transform(
{ cart, customer },
({ cart, customer }) => [
{
id: cart.id,
customer_id: customer.id,
email: customer.email,
},
]
)

updateCartsStep(cartInput)
}
)
}
)

This file was deleted.

4 changes: 2 additions & 2 deletions packages/medusa/src/api/store/carts/[id]/customer/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { updateCartCustomerWorkflow } from "@medusajs/core-flows"
import { transferCartCustomerWorkflow } from "@medusajs/core-flows"
import { HttpTypes } from "@medusajs/framework/types"

import {
Expand All @@ -11,7 +11,7 @@ export const POST = async (
req: AuthenticatedMedusaRequest,
res: MedusaResponse<HttpTypes.StoreCartResponse>
) => {
const workflow = updateCartCustomerWorkflow(req.scope)
const workflow = transferCartCustomerWorkflow(req.scope)

await workflow.run({
input: {
Expand Down

0 comments on commit 6486f2b

Please sign in to comment.