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:
@@ -54,7 +54,7 @@ describe("Order Exchange - Actions", function () {
|
||||
shipping_methods: [
|
||||
{
|
||||
id: "ship_123",
|
||||
price: 0,
|
||||
amount: 0,
|
||||
},
|
||||
],
|
||||
total: 270,
|
||||
@@ -188,11 +188,11 @@ describe("Order Exchange - Actions", function () {
|
||||
expect(changes.order.shipping_methods).toEqual([
|
||||
{
|
||||
id: "ship_123",
|
||||
price: 0,
|
||||
amount: 0,
|
||||
},
|
||||
{
|
||||
id: "shipping_345",
|
||||
price: 5,
|
||||
amount: 5,
|
||||
actions: [
|
||||
{
|
||||
action: "SHIPPING_ADD",
|
||||
@@ -203,7 +203,7 @@ describe("Order Exchange - Actions", function () {
|
||||
},
|
||||
{
|
||||
id: "return_shipping_345",
|
||||
price: 7.5,
|
||||
amount: 7.5,
|
||||
actions: [
|
||||
{
|
||||
action: "SHIPPING_ADD",
|
||||
|
||||
@@ -6,13 +6,13 @@ import {
|
||||
import {
|
||||
ChangeActionType,
|
||||
ClaimType,
|
||||
OrderChangeType,
|
||||
ReturnStatus,
|
||||
getShippingMethodsTotals,
|
||||
isString,
|
||||
promiseAll,
|
||||
} from "@medusajs/utils"
|
||||
import { ClaimItem, OrderClaim, Return, ReturnItem } from "@models"
|
||||
import { OrderChangeType } from "@types"
|
||||
|
||||
function createClaimAndReturnEntities(em, data, order) {
|
||||
const claimReference = em.create(OrderClaim, {
|
||||
|
||||
@@ -5,13 +5,13 @@ import {
|
||||
} from "@medusajs/types"
|
||||
import {
|
||||
ChangeActionType,
|
||||
OrderChangeType,
|
||||
ReturnStatus,
|
||||
getShippingMethodsTotals,
|
||||
isString,
|
||||
promiseAll,
|
||||
} from "@medusajs/utils"
|
||||
import { ExchangeItem, OrderExchange, Return, ReturnItem } from "@models"
|
||||
import { OrderChangeType } from "@types"
|
||||
|
||||
function createExchangeAndReturnEntities(em, data, order) {
|
||||
const exchangeReference = em.create(OrderExchange, {
|
||||
|
||||
@@ -5,6 +5,7 @@ import {
|
||||
} from "@medusajs/types"
|
||||
import {
|
||||
ChangeActionType,
|
||||
OrderChangeType,
|
||||
ReturnStatus,
|
||||
getShippingMethodsTotals,
|
||||
isDefined,
|
||||
@@ -12,7 +13,6 @@ import {
|
||||
promiseAll,
|
||||
} from "@medusajs/utils"
|
||||
import { Return, ReturnItem } from "@models"
|
||||
import { OrderChangeType } from "@types"
|
||||
|
||||
function createReturnReference(em, data, order) {
|
||||
return em.create(Return, {
|
||||
|
||||
@@ -2,10 +2,10 @@ import { Context, OrderTypes } from "@medusajs/types"
|
||||
import {
|
||||
ChangeActionType,
|
||||
MathBN,
|
||||
OrderChangeType,
|
||||
ReturnStatus,
|
||||
promiseAll,
|
||||
} from "@medusajs/utils"
|
||||
import { OrderChangeType } from "@types"
|
||||
|
||||
function createReturnItems(data) {
|
||||
return data.items.map((item) => ({
|
||||
|
||||
@@ -21,12 +21,14 @@ import {
|
||||
deduplicate,
|
||||
InjectManager,
|
||||
InjectTransactionManager,
|
||||
isDefined,
|
||||
isObject,
|
||||
isString,
|
||||
MathBN,
|
||||
MedusaContext,
|
||||
MedusaError,
|
||||
ModulesSdkUtils,
|
||||
OrderChangeStatus,
|
||||
OrderStatus,
|
||||
promiseAll,
|
||||
transformPropertiesToBigNumber,
|
||||
@@ -59,7 +61,6 @@ import {
|
||||
CreateOrderLineItemTaxLineDTO,
|
||||
CreateOrderShippingMethodDTO,
|
||||
CreateOrderShippingMethodTaxLineDTO,
|
||||
OrderChangeStatus,
|
||||
UpdateOrderItemDTO,
|
||||
UpdateOrderLineItemDTO,
|
||||
UpdateOrderLineItemTaxLineDTO,
|
||||
@@ -1193,7 +1194,7 @@ export default class OrderModuleService<
|
||||
return_id: dt.return_id,
|
||||
claim_id: dt.claim_id,
|
||||
exchange_id: dt.exchange_id,
|
||||
version: mapOrderVersion[dt.order_id],
|
||||
version: dt.version ?? mapOrderVersion[dt.order_id],
|
||||
}
|
||||
})
|
||||
|
||||
@@ -1979,13 +1980,20 @@ export default class OrderModuleService<
|
||||
const calculated = calculatedOrders[order.id]
|
||||
|
||||
const addedItems = {}
|
||||
const addedShippingMethods = {}
|
||||
for (const item of calculated.order.items) {
|
||||
const isExistingItem = item.id === item.detail?.item_id
|
||||
|
||||
if (!isExistingItem) {
|
||||
addedItems[item.id] = item
|
||||
}
|
||||
}
|
||||
|
||||
for (const sm of calculated.order.shipping_methods) {
|
||||
if (!isDefined(sm.shipping_option_id)) {
|
||||
addedShippingMethods[sm.id] = sm
|
||||
}
|
||||
}
|
||||
|
||||
if (Object.keys(addedItems).length > 0) {
|
||||
const addedItemDetails = await this.listLineItems(
|
||||
{ id: Object.keys(addedItems) },
|
||||
@@ -1996,22 +2004,61 @@ export default class OrderModuleService<
|
||||
)
|
||||
|
||||
calculated.order.items.forEach((item, idx) => {
|
||||
if (addedItems[item.id]) {
|
||||
const lineItem = addedItemDetails.find((d) => d.id === item.id) as any
|
||||
if (!addedItems[item.id]) {
|
||||
return
|
||||
}
|
||||
|
||||
const actions = item.actions
|
||||
delete item.actions
|
||||
const lineItem = addedItemDetails.find((d) => d.id === item.id) as any
|
||||
|
||||
const newItem = itemsToUpsert.find((d) => d.item_id === item.id)!
|
||||
calculated.order.items[idx] = {
|
||||
...lineItem,
|
||||
actions,
|
||||
quantity: newItem.quantity,
|
||||
detail: {
|
||||
...newItem,
|
||||
...item,
|
||||
},
|
||||
}
|
||||
const actions = item.actions
|
||||
delete item.actions
|
||||
|
||||
const newItem = itemsToUpsert.find((d) => d.item_id === item.id)!
|
||||
calculated.order.items[idx] = {
|
||||
...lineItem,
|
||||
actions,
|
||||
quantity: newItem.quantity,
|
||||
detail: {
|
||||
...newItem,
|
||||
...item,
|
||||
},
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (Object.keys(addedShippingMethods).length > 0) {
|
||||
const addedShippingDetails = await this.listShippingMethods(
|
||||
{ id: Object.keys(addedShippingMethods) },
|
||||
{
|
||||
relations: ["adjustments", "tax_lines"],
|
||||
},
|
||||
sharedContext
|
||||
)
|
||||
|
||||
calculated.order.shipping_methods.forEach((sm, idx) => {
|
||||
if (!addedShippingMethods[sm.id]) {
|
||||
return
|
||||
}
|
||||
|
||||
const shippingMethod = addedShippingDetails.find(
|
||||
(d) => d.id === sm.id
|
||||
) as any
|
||||
|
||||
const actions = sm.actions
|
||||
delete sm.actions
|
||||
|
||||
const newItem = shippingMethodsToUpsert.find((d) => d.id === sm.id)!
|
||||
|
||||
sm.shipping_method_id = sm.id
|
||||
delete sm.id
|
||||
|
||||
calculated.order.shipping_methods[idx] = {
|
||||
...shippingMethod,
|
||||
actions,
|
||||
detail: {
|
||||
...sm,
|
||||
...newItem,
|
||||
},
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -2579,7 +2626,9 @@ export default class OrderModuleService<
|
||||
shippingMethodsToUpsert,
|
||||
summariesToUpsert,
|
||||
orderToUpdate,
|
||||
} = applyChangesToOrder(orders, actionsMap)
|
||||
} = applyChangesToOrder(orders, actionsMap, {
|
||||
addActionReferenceToObject: true,
|
||||
})
|
||||
|
||||
await promiseAll([
|
||||
orderToUpdate.length
|
||||
|
||||
Reference in New Issue
Block a user