diff --git a/.changeset/khaki-deers-talk.md b/.changeset/khaki-deers-talk.md new file mode 100644 index 0000000000..f8c86c93f3 --- /dev/null +++ b/.changeset/khaki-deers-talk.md @@ -0,0 +1,5 @@ +--- +"@medusajs/types": patch +--- + +Add line items in cart creation 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 980076263c..d444b634ef 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 @@ -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", () => { diff --git a/packages/cart/src/services/cart-module.ts b/packages/cart/src/services/cart-module.ts index b60686ddd6..8c6a84d99b 100644 --- a/packages/cart/src/services/cart-module.ts +++ b/packages/cart/src/services/cart-module.ts @@ -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 { 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 { return await this.lineItemService_.create(data, sharedContext) diff --git a/packages/types/src/cart/mutations.ts b/packages/types/src/cart/mutations.ts index 294180cd89..6416d0a7c9 100644 --- a/packages/types/src/cart/mutations.ts +++ b/packages/types/src/cart/mutations.ts @@ -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 + + items?: CreateLineItemDTO[] } export interface UpdateCartDTO {