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

This commit is contained in:
Oli Juhl
2024-02-28 11:45:56 +01:00
committed by GitHub
parent d60f3adc03
commit c5d35ec7f2
7 changed files with 33 additions and 424 deletions

View File

@@ -1,7 +1,6 @@
import {
addToCartWorkflow,
createCartWorkflow,
deleteLineItemsStepId,
deleteLineItemsWorkflow,
findOrCreateCustomerStepId,

View File

@@ -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,

View File

@@ -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<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(
data: CartTypes.CreateAddressDTO,
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(
adjustments: CartTypes.CreateLineItemAdjustmentDTO[]
): 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_")
async setShippingMethodAdjustments(
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(
taxLines: CartTypes.CreateLineItemTaxLineDTO[]
): 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(
taxLines: CartTypes.CreateShippingMethodTaxLineDTO[]
): Promise<CartTypes.ShippingMethodTaxLineDTO[]>
@@ -1233,44 +1038,4 @@ export default class CartModuleService<
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)
}
}

View File

@@ -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))
}
)

View File

@@ -10,7 +10,7 @@ export const deleteLineItemsStep = createStep(
ModuleRegistrationName.CART
)
await service.removeLineItems(ids)
await service.softDeleteLineItems(ids)
return new StepResponse(void 0, ids)
},

View File

@@ -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<CreateCartWorkflowInputDTO>,

View File

@@ -140,13 +140,6 @@ export interface ICartModuleService extends IModuleService {
sharedContext?: Context
): 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(
filters: FilterableShippingMethodProps,
config: FindConfig<CartShippingMethodDTO>,
@@ -165,19 +158,6 @@ export interface ICartModuleService extends IModuleService {
sharedContext?: Context
): 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(
filters: FilterableLineItemAdjustmentProps,
config?: FindConfig<LineItemAdjustmentDTO>,
@@ -201,19 +181,6 @@ export interface ICartModuleService extends IModuleService {
sharedContext?: Context
): 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(
filters: FilterableShippingMethodAdjustmentProps,
config?: FindConfig<ShippingMethodAdjustmentDTO>,
@@ -241,19 +208,6 @@ export interface ICartModuleService extends IModuleService {
sharedContext?: Context
): 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(
filters: FilterableLineItemTaxLineProps,
config?: FindConfig<LineItemTaxLineDTO>,
@@ -278,19 +232,6 @@ export interface ICartModuleService extends IModuleService {
sharedContext?: Context
): Promise<LineItemTaxLineDTO[]>
removeLineItemTaxLines(
taxLineIds: string[],
sharedContext?: Context
): Promise<void>
removeLineItemTaxLines(
taxLineIds: string,
sharedContext?: Context
): Promise<void>
removeLineItemTaxLines(
selector: FilterableLineItemTaxLineProps,
sharedContext?: Context
): Promise<void>
listShippingMethodTaxLines(
filters: FilterableShippingMethodTaxLineProps,
config?: FindConfig<ShippingMethodTaxLineDTO>,
@@ -318,16 +259,33 @@ export interface ICartModuleService extends IModuleService {
sharedContext?: Context
): Promise<ShippingMethodTaxLineDTO[]>
removeShippingMethodTaxLines(
taxLineIds: string[],
delete(ids: string[], sharedContext?: Context): Promise<void>
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
): Promise<void>
removeShippingMethodTaxLines(
taxLineIds: string,
deleteLineItemAdjustments(id: string, sharedContext?: Context): Promise<void>
deleteShippingMethodAdjustments(
ids: string[],
sharedContext?: Context
): Promise<void>
removeShippingMethodTaxLines(
selector: FilterableShippingMethodTaxLineProps,
deleteShippingMethodAdjustments(
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
): Promise<void>