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 13 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
309 changes: 309 additions & 0 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 @@ -344,6 +392,267 @@ 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 orderModule.listOrderChanges(
{ order_id: order.id },
{ relations: ["actions"] }
)

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 orderModule.listOrderChanges({
order_id: order.id,
})

expect(orderChanges.length).toEqual(0)
fPolic marked this conversation as resolved.
Show resolved Hide resolved
})

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 orderModule.listOrderChanges(
{ order_id: order.id },
{ relations: ["actions"] }
)

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 orderModule.listOrderChanges(
{ order_id: order.id },
{ relations: ["actions"] }
)

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 orderModule.listOrderChanges(
{ order_id: order.id },
{ relations: ["actions"] }
)

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 orderModule.listOrderChanges({
order_id: order.id,
})

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(Date),
})
)
})

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 orderModule.listOrderChanges(
{ order_id: order.id },
{ relations: ["actions"] }
)

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 orderModule.listOrderChanges({
order_id: order.id,
})

expect(orderChanges.length).toEqual(1)
expect(orderChanges[0]).toEqual(
expect.objectContaining({
change_type: "transfer",
status: "requested",
declined_at: null,
})
)
})
})
},
})
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import {
createWorkflow,
} from "@medusajs/framework/workflows-sdk"
import { OrderPreviewDTO } from "@medusajs/types"

import { useRemoteQueryStep } from "../../../common"
import { throwIfOrderIsCancelled } from "../../utils/order-validation"
import { previewOrderChangeStep } from "../../steps"
import {
ChangeActionType,
MedusaError,
OrderChangeStatus,
} from "@medusajs/utils"

import { useRemoteQueryStep } from "../../../common"
import { throwIfOrderIsCancelled } from "../../utils/order-validation"
import { previewOrderChangeStep } from "../../steps"
import { confirmOrderChanges } from "../../steps/confirm-order-changes"

/**
Expand Down
Loading
Loading