feat(order, types): Add Credit Line to order module (#10636)
* feat(order, types): Add Credit Line to order module * chore: add action to inject credit lines
This commit is contained in:
32
packages/modules/order/src/utils/actions/credit-line-add.ts
Normal file
32
packages/modules/order/src/utils/actions/credit-line-add.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import { ChangeActionType, MedusaError } from "@medusajs/framework/utils"
|
||||
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)
|
||||
|
||||
if (!existing) {
|
||||
existing = {
|
||||
id: action.reference_id!,
|
||||
order_id: currentOrder.id,
|
||||
amount: action.amount as number,
|
||||
}
|
||||
|
||||
creditLines.push(existing)
|
||||
}
|
||||
|
||||
setActionReference(existing, action, options)
|
||||
|
||||
currentOrder.credit_lines = creditLines
|
||||
},
|
||||
validate({ action }) {
|
||||
if (action.amount == null) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_DATA,
|
||||
"Amount is required."
|
||||
)
|
||||
}
|
||||
},
|
||||
})
|
||||
@@ -1,5 +1,7 @@
|
||||
export * from "./cancel-item-fulfillment"
|
||||
export * from "./cancel-return"
|
||||
export * from "./change-shipping-address"
|
||||
export * from "./credit-line-add"
|
||||
export * from "./deliver-item"
|
||||
export * from "./fulfill-item"
|
||||
export * from "./item-add"
|
||||
@@ -12,6 +14,5 @@ export * from "./return-item"
|
||||
export * from "./ship-item"
|
||||
export * from "./shipping-add"
|
||||
export * from "./shipping-remove"
|
||||
export * from "./write-off-item"
|
||||
export * from "./transfer-customer"
|
||||
export * from "./change-shipping-address"
|
||||
export * from "./write-off-item"
|
||||
|
||||
@@ -58,6 +58,12 @@ export class OrderChangeProcessing {
|
||||
let paid = MathBN.convert(0)
|
||||
let refunded = MathBN.convert(0)
|
||||
let transactionTotal = MathBN.convert(0)
|
||||
let creditLineTotal = (this.order.credit_lines || []).reduce(
|
||||
(acc, creditLine) => MathBN.add(acc, creditLine.amount),
|
||||
MathBN.convert(0)
|
||||
)
|
||||
|
||||
const currentOrderTotal = MathBN.add(this.order.total ?? 0, creditLineTotal)
|
||||
|
||||
for (const tr of transactions) {
|
||||
if (MathBN.lt(tr.amount, 0)) {
|
||||
@@ -73,11 +79,12 @@ export class OrderChangeProcessing {
|
||||
this.summary = {
|
||||
pending_difference: 0,
|
||||
difference_sum: 0,
|
||||
current_order_total: this.order.total ?? 0,
|
||||
current_order_total: currentOrderTotal,
|
||||
original_order_total: this.order.total ?? 0,
|
||||
transaction_total: transactionTotal,
|
||||
paid_total: paid,
|
||||
refunded_total: refunded,
|
||||
credit_line_total: creditLineTotal,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -100,6 +107,7 @@ export class OrderChangeProcessing {
|
||||
}
|
||||
|
||||
const summary = this.summary
|
||||
|
||||
for (const action of this.actions) {
|
||||
if (!this.isEventActive(action)) {
|
||||
continue
|
||||
@@ -123,6 +131,13 @@ export class OrderChangeProcessing {
|
||||
if (!this.isEventDone(action) && !action.change_id) {
|
||||
summary.difference_sum = MathBN.add(summary.difference_sum, amount)
|
||||
}
|
||||
|
||||
const creditLineTotal = (this.order.credit_lines || []).reduce(
|
||||
(acc, creditLine) => MathBN.add(acc, creditLine.amount),
|
||||
MathBN.convert(0)
|
||||
)
|
||||
|
||||
summary.credit_line_total = creditLineTotal
|
||||
summary.current_order_total = MathBN.add(
|
||||
summary.current_order_total,
|
||||
amount
|
||||
@@ -200,6 +215,7 @@ export class OrderChangeProcessing {
|
||||
difference_sum: new BigNumber(summary.difference_sum),
|
||||
paid_total: new BigNumber(summary.paid_total),
|
||||
refunded_total: new BigNumber(summary.refunded_total),
|
||||
credit_line_total: new BigNumber(summary.credit_line_total),
|
||||
} as unknown as OrderSummaryDTO
|
||||
|
||||
return orderSummary
|
||||
|
||||
Reference in New Issue
Block a user