feat(cart): Line items operations (#6066)
This commit is contained in:
@@ -20,6 +20,7 @@ export default async ({
|
||||
container.register({
|
||||
cartService: asClass(defaultServices.CartService).singleton(),
|
||||
addressService: asClass(defaultServices.AddressService).singleton(),
|
||||
lineItemService: asClass(defaultServices.LineItemService).singleton(),
|
||||
})
|
||||
|
||||
if (customRepositories) {
|
||||
@@ -38,5 +39,6 @@ function loadDefaultRepositories({ container }) {
|
||||
baseRepository: asClass(defaultRepositories.BaseRepository).singleton(),
|
||||
cartRepository: asClass(defaultRepositories.CartRepository).singleton(),
|
||||
addressRepository: asClass(defaultRepositories.AddressRepository).singleton(),
|
||||
lineItemRepository: asClass(defaultRepositories.LineItemRepository).singleton(),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ import {
|
||||
OneToMany,
|
||||
OptionalProps,
|
||||
PrimaryKey,
|
||||
Property,
|
||||
Property
|
||||
} from "@mikro-orm/core"
|
||||
import Address from "./address"
|
||||
import LineItem from "./line-item"
|
||||
@@ -78,7 +78,6 @@ export default class Cart {
|
||||
|
||||
@OneToMany(() => ShippingMethod, (shippingMethod) => shippingMethod.cart, {
|
||||
cascade: [Cascade.REMOVE],
|
||||
|
||||
})
|
||||
shipping_methods = new Collection<ShippingMethod>(this)
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@ type OptionalLineItemProps =
|
||||
| "is_tax_inclusive"
|
||||
| "compare_at_unit_price"
|
||||
| "requires_shipping"
|
||||
| "cart"
|
||||
| DAL.EntityDateColumns
|
||||
|
||||
@Entity({ tableName: "cart_line_item" })
|
||||
@@ -31,12 +32,15 @@ export default class LineItem {
|
||||
@PrimaryKey({ columnType: "text" })
|
||||
id: string
|
||||
|
||||
@Property({ columnType: "text" })
|
||||
cart_id: string
|
||||
|
||||
@ManyToOne(() => Cart, {
|
||||
onDelete: "cascade",
|
||||
index: "IDX_line_item_cart_id",
|
||||
fieldName: "cart_id",
|
||||
nullable: true,
|
||||
})
|
||||
cart!: Cart
|
||||
cart?: Cart | null
|
||||
|
||||
@Property({ columnType: "text" })
|
||||
title: string
|
||||
@@ -47,7 +51,7 @@ export default class LineItem {
|
||||
@Property({ columnType: "text", nullable: true })
|
||||
thumbnail?: string | null
|
||||
|
||||
@Property({ columnType: "text" })
|
||||
@Property({ columnType: "integer" })
|
||||
quantity: number
|
||||
|
||||
@Property({
|
||||
@@ -90,13 +94,13 @@ export default class LineItem {
|
||||
@Property({ columnType: "jsonb", nullable: true })
|
||||
variant_option_values?: Record<string, unknown> | null
|
||||
|
||||
@Property({ columnType: "boolean" })
|
||||
@Property({ columnType: "boolean", default: true })
|
||||
requires_shipping = true
|
||||
|
||||
@Property({ columnType: "boolean" })
|
||||
@Property({ columnType: "boolean", default: true })
|
||||
is_discountable = true
|
||||
|
||||
@Property({ columnType: "boolean" })
|
||||
@Property({ columnType: "boolean", default: false })
|
||||
is_tax_inclusive = false
|
||||
|
||||
@Property({ columnType: "numeric", nullable: true })
|
||||
@@ -147,7 +151,7 @@ export default class LineItem {
|
||||
columnType: "timestamptz",
|
||||
defaultRaw: "now()",
|
||||
})
|
||||
created_at: Date
|
||||
created_at?: Date
|
||||
|
||||
@Property({
|
||||
onCreate: () => new Date(),
|
||||
@@ -155,7 +159,7 @@ export default class LineItem {
|
||||
columnType: "timestamptz",
|
||||
defaultRaw: "now()",
|
||||
})
|
||||
updated_at: Date
|
||||
updated_at?: Date
|
||||
|
||||
@BeforeCreate()
|
||||
onCreate() {
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
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"
|
||||
|
||||
@@ -8,25 +6,6 @@ export class AddressRepository extends DALUtils.mikroOrmBaseRepositoryFactory<
|
||||
Address,
|
||||
{
|
||||
create: CreateAddressDTO
|
||||
update: UpdateAddressDTO
|
||||
}
|
||||
>(Address) {
|
||||
constructor(...args: any[]) {
|
||||
// @ts-ignore
|
||||
super(...arguments)
|
||||
}
|
||||
|
||||
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) {}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
export { MikroOrmBaseRepository as BaseRepository } from "@medusajs/utils"
|
||||
export * from "./address"
|
||||
export * from "./cart"
|
||||
export * from "./line-item"
|
||||
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
import { DALUtils } from "@medusajs/utils"
|
||||
import { LineItem } from "@models"
|
||||
import { CreateLineItemDTO, UpdateLineItemDTO } from "../types"
|
||||
|
||||
export class LineItemRepository extends DALUtils.mikroOrmBaseRepositoryFactory<
|
||||
LineItem,
|
||||
{
|
||||
create: CreateLineItemDTO
|
||||
update: UpdateLineItemDTO
|
||||
}
|
||||
>(LineItem) {}
|
||||
@@ -1,5 +1,6 @@
|
||||
import {
|
||||
AddressDTO,
|
||||
CartAddressDTO,
|
||||
Context,
|
||||
DAL,
|
||||
FilterableAddressProps,
|
||||
@@ -46,7 +47,7 @@ export default class AddressService<TEntity extends Address = Address> {
|
||||
@InjectManager("addressRepository_")
|
||||
async list(
|
||||
filters: FilterableAddressProps = {},
|
||||
config: FindConfig<AddressDTO> = {},
|
||||
config: FindConfig<CartAddressDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity[]> {
|
||||
const queryOptions = ModulesSdkUtils.buildQuery<Address>(filters, config)
|
||||
@@ -97,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)
|
||||
@@ -109,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(
|
||||
|
||||
@@ -1,47 +1,53 @@
|
||||
import {
|
||||
AddressDTO,
|
||||
CartAddressDTO,
|
||||
CartDTO,
|
||||
Context,
|
||||
CreateCartDTO,
|
||||
DAL,
|
||||
FilterableCartProps,
|
||||
FindConfig,
|
||||
ICartModuleService,
|
||||
InternalModuleDeclaration,
|
||||
ModuleJoinerConfig,
|
||||
UpdateCartDTO,
|
||||
} from "@medusajs/types"
|
||||
|
||||
import { FilterableAddressProps } from "@medusajs/types"
|
||||
import { CartTypes } from "@medusajs/types"
|
||||
|
||||
import {
|
||||
InjectManager,
|
||||
InjectTransactionManager,
|
||||
MedusaContext,
|
||||
isObject,
|
||||
isString,
|
||||
} from "@medusajs/utils"
|
||||
import { CreateAddressDTO, UpdateAddressDTO } from "@types"
|
||||
import { LineItem } from "@models"
|
||||
import { UpdateLineItemDTO } from "@types"
|
||||
import { joinerConfig } from "../joiner-config"
|
||||
import AddressService from "./address"
|
||||
import CartService from "./cart"
|
||||
import * as services from "../services"
|
||||
|
||||
type InjectedDependencies = {
|
||||
baseRepository: DAL.RepositoryService
|
||||
cartService: CartService
|
||||
addressService: AddressService
|
||||
cartService: services.CartService
|
||||
addressService: services.AddressService
|
||||
lineItemService: services.LineItemService
|
||||
}
|
||||
|
||||
export default class CartModuleService implements ICartModuleService {
|
||||
protected baseRepository_: DAL.RepositoryService
|
||||
protected cartService_: CartService
|
||||
protected addressService_: AddressService
|
||||
protected cartService_: services.CartService
|
||||
protected addressService_: services.AddressService
|
||||
protected lineItemService_: services.LineItemService
|
||||
|
||||
constructor(
|
||||
{ baseRepository, cartService, addressService }: InjectedDependencies,
|
||||
{
|
||||
baseRepository,
|
||||
cartService,
|
||||
addressService,
|
||||
lineItemService,
|
||||
}: InjectedDependencies,
|
||||
protected readonly moduleDeclaration: InternalModuleDeclaration
|
||||
) {
|
||||
this.baseRepository_ = baseRepository
|
||||
this.cartService_ = cartService
|
||||
this.addressService_ = addressService
|
||||
this.lineItemService_ = lineItemService
|
||||
}
|
||||
|
||||
__joinerConfig(): ModuleJoinerConfig {
|
||||
@@ -51,25 +57,25 @@ export default class CartModuleService implements ICartModuleService {
|
||||
@InjectManager("baseRepository_")
|
||||
async retrieve(
|
||||
id: string,
|
||||
config: FindConfig<CartDTO> = {},
|
||||
config: FindConfig<CartTypes.CartDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<CartDTO> {
|
||||
): Promise<CartTypes.CartDTO> {
|
||||
const cart = await this.cartService_.retrieve(id, config, sharedContext)
|
||||
|
||||
return await this.baseRepository_.serialize<CartDTO>(cart, {
|
||||
return await this.baseRepository_.serialize<CartTypes.CartDTO>(cart, {
|
||||
populate: true,
|
||||
})
|
||||
}
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
async list(
|
||||
filters: FilterableCartProps = {},
|
||||
config: FindConfig<CartDTO> = {},
|
||||
filters: CartTypes.FilterableCartProps = {},
|
||||
config: FindConfig<CartTypes.CartDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<CartDTO[]> {
|
||||
): Promise<CartTypes.CartDTO[]> {
|
||||
const carts = await this.cartService_.list(filters, config, sharedContext)
|
||||
|
||||
return await this.baseRepository_.serialize<CartDTO[]>(carts, {
|
||||
return this.baseRepository_.serialize<CartTypes.CartDTO[]>(carts, {
|
||||
populate: true,
|
||||
})
|
||||
}
|
||||
@@ -77,9 +83,9 @@ export default class CartModuleService implements ICartModuleService {
|
||||
@InjectManager("baseRepository_")
|
||||
async listAndCount(
|
||||
filters: FilterableCartProps = {},
|
||||
config: FindConfig<CartDTO> = {},
|
||||
config: FindConfig<CartTypes.CartDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<[CartDTO[], number]> {
|
||||
): Promise<[CartTypes.CartDTO[], number]> {
|
||||
const [carts, count] = await this.cartService_.listAndCount(
|
||||
filters,
|
||||
config,
|
||||
@@ -87,7 +93,7 @@ export default class CartModuleService implements ICartModuleService {
|
||||
)
|
||||
|
||||
return [
|
||||
await this.baseRepository_.serialize<CartDTO[]>(carts, {
|
||||
await this.baseRepository_.serialize<CartTypes.CartDTO[]>(carts, {
|
||||
populate: true,
|
||||
}),
|
||||
count,
|
||||
@@ -95,17 +101,20 @@ export default class CartModuleService implements ICartModuleService {
|
||||
}
|
||||
|
||||
async create(
|
||||
data: CreateCartDTO[],
|
||||
data: CartTypes.CreateCartDTO[],
|
||||
sharedContext?: Context
|
||||
): Promise<CartDTO[]>
|
||||
): Promise<CartTypes.CartDTO[]>
|
||||
|
||||
async create(data: CreateCartDTO, sharedContext?: Context): Promise<CartDTO>
|
||||
async create(
|
||||
data: CartTypes.CreateCartDTO,
|
||||
sharedContext?: Context
|
||||
): Promise<CartTypes.CartDTO>
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
async create(
|
||||
data: CreateCartDTO[] | CreateCartDTO,
|
||||
data: CartTypes.CreateCartDTO[] | CartTypes.CreateCartDTO,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<CartDTO[] | CartDTO> {
|
||||
): Promise<CartTypes.CartDTO[] | CartTypes.CartDTO> {
|
||||
const input = Array.isArray(data) ? data : [data]
|
||||
|
||||
const carts = await this.create_(input, sharedContext)
|
||||
@@ -118,29 +127,34 @@ export default class CartModuleService implements ICartModuleService {
|
||||
sharedContext
|
||||
)
|
||||
|
||||
return (Array.isArray(data) ? result : result[0]) as CartDTO | CartDTO[]
|
||||
return (Array.isArray(data) ? result : result[0]) as
|
||||
| CartTypes.CartDTO
|
||||
| CartTypes.CartDTO[]
|
||||
}
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
protected async create_(
|
||||
data: CreateCartDTO[],
|
||||
data: CartTypes.CreateCartDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
) {
|
||||
return await this.cartService_.create(data, sharedContext)
|
||||
}
|
||||
|
||||
async update(
|
||||
data: UpdateCartDTO[],
|
||||
data: CartTypes.UpdateCartDTO[],
|
||||
sharedContext?: Context
|
||||
): Promise<CartDTO[]>
|
||||
): Promise<CartTypes.CartDTO[]>
|
||||
|
||||
async update(data: UpdateCartDTO, sharedContext?: Context): Promise<CartDTO>
|
||||
async update(
|
||||
data: CartTypes.UpdateCartDTO,
|
||||
sharedContext?: Context
|
||||
): Promise<CartTypes.CartDTO>
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
async update(
|
||||
data: UpdateCartDTO[] | UpdateCartDTO,
|
||||
data: CartTypes.UpdateCartDTO[] | CartTypes.UpdateCartDTO,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<CartDTO[] | CartDTO> {
|
||||
): Promise<CartTypes.CartDTO[] | CartTypes.CartDTO> {
|
||||
const input = Array.isArray(data) ? data : [data]
|
||||
const carts = await this.update_(input, sharedContext)
|
||||
|
||||
@@ -152,12 +166,14 @@ export default class CartModuleService implements ICartModuleService {
|
||||
sharedContext
|
||||
)
|
||||
|
||||
return (Array.isArray(data) ? result : result[0]) as CartDTO | CartDTO[]
|
||||
return (Array.isArray(data) ? result : result[0]) as
|
||||
| CartTypes.CartDTO
|
||||
| CartTypes.CartDTO[]
|
||||
}
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
protected async update_(
|
||||
data: UpdateCartDTO[],
|
||||
data: CartTypes.UpdateCartDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
) {
|
||||
return await this.cartService_.update(data, sharedContext)
|
||||
@@ -178,8 +194,8 @@ export default class CartModuleService implements ICartModuleService {
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
async listAddresses(
|
||||
filters: FilterableAddressProps = {},
|
||||
config: FindConfig<AddressDTO> = {},
|
||||
filters: CartTypes.FilterableAddressProps = {},
|
||||
config: FindConfig<CartTypes.CartAddressDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
) {
|
||||
const addresses = await this.addressService_.list(
|
||||
@@ -188,19 +204,260 @@ export default class CartModuleService implements ICartModuleService {
|
||||
sharedContext
|
||||
)
|
||||
|
||||
return await this.baseRepository_.serialize<CartAddressDTO[]>(addresses, {
|
||||
populate: true,
|
||||
})
|
||||
return await this.baseRepository_.serialize<CartTypes.CartAddressDTO[]>(
|
||||
addresses,
|
||||
{
|
||||
populate: true,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
async createAddresses(data: CreateAddressDTO, sharedContext?: Context)
|
||||
async createAddresses(data: CreateAddressDTO[], sharedContext?: Context)
|
||||
@InjectManager("baseRepository_")
|
||||
async retrieveLineItem(
|
||||
itemId: string,
|
||||
config: FindConfig<CartTypes.CartLineItemDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
) {
|
||||
const item = await this.lineItemService_.retrieve(
|
||||
itemId,
|
||||
config,
|
||||
sharedContext
|
||||
)
|
||||
|
||||
return await this.baseRepository_.serialize<CartTypes.CartLineItemDTO[]>(
|
||||
item,
|
||||
{
|
||||
populate: true,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
async listLineItems(
|
||||
filters: CartTypes.FilterableLineItemProps = {},
|
||||
config: FindConfig<CartTypes.CartLineItemDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
) {
|
||||
const items = await this.lineItemService_.list(
|
||||
filters,
|
||||
config,
|
||||
sharedContext
|
||||
)
|
||||
|
||||
return await this.baseRepository_.serialize<CartTypes.CartLineItemDTO[]>(
|
||||
items,
|
||||
{
|
||||
populate: true,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
addLineItems(
|
||||
data: CartTypes.CreateLineItemForCartDTO
|
||||
): Promise<CartTypes.CartLineItemDTO>
|
||||
addLineItems(
|
||||
data: CartTypes.CreateLineItemForCartDTO[]
|
||||
): Promise<CartTypes.CartLineItemDTO[]>
|
||||
addLineItems(
|
||||
cartId: string,
|
||||
items: CartTypes.CreateLineItemDTO[],
|
||||
sharedContext?: Context
|
||||
): Promise<CartTypes.CartLineItemDTO[]>
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
async addLineItems(
|
||||
cartIdOrData:
|
||||
| string
|
||||
| CartTypes.CreateLineItemForCartDTO[]
|
||||
| CartTypes.CreateLineItemForCartDTO,
|
||||
data?: CartTypes.CreateLineItemDTO[] | CartTypes.CreateLineItemDTO,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<CartTypes.CartLineItemDTO[] | CartTypes.CartLineItemDTO> {
|
||||
let items: LineItem[] = []
|
||||
if (isString(cartIdOrData)) {
|
||||
items = await this.addLineItems_(
|
||||
cartIdOrData,
|
||||
data as CartTypes.CreateLineItemDTO[],
|
||||
sharedContext
|
||||
)
|
||||
} else {
|
||||
const data = Array.isArray(cartIdOrData) ? cartIdOrData : [cartIdOrData]
|
||||
items = await this.addLineItemsBulk_(data, sharedContext)
|
||||
}
|
||||
|
||||
return await this.baseRepository_.serialize<CartTypes.CartLineItemDTO[]>(
|
||||
items,
|
||||
{
|
||||
populate: true,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
protected async addLineItems_(
|
||||
cartId: string,
|
||||
items: CartTypes.CreateLineItemDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<LineItem[]> {
|
||||
const cart = await this.retrieve(cartId, { select: ["id"] }, sharedContext)
|
||||
|
||||
const toUpdate = items.map((item) => {
|
||||
return {
|
||||
...item,
|
||||
cart_id: cart.id,
|
||||
}
|
||||
})
|
||||
|
||||
return await this.addLineItemsBulk_(toUpdate, sharedContext)
|
||||
}
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
protected async addLineItemsBulk_(
|
||||
data: CartTypes.CreateLineItemForCartDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<LineItem[]> {
|
||||
return await this.lineItemService_.create(data, sharedContext)
|
||||
}
|
||||
|
||||
updateLineItems(
|
||||
data: CartTypes.UpdateLineItemWithSelectorDTO[]
|
||||
): Promise<CartTypes.CartLineItemDTO[]>
|
||||
updateLineItems(
|
||||
selector: Partial<CartTypes.CartLineItemDTO>,
|
||||
data: CartTypes.UpdateLineItemDTO,
|
||||
sharedContext?: Context
|
||||
): Promise<CartTypes.CartLineItemDTO[]>
|
||||
updateLineItems(
|
||||
lineItemId: string,
|
||||
data: Partial<CartTypes.UpdateLineItemDTO>,
|
||||
sharedContext?: Context
|
||||
): Promise<CartTypes.CartLineItemDTO>
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
async updateLineItems(
|
||||
lineItemIdOrDataOrSelector:
|
||||
| string
|
||||
| CartTypes.UpdateLineItemWithSelectorDTO[]
|
||||
| Partial<CartTypes.CartLineItemDTO>,
|
||||
data?: CartTypes.UpdateLineItemDTO | Partial<CartTypes.UpdateLineItemDTO>,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<CartTypes.CartLineItemDTO[] | CartTypes.CartLineItemDTO> {
|
||||
let items: LineItem[] = []
|
||||
if (isString(lineItemIdOrDataOrSelector)) {
|
||||
const item = await this.updateLineItem_(
|
||||
lineItemIdOrDataOrSelector,
|
||||
data as Partial<CartTypes.UpdateLineItemDTO>,
|
||||
sharedContext
|
||||
)
|
||||
|
||||
return await this.baseRepository_.serialize<CartTypes.CartLineItemDTO>(
|
||||
item,
|
||||
{
|
||||
populate: true,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
const toUpdate = Array.isArray(lineItemIdOrDataOrSelector)
|
||||
? lineItemIdOrDataOrSelector
|
||||
: [
|
||||
{
|
||||
selector: lineItemIdOrDataOrSelector,
|
||||
data: data,
|
||||
} as CartTypes.UpdateLineItemWithSelectorDTO,
|
||||
]
|
||||
|
||||
items = await this.updateLineItemsWithSelector_(toUpdate, sharedContext)
|
||||
|
||||
return await this.baseRepository_.serialize<CartTypes.CartLineItemDTO[]>(
|
||||
items,
|
||||
{
|
||||
populate: true,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
protected async updateLineItem_(
|
||||
lineItemId: string,
|
||||
data: Partial<CartTypes.UpdateLineItemDTO>,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<LineItem> {
|
||||
const [item] = await this.lineItemService_.update(
|
||||
[{ id: lineItemId, ...data }],
|
||||
sharedContext
|
||||
)
|
||||
|
||||
return item
|
||||
}
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
protected async updateLineItemsWithSelector_(
|
||||
updates: CartTypes.UpdateLineItemWithSelectorDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<LineItem[]> {
|
||||
let toUpdate: UpdateLineItemDTO[] = []
|
||||
for (const { selector, data } of updates) {
|
||||
const items = await this.listLineItems({ ...selector }, {}, sharedContext)
|
||||
|
||||
items.forEach((item) => {
|
||||
toUpdate.push({
|
||||
...data,
|
||||
id: item.id,
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
return await this.lineItemService_.update(toUpdate, sharedContext)
|
||||
}
|
||||
|
||||
async removeLineItems(
|
||||
itemIds: string[],
|
||||
sharedContext?: Context
|
||||
): Promise<void>
|
||||
async removeLineItems(itemIds: string, sharedContext?: Context): Promise<void>
|
||||
async removeLineItems(
|
||||
selector: Partial<CartTypes.CartLineItemDTO>,
|
||||
sharedContext?: Context
|
||||
): Promise<void>
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
async removeLineItems(
|
||||
itemIdsOrSelector: string | string[] | Partial<CartTypes.CartLineItemDTO>,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<void> {
|
||||
let toDelete: string[] = []
|
||||
if (isObject(itemIdsOrSelector)) {
|
||||
const items = await this.listLineItems(
|
||||
{ ...itemIdsOrSelector } as Partial<CartTypes.CartLineItemDTO>,
|
||||
{},
|
||||
sharedContext
|
||||
)
|
||||
|
||||
toDelete = items.map((item) => item.id)
|
||||
} else {
|
||||
toDelete = Array.isArray(itemIdsOrSelector)
|
||||
? itemIdsOrSelector
|
||||
: [itemIdsOrSelector]
|
||||
}
|
||||
|
||||
await this.lineItemService_.delete(toDelete, sharedContext)
|
||||
}
|
||||
|
||||
async createAddresses(
|
||||
data: CartTypes.CreateAddressDTO,
|
||||
sharedContext?: Context
|
||||
): Promise<CartTypes.CartAddressDTO>
|
||||
async createAddresses(
|
||||
data: CartTypes.CreateAddressDTO[],
|
||||
sharedContext?: Context
|
||||
): Promise<CartTypes.CartAddressDTO[]>
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
async createAddresses(
|
||||
data: CreateAddressDTO[] | CreateAddressDTO,
|
||||
data: CartTypes.CreateAddressDTO[] | CartTypes.CreateAddressDTO,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
) {
|
||||
): Promise<CartTypes.CartAddressDTO | CartTypes.CartAddressDTO[]> {
|
||||
const input = Array.isArray(data) ? data : [data]
|
||||
const addresses = await this.createAddresses_(input, sharedContext)
|
||||
|
||||
@@ -211,26 +468,32 @@ export default class CartModuleService implements ICartModuleService {
|
||||
)
|
||||
|
||||
return (Array.isArray(data) ? result : result[0]) as
|
||||
| AddressDTO
|
||||
| AddressDTO[]
|
||||
| CartTypes.CartAddressDTO
|
||||
| CartTypes.CartAddressDTO[]
|
||||
}
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
protected async createAddresses_(
|
||||
data: CreateAddressDTO[],
|
||||
data: CartTypes.CreateAddressDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
) {
|
||||
return await this.addressService_.create(data, sharedContext)
|
||||
}
|
||||
|
||||
async updateAddresses(data: UpdateAddressDTO, sharedContext?: Context)
|
||||
async updateAddresses(data: UpdateAddressDTO[], sharedContext?: Context)
|
||||
async updateAddresses(
|
||||
data: CartTypes.UpdateAddressDTO,
|
||||
sharedContext?: Context
|
||||
): Promise<CartTypes.CartAddressDTO>
|
||||
async updateAddresses(
|
||||
data: CartTypes.UpdateAddressDTO[],
|
||||
sharedContext?: Context
|
||||
): Promise<CartTypes.CartAddressDTO[]>
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
async updateAddresses(
|
||||
data: UpdateAddressDTO[] | UpdateAddressDTO,
|
||||
data: CartTypes.UpdateAddressDTO[] | CartTypes.UpdateAddressDTO,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
) {
|
||||
): Promise<CartTypes.CartAddressDTO | CartTypes.CartAddressDTO[]> {
|
||||
const input = Array.isArray(data) ? data : [data]
|
||||
const addresses = await this.updateAddresses_(input, sharedContext)
|
||||
|
||||
@@ -241,26 +504,26 @@ export default class CartModuleService implements ICartModuleService {
|
||||
)
|
||||
|
||||
return (Array.isArray(data) ? result : result[0]) as
|
||||
| AddressDTO
|
||||
| AddressDTO[]
|
||||
| CartTypes.CartAddressDTO
|
||||
| CartTypes.CartAddressDTO[]
|
||||
}
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
protected async updateAddresses_(
|
||||
data: UpdateAddressDTO[],
|
||||
data: CartTypes.UpdateAddressDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
) {
|
||||
return await this.addressService_.update(data, sharedContext)
|
||||
}
|
||||
|
||||
async deleteAddresses(ids: string[], sharedContext?: Context)
|
||||
async deleteAddresses(ids: string, sharedContext?: Context)
|
||||
async deleteAddresses(ids: string[], sharedContext?: Context): Promise<void>
|
||||
async deleteAddresses(ids: string, sharedContext?: Context): Promise<void>
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
async deleteAddresses(
|
||||
ids: string[] | string,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
) {
|
||||
): Promise<void> {
|
||||
const addressIds = Array.isArray(ids) ? ids : [ids]
|
||||
await this.addressService_.delete(addressIds, sharedContext)
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
export { default as AddressService } from "./address"
|
||||
export { default as CartService } from "./cart"
|
||||
export { default as CartModuleService } from "./cart-module"
|
||||
export { default as LineItemService } from "./line-item"
|
||||
|
||||
|
||||
@@ -0,0 +1,128 @@
|
||||
import {
|
||||
CartLineItemDTO,
|
||||
Context,
|
||||
DAL,
|
||||
FilterableLineItemProps,
|
||||
FindConfig,
|
||||
} from "@medusajs/types"
|
||||
import {
|
||||
InjectManager,
|
||||
InjectTransactionManager,
|
||||
MedusaContext,
|
||||
MedusaError,
|
||||
ModulesSdkUtils,
|
||||
retrieveEntity,
|
||||
} from "@medusajs/utils"
|
||||
import { LineItem } from "@models"
|
||||
import { LineItemRepository } from "@repositories"
|
||||
import { CreateLineItemDTO, UpdateLineItemDTO } from "../types"
|
||||
|
||||
type InjectedDependencies = {
|
||||
lineItemRepository: DAL.RepositoryService
|
||||
}
|
||||
|
||||
export default class LineItemService<TEntity extends LineItem = LineItem> {
|
||||
protected readonly lineItemRepository_: DAL.RepositoryService<LineItem>
|
||||
|
||||
constructor({ lineItemRepository }: InjectedDependencies) {
|
||||
this.lineItemRepository_ = lineItemRepository
|
||||
}
|
||||
|
||||
@InjectManager("lineItemRepository_")
|
||||
async retrieve(
|
||||
id: string,
|
||||
config: FindConfig<CartLineItemDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity> {
|
||||
return (await retrieveEntity<LineItem, CartLineItemDTO>({
|
||||
id: id,
|
||||
entityName: LineItem.name,
|
||||
repository: this.lineItemRepository_,
|
||||
config,
|
||||
sharedContext,
|
||||
})) as TEntity
|
||||
}
|
||||
|
||||
@InjectManager("lineItemRepository_")
|
||||
async list(
|
||||
filters: FilterableLineItemProps = {},
|
||||
config: FindConfig<CartLineItemDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity[]> {
|
||||
const queryOptions = ModulesSdkUtils.buildQuery<LineItem>(filters, config)
|
||||
|
||||
return (await this.lineItemRepository_.find(
|
||||
queryOptions,
|
||||
sharedContext
|
||||
)) as TEntity[]
|
||||
}
|
||||
|
||||
@InjectManager("lineItemRepository_")
|
||||
async listAndCount(
|
||||
filters: FilterableLineItemProps = {},
|
||||
config: FindConfig<CartLineItemDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<[TEntity[], number]> {
|
||||
const queryOptions = ModulesSdkUtils.buildQuery<LineItem>(filters, config)
|
||||
|
||||
return (await this.lineItemRepository_.findAndCount(
|
||||
queryOptions,
|
||||
sharedContext
|
||||
)) as [TEntity[], number]
|
||||
}
|
||||
|
||||
@InjectTransactionManager("lineItemRepository_")
|
||||
async create(
|
||||
data: CreateLineItemDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity[]> {
|
||||
return (await (this.lineItemRepository_ as LineItemRepository).create(
|
||||
data,
|
||||
sharedContext
|
||||
)) as TEntity[]
|
||||
}
|
||||
|
||||
@InjectTransactionManager("lineItemRepository_")
|
||||
async update(
|
||||
data: UpdateLineItemDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity[]> {
|
||||
const existingLines = await this.list(
|
||||
{ id: [...data.map((d) => d.id)] },
|
||||
{},
|
||||
sharedContext
|
||||
)
|
||||
|
||||
const existingLinesMap = new Map(
|
||||
existingLines.map<[string, LineItem]>((li) => [li.id, li])
|
||||
)
|
||||
|
||||
const updates: UpdateLineItemDTO[] = []
|
||||
|
||||
for (const update of data) {
|
||||
const lineItem = existingLinesMap.get(update.id)
|
||||
|
||||
if (!lineItem) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`Line item with id "${update.id}" not found`
|
||||
)
|
||||
}
|
||||
|
||||
updates.push({ ...update, id: lineItem.id })
|
||||
}
|
||||
|
||||
return (await (this.lineItemRepository_ as LineItemRepository).update(
|
||||
updates,
|
||||
sharedContext
|
||||
)) as TEntity[]
|
||||
}
|
||||
|
||||
@InjectTransactionManager("lineItemRepository_")
|
||||
async delete(
|
||||
ids: string[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<void> {
|
||||
await this.lineItemRepository_.delete(ids, sharedContext)
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Logger } from "@medusajs/types"
|
||||
export * from "./address"
|
||||
export * from "./cart"
|
||||
export * from "./line-item"
|
||||
|
||||
export type InitializeModuleInjectableDependencies = {
|
||||
logger?: Logger
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
interface PartialUpsertLineItemDTO {
|
||||
subtitle?: string
|
||||
thumbnail?: string
|
||||
|
||||
product_id?: string
|
||||
product_title?: string
|
||||
product_description?: string
|
||||
product_subtitle?: string
|
||||
product_type?: string
|
||||
product_collection?: string
|
||||
product_handle?: string
|
||||
|
||||
variant_id?: string
|
||||
variant_sku?: string
|
||||
variant_barcode?: string
|
||||
variant_title?: string
|
||||
variant_option_values?: Record<string, unknown>
|
||||
|
||||
requires_shipping?: boolean
|
||||
is_discountable?: boolean
|
||||
is_tax_inclusive?: boolean
|
||||
|
||||
compare_at_unit_price?: number
|
||||
}
|
||||
|
||||
export interface CreateLineItemDTO extends PartialUpsertLineItemDTO {
|
||||
title: string
|
||||
quantity: number
|
||||
unit_price: number
|
||||
cart_id: string
|
||||
}
|
||||
|
||||
export interface UpdateLineItemDTO
|
||||
extends PartialUpsertLineItemDTO,
|
||||
Partial<CreateLineItemDTO> {
|
||||
id: string
|
||||
}
|
||||
Reference in New Issue
Block a user