Merge branch 'develop' into feat/payment-module-models
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@medusajs/types": patch
|
||||
---
|
||||
|
||||
Add line items in cart creation
|
||||
@@ -124,6 +124,91 @@ describe("Cart Module Service", () => {
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should create a cart with items", async () => {
|
||||
const createdCart = await service.create({
|
||||
currency_code: "eur",
|
||||
items: [
|
||||
{
|
||||
title: "test",
|
||||
quantity: 1,
|
||||
unit_price: 100,
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
const cart = await service.retrieve(createdCart.id, {
|
||||
relations: ["items"],
|
||||
})
|
||||
|
||||
expect(cart).toEqual(
|
||||
expect.objectContaining({
|
||||
id: createdCart.id,
|
||||
currency_code: "eur",
|
||||
items: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
title: "test",
|
||||
unit_price: 100,
|
||||
}),
|
||||
]),
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should create multiple carts with items", async () => {
|
||||
const createdCarts = await service.create([
|
||||
{
|
||||
currency_code: "eur",
|
||||
items: [
|
||||
{
|
||||
title: "test",
|
||||
quantity: 1,
|
||||
unit_price: 100,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
currency_code: "usd",
|
||||
items: [
|
||||
{
|
||||
title: "test-2",
|
||||
quantity: 2,
|
||||
unit_price: 200,
|
||||
},
|
||||
],
|
||||
},
|
||||
])
|
||||
|
||||
const carts = await service.list(
|
||||
{ id: createdCarts.map((c) => c.id) },
|
||||
{
|
||||
relations: ["items"],
|
||||
}
|
||||
)
|
||||
|
||||
expect(carts).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
currency_code: "eur",
|
||||
items: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
title: "test",
|
||||
unit_price: 100,
|
||||
}),
|
||||
]),
|
||||
}),
|
||||
expect.objectContaining({
|
||||
currency_code: "usd",
|
||||
items: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
title: "test-2",
|
||||
unit_price: 200,
|
||||
}),
|
||||
]),
|
||||
}),
|
||||
])
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("update", () => {
|
||||
|
||||
@@ -17,8 +17,8 @@ import {
|
||||
isObject,
|
||||
isString,
|
||||
} from "@medusajs/utils"
|
||||
import { LineItem, ShippingMethod } from "@models"
|
||||
import { UpdateLineItemDTO } from "@types"
|
||||
import { Cart, LineItem, ShippingMethod } from "@models"
|
||||
import { CreateLineItemDTO, UpdateLineItemDTO } from "@types"
|
||||
import { joinerConfig } from "../joiner-config"
|
||||
import * as services from "../services"
|
||||
|
||||
@@ -141,7 +141,30 @@ export default class CartModuleService implements ICartModuleService {
|
||||
data: CartTypes.CreateCartDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
) {
|
||||
return await this.cartService_.create(data, sharedContext)
|
||||
const lineItemsToCreate: CreateLineItemDTO[] = []
|
||||
const createdCarts: Cart[] = []
|
||||
for (const { items, ...cart } of data) {
|
||||
const [created] = await this.cartService_.create([cart], sharedContext)
|
||||
|
||||
createdCarts.push(created)
|
||||
|
||||
if (items?.length) {
|
||||
const cartItems = items.map((item) => {
|
||||
return {
|
||||
...item,
|
||||
cart_id: created.id,
|
||||
}
|
||||
})
|
||||
|
||||
lineItemsToCreate.push(...cartItems)
|
||||
}
|
||||
}
|
||||
|
||||
if (lineItemsToCreate.length) {
|
||||
await this.addLineItemsBulk_(lineItemsToCreate, sharedContext)
|
||||
}
|
||||
|
||||
return createdCarts
|
||||
}
|
||||
|
||||
async update(
|
||||
@@ -324,7 +347,7 @@ export default class CartModuleService implements ICartModuleService {
|
||||
): Promise<LineItem[]> {
|
||||
const cart = await this.retrieve(cartId, { select: ["id"] }, sharedContext)
|
||||
|
||||
const toUpdate = items.map((item) => {
|
||||
const toUpdate: CreateLineItemDTO[] = items.map((item) => {
|
||||
return {
|
||||
...item,
|
||||
cart_id: cart.id,
|
||||
@@ -336,7 +359,7 @@ export default class CartModuleService implements ICartModuleService {
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
protected async addLineItemsBulk_(
|
||||
data: CartTypes.CreateLineItemForCartDTO[],
|
||||
data: CreateLineItemDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<LineItem[]> {
|
||||
return await this.lineItemService_.create(data, sharedContext)
|
||||
|
||||
@@ -25,17 +25,15 @@ export interface CreateCartDTO {
|
||||
region_id?: string
|
||||
customer_id?: string
|
||||
sales_channel_id?: string
|
||||
|
||||
email?: string
|
||||
currency_code: string
|
||||
|
||||
shipping_address_id?: string
|
||||
billing_address_id?: string
|
||||
|
||||
shipping_address?: CreateAddressDTO | UpdateAddressDTO
|
||||
billing_address?: CreateAddressDTO | UpdateAddressDTO
|
||||
|
||||
metadata?: Record<string, unknown>
|
||||
|
||||
items?: CreateLineItemDTO[]
|
||||
}
|
||||
|
||||
export interface UpdateCartDTO {
|
||||
|
||||
Reference in New Issue
Block a user