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:
Riqwan Thamir
2025-03-17 16:20:59 +01:00
committed by GitHub
parent 0625f76cd4
commit cb6249320e
31 changed files with 630 additions and 111 deletions
@@ -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) {