chore(cart): Use module native soft-delete/delete methods (#6491)

This commit is contained in:
Oli Juhl
2024-02-28 10:45:56 +00:00
committed by GitHub
parent d60f3adc03
commit c5d35ec7f2
7 changed files with 33 additions and 424 deletions
@@ -1,7 +1,6 @@
import { import {
addToCartWorkflow, addToCartWorkflow,
createCartWorkflow, createCartWorkflow,
deleteLineItemsStepId, deleteLineItemsStepId,
deleteLineItemsWorkflow, deleteLineItemsWorkflow,
findOrCreateCustomerStepId, findOrCreateCustomerStepId,
@@ -705,7 +705,7 @@ describe("Cart Module Service", () => {
expect(item.title).toBe("test") expect(item.title).toBe("test")
await service.removeLineItems([item.id]) await service.softDeleteLineItems([item.id])
const cart = await service.retrieve(createdCart.id, { const cart = await service.retrieve(createdCart.id, {
relations: ["items"], 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, { const cart = await service.retrieve(createdCart.id, {
relations: ["items"], relations: ["items"],
@@ -847,7 +847,7 @@ describe("Cart Module Service", () => {
expect(method.id).not.toBe(null) expect(method.id).not.toBe(null)
await service.removeShippingMethods(method.id) await service.softDeleteShippingMethods([method.id])
const cart = await service.retrieve(createdCart.id, { const cart = await service.retrieve(createdCart.id, {
relations: ["shipping_methods"], relations: ["shipping_methods"],
@@ -1287,43 +1287,7 @@ describe("Cart Module Service", () => {
expect(adjustment.item_id).toBe(item.id) expect(adjustment.item_id).toBe(item.id)
await service.removeLineItemAdjustments(adjustment.id) await service.softDeleteLineItemAdjustments([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 })
const adjustments = await service.listLineItemAdjustments({ const adjustments = await service.listLineItemAdjustments({
item_id: item.id, item_id: item.id,
@@ -1831,7 +1795,7 @@ describe("Cart Module Service", () => {
expect(adjustment.shipping_method_id).toBe(method.id) expect(adjustment.shipping_method_id).toBe(method.id)
await service.removeShippingMethodAdjustments(adjustment.id) await service.softDeleteShippingMethodAdjustments([adjustment.id])
const adjustments = await service.listShippingMethodAdjustments({ const adjustments = await service.listShippingMethodAdjustments({
shipping_method_id: method.id, shipping_method_id: method.id,
@@ -1839,47 +1803,6 @@ describe("Cart Module Service", () => {
expect(adjustments?.length).toBe(0) 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", () => { describe("setLineItemTaxLines", () => {
@@ -2400,41 +2323,7 @@ describe("Cart Module Service", () => {
expect(taxLine.item_id).toBe(item.id) expect(taxLine.item_id).toBe(item.id)
await service.removeLineItemTaxLines(taxLine.id) await service.softDeleteLineItemTaxLines([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 })
const taxLines = await service.listLineItemTaxLines({ const taxLines = await service.listLineItemTaxLines({
item_id: item.id, item_id: item.id,
-235
View File
@@ -2,7 +2,6 @@ import {
CartTypes, CartTypes,
Context, Context,
DAL, DAL,
FilterableLineItemTaxLineProps,
ICartModuleService, ICartModuleService,
InternalModuleDeclaration, InternalModuleDeclaration,
ModuleJoinerConfig, ModuleJoinerConfig,
@@ -419,40 +418,6 @@ export default class CartModuleService<
return await this.lineItemService_.update(toUpdate, sharedContext) return await this.lineItemService_.update(toUpdate, sharedContext)
} }
async removeLineItems(
itemIds: string[],
sharedContext?: Context
): Promise<void>
async removeLineItems(itemIds: string, sharedContext?: Context): Promise<void>
async removeLineItems(
selector: Partial<CartTypes.CartLineItemDTO>,
sharedContext?: Context
): Promise<void>
@InjectTransactionManager("baseRepository_")
async removeLineItems(
itemIdsOrSelector: string | string[] | Partial<CartTypes.CartLineItemDTO>,
@MedusaContext() sharedContext: Context = {}
): Promise<void> {
let toDelete: string[]
if (isObject(itemIdsOrSelector)) {
const items = await this.listLineItems(
{ ...itemIdsOrSelector } as Partial<CartTypes.CartLineItemDTO>,
{},
sharedContext
)
toDelete = items.map((item) => item.id)
} else {
toDelete = Array.isArray(itemIdsOrSelector)
? itemIdsOrSelector
: [itemIdsOrSelector]
}
await this.lineItemService_.softDelete(toDelete, sharedContext)
}
async createAddresses( async createAddresses(
data: CartTypes.CreateAddressDTO, data: CartTypes.CreateAddressDTO,
sharedContext?: Context sharedContext?: Context
@@ -599,46 +564,6 @@ export default class CartModuleService<
) )
} }
async removeShippingMethods(
methodIds: string[],
sharedContext?: Context
): Promise<void>
async removeShippingMethods(
methodIds: string,
sharedContext?: Context
): Promise<void>
async removeShippingMethods(
selector: Partial<CartTypes.CartShippingMethodDTO>,
sharedContext?: Context
): Promise<void>
@InjectTransactionManager("baseRepository_")
async removeShippingMethods(
methodIdsOrSelector:
| string
| string[]
| Partial<CartTypes.CartShippingMethodDTO>,
@MedusaContext() sharedContext: Context = {}
): Promise<void> {
let toDelete: string[]
if (isObject(methodIdsOrSelector)) {
const methods = await this.listShippingMethods(
{
...(methodIdsOrSelector as Partial<CartTypes.CartShippingMethodDTO>),
},
{},
sharedContext
)
toDelete = methods.map((m) => m.id)
} else {
toDelete = Array.isArray(methodIdsOrSelector)
? methodIdsOrSelector
: [methodIdsOrSelector]
}
await this.shippingMethodService_.softDelete(toDelete, sharedContext)
}
async addLineItemAdjustments( async addLineItemAdjustments(
adjustments: CartTypes.CreateLineItemAdjustmentDTO[] adjustments: CartTypes.CreateLineItemAdjustmentDTO[]
): Promise<CartTypes.LineItemAdjustmentDTO[]> ): Promise<CartTypes.LineItemAdjustmentDTO[]>
@@ -754,46 +679,6 @@ export default class CartModuleService<
}) })
} }
async removeLineItemAdjustments(
adjustmentIds: string[],
sharedContext?: Context
): Promise<void>
async removeLineItemAdjustments(
adjustmentId: string,
sharedContext?: Context
): Promise<void>
async removeLineItemAdjustments(
selector: Partial<CartTypes.LineItemAdjustmentDTO>,
sharedContext?: Context
): Promise<void>
async removeLineItemAdjustments(
adjustmentIdsOrSelector:
| string
| string[]
| Partial<CartTypes.LineItemAdjustmentDTO>,
@MedusaContext() sharedContext: Context = {}
): Promise<void> {
let ids: string[]
if (isObject(adjustmentIdsOrSelector)) {
const adjustments = await this.listLineItemAdjustments(
{
...adjustmentIdsOrSelector,
} as Partial<CartTypes.LineItemAdjustmentDTO>,
{ select: ["id"] },
sharedContext
)
ids = adjustments.map((adj) => adj.id)
} else {
ids = Array.isArray(adjustmentIdsOrSelector)
? adjustmentIdsOrSelector
: [adjustmentIdsOrSelector]
}
await this.lineItemAdjustmentService_.softDelete(ids, sharedContext)
}
@InjectTransactionManager("baseRepository_") @InjectTransactionManager("baseRepository_")
async setShippingMethodAdjustments( async setShippingMethodAdjustments(
cartId: string, cartId: string,
@@ -923,46 +808,6 @@ export default class CartModuleService<
}) })
} }
async removeShippingMethodAdjustments(
adjustmentIds: string[],
sharedContext?: Context
): Promise<void>
async removeShippingMethodAdjustments(
adjustmentId: string,
sharedContext?: Context
): Promise<void>
async removeShippingMethodAdjustments(
selector: Partial<CartTypes.ShippingMethodAdjustmentDTO>,
sharedContext?: Context
): Promise<void>
async removeShippingMethodAdjustments(
adjustmentIdsOrSelector:
| string
| string[]
| Partial<CartTypes.ShippingMethodAdjustmentDTO>,
@MedusaContext() sharedContext: Context = {}
): Promise<void> {
let ids: string[]
if (isObject(adjustmentIdsOrSelector)) {
const adjustments = await this.listShippingMethodAdjustments(
{
...adjustmentIdsOrSelector,
} as Partial<CartTypes.ShippingMethodAdjustmentDTO>,
{ select: ["id"] },
sharedContext
)
ids = adjustments.map((adj) => adj.id)
} else {
ids = Array.isArray(adjustmentIdsOrSelector)
? adjustmentIdsOrSelector
: [adjustmentIdsOrSelector]
}
await this.shippingMethodAdjustmentService_.softDelete(ids, sharedContext)
}
addLineItemTaxLines( addLineItemTaxLines(
taxLines: CartTypes.CreateLineItemTaxLineDTO[] taxLines: CartTypes.CreateLineItemTaxLineDTO[]
): Promise<CartTypes.LineItemTaxLineDTO[]> ): Promise<CartTypes.LineItemTaxLineDTO[]>
@@ -1077,46 +922,6 @@ export default class CartModuleService<
) )
} }
removeLineItemTaxLines(
taxLineIds: string[],
sharedContext?: Context
): Promise<void>
removeLineItemTaxLines(
taxLineIds: string,
sharedContext?: Context
): Promise<void>
removeLineItemTaxLines(
selector: FilterableLineItemTaxLineProps,
sharedContext?: Context
): Promise<void>
async removeLineItemTaxLines(
taxLineIdsOrSelector:
| string
| string[]
| CartTypes.FilterableShippingMethodTaxLineProps,
@MedusaContext() sharedContext: Context = {}
): Promise<void> {
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( addShippingMethodTaxLines(
taxLines: CartTypes.CreateShippingMethodTaxLineDTO[] taxLines: CartTypes.CreateShippingMethodTaxLineDTO[]
): Promise<CartTypes.ShippingMethodTaxLineDTO[]> ): Promise<CartTypes.ShippingMethodTaxLineDTO[]>
@@ -1233,44 +1038,4 @@ export default class CartModuleService<
populate: true, populate: true,
}) })
} }
removeShippingMethodTaxLines(
taxLineIds: string[],
sharedContext?: Context
): Promise<void>
removeShippingMethodTaxLines(
taxLineIds: string,
sharedContext?: Context
): Promise<void>
removeShippingMethodTaxLines(
selector: Partial<CartTypes.ShippingMethodTaxLineDTO>,
sharedContext?: Context
): Promise<void>
async removeShippingMethodTaxLines(
taxLineIdsOrSelector:
| string
| string[]
| CartTypes.FilterableShippingMethodTaxLineProps,
@MedusaContext() sharedContext: Context = {}
): Promise<void> {
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)
}
} }
@@ -26,6 +26,6 @@ export const addToCartStep = createStep(
return return
} }
await cartService.removeLineItems(createdLineItems.map((c) => c.id)) await cartService.deleteLineItems(createdLineItems.map((c) => c.id))
} }
) )
@@ -10,7 +10,7 @@ export const deleteLineItemsStep = createStep(
ModuleRegistrationName.CART ModuleRegistrationName.CART
) )
await service.removeLineItems(ids) await service.softDeleteLineItems(ids)
return new StepResponse(void 0, ids) return new StepResponse(void 0, ids)
}, },
@@ -1,14 +1,12 @@
import { import {
AuthenticatedMedusaRequest, AuthenticatedMedusaRequest,
MedusaRequest,
MedusaResponse, MedusaResponse,
} from "../../../types/routing" } from "../../../types/routing"
import { CreateCartWorkflowInputDTO } from "@medusajs/types"
import { StorePostCartReq } from "./validators"
import { createCartWorkflow } from "@medusajs/core-flows" import { createCartWorkflow } from "@medusajs/core-flows"
import { defaultStoreCartFields } from "../carts/query-config" import { CreateCartWorkflowInputDTO } from "@medusajs/types"
import { remoteQueryObjectFromString } from "@medusajs/utils" import { remoteQueryObjectFromString } from "@medusajs/utils"
import { defaultStoreCartFields } from "../carts/query-config"
export const POST = async ( export const POST = async (
req: AuthenticatedMedusaRequest<CreateCartWorkflowInputDTO>, req: AuthenticatedMedusaRequest<CreateCartWorkflowInputDTO>,
+23 -65
View File
@@ -140,13 +140,6 @@ export interface ICartModuleService extends IModuleService {
sharedContext?: Context sharedContext?: Context
): Promise<CartLineItemDTO> ): Promise<CartLineItemDTO>
removeLineItems(itemIds: string[], sharedContext?: Context): Promise<void>
removeLineItems(itemIds: string, sharedContext?: Context): Promise<void>
removeLineItems(
selector: Partial<CartLineItemDTO>,
sharedContext?: Context
): Promise<void>
listShippingMethods( listShippingMethods(
filters: FilterableShippingMethodProps, filters: FilterableShippingMethodProps,
config: FindConfig<CartShippingMethodDTO>, config: FindConfig<CartShippingMethodDTO>,
@@ -165,19 +158,6 @@ export interface ICartModuleService extends IModuleService {
sharedContext?: Context sharedContext?: Context
): Promise<CartShippingMethodDTO[]> ): Promise<CartShippingMethodDTO[]>
removeShippingMethods(
methodIds: string[],
sharedContext?: Context
): Promise<void>
removeShippingMethods(
methodIds: string,
sharedContext?: Context
): Promise<void>
removeShippingMethods(
selector: Partial<CartShippingMethodDTO>,
sharedContext?: Context
): Promise<void>
listLineItemAdjustments( listLineItemAdjustments(
filters: FilterableLineItemAdjustmentProps, filters: FilterableLineItemAdjustmentProps,
config?: FindConfig<LineItemAdjustmentDTO>, config?: FindConfig<LineItemAdjustmentDTO>,
@@ -201,19 +181,6 @@ export interface ICartModuleService extends IModuleService {
sharedContext?: Context sharedContext?: Context
): Promise<LineItemAdjustmentDTO[]> ): Promise<LineItemAdjustmentDTO[]>
removeLineItemAdjustments(
adjustmentIds: string[],
sharedContext?: Context
): Promise<void>
removeLineItemAdjustments(
adjustmentIds: string,
sharedContext?: Context
): Promise<void>
removeLineItemAdjustments(
selector: Partial<LineItemAdjustmentDTO>,
sharedContext?: Context
): Promise<void>
listShippingMethodAdjustments( listShippingMethodAdjustments(
filters: FilterableShippingMethodAdjustmentProps, filters: FilterableShippingMethodAdjustmentProps,
config?: FindConfig<ShippingMethodAdjustmentDTO>, config?: FindConfig<ShippingMethodAdjustmentDTO>,
@@ -241,19 +208,6 @@ export interface ICartModuleService extends IModuleService {
sharedContext?: Context sharedContext?: Context
): Promise<ShippingMethodAdjustmentDTO[]> ): Promise<ShippingMethodAdjustmentDTO[]>
removeShippingMethodAdjustments(
adjustmentIds: string[],
sharedContext?: Context
): Promise<void>
removeShippingMethodAdjustments(
adjustmentId: string,
sharedContext?: Context
): Promise<void>
removeShippingMethodAdjustments(
selector: Partial<ShippingMethodAdjustmentDTO>,
sharedContext?: Context
): Promise<void>
listLineItemTaxLines( listLineItemTaxLines(
filters: FilterableLineItemTaxLineProps, filters: FilterableLineItemTaxLineProps,
config?: FindConfig<LineItemTaxLineDTO>, config?: FindConfig<LineItemTaxLineDTO>,
@@ -278,19 +232,6 @@ export interface ICartModuleService extends IModuleService {
sharedContext?: Context sharedContext?: Context
): Promise<LineItemTaxLineDTO[]> ): Promise<LineItemTaxLineDTO[]>
removeLineItemTaxLines(
taxLineIds: string[],
sharedContext?: Context
): Promise<void>
removeLineItemTaxLines(
taxLineIds: string,
sharedContext?: Context
): Promise<void>
removeLineItemTaxLines(
selector: FilterableLineItemTaxLineProps,
sharedContext?: Context
): Promise<void>
listShippingMethodTaxLines( listShippingMethodTaxLines(
filters: FilterableShippingMethodTaxLineProps, filters: FilterableShippingMethodTaxLineProps,
config?: FindConfig<ShippingMethodTaxLineDTO>, config?: FindConfig<ShippingMethodTaxLineDTO>,
@@ -318,16 +259,33 @@ export interface ICartModuleService extends IModuleService {
sharedContext?: Context sharedContext?: Context
): Promise<ShippingMethodTaxLineDTO[]> ): Promise<ShippingMethodTaxLineDTO[]>
removeShippingMethodTaxLines( delete(ids: string[], sharedContext?: Context): Promise<void>
taxLineIds: string[], delete(id: string, sharedContext?: Context): Promise<void>
deleteLineItems(ids: string[], sharedContext?: Context): Promise<void>
deleteLineItems(id: string, sharedContext?: Context): Promise<void>
deleteShippingMethods(ids: string[], sharedContext?: Context): Promise<void>
deleteShippingMethods(id: string, sharedContext?: Context): Promise<void>
deleteLineItemAdjustments(
ids: string[],
sharedContext?: Context sharedContext?: Context
): Promise<void> ): Promise<void>
removeShippingMethodTaxLines( deleteLineItemAdjustments(id: string, sharedContext?: Context): Promise<void>
taxLineIds: string, deleteShippingMethodAdjustments(
ids: string[],
sharedContext?: Context sharedContext?: Context
): Promise<void> ): Promise<void>
removeShippingMethodTaxLines( deleteShippingMethodAdjustments(
selector: FilterableShippingMethodTaxLineProps, id: string,
sharedContext?: Context
): Promise<void>
deleteLineItemTaxLines(ids: string[], sharedContext?: Context): Promise<void>
deleteLineItemTaxLines(id: string, sharedContext?: Context): Promise<void>
deleteShippingMethodTaxLines(
ids: string[],
sharedContext?: Context
): Promise<void>
deleteShippingMethodTaxLines(
id: string,
sharedContext?: Context sharedContext?: Context
): Promise<void> ): Promise<void>