feat(core-flows,types,cart): add credit lines to cart (#11419)

* feat(core-flows,types,cart): add credit lines to cart

* chore: fix specs

* chore: credit lines hook

* chore: update types

* chore: added credit line totals

* chore: add totals fields to query config

* chore: add complete cart hook

* chore: add credit lines creation to order

* chore: pr ready for review

* chore: fix tests

* Apply suggestions from code review

* chore: fix types

* chore: adjust summary calculations with new totals
This commit is contained in:
Riqwan Thamir
2025-02-24 14:34:36 +01:00
committed by GitHub
parent be566ca6fb
commit fb2e86484a
27 changed files with 802 additions and 58 deletions

View File

@@ -8,20 +8,19 @@ OrderChangeProcessing.registerActionType(ChangeActionType.CREDIT_LINE_ADD, {
let existing = creditLines.find((cl) => cl.id === action.reference_id)
if (!existing) {
existing = {
id: action.reference_id!,
const newCreditLine = {
order_id: currentOrder.id,
amount: action.amount!,
reference: action.reference,
reference_id: action.reference_id,
}
creditLines.push(existing)
creditLines.push(newCreditLine as any)
setActionReference(newCreditLine, action, options)
currentOrder.credit_lines = creditLines
}
setActionReference(existing, action, options)
currentOrder.credit_lines = creditLines
},
validate({ action }) {
if (action.amount == null) {

View File

@@ -7,6 +7,7 @@ import {
BigNumber,
ChangeActionType,
MathBN,
isDefined,
isPresent,
transformPropertiesToBigNumber,
} from "@medusajs/framework/utils"
@@ -101,10 +102,12 @@ export class OrderChangeProcessing {
}
public processActions() {
let creditLineTotal = (this.order.credit_lines || []).reduce(
(acc, creditLine) => MathBN.add(acc, creditLine.amount),
MathBN.convert(0)
)
let newCreditLineTotal = (this.order.credit_lines || [])
.filter((cl) => !isDefined(cl.id))
.reduce(
(acc, creditLine) => MathBN.add(acc, creditLine.amount),
MathBN.convert(0)
)
for (const action of this.actions) {
this.processAction_(action)
@@ -133,7 +136,11 @@ export class OrderChangeProcessing {
}
if (action.action === ChangeActionType.CREDIT_LINE_ADD) {
creditLineTotal = MathBN.add(creditLineTotal, amount)
newCreditLineTotal = MathBN.add(newCreditLineTotal, amount)
summary.current_order_total = MathBN.sub(
summary.current_order_total,
amount
)
} else {
summary.current_order_total = MathBN.add(
summary.current_order_total,
@@ -142,21 +149,13 @@ export class OrderChangeProcessing {
}
}
summary.credit_line_total = creditLineTotal
summary.accounting_total = MathBN.sub(
summary.current_order_total,
creditLineTotal
)
summary.credit_line_total = newCreditLineTotal
summary.accounting_total = summary.current_order_total
summary.transaction_total = MathBN.sum(
...this.transactions.map((tr) => tr.amount)
)
summary.current_order_total = MathBN.sub(
summary.current_order_total,
creditLineTotal
)
summary.pending_difference = MathBN.sub(
summary.current_order_total,
summary.transaction_total
@@ -241,19 +240,7 @@ export class OrderChangeProcessing {
accounting_total: new BigNumber(summary_.accounting_total),
} as any
orderSummary.accounting_total = new BigNumber(
MathBN.sub(
orderSummary.current_order_total,
orderSummary.credit_line_total
)
)
orderSummary.current_order_total = new BigNumber(
MathBN.sub(
orderSummary.current_order_total,
orderSummary.credit_line_total
)
)
orderSummary.accounting_total = orderSummary.current_order_total
orderSummary.pending_difference = MathBN.sub(
orderSummary.current_order_total,