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
@@ -1,4 +1,4 @@
import { ChangeActionType } from "@medusajs/framework/utils"
import { ChangeActionType, decorateCartTotals } from "@medusajs/framework/utils"
import { VirtualOrder } from "@types"
import { calculateOrderChange } from "../../../../utils"
@@ -100,7 +100,6 @@ describe("Action: Credit Line Add", function () {
})
const sumToJSON = JSON.parse(JSON.stringify(changes.summary))
expect(sumToJSON).toEqual({
transaction_total: 0,
original_order_total: 30,
@@ -129,8 +128,10 @@ describe("Action: Credit Line Add", function () {
},
]
const order = decorateCartTotals(originalOrder) as any
const changesSecond = calculateOrderChange({
order: originalOrder,
order,
actions: actionsSecond,
options: { addActionReferenceToObject: true },
})
@@ -139,12 +140,12 @@ describe("Action: Credit Line Add", function () {
expect(sumToJSONSecond).toEqual({
transaction_total: 0,
original_order_total: 30,
original_order_total: 20,
current_order_total: -10,
pending_difference: -10,
paid_total: 0,
refunded_total: 0,
credit_line_total: 40,
credit_line_total: 30,
accounting_total: -10,
})
})
@@ -1,6 +1,7 @@
import {
BigNumberInput,
Context,
CreateOrderCreditLineDTO,
DAL,
FilterableOrderReturnReasonProps,
FindConfig,
@@ -689,6 +690,7 @@ export default class OrderModuleService
"billing_address",
"summary",
"items",
"credit_lines",
"items.tax_lines",
"items.adjustments",
"shipping_methods",
@@ -713,9 +715,10 @@ export default class OrderModuleService
await this.createOrderAddresses_(data, sharedContext)
const lineItemsToCreate: CreateOrderLineItemDTO[] = []
const creditLinesToCreate: CreateOrderCreditLineDTO[] = []
const createdOrders: InferEntityType<typeof Order>[] = []
for (const { items, shipping_methods, ...order } of data) {
for (const { items, shipping_methods, credit_lines, ...order } of data) {
const ord = order as any
const shippingMethods = shipping_methods?.map((sm: any) => {
@@ -746,6 +749,16 @@ export default class OrderModuleService
const created = await this.orderService_.create(ord, sharedContext)
creditLinesToCreate.push(
...(credit_lines || []).map((creditLine) => ({
amount: creditLine.amount,
reference: creditLine.reference,
reference_id: creditLine.reference_id,
metadata: creditLine.metadata,
order_id: created.id,
}))
)
createdOrders.push(created)
if (items?.length) {
@@ -764,6 +777,10 @@ export default class OrderModuleService
await this.createOrderLineItemsBulk_(lineItemsToCreate, sharedContext)
}
if (creditLinesToCreate.length) {
await super.createOrderCreditLines(creditLinesToCreate, sharedContext)
}
return createdOrders
}
@@ -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) {
@@ -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,