From c5d35ec7f267d35148039fe58887356e2ec8805b Mon Sep 17 00:00:00 2001 From: Oli Juhl <59018053+olivermrbl@users.noreply.github.com> Date: Wed, 28 Feb 2024 11:45:56 +0100 Subject: [PATCH] chore(cart): Use module native soft-delete/delete methods (#6491) --- .../cart/store/cart.workflows.spec.ts | 1 - .../services/cart-module/index.spec.ts | 123 +-------- packages/cart/src/services/cart-module.ts | 235 ------------------ .../src/definition/cart/steps/add-to-cart.ts | 2 +- .../line-item/steps/delete-line-items.ts | 2 +- .../medusa/src/api-v2/store/carts/route.ts | 6 +- packages/types/src/cart/service.ts | 88 ++----- 7 files changed, 33 insertions(+), 424 deletions(-) diff --git a/integration-tests/plugins/__tests__/cart/store/cart.workflows.spec.ts b/integration-tests/plugins/__tests__/cart/store/cart.workflows.spec.ts index 021d1f9d94..29d0fcb205 100644 --- a/integration-tests/plugins/__tests__/cart/store/cart.workflows.spec.ts +++ b/integration-tests/plugins/__tests__/cart/store/cart.workflows.spec.ts @@ -1,7 +1,6 @@ import { addToCartWorkflow, createCartWorkflow, - deleteLineItemsStepId, deleteLineItemsWorkflow, findOrCreateCustomerStepId, diff --git a/packages/cart/integration-tests/__tests__/services/cart-module/index.spec.ts b/packages/cart/integration-tests/__tests__/services/cart-module/index.spec.ts index 5440b9938b..78574be8ff 100644 --- a/packages/cart/integration-tests/__tests__/services/cart-module/index.spec.ts +++ b/packages/cart/integration-tests/__tests__/services/cart-module/index.spec.ts @@ -705,7 +705,7 @@ describe("Cart Module Service", () => { expect(item.title).toBe("test") - await service.removeLineItems([item.id]) + await service.softDeleteLineItems([item.id]) const cart = await service.retrieve(createdCart.id, { relations: ["items"], @@ -734,7 +734,7 @@ describe("Cart Module Service", () => { }, ]) - await service.removeLineItems([item.id, item2.id]) + await service.softDeleteLineItems([item.id, item2.id]) const cart = await service.retrieve(createdCart.id, { relations: ["items"], @@ -847,7 +847,7 @@ describe("Cart Module Service", () => { expect(method.id).not.toBe(null) - await service.removeShippingMethods(method.id) + await service.softDeleteShippingMethods([method.id]) const cart = await service.retrieve(createdCart.id, { relations: ["shipping_methods"], @@ -1287,43 +1287,7 @@ describe("Cart Module Service", () => { expect(adjustment.item_id).toBe(item.id) - await service.removeLineItemAdjustments(adjustment.id) - - const adjustments = await service.listLineItemAdjustments({ - item_id: item.id, - }) - - expect(adjustments?.length).toBe(0) - }) - - it("should remove a line item succesfully with selector", async () => { - const [createdCart] = await service.create([ - { - currency_code: "eur", - }, - ]) - - const [item] = await service.addLineItems(createdCart.id, [ - { - quantity: 1, - unit_price: 100, - title: "test", - }, - ]) - - const [adjustment] = await service.addLineItemAdjustments( - createdCart.id, - [ - { - item_id: item.id, - amount: 50, - }, - ] - ) - - expect(adjustment.item_id).toBe(item.id) - - await service.removeLineItemAdjustments({ item_id: item.id }) + await service.softDeleteLineItemAdjustments([adjustment.id]) const adjustments = await service.listLineItemAdjustments({ item_id: item.id, @@ -1831,7 +1795,7 @@ describe("Cart Module Service", () => { expect(adjustment.shipping_method_id).toBe(method.id) - await service.removeShippingMethodAdjustments(adjustment.id) + await service.softDeleteShippingMethodAdjustments([adjustment.id]) const adjustments = await service.listShippingMethodAdjustments({ shipping_method_id: method.id, @@ -1839,47 +1803,6 @@ describe("Cart Module Service", () => { expect(adjustments?.length).toBe(0) }) - - it("should remove a shipping method succesfully with selector", async () => { - const [createdCart] = await service.create([ - { - currency_code: "eur", - }, - ]) - - const [shippingMethod] = await service.addShippingMethods( - createdCart.id, - [ - { - amount: 100, - name: "test", - }, - ] - ) - - const [adjustment] = await service.addShippingMethodAdjustments( - createdCart.id, - [ - { - shipping_method_id: shippingMethod.id, - amount: 50, - code: "50%", - }, - ] - ) - - expect(adjustment.shipping_method_id).toBe(shippingMethod.id) - - await service.removeShippingMethodAdjustments({ - shipping_method_id: shippingMethod.id, - }) - - const adjustments = await service.listShippingMethodAdjustments({ - shipping_method_id: shippingMethod.id, - }) - - expect(adjustments?.length).toBe(0) - }) }) describe("setLineItemTaxLines", () => { @@ -2400,41 +2323,7 @@ describe("Cart Module Service", () => { expect(taxLine.item_id).toBe(item.id) - await service.removeLineItemTaxLines(taxLine.id) - - const taxLines = await service.listLineItemTaxLines({ - item_id: item.id, - }) - - expect(taxLines?.length).toBe(0) - }) - - it("should remove line item tax lines succesfully with selector", async () => { - const [createdCart] = await service.create([ - { - currency_code: "eur", - }, - ]) - - const [item] = await service.addLineItems(createdCart.id, [ - { - quantity: 1, - unit_price: 100, - title: "test", - }, - ]) - - const [taxLine] = await service.addLineItemTaxLines(createdCart.id, [ - { - item_id: item.id, - rate: 20, - code: "TX", - }, - ]) - - expect(taxLine.item_id).toBe(item.id) - - await service.removeLineItemTaxLines({ item_id: item.id }) + await service.softDeleteLineItemTaxLines([taxLine.id]) const taxLines = await service.listLineItemTaxLines({ item_id: item.id, diff --git a/packages/cart/src/services/cart-module.ts b/packages/cart/src/services/cart-module.ts index 9ad39eed45..89408ab719 100644 --- a/packages/cart/src/services/cart-module.ts +++ b/packages/cart/src/services/cart-module.ts @@ -2,7 +2,6 @@ import { CartTypes, Context, DAL, - FilterableLineItemTaxLineProps, ICartModuleService, InternalModuleDeclaration, ModuleJoinerConfig, @@ -419,40 +418,6 @@ export default class CartModuleService< return await this.lineItemService_.update(toUpdate, sharedContext) } - async removeLineItems( - itemIds: string[], - sharedContext?: Context - ): Promise - async removeLineItems(itemIds: string, sharedContext?: Context): Promise - async removeLineItems( - selector: Partial, - sharedContext?: Context - ): Promise - - @InjectTransactionManager("baseRepository_") - async removeLineItems( - itemIdsOrSelector: string | string[] | Partial, - @MedusaContext() sharedContext: Context = {} - ): Promise { - let toDelete: string[] - - if (isObject(itemIdsOrSelector)) { - const items = await this.listLineItems( - { ...itemIdsOrSelector } as Partial, - {}, - sharedContext - ) - - toDelete = items.map((item) => item.id) - } else { - toDelete = Array.isArray(itemIdsOrSelector) - ? itemIdsOrSelector - : [itemIdsOrSelector] - } - - await this.lineItemService_.softDelete(toDelete, sharedContext) - } - async createAddresses( data: CartTypes.CreateAddressDTO, sharedContext?: Context @@ -599,46 +564,6 @@ export default class CartModuleService< ) } - async removeShippingMethods( - methodIds: string[], - sharedContext?: Context - ): Promise - async removeShippingMethods( - methodIds: string, - sharedContext?: Context - ): Promise - async removeShippingMethods( - selector: Partial, - sharedContext?: Context - ): Promise - - @InjectTransactionManager("baseRepository_") - async removeShippingMethods( - methodIdsOrSelector: - | string - | string[] - | Partial, - @MedusaContext() sharedContext: Context = {} - ): Promise { - let toDelete: string[] - if (isObject(methodIdsOrSelector)) { - const methods = await this.listShippingMethods( - { - ...(methodIdsOrSelector as Partial), - }, - {}, - sharedContext - ) - - toDelete = methods.map((m) => m.id) - } else { - toDelete = Array.isArray(methodIdsOrSelector) - ? methodIdsOrSelector - : [methodIdsOrSelector] - } - await this.shippingMethodService_.softDelete(toDelete, sharedContext) - } - async addLineItemAdjustments( adjustments: CartTypes.CreateLineItemAdjustmentDTO[] ): Promise @@ -754,46 +679,6 @@ export default class CartModuleService< }) } - async removeLineItemAdjustments( - adjustmentIds: string[], - sharedContext?: Context - ): Promise - async removeLineItemAdjustments( - adjustmentId: string, - sharedContext?: Context - ): Promise - async removeLineItemAdjustments( - selector: Partial, - sharedContext?: Context - ): Promise - - async removeLineItemAdjustments( - adjustmentIdsOrSelector: - | string - | string[] - | Partial, - @MedusaContext() sharedContext: Context = {} - ): Promise { - let ids: string[] - if (isObject(adjustmentIdsOrSelector)) { - const adjustments = await this.listLineItemAdjustments( - { - ...adjustmentIdsOrSelector, - } as Partial, - { select: ["id"] }, - sharedContext - ) - - ids = adjustments.map((adj) => adj.id) - } else { - ids = Array.isArray(adjustmentIdsOrSelector) - ? adjustmentIdsOrSelector - : [adjustmentIdsOrSelector] - } - - await this.lineItemAdjustmentService_.softDelete(ids, sharedContext) - } - @InjectTransactionManager("baseRepository_") async setShippingMethodAdjustments( cartId: string, @@ -923,46 +808,6 @@ export default class CartModuleService< }) } - async removeShippingMethodAdjustments( - adjustmentIds: string[], - sharedContext?: Context - ): Promise - async removeShippingMethodAdjustments( - adjustmentId: string, - sharedContext?: Context - ): Promise - async removeShippingMethodAdjustments( - selector: Partial, - sharedContext?: Context - ): Promise - - async removeShippingMethodAdjustments( - adjustmentIdsOrSelector: - | string - | string[] - | Partial, - @MedusaContext() sharedContext: Context = {} - ): Promise { - let ids: string[] - if (isObject(adjustmentIdsOrSelector)) { - const adjustments = await this.listShippingMethodAdjustments( - { - ...adjustmentIdsOrSelector, - } as Partial, - { select: ["id"] }, - sharedContext - ) - - ids = adjustments.map((adj) => adj.id) - } else { - ids = Array.isArray(adjustmentIdsOrSelector) - ? adjustmentIdsOrSelector - : [adjustmentIdsOrSelector] - } - - await this.shippingMethodAdjustmentService_.softDelete(ids, sharedContext) - } - addLineItemTaxLines( taxLines: CartTypes.CreateLineItemTaxLineDTO[] ): Promise @@ -1077,46 +922,6 @@ export default class CartModuleService< ) } - removeLineItemTaxLines( - taxLineIds: string[], - sharedContext?: Context - ): Promise - removeLineItemTaxLines( - taxLineIds: string, - sharedContext?: Context - ): Promise - removeLineItemTaxLines( - selector: FilterableLineItemTaxLineProps, - sharedContext?: Context - ): Promise - - async removeLineItemTaxLines( - taxLineIdsOrSelector: - | string - | string[] - | CartTypes.FilterableShippingMethodTaxLineProps, - @MedusaContext() sharedContext: Context = {} - ): Promise { - let ids: string[] - if (isObject(taxLineIdsOrSelector)) { - const taxLines = await this.listLineItemTaxLines( - { - ...(taxLineIdsOrSelector as CartTypes.FilterableLineItemTaxLineProps), - }, - { select: ["id"] }, - sharedContext - ) - - ids = taxLines.map((taxLine) => taxLine.id) - } else { - ids = Array.isArray(taxLineIdsOrSelector) - ? taxLineIdsOrSelector - : [taxLineIdsOrSelector] - } - - await this.lineItemTaxLineService_.softDelete(ids, sharedContext) - } - addShippingMethodTaxLines( taxLines: CartTypes.CreateShippingMethodTaxLineDTO[] ): Promise @@ -1233,44 +1038,4 @@ export default class CartModuleService< populate: true, }) } - - removeShippingMethodTaxLines( - taxLineIds: string[], - sharedContext?: Context - ): Promise - removeShippingMethodTaxLines( - taxLineIds: string, - sharedContext?: Context - ): Promise - removeShippingMethodTaxLines( - selector: Partial, - sharedContext?: Context - ): Promise - - async removeShippingMethodTaxLines( - taxLineIdsOrSelector: - | string - | string[] - | CartTypes.FilterableShippingMethodTaxLineProps, - @MedusaContext() sharedContext: Context = {} - ): Promise { - let ids: string[] - if (isObject(taxLineIdsOrSelector)) { - const taxLines = await this.listShippingMethodTaxLines( - { - ...(taxLineIdsOrSelector as CartTypes.FilterableShippingMethodTaxLineProps), - }, - { select: ["id"] }, - sharedContext - ) - - ids = taxLines.map((taxLine) => taxLine.id) - } else { - ids = Array.isArray(taxLineIdsOrSelector) - ? taxLineIdsOrSelector - : [taxLineIdsOrSelector] - } - - await this.shippingMethodTaxLineService_.softDelete(ids, sharedContext) - } } diff --git a/packages/core-flows/src/definition/cart/steps/add-to-cart.ts b/packages/core-flows/src/definition/cart/steps/add-to-cart.ts index 59b26b34e3..58a0d3c532 100644 --- a/packages/core-flows/src/definition/cart/steps/add-to-cart.ts +++ b/packages/core-flows/src/definition/cart/steps/add-to-cart.ts @@ -26,6 +26,6 @@ export const addToCartStep = createStep( return } - await cartService.removeLineItems(createdLineItems.map((c) => c.id)) + await cartService.deleteLineItems(createdLineItems.map((c) => c.id)) } ) diff --git a/packages/core-flows/src/definition/line-item/steps/delete-line-items.ts b/packages/core-flows/src/definition/line-item/steps/delete-line-items.ts index 66ea68c338..567351d2e8 100644 --- a/packages/core-flows/src/definition/line-item/steps/delete-line-items.ts +++ b/packages/core-flows/src/definition/line-item/steps/delete-line-items.ts @@ -10,7 +10,7 @@ export const deleteLineItemsStep = createStep( ModuleRegistrationName.CART ) - await service.removeLineItems(ids) + await service.softDeleteLineItems(ids) return new StepResponse(void 0, ids) }, diff --git a/packages/medusa/src/api-v2/store/carts/route.ts b/packages/medusa/src/api-v2/store/carts/route.ts index 16d66f1aff..90b7e1f64a 100644 --- a/packages/medusa/src/api-v2/store/carts/route.ts +++ b/packages/medusa/src/api-v2/store/carts/route.ts @@ -1,14 +1,12 @@ import { AuthenticatedMedusaRequest, - MedusaRequest, MedusaResponse, } from "../../../types/routing" -import { CreateCartWorkflowInputDTO } from "@medusajs/types" -import { StorePostCartReq } from "./validators" import { createCartWorkflow } from "@medusajs/core-flows" -import { defaultStoreCartFields } from "../carts/query-config" +import { CreateCartWorkflowInputDTO } from "@medusajs/types" import { remoteQueryObjectFromString } from "@medusajs/utils" +import { defaultStoreCartFields } from "../carts/query-config" export const POST = async ( req: AuthenticatedMedusaRequest, diff --git a/packages/types/src/cart/service.ts b/packages/types/src/cart/service.ts index 6349b6a9d1..e1394969a7 100644 --- a/packages/types/src/cart/service.ts +++ b/packages/types/src/cart/service.ts @@ -140,13 +140,6 @@ export interface ICartModuleService extends IModuleService { sharedContext?: Context ): Promise - removeLineItems(itemIds: string[], sharedContext?: Context): Promise - removeLineItems(itemIds: string, sharedContext?: Context): Promise - removeLineItems( - selector: Partial, - sharedContext?: Context - ): Promise - listShippingMethods( filters: FilterableShippingMethodProps, config: FindConfig, @@ -165,19 +158,6 @@ export interface ICartModuleService extends IModuleService { sharedContext?: Context ): Promise - removeShippingMethods( - methodIds: string[], - sharedContext?: Context - ): Promise - removeShippingMethods( - methodIds: string, - sharedContext?: Context - ): Promise - removeShippingMethods( - selector: Partial, - sharedContext?: Context - ): Promise - listLineItemAdjustments( filters: FilterableLineItemAdjustmentProps, config?: FindConfig, @@ -201,19 +181,6 @@ export interface ICartModuleService extends IModuleService { sharedContext?: Context ): Promise - removeLineItemAdjustments( - adjustmentIds: string[], - sharedContext?: Context - ): Promise - removeLineItemAdjustments( - adjustmentIds: string, - sharedContext?: Context - ): Promise - removeLineItemAdjustments( - selector: Partial, - sharedContext?: Context - ): Promise - listShippingMethodAdjustments( filters: FilterableShippingMethodAdjustmentProps, config?: FindConfig, @@ -241,19 +208,6 @@ export interface ICartModuleService extends IModuleService { sharedContext?: Context ): Promise - removeShippingMethodAdjustments( - adjustmentIds: string[], - sharedContext?: Context - ): Promise - removeShippingMethodAdjustments( - adjustmentId: string, - sharedContext?: Context - ): Promise - removeShippingMethodAdjustments( - selector: Partial, - sharedContext?: Context - ): Promise - listLineItemTaxLines( filters: FilterableLineItemTaxLineProps, config?: FindConfig, @@ -278,19 +232,6 @@ export interface ICartModuleService extends IModuleService { sharedContext?: Context ): Promise - removeLineItemTaxLines( - taxLineIds: string[], - sharedContext?: Context - ): Promise - removeLineItemTaxLines( - taxLineIds: string, - sharedContext?: Context - ): Promise - removeLineItemTaxLines( - selector: FilterableLineItemTaxLineProps, - sharedContext?: Context - ): Promise - listShippingMethodTaxLines( filters: FilterableShippingMethodTaxLineProps, config?: FindConfig, @@ -318,16 +259,33 @@ export interface ICartModuleService extends IModuleService { sharedContext?: Context ): Promise - removeShippingMethodTaxLines( - taxLineIds: string[], + delete(ids: string[], sharedContext?: Context): Promise + delete(id: string, sharedContext?: Context): Promise + deleteLineItems(ids: string[], sharedContext?: Context): Promise + deleteLineItems(id: string, sharedContext?: Context): Promise + deleteShippingMethods(ids: string[], sharedContext?: Context): Promise + deleteShippingMethods(id: string, sharedContext?: Context): Promise + deleteLineItemAdjustments( + ids: string[], sharedContext?: Context ): Promise - removeShippingMethodTaxLines( - taxLineIds: string, + deleteLineItemAdjustments(id: string, sharedContext?: Context): Promise + deleteShippingMethodAdjustments( + ids: string[], sharedContext?: Context ): Promise - removeShippingMethodTaxLines( - selector: FilterableShippingMethodTaxLineProps, + deleteShippingMethodAdjustments( + id: string, + sharedContext?: Context + ): Promise + deleteLineItemTaxLines(ids: string[], sharedContext?: Context): Promise + deleteLineItemTaxLines(id: string, sharedContext?: Context): Promise + deleteShippingMethodTaxLines( + ids: string[], + sharedContext?: Context + ): Promise + deleteShippingMethodTaxLines( + id: string, sharedContext?: Context ): Promise