From 8dd9ae3d35b90b54f3490df3e0da61180f671ca4 Mon Sep 17 00:00:00 2001 From: Adrien de Peretti Date: Thu, 11 Sep 2025 11:50:58 +0200 Subject: [PATCH] chore(): Improve cart module (#13472) * chore(): Improve cart module * Create giant-spoons-crash.md --- .changeset/giant-spoons-crash.md | 5 +++ .../modules/cart/src/services/cart-module.ts | 38 +++++++++++++++---- 2 files changed, 35 insertions(+), 8 deletions(-) create mode 100644 .changeset/giant-spoons-crash.md diff --git a/.changeset/giant-spoons-crash.md b/.changeset/giant-spoons-crash.md new file mode 100644 index 0000000000..1338fb6911 --- /dev/null +++ b/.changeset/giant-spoons-crash.md @@ -0,0 +1,5 @@ +--- +"@medusajs/cart": patch +--- + +chore(): Improve cart module diff --git a/packages/modules/cart/src/services/cart-module.ts b/packages/modules/cart/src/services/cart-module.ts index 85ddfcd39e..3a73d526f0 100644 --- a/packages/modules/cart/src/services/cart-module.ts +++ b/packages/modules/cart/src/services/cart-module.ts @@ -460,7 +460,7 @@ export default class CartModuleService items: CartTypes.CreateLineItemDTO[], @MedusaContext() sharedContext: Context = {} ): Promise[]> { - const cart = await this.retrieveCart( + const cart = await this.cartService_.retrieve( cartId, { select: ["id"] }, sharedContext @@ -731,7 +731,7 @@ export default class CartModuleService data: CartTypes.CreateShippingMethodForSingleCartDTO[], @MedusaContext() sharedContext: Context = {} ): Promise[]> { - const cart = await this.retrieveCart( + const cart = await this.cartService_.retrieve( cartId, { select: ["id"] }, sharedContext @@ -802,7 +802,7 @@ export default class CartModuleService ): Promise[]> { let addedAdjustments: InferEntityType[] = [] if (isString(cartIdOrData)) { - const cart = await this.retrieveCart( + const cart = await this.cartService_.retrieve( cartIdOrData, { select: ["id"], relations: ["items"] }, sharedContext @@ -1005,7 +1005,7 @@ export default class CartModuleService )[], @MedusaContext() sharedContext: Context = {} ): Promise[]> { - const cart = await this.retrieveCart( + const cart = await this.cartService_.retrieve( cartId, { select: ["id"], relations: ["items.adjustments"] }, sharedContext @@ -1079,7 +1079,7 @@ export default class CartModuleService )[], @MedusaContext() sharedContext: Context = {} ): Promise[]> { - const cart = await this.retrieveCart( + const cart = await this.cartService_.retrieve( cartId, { select: ["id"], relations: ["shipping_methods.adjustments"] }, sharedContext @@ -1176,7 +1176,7 @@ export default class CartModuleService let addedAdjustments: InferEntityType[] = [] if (isString(cartIdOrData)) { - const cart = await this.retrieveCart( + const cart = await this.cartService_.retrieve( cartIdOrData, { select: ["id"], relations: ["shipping_methods"] }, sharedContext @@ -1267,7 +1267,18 @@ export default class CartModuleService let addedTaxLines: InferEntityType[] if (isString(cartIdOrData)) { // existence check - await this.retrieveCart(cartIdOrData, { select: ["id"] }, sharedContext) + const cart = await this.cartService_.retrieve( + cartIdOrData, + { select: ["id"] }, + sharedContext + ) + + if (!cart) { + throw new MedusaError( + MedusaError.Types.INVALID_DATA, + `Cart with id ${cartIdOrData} does not exist` + ) + } const lines = Array.isArray(taxLines) ? taxLines : [taxLines] @@ -1416,7 +1427,18 @@ export default class CartModuleService let addedTaxLines: InferEntityType[] if (isString(cartIdOrData)) { // existence check - await this.retrieveCart(cartIdOrData, { select: ["id"] }, sharedContext) + const cart = await this.cartService_.retrieve( + cartIdOrData, + { select: ["id"] }, + sharedContext + ) + + if (!cart) { + throw new MedusaError( + MedusaError.Types.INVALID_DATA, + `Cart with id ${cartIdOrData} does not exist` + ) + } const lines = Array.isArray(taxLines) ? taxLines : [taxLines]