From b7290e5747c690636a8d6e00eb7cf7852768df8c Mon Sep 17 00:00:00 2001 From: olivermrbl Date: Mon, 15 Jan 2024 15:09:30 +0100 Subject: [PATCH] fix --- packages/cart/src/services/cart-module.ts | 26 +++++++++++++++++------ packages/types/src/cart/service.ts | 7 +++++- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/packages/cart/src/services/cart-module.ts b/packages/cart/src/services/cart-module.ts index b32b73acce5e5..7da34f648623d 100644 --- a/packages/cart/src/services/cart-module.ts +++ b/packages/cart/src/services/cart-module.ts @@ -254,7 +254,7 @@ export default class CartModuleService implements ICartModuleService { addLineItems( data: CartTypes.CreateLineItemForCartDTO, sharedContext?: Context - ): Promise + ): Promise addLineItems( data: CartTypes.CreateLineItemForCartDTO[], sharedContext?: Context @@ -264,6 +264,11 @@ export default class CartModuleService implements ICartModuleService { items: CartTypes.CreateLineItemDTO[], sharedContext?: Context ): Promise + addLineItems( + cartId: string, + items: CartTypes.CreateLineItemDTO, + sharedContext?: Context + ): Promise @InjectManager("baseRepository_") async addLineItems( @@ -271,14 +276,19 @@ export default class CartModuleService implements ICartModuleService { | string | CartTypes.CreateLineItemForCartDTO[] | CartTypes.CreateLineItemForCartDTO, - dataOrSharedContext?: CartTypes.CreateLineItemDTO[] | Context, + dataOrSharedContext?: + | CartTypes.CreateLineItemDTO[] + | CartTypes.CreateLineItemDTO + | Context, @MedusaContext() sharedContext: Context = {} - ): Promise { + ): Promise { let items: LineItem[] = [] if (isString(cartIdOrData)) { items = await this.addLineItems_( cartIdOrData, - dataOrSharedContext as CartTypes.CreateLineItemDTO[], + dataOrSharedContext as + | CartTypes.CreateLineItemDTO[] + | CartTypes.CreateLineItemDTO, sharedContext ) } else if (Array.isArray(cartIdOrData)) { @@ -304,19 +314,21 @@ export default class CartModuleService implements ICartModuleService { @InjectTransactionManager("baseRepository_") protected async addLineItems_( cartId: string, - data: CartTypes.CreateLineItemDTO[], + data: CartTypes.CreateLineItemDTO[] | CartTypes.CreateLineItemDTO, @MedusaContext() sharedContext: Context = {} ): Promise { const cart = await this.retrieve(cartId, { select: ["id"] }, sharedContext) - const items = data.map((item) => { + const items = Array.isArray(data) ? data : [data] + + const toUpdate = items.map((item) => { return { ...item, cart_id: cart.id, } }) - return await this.addLineItemsBulk_(items, sharedContext) + return await this.addLineItemsBulk_(toUpdate, sharedContext) } @InjectTransactionManager("baseRepository_") diff --git a/packages/types/src/cart/service.ts b/packages/types/src/cart/service.ts index e5840d9bd2f15..243b432d44dfd 100644 --- a/packages/types/src/cart/service.ts +++ b/packages/types/src/cart/service.ts @@ -76,7 +76,7 @@ export interface ICartModuleService extends IModuleService { addLineItems( data: CreateLineItemForCartDTO, sharedContext?: Context - ): Promise + ): Promise addLineItems( data: CreateLineItemForCartDTO[], sharedContext?: Context @@ -86,6 +86,11 @@ export interface ICartModuleService extends IModuleService { items: CreateLineItemDTO[], sharedContext?: Context ): Promise + addLineItems( + cartId: string, + items: CreateLineItemDTO, + sharedContext?: Context + ): Promise updateLineItems( data: UpdateLineItemDTO[],