Skip to content

Commit

Permalink
finally settle on API
Browse files Browse the repository at this point in the history
  • Loading branch information
olivermrbl committed Jan 15, 2024
1 parent b7290e5 commit ad51b61
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ describe("Cart Module Service", () => {
})

describe("updateLineItems", () => {
it("should update a line item in cart succesfully", async () => {
it("should update a line item in cart succesfully with selector approach", async () => {
const [createdCart] = await service.create([
{
currency_code: "eur",
Expand Down Expand Up @@ -448,7 +448,35 @@ describe("Cart Module Service", () => {
expect(updatedItem.title).toBe("test2")
})

it("should update multiples line items in cart succesfully", async () => {
it("should update a line item in cart succesfully with id approach", async () => {
const [createdCart] = await service.create([
{
currency_code: "eur",
},
])

const [item] = await service.addLineItems(createdCart.id, [
{
quantity: 1,
unit_price: 100,
title: "test",
tax_lines: [],
},
])

expect(item.title).toBe("test")

const updatedItem = await service.updateLineItems(
item.id,
{
title: "test2",
}
)

expect(updatedItem.title).toBe("test2")
})

it("should update line items in carts succesfully with multi-selector approach", async () => {
const [createdCart] = await service.create([
{
currency_code: "eur",
Expand Down Expand Up @@ -488,14 +516,16 @@ describe("Cart Module Service", () => {

const updatedItems = await service.updateLineItems([
{
cart_id: createdCart.id,
id: itemOne!.id,
title: "changed-test",
selector: { cart_id: createdCart.id },
data: {
title: "changed-test",
}
},
{
cart_id: createdCart.id,
id: itemTwo!.id,
title: "changed-other-test",
selector: { id: itemTwo!.id },
data: {
title: "changed-other-test",
}
},
])

Expand Down
119 changes: 58 additions & 61 deletions packages/cart/src/services/cart-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
isString,
} from "@medusajs/utils"
import { LineItem } from "@models"
import { UpdateLineItemDTO } from "@types"
import { joinerConfig } from "../joiner-config"
import * as services from "../services"

Expand Down Expand Up @@ -264,11 +265,6 @@ 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(
Expand Down Expand Up @@ -340,7 +336,7 @@ export default class CartModuleService implements ICartModuleService {
}

updateLineItems(
data: CartTypes.UpdateLineItemDTO[],
data: CartTypes.UpdateLineItemWithSelectorDTO[],
sharedContext?: Context
): Promise<CartTypes.CartLineItemDTO[]>
updateLineItems(
Expand All @@ -349,46 +345,55 @@ export default class CartModuleService implements ICartModuleService {
sharedContext?: Context
): Promise<CartTypes.CartLineItemDTO[]>
updateLineItems(
cartId: string,
lineItemId: string,
data: Partial<CartTypes.UpdateLineItemDTO>,
sharedContext?: Context
): Promise<CartTypes.CartLineItemDTO[]>
): Promise<CartTypes.CartLineItemDTO>

@InjectManager("baseRepository_")
async updateLineItems(
cartIdOrDataOrSelector:
lineItemIdOrDataOrSelector:
| string
| Partial<CartTypes.CartLineItemDTO>
| CartTypes.UpdateLineItemDTO[],
| CartTypes.UpdateLineItemWithSelectorDTO[]
| Partial<CartTypes.CartLineItemDTO>,
@MedusaContext()
dataOrSharedContext?:
| CartTypes.UpdateLineItemDTO[]
| CartTypes.UpdateLineItemDTO
| Partial<CartTypes.UpdateLineItemDTO>
| Context,
@MedusaContext() sharedContext: Context = {}
): Promise<CartTypes.CartLineItemDTO[]> {
@MedusaContext()
sharedContext: Context = {}
): Promise<CartTypes.CartLineItemDTO[] | CartTypes.CartLineItemDTO> {
let items: LineItem[] = []
if (isString(cartIdOrDataOrSelector)) {
// Case: Single cart update
items = await this.updateCartLineItems_(
cartIdOrDataOrSelector,
if (isString(lineItemIdOrDataOrSelector)) {
const item = await this.updateSingleLineItem_(
lineItemIdOrDataOrSelector,
dataOrSharedContext as Partial<CartTypes.UpdateLineItemDTO>,
sharedContext
)
} 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

return await this.baseRepository_.serialize<CartTypes.CartLineItemDTO>(
item,
{
populate: true,
}
)
}

const toUpdate = Array.isArray(lineItemIdOrDataOrSelector)
? lineItemIdOrDataOrSelector
: [
{
selector: lineItemIdOrDataOrSelector,
data: dataOrSharedContext,
} as CartTypes.UpdateLineItemWithSelectorDTO,
]

items = await this.updateLineItemsWithSelectorBulk_(
toUpdate,
dataOrSharedContext as Context
)

return await this.baseRepository_.serialize<CartTypes.CartLineItemDTO[]>(
items,
{
Expand All @@ -397,48 +402,40 @@ export default class CartModuleService implements ICartModuleService {
)
}

/**
* Example "Update thumbnail for all line items with variant_id 123"
* await updateLineItems({ variant_id: "123" }, { thubmnail: "https://..." })
*/
@InjectTransactionManager("baseRepository_")
protected async updateSelectorLineItems_(
selector: Partial<CartTypes.CartLineItemDTO>,
protected async updateSingleLineItem_(
lineItemId: string,
data: Partial<CartTypes.UpdateLineItemDTO>,
@MedusaContext() sharedContext: Context = {}
): Promise<LineItem[]> {
const items = await this.listLineItems({ ...selector }, {})

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

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

@InjectManager("baseRepository_")
protected async updateCartLineItems_(
cartId: string,
data: Partial<CartTypes.UpdateLineItemDTO>,
@MedusaContext() sharedContext: Context = {}
): Promise<LineItem[]> {
return await this.updateSelectorLineItems_(
{ cart_id: cartId },
data,
): Promise<LineItem> {
const [item] = await this.lineItemService_.update(
[{ id: lineItemId, ...data }],
sharedContext
)

return item
}

@InjectTransactionManager("baseRepository_")
protected async updateLineItemsBulk_(
data: CartTypes.UpdateLineItemDTO[],
protected async updateLineItemsWithSelectorBulk_(
updates: CartTypes.UpdateLineItemWithSelectorDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<LineItem[]> {
return await this.lineItemService_.update(data, sharedContext)
let toUpdate: UpdateLineItemDTO[] = []
for (const { selector, data } of updates) {
const items = await this.listLineItems({ ...selector }, {})

items.forEach((item) => {
toUpdate.push({
...data,
id: item.id,
})
})
}

return await this.lineItemService_.update(toUpdate, sharedContext)
}

async removeLineItems(
itemIds: string[],
sharedContext?: Context
Expand Down
7 changes: 7 additions & 0 deletions packages/types/src/cart/mutations.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { CartLineItemDTO } from "./common"

export interface UpsertAddressDTO {
customer_id?: string
company?: string
Expand Down Expand Up @@ -126,6 +128,11 @@ export interface CreateLineItemForCartDTO extends CreateLineItemDTO {
cart_id: string
}

export interface UpdateLineItemWithSelectorDTO {
selector: Partial<CartLineItemDTO>
data: Partial<UpdateLineItemDTO>
}

export interface UpdateLineItemDTO
extends Omit<
CreateLineItemDTO,
Expand Down
12 changes: 4 additions & 8 deletions packages/types/src/cart/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
UpdateAddressDTO,
UpdateCartDTO,
UpdateLineItemDTO,
UpdateLineItemWithSelectorDTO,
} from "./mutations"

export interface ICartModuleService extends IModuleService {
Expand Down Expand Up @@ -86,14 +87,9 @@ export interface ICartModuleService extends IModuleService {
items: CreateLineItemDTO[],
sharedContext?: Context
): Promise<CartLineItemDTO[]>
addLineItems(
cartId: string,
items: CreateLineItemDTO,
sharedContext?: Context
): Promise<CartLineItemDTO>

updateLineItems(
data: UpdateLineItemDTO[],
data: UpdateLineItemWithSelectorDTO[],
sharedContext?: Context
): Promise<CartLineItemDTO[]>
updateLineItems(
Expand All @@ -102,10 +98,10 @@ export interface ICartModuleService extends IModuleService {
sharedContext?: Context
): Promise<CartLineItemDTO[]>
updateLineItems(
cartId: string,
lineId: string,
data: Partial<UpdateLineItemDTO>,
sharedContext?: Context
): Promise<CartLineItemDTO[]>
): Promise<CartLineItemDTO>

removeLineItems(itemIds: string[], sharedContext?: Context): Promise<void>
removeLineItems(itemIds: string, sharedContext?: Context): Promise<void>
Expand Down

0 comments on commit ad51b61

Please sign in to comment.