Skip to content

Commit

Permalink
feat(core-flows,dashboard,types,medusa): delete shipping methods when…
Browse files Browse the repository at this point in the history
… all inbound/outbound items are deleted (medusajs#9106)

* feat(core-flows,dashboard,types,medusa): delete shipping methods when all inbound/outbound items are deleted

* chore: fix specs
  • Loading branch information
riqwan authored Sep 11, 2024
1 parent e9280fb commit 24704f4
Show file tree
Hide file tree
Showing 17 changed files with 594 additions and 64 deletions.
111 changes: 101 additions & 10 deletions integration-tests/http/__tests__/claims/claims.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,8 @@ medusaIntegrationTestRunner({
})

describe("with only outbound items", () => {
let orderResult

beforeEach(async () => {
await api.post(
`/admin/orders/${order.id}/fulfillments`,
Expand Down Expand Up @@ -906,7 +908,7 @@ medusaIntegrationTestRunner({
adminHeaders
)

await api.post(
const { data } = await api.post(
`/admin/claims/${claimId}/outbound/items`,
{
items: [
Expand Down Expand Up @@ -965,7 +967,7 @@ medusaIntegrationTestRunner({
adminHeaders
)

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

Expand Down Expand Up @@ -1040,9 +1042,55 @@ medusaIntegrationTestRunner({

expect(orderResult.fulfillment_status).toEqual("shipped")
})

it("should remove outbound shipping method when outbound items are completely removed", async () => {
orderResult = (
await api.get(`/admin/orders/${order.id}/preview`, adminHeaders)
).data.order

const claimItems = orderResult.items.filter(
(item) =>
!!item.actions?.find((action) => action.action === "ITEM_ADD")
)

const claimShippingMethods = orderResult.shipping_methods.filter(
(item) =>
!!item.actions?.find((action) => action.action === "SHIPPING_ADD")
)

expect(claimItems).toHaveLength(1)
expect(claimShippingMethods).toHaveLength(1)

await api.delete(
`/admin/claims/${claimId}/outbound/items/${claimItems[0].actions[0].id}`,
adminHeaders
)

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

const updatedClaimItems = orderResult.items.filter(
(item) =>
!!item.actions?.find((action) => action.action === "ITEM_ADD")
)

const updatedClaimShippingMethods =
orderResult.shipping_methods.filter(
(item) =>
!!item.actions?.find(
(action) => action.action === "SHIPPING_ADD"
)
)

expect(updatedClaimItems).toHaveLength(0)
expect(updatedClaimShippingMethods).toHaveLength(0)
})
})

describe("with only inbound items", () => {
let orderResult

beforeEach(async () => {
await api.post(
`/admin/orders/${order.id}/fulfillments`,
Expand Down Expand Up @@ -1072,7 +1120,7 @@ medusaIntegrationTestRunner({
claimId = baseClaim.id
item = order.items[0]

let result = await api.post(
await api.post(
`/admin/claims/${claimId}/inbound/items`,
{
items: [
Expand All @@ -1091,7 +1139,9 @@ medusaIntegrationTestRunner({
{ shipping_option_id: returnShippingOption.id },
adminHeaders
)
})

it("test inbound only", async () => {
// Claim Items
await api.post(
`/admin/claims/${claimId}/claim-items`,
Expand All @@ -1109,20 +1159,18 @@ medusaIntegrationTestRunner({

await api.post(`/admin/claims/${claimId}/request`, {}, adminHeaders)

result = (
const claims = (
await api.get(
`/admin/claims?fields=*claim_items,*additional_items`,
adminHeaders
)
).data.claims

expect(result).toHaveLength(1)
expect(result[0].additional_items).toHaveLength(0)
expect(result[0].claim_items).toHaveLength(1)
expect(result[0].canceled_at).toBeNull()
})
expect(claims).toHaveLength(1)
expect(claims[0].additional_items).toHaveLength(0)
expect(claims[0].claim_items).toHaveLength(1)
expect(claims[0].canceled_at).toBeNull()

it("test inbound only", async () => {
const orderCheck = (
await api.get(`/admin/orders/${order.id}`, adminHeaders)
).data.order
Expand All @@ -1137,6 +1185,49 @@ medusaIntegrationTestRunner({

expect(true).toBe(true)
})

it("should remove inbound shipping method when inbound items are completely removed", async () => {
orderResult = (
await api.get(`/admin/orders/${order.id}/preview`, adminHeaders)
).data.order

const returnItems = orderResult.items.filter(
(item) =>
!!item.actions?.find((action) => action.action === "RETURN_ITEM")
)
const returnShippingMethods = orderResult.shipping_methods.filter(
(item) =>
!!item.actions?.find((action) => action.action === "SHIPPING_ADD")
)

expect(returnItems).toHaveLength(1)
expect(returnShippingMethods).toHaveLength(1)

await api.delete(
`/admin/claims/${claimId}/inbound/items/${returnItems[0].actions[0].id}`,
adminHeaders
)

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

const updatedReturnItems = orderResult.items.filter(
(item) =>
!!item.actions?.find((action) => action.action === "RETURN_ITEM")
)

const updatedReturnShippingMethods =
orderResult.shipping_methods.filter(
(item) =>
!!item.actions?.find(
(action) => action.action === "SHIPPING_ADD"
)
)

expect(updatedReturnItems).toHaveLength(0)
expect(updatedReturnShippingMethods).toHaveLength(0)
})
})
})

Expand Down
198 changes: 198 additions & 0 deletions integration-tests/http/__tests__/exchanges/exchanges.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ medusaIntegrationTestRunner({
testSuite: ({ dbConnection, getContainer, api }) => {
let order, order2
let returnShippingOption
let outboundShippingOption
let shippingProfile
let fulfillmentSet
let returnReason
Expand Down Expand Up @@ -345,6 +346,44 @@ medusaIntegrationTestRunner({
],
}

const outboundShippingOptionPayload = {
name: "Oubound shipping",
service_zone_id: fulfillmentSet.service_zones[0].id,
shipping_profile_id: shippingProfile.id,
provider_id: shippingProviderId,
price_type: "flat",
type: {
label: "Test type",
description: "Test description",
code: "test-code",
},
prices: [
{
currency_code: "usd",
amount: 20,
},
],
rules: [
{
operator: RuleOperator.EQ,
attribute: "is_return",
value: "false",
},
{
operator: RuleOperator.EQ,
attribute: "enabled_in_store",
value: "true",
},
],
}
outboundShippingOption = (
await api.post(
"/admin/shipping-options",
outboundShippingOptionPayload,
adminHeaders
)
).data.shipping_option

returnShippingOption = (
await api.post(
"/admin/shipping-options",
Expand Down Expand Up @@ -541,6 +580,165 @@ medusaIntegrationTestRunner({
).data.exchanges
expect(result[0].canceled_at).toBeDefined()
})

describe("with inbound and outbound items", () => {
let exchange
let orderPreview

beforeEach(async () => {
exchange = (
await api.post(
"/admin/exchanges",
{
order_id: order.id,
description: "Test",
},
adminHeaders
)
).data.exchange

const item = order.items[0]

await api.post(
`/admin/exchanges/${exchange.id}/inbound/items`,
{
items: [
{
id: item.id,
reason_id: returnReason.id,
quantity: 2,
},
],
},
adminHeaders
)

await api.post(
`/admin/exchanges/${exchange.id}/inbound/shipping-method`,
{ shipping_option_id: returnShippingOption.id },
adminHeaders
)

await api.post(
`/admin/exchanges/${exchange.id}/outbound/items`,
{
items: [
{
variant_id: productExtra.variants[0].id,
quantity: 2,
},
],
},
adminHeaders
)

await api.post(
`/admin/exchanges/${exchange.id}/outbound/shipping-method`,
{ shipping_option_id: outboundShippingOption.id },
adminHeaders
)

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

it("should remove outbound shipping method when outbound items are completely removed", async () => {
orderPreview = (
await api.get(`/admin/orders/${order.id}/preview`, adminHeaders)
).data.order

const exchangeItems = orderPreview.items.filter(
(item) =>
!!item.actions?.find((action) => action.action === "ITEM_ADD")
)

const exchangeShippingMethods = orderPreview.shipping_methods.filter(
(item) =>
!!item.actions?.find(
(action) =>
action.action === "SHIPPING_ADD" && !action.return_id
)
)

expect(exchangeItems).toHaveLength(1)
expect(exchangeShippingMethods).toHaveLength(1)

await api.delete(
`/admin/exchanges/${exchange.id}/outbound/items/${exchangeItems[0].actions[0].id}`,
adminHeaders
)

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

const updatedExchangeItems = orderPreview.items.filter(
(item) =>
!!item.actions?.find((action) => action.action === "ITEM_ADD")
)

const updatedClaimShippingMethods =
orderPreview.shipping_methods.filter(
(item) =>
!!item.actions?.find(
(action) =>
action.action === "SHIPPING_ADD" && !action.return_id
)
)

expect(updatedExchangeItems).toHaveLength(0)
expect(updatedClaimShippingMethods).toHaveLength(0)
})

it("should remove inbound shipping method when inbound items are completely removed", async () => {
orderPreview = (
await api.get(`/admin/orders/${order.id}/preview`, adminHeaders)
).data.order

const exchangeItems = orderPreview.items.filter(
(item) =>
!!item.actions?.find((action) => action.action === "RETURN_ITEM")
)

const exchangeShippingMethods = orderPreview.shipping_methods.filter(
(item) =>
!!item.actions?.find(
(action) =>
action.action === "SHIPPING_ADD" && !!action.return_id
)
)

expect(exchangeItems).toHaveLength(1)
expect(exchangeShippingMethods).toHaveLength(1)

await api.delete(
`/admin/exchanges/${exchange.id}/inbound/items/${exchangeItems[0].actions[0].id}`,
adminHeaders
)

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

const updatedExchangeItems = orderPreview.items.filter(
(item) =>
!!item.actions?.find((action) => action.action === "RETURN_ITEM")
)

const updatedClaimShippingMethods =
orderPreview.shipping_methods.filter(
(item) =>
!!item.actions?.find(
(action) =>
action.action === "SHIPPING_ADD" && !!action.return_id
)
)

expect(updatedExchangeItems).toHaveLength(0)
expect(updatedClaimShippingMethods).toHaveLength(0)
})
})
})
},
})
Loading

0 comments on commit 24704f4

Please sign in to comment.