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

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

* chore: fix specs
This commit is contained in:
Riqwan Thamir
2024-09-11 21:24:01 +02:00
committed by GitHub
parent e9280fb254
commit 24704f420a
17 changed files with 594 additions and 64 deletions
@@ -8,10 +8,12 @@ import {
} from "@medusajs/types"
import { ChangeActionType, OrderChangeStatus } from "@medusajs/utils"
import {
WorkflowData,
WorkflowResponse,
createStep,
createWorkflow,
transform,
when,
WorkflowData,
WorkflowResponse,
} from "@medusajs/workflows-sdk"
import { useRemoteQueryStep } from "../../../common"
import {
@@ -22,6 +24,7 @@ import {
throwIfIsCancelled,
throwIfOrderChangeIsNotActive,
} from "../../utils/order-validation"
import { removeClaimShippingMethodWorkflow } from "./remove-claim-shipping-method"
/**
* This step validates that new items can be removed from a claim.
@@ -104,6 +107,62 @@ export const removeAddItemClaimActionWorkflow = createWorkflow(
deleteOrderChangeActionsStep({ ids: [input.action_id] })
const updatedOrderChange: OrderChangeDTO = useRemoteQueryStep({
entry_point: "order_change",
fields: [
"actions.action",
"actions.claim_id",
"actions.id",
"actions.return_id",
],
variables: {
filters: {
order_id: orderClaim.order_id,
claim_id: orderClaim.id,
status: [OrderChangeStatus.PENDING, OrderChangeStatus.REQUESTED],
},
},
list: false,
}).config({ name: "updated-order-change-query" })
const actionIdToDelete = transform(
{ updatedOrderChange, orderClaim },
({
updatedOrderChange: { actions = [] },
orderClaim: { id: orderClaimId },
}) => {
const itemActions = actions.filter((c) => c.action === "ITEM_ADD")
if (itemActions.length) {
return null
}
const shippingAction = actions.find(
(c) =>
c.action === "SHIPPING_ADD" &&
c.claim_id === orderClaimId &&
!c.return_id
)
if (!shippingAction) {
return null
}
return shippingAction.id
}
)
when({ actionIdToDelete }, ({ actionIdToDelete }) => {
return !!actionIdToDelete
}).then(() => {
removeClaimShippingMethodWorkflow.runAsStep({
input: {
claim_id: orderClaim.id!,
action_id: actionIdToDelete,
},
})
})
return new WorkflowResponse(previewOrderChangeStep(order.id))
}
)
@@ -12,6 +12,8 @@ import {
WorkflowResponse,
createStep,
createWorkflow,
transform,
when,
} from "@medusajs/workflows-sdk"
import { useRemoteQueryStep } from "../../../common"
import {
@@ -22,6 +24,7 @@ import {
throwIfIsCancelled,
throwIfOrderChangeIsNotActive,
} from "../../utils/order-validation"
import { removeExchangeShippingMethodWorkflow } from "./remove-exchange-shipping-method"
/**
* This step validates that a new item can be removed from an exchange.
@@ -104,6 +107,62 @@ export const removeItemExchangeActionWorkflow = createWorkflow(
deleteOrderChangeActionsStep({ ids: [input.action_id] })
const updatedOrderChange: OrderChangeDTO = useRemoteQueryStep({
entry_point: "order_change",
fields: [
"actions.action",
"actions.id",
"actions.exchange_id",
"actions.return_id",
],
variables: {
filters: {
order_id: orderExchange.order_id,
exchange_id: orderExchange.id,
status: [OrderChangeStatus.PENDING, OrderChangeStatus.REQUESTED],
},
},
list: false,
}).config({ name: "updated-order-change-query" })
const actionIdToDelete = transform(
{ updatedOrderChange, orderExchange },
({
updatedOrderChange: { actions = [] },
orderExchange: { id: orderExchangeId },
}) => {
const itemActions = actions.filter((c) => c.action === "ITEM_ADD")
if (itemActions.length) {
return null
}
const shippingAction = actions.find(
(c) =>
c.action === "SHIPPING_ADD" &&
c.exchange_id === orderExchangeId &&
!c.return_id
)
if (!shippingAction) {
return null
}
return shippingAction.id
}
)
when({ actionIdToDelete }, ({ actionIdToDelete }) => {
return !!actionIdToDelete
}).then(() => {
removeExchangeShippingMethodWorkflow.runAsStep({
input: {
exchange_id: orderExchange.id!,
action_id: actionIdToDelete,
},
})
})
return new WorkflowResponse(previewOrderChangeStep(order.id))
}
)
@@ -12,6 +12,8 @@ import {
WorkflowResponse,
createStep,
createWorkflow,
transform,
when,
} from "@medusajs/workflows-sdk"
import { useRemoteQueryStep } from "../../../common"
import {
@@ -22,6 +24,8 @@ import {
throwIfIsCancelled,
throwIfOrderChangeIsNotActive,
} from "../../utils/order-validation"
import { removeReturnShippingMethodWorkflow } from "./remove-return-shipping-method"
import { updateReturnWorkflow } from "./update-return"
/**
* This step validates that a return item can be removed.
@@ -116,6 +120,61 @@ export const removeItemReturnActionWorkflow = createWorkflow(
deleteOrderChangeActionsStep({ ids: [input.action_id] })
const updatedOrderChange: OrderChangeDTO = useRemoteQueryStep({
entry_point: "order_change",
fields: ["actions.action", "actions.return_id", "actions.id"],
variables: {
filters: {
order_id: orderReturn.order_id,
return_id: orderReturn.id,
status: [OrderChangeStatus.PENDING, OrderChangeStatus.REQUESTED],
},
},
list: false,
}).config({ name: "updated-order-change-query" })
const actionIdToDelete = transform(
{ updatedOrderChange, orderReturn },
({
updatedOrderChange: { actions = [] },
orderReturn: { id: returnId },
}) => {
const itemActions = actions.filter((c) => c.action === "RETURN_ITEM")
if (itemActions.length) {
return null
}
const shippingAction = actions.find(
(c) => c.action === "SHIPPING_ADD" && c.return_id === returnId
)
if (!shippingAction) {
return null
}
return shippingAction.id
}
)
when({ actionIdToDelete }, ({ actionIdToDelete }) => {
return !!actionIdToDelete
}).then(() => {
removeReturnShippingMethodWorkflow.runAsStep({
input: {
return_id: orderReturn.id!,
action_id: actionIdToDelete,
},
})
updateReturnWorkflow.runAsStep({
input: {
return_id: orderReturn.id,
location_id: null,
},
})
})
return new WorkflowResponse(previewOrderChangeStep(order.id))
}
)