Skip to content

Commit

Permalink
address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
olivermrbl committed Jan 16, 2024
1 parent ad51b61 commit 23dd46b
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 70 deletions.
12 changes: 6 additions & 6 deletions packages/cart/src/models/line-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,14 @@ export default class LineItem {
@Property({ columnType: "jsonb", nullable: true })
variant_option_values?: Record<string, unknown> | null

@Property({ columnType: "boolean" })
requires_shipping? = true
@Property({ columnType: "boolean", default: true })
requires_shipping = true

@Property({ columnType: "boolean" })
is_discountable? = true
@Property({ columnType: "boolean", default: true })
is_discountable = true

@Property({ columnType: "boolean" })
is_tax_inclusive? = false
@Property({ columnType: "boolean", default: false })
is_tax_inclusive = false

@Property({ columnType: "numeric", nullable: true })
compare_at_unit_price?: number
Expand Down
20 changes: 2 additions & 18 deletions packages/cart/src/repositories/address.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,11 @@
import { Context } from "@medusajs/types"
import { DALUtils } from "@medusajs/utils"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { Address } from "@models"
import { CreateAddressDTO, UpdateAddressDTO } from "../types"

export class AddressRepository extends DALUtils.mikroOrmBaseRepositoryFactory<
Address,
{
create: CreateAddressDTO
update: UpdateAddressDTO
}
>(Address) {
async update(
data: { address: Address; update: UpdateAddressDTO }[],
context: Context = {}
): Promise<Address[]> {
const manager = this.getActiveManager<SqlEntityManager>(context)

const entities = data.map(({ address, update }) => {
return manager.assign(address, update)
})

manager.persist(entities)

return entities
}
}
>(Address) {}
27 changes: 7 additions & 20 deletions packages/cart/src/repositories/line-item.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,11 @@
import { Context } from "@medusajs/types"
import { DALUtils } from "@medusajs/utils"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { LineItem } from "@models"
import { UpdateLineItemDTO } from "../types"
import { CreateLineItemDTO, UpdateLineItemDTO } from "../types"

export class LineItemRepository extends DALUtils.mikroOrmBaseRepositoryFactory<LineItem>(
LineItem
) {
async update(
data: { lineItem: LineItem; update: UpdateLineItemDTO }[],
context: Context = {}
): Promise<LineItem[]> {
const manager = this.getActiveManager<SqlEntityManager>(context)

const entities = data.map(({ lineItem, update }) => {
return manager.assign(lineItem, update)
})

manager.persist(entities)

return entities
export class LineItemRepository extends DALUtils.mikroOrmBaseRepositoryFactory<
LineItem,
{
create: CreateLineItemDTO
update: UpdateLineItemDTO
}
}
>(LineItem) {}
4 changes: 2 additions & 2 deletions packages/cart/src/services/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export default class AddressService<TEntity extends Address = Address> {
existingAddresses.map<[string, Address]>((addr) => [addr.id, addr])
)

const updates: { address: Address; update: UpdateAddressDTO }[] = []
const updates: UpdateAddressDTO[] = []

for (const update of data) {
const address = existingAddressesMap.get(update.id)
Expand All @@ -110,7 +110,7 @@ export default class AddressService<TEntity extends Address = Address> {
)
}

updates.push({ address, update })
updates.push({ ...update, id: address.id })
}

return (await (this.addressRepository_ as AddressRepository).update(
Expand Down
29 changes: 11 additions & 18 deletions packages/cart/src/services/cart-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ export default class CartModuleService implements ICartModuleService {

@InjectManager("baseRepository_")
async listLineItems(
filters = {},
filters: CartTypes.FilterableLineItemProps = {},
config: FindConfig<CartTypes.CartLineItemDTO> = {},
@MedusaContext() sharedContext: Context = {}
) {
Expand Down Expand Up @@ -282,21 +282,12 @@ export default class CartModuleService implements ICartModuleService {
if (isString(cartIdOrData)) {
items = await this.addLineItems_(
cartIdOrData,
dataOrSharedContext as
| CartTypes.CreateLineItemDTO[]
| CartTypes.CreateLineItemDTO,
dataOrSharedContext as CartTypes.CreateLineItemDTO[],
sharedContext
)
} else if (Array.isArray(cartIdOrData)) {
items = await this.addLineItemsBulk_(
cartIdOrData,
dataOrSharedContext as Context
)
} else {
items = await this.addLineItemsBulk_(
[cartIdOrData],
dataOrSharedContext as Context
)
const data = Array.isArray(cartIdOrData) ? cartIdOrData : [cartIdOrData]
items = await this.addLineItemsBulk_(data, dataOrSharedContext as Context)
}

return await this.baseRepository_.serialize<CartTypes.CartLineItemDTO[]>(
Expand All @@ -310,13 +301,11 @@ export default class CartModuleService implements ICartModuleService {
@InjectTransactionManager("baseRepository_")
protected async addLineItems_(
cartId: string,
data: CartTypes.CreateLineItemDTO[] | CartTypes.CreateLineItemDTO,
items: CartTypes.CreateLineItemDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<LineItem[]> {
const cart = await this.retrieve(cartId, { select: ["id"] }, sharedContext)

const items = Array.isArray(data) ? data : [data]

const toUpdate = items.map((item) => {
return {
...item,
Expand Down Expand Up @@ -423,7 +412,7 @@ export default class CartModuleService implements ICartModuleService {
): Promise<LineItem[]> {
let toUpdate: UpdateLineItemDTO[] = []
for (const { selector, data } of updates) {
const items = await this.listLineItems({ ...selector }, {})
const items = await this.listLineItems({ ...selector }, {}, sharedContext)

items.forEach((item) => {
toUpdate.push({
Expand Down Expand Up @@ -453,7 +442,11 @@ export default class CartModuleService implements ICartModuleService {
): Promise<void> {
let toDelete: string[] = []
if (isObject(itemIdsOrSelector)) {
const items = await this.listLineItems({ ...itemIdsOrSelector }, {})
const items = await this.listLineItems(
{ ...itemIdsOrSelector } as Partial<CartTypes.CartLineItemDTO>,
{},
sharedContext
)

toDelete = items.map((item) => item.id)
} else {
Expand Down
4 changes: 2 additions & 2 deletions packages/cart/src/services/line-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export default class LineItemService<TEntity extends LineItem = LineItem> {
existingLines.map<[string, LineItem]>((li) => [li.id, li])
)

const updates: { lineItem: LineItem; update: UpdateLineItemDTO }[] = []
const updates: UpdateLineItemDTO[] = []

for (const update of data) {
const lineItem = existingLinesMap.get(update.id)
Expand All @@ -109,7 +109,7 @@ export default class LineItemService<TEntity extends LineItem = LineItem> {
)
}

updates.push({ lineItem, update })
updates.push({ ...update, id: lineItem.id })
}

return (await (this.lineItemRepository_ as LineItemRepository).update(
Expand Down
4 changes: 0 additions & 4 deletions packages/cart/src/types/line-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,4 @@ export interface CreateLineItemDTO extends PartialUpsertLineItemDTO {

export interface UpdateLineItemDTO extends PartialUpsertLineItemDTO {
id: string
title?: string
quantity?: number
unit_price?: number
cart_id?: string
}
4 changes: 4 additions & 0 deletions packages/types/src/cart/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,10 @@ export interface FilterableAddressProps
export interface FilterableLineItemProps
extends BaseFilterable<FilterableLineItemProps> {
id?: string | string[]
cart_id?: string | string[]
title?: string
variant_id?: string | string[]
product_id?: string | string[]
}

/**
Expand Down

0 comments on commit 23dd46b

Please sign in to comment.