fix(utils,core-flows): subtotal calculation and returns location (#13497)

* fix(utils,core-flows): subtotal calculation and returns location

* changeset

* fix test

* var

* rm extra field from test

* fix original total

* fix partial refunds and pending difference

* fix test

* fix test

* test

* extract to util

* original total and update payment when receive return

* original_subtotal

* default fields

* test

* calculate pending difference

* revert claims test

* pending difference

* creadit line fix

* if
This commit is contained in:
Carlos R. L. Rodrigues
2025-09-18 12:50:40 -03:00
committed by GitHub
parent 4736c58da5
commit 9563ee446f
37 changed files with 746 additions and 204 deletions

View File

@@ -31,24 +31,43 @@ export const addOrderTransactionStep = createStep(
return new StepResponse(null)
}
const existingQuery: any[] = []
for (const trx of trxsData) {
const existing = await service.listOrderTransactions(
{
order_id: trx.order_id,
reference: trx.reference,
reference_id: trx.reference_id,
},
{
select: ["id"],
}
)
existingQuery.push({
order_id: trx.order_id,
reference: trx.reference,
reference_id: trx.reference_id,
})
}
if (existing.length) {
return new StepResponse(null)
const existing = await service.listOrderTransactions(
{
$or: existingQuery,
},
{
select: ["order_id", "reference", "reference_id"],
}
)
const existingSet = new Set<string>(
existing.map(
(trx) => `${trx.order_id}-${trx.reference}-${trx.reference_id}`
)
)
const selectedData: CreateOrderTransactionDTO[] = []
for (const trx of trxsData) {
if (
!existingSet.has(`${trx.order_id}-${trx.reference}-${trx.reference_id}`)
) {
selectedData.push(trx)
}
}
const created = await service.addOrderTransactions(trxsData)
if (!selectedData.length) {
return new StepResponse(null)
}
const created = await service.addOrderTransactions(selectedData)
return new StepResponse(
(Array.isArray(data)

View File

@@ -9,6 +9,7 @@ import {
MedusaError,
OrderStatus,
arrayDifference,
deepFlatMap,
isPresent,
} from "@medusajs/framework/utils"
@@ -21,6 +22,58 @@ export function throwIfOrderIsCancelled({ order }: { order: OrderDTO }) {
}
}
export function throwIfManagedItemsNotStockedAtReturnLocation({
order,
orderReturn,
inputItems,
}: {
order: Pick<OrderDTO, "items">
orderReturn: Pick<ReturnDTO, "location_id">
inputItems: OrderWorkflow.CreateOrderFulfillmentWorkflowInput["items"]
}) {
if (!orderReturn?.location_id) {
return
}
const inputItemIds = new Set(inputItems.map((i) => i.id))
const requestedOrderItems = order.items?.filter((oi: any) =>
inputItemIds.has(oi.id)
)
const invalidManagedItems: string[] = []
for (const orderItem of requestedOrderItems ?? []) {
const variant = (orderItem as any)?.variant
if (!variant?.manage_inventory) {
continue
}
let hasStockAtLocation = false
deepFlatMap(
orderItem,
"variant.inventory_items.inventory.location_levels",
({ location_levels }) => {
if (location_levels?.location_id === orderReturn.location_id) {
hasStockAtLocation = true
}
}
)
if (!hasStockAtLocation) {
invalidManagedItems.push(orderItem.id)
}
}
if (invalidManagedItems.length) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
`Cannot request item return at location ${
orderReturn.location_id
} for managed inventory items: ${invalidManagedItems.join(", ")}`
)
}
}
export function throwIfItemsDoesNotExistsInOrder({
order,
inputItems,

View File

@@ -6,7 +6,12 @@ import {
OrderWorkflow,
ReturnDTO,
} from "@medusajs/framework/types"
import { ChangeActionType, OrderChangeStatus } from "@medusajs/framework/utils"
import {
ChangeActionType,
OrderChangeStatus,
deepFlatMap,
isDefined,
} from "@medusajs/framework/utils"
import {
WorkflowData,
WorkflowResponse,
@@ -23,6 +28,7 @@ import { updateOrderChangesStep } from "../../steps/update-order-changes"
import {
throwIfIsCancelled,
throwIfItemsDoesNotExistsInOrder,
throwIfManagedItemsNotStockedAtReturnLocation,
throwIfOrderChangeIsNotActive,
} from "../../utils/order-validation"
import { createOrderChangeActionsWorkflow } from "../create-order-change-actions"
@@ -105,6 +111,11 @@ export const orderClaimRequestItemReturnValidationStep = createStep(
throwIfIsCancelled(orderReturn, "Return")
throwIfOrderChangeIsNotActive({ orderChange })
throwIfItemsDoesNotExistsInOrder({ order, inputItems: items })
throwIfManagedItemsNotStockedAtReturnLocation({
order,
orderReturn,
inputItems: items,
})
}
)
@@ -154,34 +165,23 @@ export const orderClaimRequestItemReturnWorkflow = createWorkflow(
}).then(() => {
return useRemoteQueryStep({
entry_point: "return",
fields: ["id", "status", "order_id", "canceled_at"],
fields: ["id", "status", "order_id", "location_id", "canceled_at"],
variables: { id: orderClaim.return_id },
list: false,
throw_if_key_not_found: true,
}).config({ name: "return-query" }) as ReturnDTO
})
const createdReturn = when({ orderClaim }, ({ orderClaim }) => {
return !orderClaim.return_id
}).then(() => {
return createReturnsStep([
{
order_id: orderClaim.order_id,
claim_id: orderClaim.id,
},
])
})
const orderReturn: ReturnDTO = transform(
{ createdReturn, existingOrderReturn },
({ createdReturn, existingOrderReturn }) => {
return existingOrderReturn ?? (createdReturn?.[0] as ReturnDTO)
}
)
const order: OrderDTO = useRemoteQueryStep({
entry_point: "orders",
fields: ["id", "status", "items.*"],
fields: [
"id",
"status",
"items.*",
"items.variant.manage_inventory",
"items.variant.inventory_items.inventory_item_id",
"items.variant.inventory_items.inventory.location_levels.location_id",
],
variables: { id: orderClaim.order_id },
list: false,
throw_if_key_not_found: true,
@@ -202,6 +202,51 @@ export const orderClaimRequestItemReturnWorkflow = createWorkflow(
name: "order-change-query",
})
const pickItemLocationId = transform(
{ order, input },
({ order, input }) => {
if (input.location_id) {
return input.location_id
}
// pick the first item location
const item = order?.items?.find(
(item) => item.id === input.items[0].id
) as any
let locationId: string | undefined
deepFlatMap(
item,
"variant.inventory_items.inventory.location_levels",
({ location_levels }) => {
if (!locationId && isDefined(location_levels?.location_id)) {
locationId = location_levels.location_id
}
}
)
return locationId
}
)
const createdReturn = when({ orderClaim }, ({ orderClaim }) => {
return !orderClaim.return_id
}).then(() => {
return createReturnsStep([
{
order_id: orderClaim.order_id,
claim_id: orderClaim.id,
location_id: pickItemLocationId,
},
])
})
const orderReturn: ReturnDTO = transform(
{ createdReturn, existingOrderReturn },
({ createdReturn, existingOrderReturn }) => {
return existingOrderReturn ?? (createdReturn?.[0] as ReturnDTO)
}
)
when({ createdReturn }, ({ createdReturn }) => {
return !!createdReturn?.length
}).then(() => {

View File

@@ -13,8 +13,8 @@ import {
} from "@medusajs/framework/workflows-sdk"
import { useRemoteQueryStep } from "../../common"
import { updatePaymentCollectionStep } from "../../payment-collection"
import { createOrderPaymentCollectionWorkflow } from "./create-order-payment-collection"
import { cancelPaymentCollectionWorkflow } from "../../payment-collection/workflows/cancel-payment-collection"
import { createOrderPaymentCollectionWorkflow } from "./create-order-payment-collection"
/**
* The details of the order payment collection to create or update.
@@ -65,7 +65,7 @@ export const createOrUpdateOrderPaymentCollectionWorkflow = createWorkflow(
) => {
const order = useRemoteQueryStep({
entry_point: "order",
fields: ["id", "summary", "currency_code", "region_id"],
fields: ["id", "summary", "total", "currency_code", "region_id"],
variables: { id: input.order_id },
throw_if_key_not_found: true,
list: false,

View File

@@ -90,7 +90,7 @@ export const createOrderCreditLinesWorkflow = createWorkflow(
) => {
const orderQuery = useQueryGraphStep({
entity: "orders",
fields: ["id", "status", "summary"],
fields: ["id", "status", "summary", "total"],
filters: { id: input.id },
options: { throwIfKeyNotFound: true },
}).config({ name: "get-order" })

View File

@@ -5,8 +5,8 @@ import {
createWorkflow,
transform,
} from "@medusajs/framework/workflows-sdk"
import { createRemoteLinkStep, useRemoteQueryStep } from "../../common"
import { createPaymentCollectionsStep } from "../../cart"
import { createRemoteLinkStep, useRemoteQueryStep } from "../../common"
/**
* The details of the payment collection to create.
@@ -27,10 +27,10 @@ export const createOrderPaymentCollectionWorkflowId =
/**
* This workflow creates a payment collection for an order. It's used by the
* [Create Payment Collection Admin API Route](https://docs.medusajs.com/api/admin#payment-collections_postpaymentcollections).
*
*
* You can use this workflow within your customizations or your own custom workflows, allowing you to wrap custom logic around
* creating a payment collection for an order.
*
*
* @example
* const { result } = await createOrderPaymentCollectionWorkflow(container)
* .run({
@@ -39,19 +39,17 @@ export const createOrderPaymentCollectionWorkflowId =
* amount: 10,
* }
* })
*
*
* @summary
*
*
* Create a payment collection for an order.
*/
export const createOrderPaymentCollectionWorkflow = createWorkflow(
createOrderPaymentCollectionWorkflowId,
(
input: WorkflowData<CreateOrderPaymentCollectionWorkflowInput>
) => {
(input: WorkflowData<CreateOrderPaymentCollectionWorkflowInput>) => {
const order = useRemoteQueryStep({
entry_point: "order",
fields: ["id", "summary", "currency_code", "region_id"],
fields: ["id", "summary", "total", "currency_code", "region_id"],
variables: { id: input.order_id },
throw_if_key_not_found: true,
list: false,

View File

@@ -6,14 +6,19 @@ import {
OrderWorkflow,
ReturnDTO,
} from "@medusajs/framework/types"
import { ChangeActionType, OrderChangeStatus } from "@medusajs/framework/utils"
import {
WorkflowData,
WorkflowResponse,
ChangeActionType,
deepFlatMap,
isDefined,
OrderChangeStatus,
} from "@medusajs/framework/utils"
import {
createStep,
createWorkflow,
transform,
when,
WorkflowData,
WorkflowResponse,
} from "@medusajs/framework/workflows-sdk"
import { useRemoteQueryStep } from "../../../common"
import { updateOrderExchangesStep } from "../../steps/exchange/update-order-exchanges"
@@ -23,6 +28,7 @@ import { updateOrderChangesStep } from "../../steps/update-order-changes"
import {
throwIfIsCancelled,
throwIfItemsDoesNotExistsInOrder,
throwIfManagedItemsNotStockedAtReturnLocation,
throwIfOrderChangeIsNotActive,
} from "../../utils/order-validation"
import { createOrderChangeActionsWorkflow } from "../create-order-change-actions"
@@ -106,6 +112,11 @@ export const exchangeRequestItemReturnValidationStep = createStep(
throwIfIsCancelled(orderReturn, "Return")
throwIfOrderChangeIsNotActive({ orderChange })
throwIfItemsDoesNotExistsInOrder({ order, inputItems: items })
throwIfManagedItemsNotStockedAtReturnLocation({
order,
orderReturn,
inputItems: items,
})
}
)
@@ -144,7 +155,7 @@ export const orderExchangeRequestItemReturnWorkflow = createWorkflow(
): WorkflowResponse<OrderPreviewDTO> {
const orderExchange = useRemoteQueryStep({
entry_point: "order_exchange",
fields: ["id", "order_id", "return_id", "canceled_at"],
fields: ["id", "order_id", "return_id", "location_id", "canceled_at"],
variables: { id: input.exchange_id },
list: false,
throw_if_key_not_found: true,
@@ -162,27 +173,16 @@ export const orderExchangeRequestItemReturnWorkflow = createWorkflow(
}).config({ name: "return-query" }) as ReturnDTO
})
const createdReturn = when({ orderExchange }, ({ orderExchange }) => {
return !orderExchange.return_id
}).then(() => {
return createReturnsStep([
{
order_id: orderExchange.order_id,
exchange_id: orderExchange.id,
},
])
})
const orderReturn: ReturnDTO = transform(
{ createdReturn, existingOrderReturn, orderExchange },
({ createdReturn, existingOrderReturn, orderExchange }) => {
return existingOrderReturn ?? (createdReturn?.[0] as ReturnDTO)
}
)
const order: OrderDTO = useRemoteQueryStep({
entry_point: "orders",
fields: ["id", "status", "items.*"],
fields: [
"id",
"status",
"items.*",
"items.variant.manage_inventory",
"items.variant.inventory_items.inventory_item_id",
"items.variant.inventory_items.inventory.location_levels.location_id",
],
variables: { id: orderExchange.order_id },
list: false,
throw_if_key_not_found: true,
@@ -204,6 +204,51 @@ export const orderExchangeRequestItemReturnWorkflow = createWorkflow(
status: [OrderChangeStatus.PENDING, OrderChangeStatus.REQUESTED],
})
const pickItemLocationId = transform(
{ order, input },
({ order, input }) => {
if (input.location_id) {
return input.location_id
}
// pick the first item location
const item = order?.items?.find(
(item) => item.id === input.items[0].id
) as any
let locationId: string | undefined
deepFlatMap(
item,
"variant.inventory_items.inventory.location_levels",
({ location_levels }) => {
if (!locationId && isDefined(location_levels?.location_id)) {
locationId = location_levels.location_id
}
}
)
return locationId
}
)
const createdReturn = when({ orderExchange }, ({ orderExchange }) => {
return !orderExchange.return_id
}).then(() => {
return createReturnsStep([
{
order_id: orderExchange.order_id,
location_id: pickItemLocationId,
exchange_id: orderExchange.id,
},
])
})
const orderReturn: ReturnDTO = transform(
{ createdReturn, existingOrderReturn, orderExchange },
({ createdReturn, existingOrderReturn, orderExchange }) => {
return existingOrderReturn ?? (createdReturn?.[0] as ReturnDTO)
}
)
when({ createdReturn }, ({ createdReturn }) => {
return !!createdReturn?.length
}).then(() => {

View File

@@ -234,6 +234,7 @@ export const markOrderFulfillmentAsDeliveredWorkflow = createWorkflow(
fields: [
"id",
"summary",
"total",
"currency_code",
"region_id",
"fulfillments.id",

View File

@@ -42,7 +42,7 @@ export const createOrderRefundCreditLinesWorkflow = createWorkflow(
) {
const orderQuery = useQueryGraphStep({
entity: "orders",
fields: ["id", "status", "summary", "payment_collections.id"],
fields: ["id", "status", "summary", "total", "payment_collections.id"],
filters: { id: input.order_id },
options: { throwIfKeyNotFound: true },
}).config({ name: "get-order" })

View File

@@ -29,6 +29,7 @@ export const refundCapturedPaymentsWorkflow = createWorkflow(
"id",
"status",
"summary",
"total",
"payment_collections.payments.id",
"payment_collections.payments.amount",
"payment_collections.payments.refunds.id",

View File

@@ -34,6 +34,7 @@ import {
throwIfIsCancelled,
throwIfOrderChangeIsNotActive,
} from "../../utils/order-validation"
import { createOrUpdateOrderPaymentCollectionWorkflow } from "../create-or-update-order-payment-collection"
/**
* The data to validate that a return receival can be confirmed.
@@ -56,14 +57,14 @@ export type ConfirmReceiveReturnValidationStepInput = {
/**
* This step validates that a return receival can be confirmed.
* If the order or return is canceled, or the order change is not active, the step will throw an error.
*
*
* :::note
*
*
* You can retrieve an order, return, and order change details using [Query](https://docs.medusajs.com/learn/fundamentals/module-links/query),
* or [useQueryGraphStep](https://docs.medusajs.com/resources/references/medusa-workflows/steps/useQueryGraphStep).
*
*
* :::
*
*
* @example
* const data = confirmReceiveReturnValidationStep({
* order: {
@@ -182,10 +183,10 @@ export const confirmReturnReceiveWorkflowId = "confirm-return-receive"
/**
* This workflow confirms a return receival request. It's used by the
* [Confirm Return Receival Admin API Route](https://docs.medusajs.com/api/admin#returns_postreturnsidreceiveconfirm).
*
*
* You can use this workflow within your customizations or your own custom workflows, allowing you
* to confirm a return receival in your custom flow.
*
*
* @example
* const { result } = await confirmReturnReceiveWorkflow(container)
* .run({
@@ -193,9 +194,9 @@ export const confirmReturnReceiveWorkflowId = "confirm-return-receive"
* return_id: "return_123",
* }
* })
*
*
* @summary
*
*
* Confirm a return receival request.
*/
export const confirmReturnReceiveWorkflow = createWorkflow(
@@ -363,7 +364,15 @@ export const confirmReturnReceiveWorkflow = createWorkflow(
orderId: order.id,
confirmed_by: input.confirmed_by,
}),
adjustInventoryLevelsStep(inventoryAdjustment),
adjustInventoryLevelsStep(inventoryAdjustment)
)
parallelize(
createOrUpdateOrderPaymentCollectionWorkflow.runAsStep({
input: {
order_id: order.id,
},
}),
emitEventStep({
eventName: OrderWorkflowEvents.RETURN_RECEIVED,
data: {

View File

@@ -1,13 +1,13 @@
import {
AdditionalData,
BigNumberInput,
CreateOrderShippingMethodDTO,
FulfillmentWorkflow,
OrderDTO,
ReturnDTO,
OrderWorkflow,
ReturnDTO,
ShippingOptionDTO,
WithCalculatedPrice,
AdditionalData,
} from "@medusajs/framework/types"
import {
MathBN,
@@ -25,6 +25,7 @@ import {
parallelize,
transform,
} from "@medusajs/framework/workflows-sdk"
import { pricingContextResult } from "../../../cart/utils/schemas"
import {
createRemoteLinkStep,
emitEventStep,
@@ -38,7 +39,6 @@ import {
throwIfOrderIsCancelled,
} from "../../utils/order-validation"
import { validateReturnReasons } from "../../utils/validate-return-reason"
import { pricingContextResult } from "../../../cart/utils/schemas"
function prepareShippingMethodData({
orderId,
@@ -311,11 +311,11 @@ export const createAndCompleteReturnOrderWorkflowId =
* @summary
*
* Create and complete a return for an order.
*
*
* @property hooks.setPricingContext - This hook is executed before the return's shipping method is created. You can consume this hook to return any custom context useful for the prices retrieval of the shipping method's option.
*
*
* For example, assuming you have the following custom pricing rule:
*
*
* ```json
* {
* "attribute": "location_id",
@@ -323,13 +323,13 @@ export const createAndCompleteReturnOrderWorkflowId =
* "value": "sloc_123",
* }
* ```
*
*
* You can consume the `setPricingContext` hook to add the `location_id` context to the prices calculation:
*
*
* ```ts
* import { createAndCompleteReturnOrderWorkflow } from "@medusajs/medusa/core-flows";
* import { StepResponse } from "@medusajs/workflows-sdk";
*
*
* createAndCompleteReturnOrderWorkflow.hooks.setPricingContext((
* { order, additional_data }, { container }
* ) => {
@@ -338,13 +338,13 @@ export const createAndCompleteReturnOrderWorkflowId =
* });
* });
* ```
*
*
* The price of the shipping method's option will now be retrieved using the context you return.
*
*
* :::note
*
*
* Learn more about prices calculation context in the [Prices Calculation](https://docs.medusajs.com/resources/commerce-modules/pricing/price-calculation) documentation.
*
*
* :::
*/
export const createAndCompleteReturnOrderWorkflow = createWorkflow(
@@ -423,6 +423,7 @@ export const createAndCompleteReturnOrderWorkflow = createWorkflow(
const returnCreated = createCompleteReturnStep({
order_id: input.order_id,
location_id: input.location_id,
items: input.items,
shipping_method: shippingMethodData,
created_by: input.created_by,

View File

@@ -146,31 +146,33 @@ export const refundPaymentWorkflow = createWorkflow(
const order = useRemoteQueryStep({
entry_point: "order",
fields: ["id", "summary", "currency_code", "region_id"],
fields: ["id", "summary", "total", "currency_code", "region_id"],
variables: { id: orderPaymentCollection.order.id },
throw_if_key_not_found: true,
list: false,
}).config({ name: "order" })
validateRefundStep({ order, payment, amount: input.amount })
refundPaymentStep(input)
const refundPayment = refundPaymentStep(input)
when({ orderPaymentCollection }, ({ orderPaymentCollection }) => {
return !!orderPaymentCollection?.order?.id
}).then(() => {
const orderTransactionData = transform(
{ input, payment, orderPaymentCollection },
({ input, payment, orderPaymentCollection }) => {
return {
order_id: orderPaymentCollection.order.id,
amount: MathBN.mult(
input.amount ?? payment.raw_amount ?? payment.amount,
-1
),
currency_code: payment.currency_code ?? order.currency_code,
reference_id: payment.id,
reference: "refund",
}
{ input, refundPayment, orderPaymentCollection, order },
({ input, refundPayment, orderPaymentCollection, order }) => {
return refundPayment.refunds?.map((refund) => {
return {
order_id: orderPaymentCollection.order.id,
amount: MathBN.mult(
input.amount ?? refund.raw_amount ?? refund.amount,
-1
),
currency_code: refundPayment.currency_code ?? order.currency_code,
reference_id: refund.id,
reference: "refund",
}
})
}
)