Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(medusa,core-flows,types,js-sdk): decline / cancel order transfer #10202

Merged
merged 20 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
336 changes: 328 additions & 8 deletions integration-tests/http/__tests__/order/admin/transfer-flow.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,54 @@ medusaIntegrationTestRunner({
expect(finalOrderResult.customer_id).toEqual(customer.id)
})

it("should cancel an order transfer request from admin successfully", async () => {
await api.post(
`/admin/orders/${order.id}/transfer`,
{
customer_id: customer.id,
},
adminHeaders
)

await api.get(
`/admin/orders/${order.id}?fields=+customer_id,+email`,
adminHeaders
)

let orderPreviewResult = (
await api.get(`/admin/orders/${order.id}/preview`, adminHeaders)
).data.order

expect(orderPreviewResult).toEqual(
expect.objectContaining({
customer_id: customer.id,
order_change: expect.objectContaining({
change_type: "transfer",
status: "requested",
requested_by: user.id,
}),
})
)

await api.post(
`/admin/orders/${order.id}/transfer/cancel`,
{},
adminHeaders
)

orderPreviewResult = (
await api.get(`/admin/orders/${order.id}/preview`, adminHeaders)
).data.order

expect(orderPreviewResult.order_change).not.toBeDefined()
fPolic marked this conversation as resolved.
Show resolved Hide resolved

const orderChangesResult = (
await api.get(`/admin/orders/${order.id}/changes`, adminHeaders)
).data.order_changes

expect(orderChangesResult.length).toEqual(0)
})

it("should fail to request order transfer to a guest customer", async () => {
const customer = (
await api.post(
Expand Down Expand Up @@ -238,13 +286,9 @@ medusaIntegrationTestRunner({
let storeHeaders
let signInToken

let orderModule

beforeEach(async () => {
const container = getContainer()

orderModule = await container.resolve(Modules.ORDER)

const publishableKey = await generatePublishableKey(container)
storeHeaders = generateStoreHeaders({ publishableKey })

Expand Down Expand Up @@ -301,10 +345,12 @@ medusaIntegrationTestRunner({
expect(storeOrder.email).toEqual("tony@stark-industries.com")
expect(storeOrder.customer_id).not.toEqual(customer.id)

const orderChanges = await orderModule.listOrderChanges(
{ order_id: order.id },
{ relations: ["actions"] }
)
const orderChanges = (
await api.get(
`/admin/orders/${order.id}/changes?fields=*actions`,
adminHeaders
)
).data.order_changes

expect(orderChanges.length).toEqual(1)
expect(orderChanges[0]).toEqual(
Expand Down Expand Up @@ -344,6 +390,280 @@ medusaIntegrationTestRunner({
// 4. Customer account is now associated with the order (email on the order is still as original, guest email)
expect(finalOrder.customer_id).toEqual(customer.id)
})

it("should cancel a customer transfer request as an admin", async () => {
await api.post(
`/store/orders/${order.id}/transfer/request`,
{},
{
headers: {
authorization: `Bearer ${signInToken}`,
...storeHeaders.headers,
},
}
)

let orderChanges = (
await api.get(
`/admin/orders/${order.id}/changes?fields=*actions`,
adminHeaders
)
).data.order_changes

expect(orderChanges.length).toEqual(1)
expect(orderChanges[0]).toEqual(
expect.objectContaining({
change_type: "transfer",
status: "requested",
requested_by: customer.id,
created_by: customer.id,
confirmed_by: null,
confirmed_at: null,
declined_by: null,
actions: expect.arrayContaining([
expect.objectContaining({
version: 2,
action: "TRANSFER_CUSTOMER",
reference: "customer",
reference_id: customer.id,
details: expect.objectContaining({
token: expect.any(String),
original_email: "tony@stark-industries.com",
}),
}),
]),
})
)

// Admin cancels the transfer request
await api.post(
`/admin/orders/${order.id}/transfer/cancel`,
{},
adminHeaders
)

orderChanges = (
await api.get(`/admin/orders/${order.id}/changes`, adminHeaders)
).data.order_changes

expect(orderChanges.length).toEqual(0)
})

it("customer should be able to cancel their own transfer request", async () => {
await api.post(
`/store/orders/${order.id}/transfer/request`,
{},
{
headers: {
authorization: `Bearer ${signInToken}`,
...storeHeaders.headers,
},
}
)

let orderChanges = (
await api.get(
`/admin/orders/${order.id}/changes?fields=*actions`,
adminHeaders
)
).data.order_changes

expect(orderChanges.length).toEqual(1)
expect(orderChanges[0]).toEqual(
expect.objectContaining({
change_type: "transfer",
status: "requested",
requested_by: customer.id,
created_by: customer.id,
confirmed_by: null,
confirmed_at: null,
declined_by: null,
actions: expect.arrayContaining([
expect.objectContaining({
version: 2,
action: "TRANSFER_CUSTOMER",
reference: "customer",
reference_id: customer.id,
details: expect.objectContaining({
token: expect.any(String),
original_email: "tony@stark-industries.com",
}),
}),
]),
})
)

await api.post(
`/store/orders/${order.id}/transfer/cancel`,
{},
{
headers: {
authorization: `Bearer ${signInToken}`,
...storeHeaders.headers,
},
}
)

orderChanges = (
await api.get(
`/admin/orders/${order.id}/changes?fields=*actions`,
adminHeaders
)
).data.order_changes

expect(orderChanges.length).toEqual(0)
})

it("original customer should be able to decline a transfer request", async () => {
await api.post(
`/store/orders/${order.id}/transfer/request`,
{},
{
headers: {
authorization: `Bearer ${signInToken}`,
...storeHeaders.headers,
},
}
)

let orderChanges = (
await api.get(
`/admin/orders/${order.id}/changes?fields=*actions`,
adminHeaders
)
).data.order_changes

expect(orderChanges.length).toEqual(1)
expect(orderChanges[0]).toEqual(
expect.objectContaining({
change_type: "transfer",
status: "requested",
requested_by: customer.id,
created_by: customer.id,
confirmed_by: null,
confirmed_at: null,
declined_by: null,
actions: expect.arrayContaining([
expect.objectContaining({
version: 2,
action: "TRANSFER_CUSTOMER",
reference: "customer",
reference_id: customer.id,
details: expect.objectContaining({
token: expect.any(String),
original_email: "tony@stark-industries.com",
}),
}),
]),
})
)

await api.post(
`/store/orders/${order.id}/transfer/decline`,
{ token: orderChanges[0].actions[0].details.token },
{
headers: {
...storeHeaders.headers,
},
}
)

orderChanges = (
await api.get(
`/admin/orders/${order.id}/changes?fields=+declined_at`,
adminHeaders
)
).data.order_changes

expect(orderChanges.length).toEqual(1)
expect(orderChanges[0]).toEqual(
expect.objectContaining({
change_type: "transfer",
status: "declined",
requested_by: customer.id,
created_by: customer.id,
declined_at: expect.any(String),
})
)
})

it("shound not decline a transfer request without proper token", async () => {
await api.post(
`/store/orders/${order.id}/transfer/request`,
{},
{
headers: {
authorization: `Bearer ${signInToken}`,
...storeHeaders.headers,
},
}
)

let orderChanges = (
await api.get(
`/admin/orders/${order.id}/changes?fields=*actions`,
adminHeaders
)
).data.order_changes

expect(orderChanges.length).toEqual(1)
expect(orderChanges[0]).toEqual(
expect.objectContaining({
change_type: "transfer",
status: "requested",
requested_by: customer.id,
created_by: customer.id,
confirmed_by: null,
confirmed_at: null,
declined_by: null,
actions: expect.arrayContaining([
expect.objectContaining({
version: 2,
action: "TRANSFER_CUSTOMER",
reference: "customer",
reference_id: customer.id,
details: expect.objectContaining({
token: expect.any(String),
original_email: "tony@stark-industries.com",
}),
}),
]),
})
)

const error = await api
.post(
`/store/orders/${order.id}/transfer/decline`,
{ token: "fake-token" },
riqwan marked this conversation as resolved.
Show resolved Hide resolved
{
headers: {
...storeHeaders.headers,
},
}
)
.catch((e) => e)

expect(error.response.status).toBe(400)
expect(error.response.data).toEqual(
expect.objectContaining({
type: "not_allowed",
message: "Invalid token.",
})
)

orderChanges = (
await api.get(`/admin/orders/${order.id}/changes`, adminHeaders)
).data.order_changes

expect(orderChanges.length).toEqual(1)
expect(orderChanges[0]).toEqual(
expect.objectContaining({
change_type: "transfer",
status: "requested",
declined_at: null,
})
)
})
})
},
})
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ export const ProductOrganizationSection = ({
title={t("fields.collection")}
value={
product.collection ? (
<Badge size="2xsmall" className="w-fit" asChild>
<Badge size="2xsmall" className="max-w-[182px]" asChild>
<Link to={`/collections/${product.collection.id}`}>
{product.collection.title}
<span className="truncate">{product.collection.title}</span>
</Link>
</Badge>
) : undefined
Expand Down
2 changes: 2 additions & 0 deletions packages/core/core-flows/src/order/workflows/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,5 @@ export * from "./update-order-changes"
export * from "./update-tax-lines"
export * from "./transfer/request-order-transfer"
export * from "./transfer/accept-order-transfer"
export * from "./transfer/cancel-order-transfer"
export * from "./transfer/decline-order-transfer"
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ export const cancelBeginOrderEditWorkflowId = "cancel-begin-order-edit"
*/
export const cancelBeginOrderEditWorkflow = createWorkflow(
cancelBeginOrderEditWorkflowId,
function (input: CancelBeginOrderEditWorkflowInput): WorkflowData<void> {
function (
input: WorkflowData<CancelBeginOrderEditWorkflowInput>
): WorkflowData<void> {
const order: OrderDTO = useRemoteQueryStep({
entry_point: "orders",
fields: ["id", "version", "canceled_at"],
Expand Down
Loading
Loading