Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
olivermrbl committed Jan 15, 2024
1 parent d905e0c commit 3eab5e0
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -372,10 +372,10 @@ describe("Cart Module Service", () => {
])
.catch((e) => e)

expect(error.message).toContain("Failed to create line items. Ensure you are passing valid data, including valid cart id(s)")
expect(error.message).toContain("Cart with id: foo was not found")
})

it("should throw an error when required params are not passed", async () => {
it("should throw an error when required params are not passed adding to a single cart", async () => {
const [createdCart] = await service.create([
{
currency_code: "eur",
Expand All @@ -392,7 +392,29 @@ describe("Cart Module Service", () => {
.catch((e) => e)

expect(error.message).toContain(
"Failed to create line items. Ensure you are passing valid data, including valid cart id(s)"
"Value for LineItem.unit_price is required, 'undefined' found"
)
})

it("should throw a generic error when required params are not passed using bulk add method", async () => {
const [createdCart] = await service.create([
{
currency_code: "eur",
},
])

const error = await service
.addLineItems([
{
cart_id: createdCart.id,
quantity: 1,
title: "test",
},
] as any)
.catch((e) => e)

expect(error.message).toContain(
"Value for LineItem.unit_price is required, 'undefined' found"
)
})
})
Expand Down
108 changes: 47 additions & 61 deletions packages/cart/src/services/cart-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import {
InjectManager,
InjectTransactionManager,
MedusaContext,
MedusaError,
isObject,
isString,
} from "@medusajs/utils"
import { LineItem } from "@models"
import { joinerConfig } from "../joiner-config"
import * as services from "../services"

Expand Down Expand Up @@ -274,24 +274,30 @@ export default class CartModuleService implements ICartModuleService {
dataOrSharedContext?: CartTypes.CreateLineItemDTO[] | Context,
@MedusaContext() sharedContext: Context = {}
): Promise<CartTypes.CartLineItemDTO[]> {
let items: LineItem[] = []
if (isString(cartIdOrData)) {
return await this.addLineItems_(
items = await this.addLineItems_(
cartIdOrData,
dataOrSharedContext as CartTypes.CreateLineItemDTO[],
sharedContext
)
}

if (Array.isArray(cartIdOrData)) {
return await this.addLineItemsBulk_(
} else if (Array.isArray(cartIdOrData)) {
items = await this.addLineItemsBulk_(
cartIdOrData,
dataOrSharedContext as Context
)
} else {
items = await this.addLineItemsBulk_(
[cartIdOrData],
dataOrSharedContext as Context
)
}

return await this.addLineItemsBulk_(
[cartIdOrData],
dataOrSharedContext as Context
return await this.baseRepository_.serialize<CartTypes.CartLineItemDTO[]>(
items,
{
populate: true,
}
)
}

Expand All @@ -300,11 +306,13 @@ export default class CartModuleService implements ICartModuleService {
cartId: string,
data: CartTypes.CreateLineItemDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<CartTypes.CartLineItemDTO[]> {
): Promise<LineItem[]> {
const cart = await this.retrieve(cartId, { select: ["id"] }, sharedContext)

const items = data.map((item) => {
return {
...item,
cart_id: cartId,
cart_id: cart.id,
}
})

Expand All @@ -315,21 +323,8 @@ export default class CartModuleService implements ICartModuleService {
protected async addLineItemsBulk_(
data: CartTypes.CreateLineItemForCartDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<CartTypes.CartLineItemDTO[]> {
const items = await this.lineItemService_
.create(data, sharedContext)
.catch((e) => {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
"Failed to create line items. Ensure you are passing valid data, including valid cart id(s)"
)
})

return await this.listLineItems(
{ id: items.flat().map((c) => c.id) },
{},
sharedContext
)
): Promise<LineItem[]> {
return await this.lineItemService_.create(data, sharedContext)
}

updateLineItems(
Expand Down Expand Up @@ -357,30 +352,36 @@ export default class CartModuleService implements ICartModuleService {
| CartTypes.UpdateLineItemDTO[]
| Partial<CartTypes.UpdateLineItemDTO>
| Context,
@MedusaContext() sharedContext: Context = {}
@MedusaContext() sharedContext: Context = {}
): Promise<CartTypes.CartLineItemDTO[]> {
// Case: Single cart update
let items: LineItem[] = []
if (isString(cartIdOrDataOrSelector)) {
return await this.updateCartLineItems_(
// Case: Single cart update
items = await this.updateCartLineItems_(
cartIdOrDataOrSelector,
dataOrSharedContext as Partial<CartTypes.UpdateLineItemDTO>,
sharedContext
)
}

// Case: Bulk update
if (Array.isArray(cartIdOrDataOrSelector)) {
return await this.updateLineItemsBulk_(
} else if (Array.isArray(cartIdOrDataOrSelector)) {
// Case: Bulk update
items = await this.updateLineItemsBulk_(
cartIdOrDataOrSelector,
dataOrSharedContext as Context
)
} else {
// Case: Selector update
items = await this.updateSelectorLineItems_(
cartIdOrDataOrSelector as Partial<CartTypes.CartLineItemDTO>,
dataOrSharedContext as CartTypes.UpdateLineItemDTO,
sharedContext
)
}

// Case: Selector update
return await this.updateSelectorLineItems_(
cartIdOrDataOrSelector,
dataOrSharedContext as CartTypes.UpdateLineItemDTO,
sharedContext
return await this.baseRepository_.serialize<CartTypes.CartLineItemDTO[]>(
items,
{
populate: true,
}
)
}

Expand All @@ -393,7 +394,7 @@ export default class CartModuleService implements ICartModuleService {
selector: Partial<CartTypes.CartLineItemDTO>,
data: Partial<CartTypes.UpdateLineItemDTO>,
@MedusaContext() sharedContext: Context = {}
): Promise<CartTypes.CartLineItemDTO[]> {
): Promise<LineItem[]> {
const items = await this.listLineItems({ ...selector }, {})

const updates = items.map((item) => {
Expand All @@ -403,18 +404,16 @@ export default class CartModuleService implements ICartModuleService {
}
})

await this.lineItemService_.update(updates, sharedContext)

return this.listLineItems({ ...selector }, {})
return await this.lineItemService_.update(updates, sharedContext)
}

@InjectManager("baseRepository_")
protected async updateCartLineItems_(
cartId: string, //
cartId: string,
data: Partial<CartTypes.UpdateLineItemDTO>,
@MedusaContext() sharedContext: Context = {}
): Promise<CartTypes.CartLineItemDTO[]> {
return this.updateSelectorLineItems_(
): Promise<LineItem[]> {
return await this.updateSelectorLineItems_(
{ cart_id: cartId },
data,
sharedContext
Expand All @@ -425,21 +424,8 @@ export default class CartModuleService implements ICartModuleService {
protected async updateLineItemsBulk_(
data: CartTypes.UpdateLineItemDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<CartTypes.CartLineItemDTO[]> {
const items = await this.lineItemService_
.update(data, sharedContext)
.catch((e) => {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
"Failed to update line items"
)
})

return await this.listLineItems(
{ id: items.flat().map((c) => c.id) },
{},
sharedContext
)
): Promise<LineItem[]> {
return await this.lineItemService_.update(data, sharedContext)
}
async removeLineItems(
itemIds: string[],
Expand Down

0 comments on commit 3eab5e0

Please sign in to comment.