feat: Add exchange return shipping (#8108)
* wip * finalize tests * feat: Add exchange return shipping * add shipping to preview * test input * move utils and ignore already inserted shipping method * use custom price --------- Co-authored-by: Carlos R. L. Rodrigues <rodrigolr@gmail.com>
This commit is contained in:
@@ -17,7 +17,7 @@ OrderChangeProcessing.registerActionType(ChangeActionType.SHIPPING_ADD, {
|
||||
return_id: action.return_id,
|
||||
claim_id: action.claim_id,
|
||||
exchange_id: action.exchange_id,
|
||||
price: action.amount as number,
|
||||
amount: action.amount as number,
|
||||
}
|
||||
shipping.push(existing)
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ OrderChangeProcessing.registerActionType(ChangeActionType.SHIPPING_REMOVE, {
|
||||
return_id: action.return_id,
|
||||
claim_id: action.claim_id,
|
||||
exchange_id: action.exchange_id,
|
||||
price: action.amount as number,
|
||||
amount: action.amount as number,
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
import { OrderChangeActionDTO } from "@medusajs/types"
|
||||
import { createRawPropertiesFromBigNumber } from "@medusajs/utils"
|
||||
import {
|
||||
ChangeActionType,
|
||||
createRawPropertiesFromBigNumber,
|
||||
isDefined,
|
||||
} from "@medusajs/utils"
|
||||
import { OrderItem, OrderShippingMethod } from "@models"
|
||||
import { calculateOrderChange } from "./calculate-order-change"
|
||||
|
||||
@@ -35,7 +39,7 @@ export function applyChangesToOrder(
|
||||
|
||||
calculatedOrders[order.id] = calculated
|
||||
|
||||
const version = actionsMap[order.id][0].version ?? 1
|
||||
const version = actionsMap[order.id]?.[0]?.version ?? order.version
|
||||
|
||||
for (const item of calculated.order.items) {
|
||||
const isExistingItem = item.id === item.detail?.item_id
|
||||
@@ -68,17 +72,36 @@ export function applyChangesToOrder(
|
||||
|
||||
if (version > order.version) {
|
||||
for (const shippingMethod of calculated.order.shipping_methods ?? []) {
|
||||
if (!shippingMethod) {
|
||||
const shippingMethod_ = shippingMethod as any
|
||||
const isNewShippingMethod = !isDefined(shippingMethod_?.detail)
|
||||
if (!shippingMethod_) {
|
||||
continue
|
||||
}
|
||||
|
||||
const sm = {
|
||||
...((shippingMethod as any).detail ?? shippingMethod),
|
||||
version,
|
||||
let associatedMethodId
|
||||
let hasShippingMethod = false
|
||||
if (isNewShippingMethod) {
|
||||
associatedMethodId = shippingMethod_.actions?.find((sm) => {
|
||||
return (
|
||||
sm.action === ChangeActionType.SHIPPING_ADD && sm.reference_id
|
||||
)
|
||||
})
|
||||
hasShippingMethod = !!associatedMethodId
|
||||
} else {
|
||||
associatedMethodId = shippingMethod_?.detail?.shipping_method_id
|
||||
}
|
||||
|
||||
const sm = {
|
||||
...(isNewShippingMethod ? shippingMethod_ : shippingMethod_.detail),
|
||||
version,
|
||||
shipping_method_id: associatedMethodId,
|
||||
} as any
|
||||
|
||||
delete sm.id
|
||||
shippingMethodsToUpsert.push(sm)
|
||||
|
||||
if (!hasShippingMethod) {
|
||||
shippingMethodsToUpsert.push(sm)
|
||||
}
|
||||
}
|
||||
|
||||
orderToUpdate.push({
|
||||
|
||||
Reference in New Issue
Block a user