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:
Oli Juhl
2024-07-15 22:04:20 +02:00
committed by GitHub
parent b38c0488be
commit ffd4b195ee
25 changed files with 511 additions and 165 deletions

View File

@@ -0,0 +1,12 @@
import { Migration } from "@mikro-orm/migrations"
export class Migration20240715102100 extends Migration {
async up(): Promise<void> {
const sql = `
ALTER TABLE "return"
ADD COLUMN if NOT exists "location_id" TEXT NULL;
`
this.addSql(sql)
}
}

View File

@@ -2,6 +2,8 @@ import { DAL } from "@medusajs/types"
import {
createPsqlIndexStatementHelper,
generateEntityId,
OrderChangeStatus,
OrderChangeType,
} from "@medusajs/utils"
import {
BeforeCreate,
@@ -10,14 +12,14 @@ import {
Entity,
Enum,
ManyToOne,
OnInit,
OneToMany,
OnInit,
OptionalProps,
PrimaryKey,
Property,
Rel,
} from "@mikro-orm/core"
import { OrderChangeStatus, OrderChangeType } from "@types"
import {} from "@types"
import OrderClaim from "./claim"
import OrderExchange from "./exchange"
import Order from "./order"

View File

@@ -116,6 +116,9 @@ export default class Return {
@Enum({ items: () => ReturnStatus, default: ReturnStatus.REQUESTED })
status: ReturnStatus = ReturnStatus.REQUESTED
@Property({ columnType: "text", nullable: true })
location_id: string | null = null
@Property({ columnType: "boolean", nullable: true })
no_notification: boolean | null = null

View File

@@ -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",

View File

@@ -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, {

View File

@@ -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, {

View File

@@ -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, {

View File

@@ -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) => ({

View File

@@ -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

View File

@@ -1,34 +1,5 @@
import { OrderTypes } from "@medusajs/types"
export enum OrderChangeStatus {
/**
* The order change is confirmed.
*/
CONFIRMED = "confirmed",
/**
* The order change is declined.
*/
DECLINED = "declined",
/**
* The order change is requested.
*/
REQUESTED = "requested",
/**
* The order change is pending.
*/
PENDING = "pending",
/**
* The order change is canceled.
*/
CANCELED = "canceled",
}
export enum OrderChangeType {
RETURN = "return",
EXCHANGE = "exchange",
CLAIM = "claim",
EDIT = "edit",
}
import { OrderChangeType } from "@medusajs/utils"
export interface CreateOrderChangeDTO extends OrderTypes.CreateOrderChangeDTO {
change_type?: OrderChangeType

View File

@@ -47,7 +47,7 @@ export type VirtualOrder = {
exchange_id?: string
}
price: BigNumberInput
amount: BigNumberInput
}[]
total: BigNumberInput

View File

@@ -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)
}

View File

@@ -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,
})
}
},

View File

@@ -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({