Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
olivermrbl committed Jan 15, 2024
1 parent 3eab5e0 commit b7290e5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
26 changes: 19 additions & 7 deletions packages/cart/src/services/cart-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ export default class CartModuleService implements ICartModuleService {
addLineItems(
data: CartTypes.CreateLineItemForCartDTO,
sharedContext?: Context
): Promise<CartTypes.CartLineItemDTO[]>
): Promise<CartTypes.CartLineItemDTO>
addLineItems(
data: CartTypes.CreateLineItemForCartDTO[],
sharedContext?: Context
Expand All @@ -264,21 +264,31 @@ export default class CartModuleService implements ICartModuleService {
items: CartTypes.CreateLineItemDTO[],
sharedContext?: Context
): Promise<CartTypes.CartLineItemDTO[]>
addLineItems(
cartId: string,
items: CartTypes.CreateLineItemDTO,
sharedContext?: Context
): Promise<CartTypes.CartLineItemDTO>

@InjectManager("baseRepository_")
async addLineItems(
cartIdOrData:
| string
| CartTypes.CreateLineItemForCartDTO[]
| CartTypes.CreateLineItemForCartDTO,
dataOrSharedContext?: CartTypes.CreateLineItemDTO[] | Context,
dataOrSharedContext?:
| CartTypes.CreateLineItemDTO[]
| CartTypes.CreateLineItemDTO
| Context,
@MedusaContext() sharedContext: Context = {}
): Promise<CartTypes.CartLineItemDTO[]> {
): Promise<CartTypes.CartLineItemDTO[] | CartTypes.CartLineItemDTO> {
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)) {
Expand All @@ -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<LineItem[]> {
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_")
Expand Down
7 changes: 6 additions & 1 deletion packages/types/src/cart/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export interface ICartModuleService extends IModuleService {
addLineItems(
data: CreateLineItemForCartDTO,
sharedContext?: Context
): Promise<CartLineItemDTO[]>
): Promise<CartLineItemDTO>
addLineItems(
data: CreateLineItemForCartDTO[],
sharedContext?: Context
Expand All @@ -86,6 +86,11 @@ export interface ICartModuleService extends IModuleService {
items: CreateLineItemDTO[],
sharedContext?: Context
): Promise<CartLineItemDTO[]>
addLineItems(
cartId: string,
items: CreateLineItemDTO,
sharedContext?: Context
): Promise<CartLineItemDTO>

updateLineItems(
data: UpdateLineItemDTO[],
Expand Down

0 comments on commit b7290e5

Please sign in to comment.