feat(medusa,core-flows,types,js-sdk): decline / cancel order transfer (#10202)
**What** - **core-flows**: - cancel transfer - decline transfer - **API**: - admin cancel transfer - store decline transfer - store cancel transfer - **js-sdk**: - add methods for store endpoints --- CLOSES CMRC-726
This commit is contained in:
@@ -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()
|
||||
|
||||
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(
|
||||
@@ -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 })
|
||||
|
||||
@@ -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(
|
||||
@@ -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" },
|
||||
{
|
||||
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,
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user