1 parent
6500f18b9b
commit
1ed5f918c3
17 files changed
+378
-49
No files matched your search
@@ -258,6 +258,56 @@ describe("Cart Module Service", () => {
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should update a cart with selector successfully", async () => {
|
||||
const [createdCart] = await service.create([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
])
|
||||
|
||||
const [updatedCart] = await service.update(
|
||||
{ id: createdCart.id },
|
||||
{
|
||||
email: "test@email.com",
|
||||
}
|
||||
)
|
||||
|
||||
const [cart] = await service.list({ id: [createdCart.id] })
|
||||
|
||||
expect(cart).toEqual(
|
||||
expect.objectContaining({
|
||||
id: createdCart.id,
|
||||
currency_code: "eur",
|
||||
email: updatedCart.email,
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should update a cart with id successfully", async () => {
|
||||
const [createdCart] = await service.create([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
])
|
||||
|
||||
const updatedCart = await service.update(
|
||||
createdCart.id,
|
||||
{
|
||||
email: "test@email.com",
|
||||
}
|
||||
)
|
||||
|
||||
const [cart] = await service.list({ id: [createdCart.id] })
|
||||
|
||||
expect(cart).toEqual(
|
||||
expect.objectContaining({
|
||||
id: createdCart.id,
|
||||
currency_code: "eur",
|
||||
email: updatedCart.email,
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("delete", () => {
|
||||
|
||||
@@ -190,43 +190,74 @@ export default class CartModuleService<
|
||||
return createdCarts
|
||||
}
|
||||
|
||||
async update(data: CartTypes.UpdateCartDTO[]): Promise<CartTypes.CartDTO[]>
|
||||
async update(
|
||||
data: CartTypes.UpdateCartDTO[],
|
||||
cartId: string,
|
||||
data: CartTypes.UpdateCartDataDTO,
|
||||
sharedContext?: Context
|
||||
): Promise<CartTypes.CartDTO>
|
||||
async update(
|
||||
selector: Partial<CartTypes.CartDTO>,
|
||||
data: CartTypes.UpdateCartDataDTO,
|
||||
sharedContext?: Context
|
||||
): Promise<CartTypes.CartDTO[]>
|
||||
|
||||
async update(
|
||||
data: CartTypes.UpdateCartDTO,
|
||||
sharedContext?: Context
|
||||
): Promise<CartTypes.CartDTO>
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
async update(
|
||||
data: CartTypes.UpdateCartDTO[] | CartTypes.UpdateCartDTO,
|
||||
dataOrIdOrSelector:
|
||||
| CartTypes.UpdateCartDTO[]
|
||||
| string
|
||||
| Partial<CartTypes.CartDTO>,
|
||||
data?: CartTypes.UpdateCartDataDTO,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<CartTypes.CartDTO[] | CartTypes.CartDTO> {
|
||||
const input = Array.isArray(data) ? data : [data]
|
||||
const carts = await this.update_(input, sharedContext)
|
||||
const result = await this.update_(dataOrIdOrSelector, data, sharedContext)
|
||||
|
||||
const result = await this.list(
|
||||
{ id: carts.map((p) => p!.id) },
|
||||
{
|
||||
relations: ["shipping_address", "billing_address"],
|
||||
},
|
||||
sharedContext
|
||||
)
|
||||
const serializedResult = await this.baseRepository_.serialize<
|
||||
CartTypes.CartDTO[]
|
||||
>(result, {
|
||||
populate: true,
|
||||
})
|
||||
|
||||
return (Array.isArray(data) ? result : result[0]) as
|
||||
| CartTypes.CartDTO
|
||||
| CartTypes.CartDTO[]
|
||||
return isString(dataOrIdOrSelector) ? serializedResult[0] : serializedResult
|
||||
}
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
protected async update_(
|
||||
data: CartTypes.UpdateCartDTO[],
|
||||
dataOrIdOrSelector:
|
||||
| CartTypes.UpdateCartDTO[]
|
||||
| string
|
||||
| Partial<CartTypes.CartDTO>,
|
||||
data?: CartTypes.UpdateCartDataDTO,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
) {
|
||||
return await this.cartService_.update(data, sharedContext)
|
||||
let toUpdate: CartTypes.UpdateCartDTO[] = []
|
||||
if (isString(dataOrIdOrSelector)) {
|
||||
toUpdate = [
|
||||
{
|
||||
id: dataOrIdOrSelector,
|
||||
...data,
|
||||
},
|
||||
]
|
||||
} else if (Array.isArray(dataOrIdOrSelector)) {
|
||||
toUpdate = dataOrIdOrSelector
|
||||
} else {
|
||||
const carts = await this.cartService_.list(
|
||||
{ ...dataOrIdOrSelector },
|
||||
{ select: ["id"] },
|
||||
sharedContext
|
||||
)
|
||||
|
||||
toUpdate = carts.map((cart) => {
|
||||
return {
|
||||
...data,
|
||||
id: cart.id,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
const result = await this.cartService_.update(toUpdate, sharedContext)
|
||||
return result
|
||||
}
|
||||
|
||||
addLineItems(
|
||||
|
||||
@@ -14,12 +14,12 @@ export interface CreateCartDTO {
|
||||
|
||||
export interface UpdateCartDTO {
|
||||
id: string
|
||||
region_id?: string
|
||||
customer_id?: string
|
||||
sales_channel_id?: string
|
||||
email?: string
|
||||
currency_code?: string
|
||||
metadata?: Record<string, unknown>
|
||||
region_id?: string | null
|
||||
customer_id?: string | null
|
||||
sales_channel_id?: string | null
|
||||
email?: string | null
|
||||
currency_code?: string | null
|
||||
metadata?: Record<string, unknown> | null
|
||||
|
||||
adjustments?: (CreateLineItemAdjustmentDTO | UpdateLineItemAdjustmentDTO)[]
|
||||
}
|
||||
Reference in new issue
Block a user