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