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
@@ -17,6 +17,7 @@ moduleIntegrationTestRunner<ICartModuleService>({
expect(Object.keys(linkable)).toEqual([
"cart",
"creditLine",
"address",
"lineItem",
"lineItemAdjustment",
@@ -40,6 +41,15 @@ moduleIntegrationTestRunner<ICartModuleService>({
field: "cart",
},
},
creditLine: {
id: {
entity: "CreditLine",
field: "creditLine",
linkable: "credit_line_id",
primaryKey: "id",
serviceName: "cart",
},
},
address: {
id: {
linkable: "address_id",
@@ -2764,6 +2774,22 @@ moduleIntegrationTestRunner<ICartModuleService>({
},
},
],
credit_lines: [],
credit_lines_subtotal: 0,
credit_lines_tax_total: 0,
credit_lines_total: 0,
raw_credit_lines_subtotal: {
precision: 20,
value: "0",
},
raw_credit_lines_tax_total: {
precision: 20,
value: "0",
},
raw_credit_lines_total: {
precision: 20,
value: "0",
},
total: 210,
subtotal: 510,
tax_total: 0,
@@ -163,6 +163,7 @@
"keyName": "IDX_cart_address_deleted_at",
"columnNames": [],
"composite": false,
"constraint": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_address_deleted_at\" ON \"cart_address\" (deleted_at) WHERE deleted_at IS NULL"
@@ -173,12 +174,14 @@
"id"
],
"composite": false,
"constraint": true,
"primary": true,
"unique": true
}
],
"checks": [],
"foreignKeys": {}
"foreignKeys": {},
"nativeEnums": {}
},
{
"columns": {
@@ -313,6 +316,7 @@
"keyName": "IDX_cart_deleted_at",
"columnNames": [],
"composite": false,
"constraint": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_deleted_at\" ON \"cart\" (deleted_at) WHERE deleted_at IS NULL"
@@ -321,6 +325,7 @@
"keyName": "IDX_cart_region_id",
"columnNames": [],
"composite": false,
"constraint": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_region_id\" ON \"cart\" (region_id) WHERE deleted_at IS NULL AND region_id IS NOT NULL"
@@ -329,6 +334,7 @@
"keyName": "IDX_cart_customer_id",
"columnNames": [],
"composite": false,
"constraint": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_customer_id\" ON \"cart\" (customer_id) WHERE deleted_at IS NULL AND customer_id IS NOT NULL"
@@ -337,6 +343,7 @@
"keyName": "IDX_cart_sales_channel_id",
"columnNames": [],
"composite": false,
"constraint": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_sales_channel_id\" ON \"cart\" (sales_channel_id) WHERE deleted_at IS NULL AND sales_channel_id IS NOT NULL"
@@ -345,6 +352,7 @@
"keyName": "IDX_cart_curency_code",
"columnNames": [],
"composite": false,
"constraint": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_curency_code\" ON \"cart\" (currency_code) WHERE deleted_at IS NULL"
@@ -353,6 +361,7 @@
"keyName": "IDX_cart_shipping_address_id",
"columnNames": [],
"composite": false,
"constraint": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_shipping_address_id\" ON \"cart\" (shipping_address_id) WHERE deleted_at IS NULL AND shipping_address_id IS NOT NULL"
@@ -361,6 +370,7 @@
"keyName": "IDX_cart_billing_address_id",
"columnNames": [],
"composite": false,
"constraint": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_billing_address_id\" ON \"cart\" (billing_address_id) WHERE deleted_at IS NULL AND billing_address_id IS NOT NULL"
@@ -371,6 +381,7 @@
"id"
],
"composite": false,
"constraint": true,
"primary": true,
"unique": true
}
@@ -403,7 +414,164 @@
"deleteRule": "set null",
"updateRule": "cascade"
}
}
},
"nativeEnums": {}
},
{
"columns": {
"id": {
"name": "id",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
},
"cart_id": {
"name": "cart_id",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
},
"reference": {
"name": "reference",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"mappedType": "text"
},
"reference_id": {
"name": "reference_id",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"mappedType": "text"
},
"amount": {
"name": "amount",
"type": "numeric",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "decimal"
},
"raw_amount": {
"name": "raw_amount",
"type": "jsonb",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "json"
},
"metadata": {
"name": "metadata",
"type": "jsonb",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"mappedType": "json"
},
"created_at": {
"name": "created_at",
"type": "timestamptz",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"length": 6,
"default": "now()",
"mappedType": "datetime"
},
"updated_at": {
"name": "updated_at",
"type": "timestamptz",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"length": 6,
"default": "now()",
"mappedType": "datetime"
},
"deleted_at": {
"name": "deleted_at",
"type": "timestamptz",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"length": 6,
"mappedType": "datetime"
}
},
"name": "credit_line",
"schema": "public",
"indexes": [
{
"keyName": "IDX_credit_line_cart_id",
"columnNames": [],
"composite": false,
"constraint": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_credit_line_cart_id\" ON \"credit_line\" (cart_id) WHERE deleted_at IS NULL"
},
{
"keyName": "IDX_credit_line_deleted_at",
"columnNames": [],
"composite": false,
"constraint": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_credit_line_deleted_at\" ON \"credit_line\" (deleted_at) WHERE deleted_at IS NULL"
},
{
"keyName": "IDX_cart_credit_line_reference_reference_id",
"columnNames": [],
"composite": false,
"constraint": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_credit_line_reference_reference_id\" ON \"credit_line\" (reference, reference_id) WHERE deleted_at IS NOT NULL"
},
{
"keyName": "credit_line_pkey",
"columnNames": [
"id"
],
"composite": false,
"constraint": true,
"primary": true,
"unique": true
}
],
"checks": [],
"foreignKeys": {
"credit_line_cart_id_foreign": {
"constraintName": "credit_line_cart_id_foreign",
"columnNames": [
"cart_id"
],
"localTableName": "public.credit_line",
"referencedColumnNames": [
"id"
],
"referencedTableName": "public.cart",
"updateRule": "cascade"
}
},
"nativeEnums": {}
},
{
"columns": {
@@ -703,6 +871,7 @@
"keyName": "IDX_cart_line_item_cart_id",
"columnNames": [],
"composite": false,
"constraint": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_line_item_cart_id\" ON \"cart_line_item\" (cart_id) WHERE deleted_at IS NULL"
@@ -711,6 +880,7 @@
"keyName": "IDX_cart_line_item_deleted_at",
"columnNames": [],
"composite": false,
"constraint": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_line_item_deleted_at\" ON \"cart_line_item\" (deleted_at) WHERE deleted_at IS NULL"
@@ -719,6 +889,7 @@
"keyName": "IDX_line_item_cart_id",
"columnNames": [],
"composite": false,
"constraint": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_line_item_cart_id\" ON \"cart_line_item\" (cart_id) WHERE deleted_at IS NULL"
@@ -727,6 +898,7 @@
"keyName": "IDX_line_item_variant_id",
"columnNames": [],
"composite": false,
"constraint": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_line_item_variant_id\" ON \"cart_line_item\" (variant_id) WHERE deleted_at IS NULL AND variant_id IS NOT NULL"
@@ -735,6 +907,7 @@
"keyName": "IDX_line_item_product_id",
"columnNames": [],
"composite": false,
"constraint": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_line_item_product_id\" ON \"cart_line_item\" (product_id) WHERE deleted_at IS NULL AND product_id IS NOT NULL"
@@ -743,6 +916,7 @@
"keyName": "IDX_line_item_product_type_id",
"columnNames": [],
"composite": false,
"constraint": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_line_item_product_type_id\" ON \"cart_line_item\" (product_type_id) WHERE deleted_at IS NULL AND product_type_id IS NOT NULL"
@@ -753,6 +927,7 @@
"id"
],
"composite": false,
"constraint": true,
"primary": true,
"unique": true
}
@@ -772,7 +947,8 @@
"deleteRule": "cascade",
"updateRule": "cascade"
}
}
},
"nativeEnums": {}
},
{
"columns": {
@@ -897,6 +1073,7 @@
"keyName": "IDX_cart_line_item_adjustment_item_id",
"columnNames": [],
"composite": false,
"constraint": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_line_item_adjustment_item_id\" ON \"cart_line_item_adjustment\" (item_id) WHERE deleted_at IS NULL"
@@ -905,6 +1082,7 @@
"keyName": "IDX_cart_line_item_adjustment_deleted_at",
"columnNames": [],
"composite": false,
"constraint": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_line_item_adjustment_deleted_at\" ON \"cart_line_item_adjustment\" (deleted_at) WHERE deleted_at IS NULL"
@@ -913,6 +1091,7 @@
"keyName": "IDX_line_item_adjustment_promotion_id",
"columnNames": [],
"composite": false,
"constraint": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_line_item_adjustment_promotion_id\" ON \"cart_line_item_adjustment\" (promotion_id) WHERE deleted_at IS NULL AND promotion_id IS NOT NULL"
@@ -921,6 +1100,7 @@
"keyName": "IDX_adjustment_item_id",
"columnNames": [],
"composite": false,
"constraint": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_adjustment_item_id\" ON \"cart_line_item_adjustment\" (item_id) WHERE deleted_at IS NULL"
@@ -931,6 +1111,7 @@
"id"
],
"composite": false,
"constraint": true,
"primary": true,
"unique": true
}
@@ -956,7 +1137,8 @@
"deleteRule": "cascade",
"updateRule": "cascade"
}
}
},
"nativeEnums": {}
},
{
"columns": {
@@ -1072,6 +1254,7 @@
"keyName": "IDX_cart_line_item_tax_line_item_id",
"columnNames": [],
"composite": false,
"constraint": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_line_item_tax_line_item_id\" ON \"cart_line_item_tax_line\" (item_id) WHERE deleted_at IS NULL"
@@ -1080,6 +1263,7 @@
"keyName": "IDX_cart_line_item_tax_line_deleted_at",
"columnNames": [],
"composite": false,
"constraint": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_line_item_tax_line_deleted_at\" ON \"cart_line_item_tax_line\" (deleted_at) WHERE deleted_at IS NULL"
@@ -1088,6 +1272,7 @@
"keyName": "IDX_line_item_tax_line_tax_rate_id",
"columnNames": [],
"composite": false,
"constraint": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_line_item_tax_line_tax_rate_id\" ON \"cart_line_item_tax_line\" (tax_rate_id) WHERE deleted_at IS NULL AND tax_rate_id IS NOT NULL"
@@ -1096,6 +1281,7 @@
"keyName": "IDX_tax_line_item_id",
"columnNames": [],
"composite": false,
"constraint": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_tax_line_item_id\" ON \"cart_line_item_tax_line\" (item_id) WHERE deleted_at IS NULL"
@@ -1106,6 +1292,7 @@
"id"
],
"composite": false,
"constraint": true,
"primary": true,
"unique": true
}
@@ -1125,7 +1312,8 @@
"deleteRule": "cascade",
"updateRule": "cascade"
}
}
},
"nativeEnums": {}
},
{
"columns": {
@@ -1260,6 +1448,7 @@
"keyName": "IDX_cart_shipping_method_cart_id",
"columnNames": [],
"composite": false,
"constraint": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_shipping_method_cart_id\" ON \"cart_shipping_method\" (cart_id) WHERE deleted_at IS NULL"
@@ -1268,6 +1457,7 @@
"keyName": "IDX_cart_shipping_method_deleted_at",
"columnNames": [],
"composite": false,
"constraint": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_shipping_method_deleted_at\" ON \"cart_shipping_method\" (deleted_at) WHERE deleted_at IS NULL"
@@ -1276,6 +1466,7 @@
"keyName": "IDX_shipping_method_cart_id",
"columnNames": [],
"composite": false,
"constraint": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_shipping_method_cart_id\" ON \"cart_shipping_method\" (cart_id) WHERE deleted_at IS NULL"
@@ -1284,6 +1475,7 @@
"keyName": "IDX_shipping_method_option_id",
"columnNames": [],
"composite": false,
"constraint": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_shipping_method_option_id\" ON \"cart_shipping_method\" (shipping_option_id) WHERE deleted_at IS NULL AND shipping_option_id IS NOT NULL"
@@ -1294,6 +1486,7 @@
"id"
],
"composite": false,
"constraint": true,
"primary": true,
"unique": true
}
@@ -1319,7 +1512,8 @@
"deleteRule": "cascade",
"updateRule": "cascade"
}
}
},
"nativeEnums": {}
},
{
"columns": {
@@ -1444,6 +1638,7 @@
"keyName": "IDX_cart_shipping_method_adjustment_shipping_method_id",
"columnNames": [],
"composite": false,
"constraint": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_shipping_method_adjustment_shipping_method_id\" ON \"cart_shipping_method_adjustment\" (shipping_method_id) WHERE deleted_at IS NULL"
@@ -1452,6 +1647,7 @@
"keyName": "IDX_cart_shipping_method_adjustment_deleted_at",
"columnNames": [],
"composite": false,
"constraint": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_shipping_method_adjustment_deleted_at\" ON \"cart_shipping_method_adjustment\" (deleted_at) WHERE deleted_at IS NULL"
@@ -1460,6 +1656,7 @@
"keyName": "IDX_shipping_method_adjustment_promotion_id",
"columnNames": [],
"composite": false,
"constraint": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_shipping_method_adjustment_promotion_id\" ON \"cart_shipping_method_adjustment\" (promotion_id) WHERE deleted_at IS NULL AND promotion_id IS NOT NULL"
@@ -1468,6 +1665,7 @@
"keyName": "IDX_adjustment_shipping_method_id",
"columnNames": [],
"composite": false,
"constraint": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_adjustment_shipping_method_id\" ON \"cart_shipping_method_adjustment\" (shipping_method_id) WHERE deleted_at IS NULL"
@@ -1478,6 +1676,7 @@
"id"
],
"composite": false,
"constraint": true,
"primary": true,
"unique": true
}
@@ -1496,7 +1695,8 @@
"referencedTableName": "public.cart_shipping_method",
"updateRule": "cascade"
}
}
},
"nativeEnums": {}
},
{
"columns": {
@@ -1612,6 +1812,7 @@
"keyName": "IDX_cart_shipping_method_tax_line_shipping_method_id",
"columnNames": [],
"composite": false,
"constraint": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_shipping_method_tax_line_shipping_method_id\" ON \"cart_shipping_method_tax_line\" (shipping_method_id) WHERE deleted_at IS NULL"
@@ -1620,6 +1821,7 @@
"keyName": "IDX_cart_shipping_method_tax_line_deleted_at",
"columnNames": [],
"composite": false,
"constraint": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_shipping_method_tax_line_deleted_at\" ON \"cart_shipping_method_tax_line\" (deleted_at) WHERE deleted_at IS NULL"
@@ -1628,6 +1830,7 @@
"keyName": "IDX_tax_line_shipping_method_id",
"columnNames": [],
"composite": false,
"constraint": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_tax_line_shipping_method_id\" ON \"cart_shipping_method_tax_line\" (shipping_method_id) WHERE deleted_at IS NULL"
@@ -1636,6 +1839,7 @@
"keyName": "IDX_shipping_method_tax_line_tax_rate_id",
"columnNames": [],
"composite": false,
"constraint": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_shipping_method_tax_line_tax_rate_id\" ON \"cart_shipping_method_tax_line\" (tax_rate_id) WHERE deleted_at IS NULL AND tax_rate_id IS NOT NULL"
@@ -1646,6 +1850,7 @@
"id"
],
"composite": false,
"constraint": true,
"primary": true,
"unique": true
}
@@ -1664,7 +1869,9 @@
"referencedTableName": "public.cart_shipping_method",
"updateRule": "cascade"
}
}
},
"nativeEnums": {}
}
]
],
"nativeEnums": {}
}
@@ -0,0 +1,29 @@
import { Migration } from "@mikro-orm/migrations"
export class Migration20250212131240 extends Migration {
override async up(): Promise<void> {
this.addSql(
`create table if not exists "credit_line" ("id" text not null, "cart_id" text not null, "reference" text null, "reference_id" text null, "amount" numeric not null, "raw_amount" jsonb not null, "metadata" jsonb null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "credit_line_pkey" primary key ("id"));`
)
this.addSql(
`CREATE INDEX IF NOT EXISTS "IDX_credit_line_cart_id" ON "credit_line" (cart_id) WHERE deleted_at IS NULL;`
)
this.addSql(
`CREATE INDEX IF NOT EXISTS "IDX_credit_line_deleted_at" ON "credit_line" (deleted_at) WHERE deleted_at IS NULL;`
)
this.addSql(
`CREATE INDEX IF NOT EXISTS "IDX_cart_credit_line_reference_reference_id" ON "credit_line" (reference, reference_id) WHERE deleted_at IS NOT NULL;`
)
this.addSql(
`alter table if exists "credit_line" add constraint "credit_line_cart_id_foreign" foreign key ("cart_id") references "cart" ("id") on update cascade;`
)
}
override async down(): Promise<void> {
this.addSql(`drop table if exists "credit_line" cascade;`)
}
}
+4
View File
@@ -1,5 +1,6 @@
import { model } from "@medusajs/framework/utils"
import Address from "./address"
import CreditLine from "./credit-line"
import LineItem from "./line-item"
import ShippingMethod from "./shipping-method"
@@ -28,6 +29,9 @@ const Cart = model
items: model.hasMany(() => LineItem, {
mappedBy: "cart",
}),
credit_lines: model.hasMany(() => CreditLine, {
mappedBy: "cart",
}),
shipping_methods: model.hasMany(() => ShippingMethod, {
mappedBy: "cart",
}),
@@ -0,0 +1,25 @@
import { model } from "@medusajs/framework/utils"
import Cart from "./cart"
const CreditLine = model
.define("CreditLine", {
id: model.id({ prefix: "cacl" }).primaryKey(),
cart: model.belongsTo(() => Cart, {
mappedBy: "credit_lines",
}),
reference: model.text().nullable(),
reference_id: model.text().nullable(),
amount: model.bigNumber(),
raw_amount: model.json(),
metadata: model.json().nullable(),
})
.indexes([
{
name: "IDX_cart_credit_line_reference_reference_id",
on: ["reference", "reference_id"],
unique: false,
where: "deleted_at IS NOT NULL",
},
])
export default CreditLine
@@ -1,5 +1,6 @@
export { default as Address } from "./address"
export { default as Cart } from "./cart"
export { default as CreditLine } from "./credit-line"
export { default as LineItem } from "./line-item"
export { default as LineItemAdjustment } from "./line-item-adjustment"
export { default as LineItemTaxLine } from "./line-item-tax-line"
@@ -24,6 +24,7 @@ import {
import {
Address,
Cart,
CreditLine,
LineItem,
LineItemAdjustment,
LineItemTaxLine,
@@ -54,6 +55,7 @@ type InjectedDependencies = {
const generateMethodForModels = {
Cart,
CreditLine,
Address,
LineItem,
LineItemAdjustment,
@@ -66,6 +68,7 @@ const generateMethodForModels = {
export default class CartModuleService
extends ModulesSdkUtils.MedusaService<{
Cart: { dto: CartTypes.CartDTO }
CreditLine: { dto: CartTypes.CartCreditLineDTO }
Address: { dto: CartTypes.CartAddressDTO }
LineItem: { dto: CartTypes.CartLineItemDTO }
LineItemAdjustment: { dto: CartTypes.LineItemAdjustmentDTO }
@@ -170,6 +173,7 @@ export default class CartModuleService
const requiredFieldsForTotals = [
"items",
"credit_lines",
"items.tax_lines",
"items.adjustments",
"shipping_methods",
@@ -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,