forked from medusajs/medusa
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core-flows,medusa): remove cart customer validation + rename wor…
…kflow (medusajs#10207) what: - removes cart customer takeover validation - change workflow from update to transfer
- Loading branch information
Showing
6 changed files
with
80 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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`, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
packages/core/core-flows/src/cart/workflows/transfer-cart-customer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
) | ||
} | ||
) |
98 changes: 0 additions & 98 deletions
98
packages/core/core-flows/src/cart/workflows/update-cart-customer.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters