fix(types,order,medusa): Create credit lines + hooks (#11569)
what: - api/workflows to create credit lines - hooks to enable extending credit lines
This commit is contained in:
@@ -112,6 +112,7 @@ type InjectedDependencies = {
|
||||
returnItemService: ModulesSdkTypes.IMedusaInternalService<any>
|
||||
orderClaimService: ModulesSdkTypes.IMedusaInternalService<any>
|
||||
orderExchangeService: ModulesSdkTypes.IMedusaInternalService<any>
|
||||
orderCreditLineService: ModulesSdkTypes.IMedusaInternalService<any>
|
||||
}
|
||||
|
||||
const generateMethodForModels = {
|
||||
@@ -286,6 +287,9 @@ export default class OrderModuleService
|
||||
protected orderExchangeItemService_: ModulesSdkTypes.IMedusaInternalService<
|
||||
InferEntityType<typeof OrderExchangeItem>
|
||||
>
|
||||
protected orderCreditLineService_: ModulesSdkTypes.IMedusaInternalService<
|
||||
InferEntityType<typeof OrderCreditLine>
|
||||
>
|
||||
|
||||
constructor(
|
||||
{
|
||||
@@ -309,6 +313,7 @@ export default class OrderModuleService
|
||||
returnItemService,
|
||||
orderClaimService,
|
||||
orderExchangeService,
|
||||
orderCreditLineService,
|
||||
}: InjectedDependencies,
|
||||
protected readonly moduleDeclaration: InternalModuleDeclaration
|
||||
) {
|
||||
@@ -336,6 +341,7 @@ export default class OrderModuleService
|
||||
this.returnItemService_ = returnItemService
|
||||
this.orderClaimService_ = orderClaimService
|
||||
this.orderExchangeService_ = orderExchangeService
|
||||
this.orderCreditLineService_ = orderCreditLineService
|
||||
}
|
||||
|
||||
__joinerConfig(): ModuleJoinerConfig {
|
||||
@@ -363,6 +369,9 @@ export default class OrderModuleService
|
||||
"original_shipping_tax_total",
|
||||
"original_shipping_subtotal",
|
||||
"original_shipping_total",
|
||||
"credit_line_total",
|
||||
"credit_line_tax_total",
|
||||
"credit_line_subtotal",
|
||||
]
|
||||
|
||||
const includeTotals = (config?.select ?? []).some((field) =>
|
||||
@@ -381,6 +390,7 @@ export default class OrderModuleService
|
||||
config.select ??= []
|
||||
|
||||
const requiredRelationsForTotals = [
|
||||
"credit_lines",
|
||||
"items",
|
||||
"items.tax_lines",
|
||||
"items.adjustments",
|
||||
@@ -733,6 +743,7 @@ export default class OrderModuleService
|
||||
...ord,
|
||||
shipping_methods,
|
||||
items,
|
||||
credit_lines,
|
||||
}) as any
|
||||
|
||||
const calculated = calculateOrderChange({
|
||||
@@ -750,8 +761,8 @@ export default class OrderModuleService
|
||||
const created = await this.orderService_.create(ord, sharedContext)
|
||||
|
||||
creditLinesToCreate.push(
|
||||
...(credit_lines || []).map((creditLine) => ({
|
||||
amount: creditLine.amount,
|
||||
...(credit_lines ?? []).map((creditLine) => ({
|
||||
amount: MathBN.convert(creditLine.amount),
|
||||
reference: creditLine.reference,
|
||||
reference_id: creditLine.reference_id,
|
||||
metadata: creditLine.metadata,
|
||||
@@ -778,7 +789,10 @@ export default class OrderModuleService
|
||||
}
|
||||
|
||||
if (creditLinesToCreate.length) {
|
||||
await super.createOrderCreditLines(creditLinesToCreate, sharedContext)
|
||||
await this.orderCreditLineService_.create(
|
||||
creditLinesToCreate,
|
||||
sharedContext
|
||||
)
|
||||
}
|
||||
|
||||
return createdOrders
|
||||
@@ -2265,6 +2279,7 @@ export default class OrderModuleService
|
||||
) {
|
||||
const addedItems = {}
|
||||
const addedShippingMethods = {}
|
||||
|
||||
for (const item of order.items) {
|
||||
const isExistingItem = item.id === item.detail?.item_id
|
||||
if (!isExistingItem) {
|
||||
@@ -3089,7 +3104,8 @@ export default class OrderModuleService
|
||||
if (!ordersIds.length) {
|
||||
return {
|
||||
items: [],
|
||||
shippingMethods: [],
|
||||
shipping_methods: [],
|
||||
credit_lines: [],
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3107,6 +3123,7 @@ export default class OrderModuleService
|
||||
shippingMethodsToUpsert,
|
||||
summariesToUpsert,
|
||||
orderToUpdate,
|
||||
creditLinesToCreate,
|
||||
} = await applyChangesToOrder(orders, actionsMap, {
|
||||
addActionReferenceToObject: true,
|
||||
includeTaxLinesAndAdjustementsToPreview: async (...args) => {
|
||||
@@ -3118,7 +3135,14 @@ export default class OrderModuleService
|
||||
},
|
||||
})
|
||||
|
||||
await promiseAll([
|
||||
const [
|
||||
_orderUpdate,
|
||||
_orderChangeActionUpdate,
|
||||
orderItems,
|
||||
_orderSummaryUpdate,
|
||||
orderShippingMethods,
|
||||
createdOrderCreditLines,
|
||||
] = await promiseAll([
|
||||
orderToUpdate.length
|
||||
? this.orderService_.update(orderToUpdate, sharedContext)
|
||||
: null,
|
||||
@@ -3137,11 +3161,18 @@ export default class OrderModuleService
|
||||
sharedContext
|
||||
)
|
||||
: null,
|
||||
creditLinesToCreate.length
|
||||
? this.orderCreditLineService_.create(
|
||||
creditLinesToCreate,
|
||||
sharedContext
|
||||
)
|
||||
: null,
|
||||
])
|
||||
|
||||
return {
|
||||
items: itemsToUpsert as any,
|
||||
shippingMethods: shippingMethodsToUpsert as any,
|
||||
items: orderItems ?? [],
|
||||
shipping_methods: orderShippingMethods ?? [],
|
||||
credit_lines: createdOrderCreditLines ?? ([] as any),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
import { BigNumberInput } from "@medusajs/framework/types"
|
||||
import {
|
||||
BigNumberInput,
|
||||
CreateOrderCreditLineDTO,
|
||||
OrderCreditLineDTO,
|
||||
} from "@medusajs/framework/types"
|
||||
|
||||
export type VirtualOrder = {
|
||||
id: string
|
||||
@@ -54,13 +58,7 @@ export type VirtualOrder = {
|
||||
amount: BigNumberInput
|
||||
}[]
|
||||
|
||||
credit_lines: {
|
||||
id: string
|
||||
order_id: string
|
||||
reference_id?: string
|
||||
reference?: string
|
||||
amount: BigNumberInput
|
||||
}[]
|
||||
credit_lines: (OrderCreditLineDTO | CreateOrderCreditLineDTO)[]
|
||||
|
||||
summary?: {
|
||||
pending_difference: BigNumberInput
|
||||
|
||||
@@ -1,26 +1,36 @@
|
||||
import { ChangeActionType, MedusaError } from "@medusajs/framework/utils"
|
||||
import {
|
||||
ChangeActionType,
|
||||
MathBN,
|
||||
MedusaError,
|
||||
} from "@medusajs/framework/utils"
|
||||
import { CreateOrderCreditLineDTO, OrderCreditLineDTO } from "@medusajs/types"
|
||||
import { OrderChangeProcessing } from "../calculate-order-change"
|
||||
import { setActionReference } from "../set-action-reference"
|
||||
|
||||
OrderChangeProcessing.registerActionType(ChangeActionType.CREDIT_LINE_ADD, {
|
||||
operation({ action, currentOrder, options }) {
|
||||
const creditLines = currentOrder.credit_lines ?? []
|
||||
let existing = creditLines.find((cl) => cl.id === action.reference_id)
|
||||
const creditLines: (OrderCreditLineDTO | CreateOrderCreditLineDTO)[] =
|
||||
currentOrder.credit_lines ?? []
|
||||
const existing = creditLines.find(
|
||||
(cl) => "id" in cl && cl?.id === action.reference_id
|
||||
)
|
||||
|
||||
if (!existing) {
|
||||
const newCreditLine = {
|
||||
order_id: currentOrder.id,
|
||||
amount: action.amount!,
|
||||
reference: action.reference,
|
||||
reference_id: action.reference_id,
|
||||
}
|
||||
|
||||
creditLines.push(newCreditLine as any)
|
||||
|
||||
setActionReference(newCreditLine, action, options)
|
||||
|
||||
currentOrder.credit_lines = creditLines
|
||||
if (existing) {
|
||||
return
|
||||
}
|
||||
|
||||
const newCreditLine = {
|
||||
order_id: currentOrder.id,
|
||||
amount: MathBN.convert(action.amount!),
|
||||
reference: action.reference!,
|
||||
reference_id: action.reference_id!,
|
||||
}
|
||||
|
||||
creditLines.push(newCreditLine)
|
||||
|
||||
setActionReference(newCreditLine, action, options)
|
||||
|
||||
currentOrder.credit_lines = creditLines
|
||||
},
|
||||
validate({ action }) {
|
||||
if (action.amount == null) {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import {
|
||||
CreateOrderCreditLineDTO,
|
||||
InferEntityType,
|
||||
OrderChangeActionDTO,
|
||||
OrderDTO,
|
||||
@@ -29,6 +30,7 @@ export async function applyChangesToOrder(
|
||||
}
|
||||
) {
|
||||
const itemsToUpsert: InferEntityType<typeof OrderItem>[] = []
|
||||
const creditLinesToCreate: CreateOrderCreditLineDTO[] = []
|
||||
const shippingMethodsToUpsert: InferEntityType<typeof OrderShippingMethod>[] =
|
||||
[]
|
||||
const summariesToUpsert: any[] = []
|
||||
@@ -96,6 +98,22 @@ export async function applyChangesToOrder(
|
||||
itemsToUpsert.push(itemToUpsert)
|
||||
}
|
||||
|
||||
const creditLines = (calculated.order.credit_lines ?? []).filter(
|
||||
(creditLine) => !("id" in creditLine)
|
||||
)
|
||||
|
||||
for (const creditLine of creditLines) {
|
||||
const creditLineToCreate = {
|
||||
order_id: order.id,
|
||||
amount: creditLine.amount,
|
||||
reference: creditLine.reference,
|
||||
reference_id: creditLine.reference_id,
|
||||
metadata: creditLine.metadata,
|
||||
}
|
||||
|
||||
creditLinesToCreate.push(creditLineToCreate)
|
||||
}
|
||||
|
||||
if (version > order.version) {
|
||||
for (const shippingMethod of calculated.order.shipping_methods ?? []) {
|
||||
const shippingMethod_ = shippingMethod as any
|
||||
@@ -172,6 +190,7 @@ export async function applyChangesToOrder(
|
||||
|
||||
return {
|
||||
itemsToUpsert,
|
||||
creditLinesToCreate,
|
||||
shippingMethodsToUpsert,
|
||||
summariesToUpsert,
|
||||
orderToUpdate,
|
||||
|
||||
@@ -7,7 +7,6 @@ import {
|
||||
BigNumber,
|
||||
ChangeActionType,
|
||||
MathBN,
|
||||
isDefined,
|
||||
isPresent,
|
||||
transformPropertiesToBigNumber,
|
||||
} from "@medusajs/framework/utils"
|
||||
@@ -102,8 +101,8 @@ export class OrderChangeProcessing {
|
||||
}
|
||||
|
||||
public processActions() {
|
||||
let newCreditLineTotal = (this.order.credit_lines || [])
|
||||
.filter((cl) => !isDefined(cl.id))
|
||||
let newCreditLineTotal = (this.order.credit_lines ?? [])
|
||||
.filter((cl) => !("id" in cl))
|
||||
.reduce(
|
||||
(acc, creditLine) => MathBN.add(acc, creditLine.amount),
|
||||
MathBN.convert(0)
|
||||
|
||||
Reference in New Issue
Block a user