feat(cart): Shipping methods (#6101)
This commit is contained in:
@@ -17,7 +17,7 @@ import {
|
||||
isObject,
|
||||
isString,
|
||||
} from "@medusajs/utils"
|
||||
import { LineItem } from "@models"
|
||||
import { LineItem, ShippingMethod } from "@models"
|
||||
import { UpdateLineItemDTO } from "@types"
|
||||
import { joinerConfig } from "../joiner-config"
|
||||
import * as services from "../services"
|
||||
@@ -27,6 +27,7 @@ type InjectedDependencies = {
|
||||
cartService: services.CartService
|
||||
addressService: services.AddressService
|
||||
lineItemService: services.LineItemService
|
||||
shippingMethodService: services.ShippingMethodService
|
||||
}
|
||||
|
||||
export default class CartModuleService implements ICartModuleService {
|
||||
@@ -34,6 +35,7 @@ export default class CartModuleService implements ICartModuleService {
|
||||
protected cartService_: services.CartService
|
||||
protected addressService_: services.AddressService
|
||||
protected lineItemService_: services.LineItemService
|
||||
protected shippingMethodService_: services.ShippingMethodService
|
||||
|
||||
constructor(
|
||||
{
|
||||
@@ -41,6 +43,7 @@ export default class CartModuleService implements ICartModuleService {
|
||||
cartService,
|
||||
addressService,
|
||||
lineItemService,
|
||||
shippingMethodService,
|
||||
}: InjectedDependencies,
|
||||
protected readonly moduleDeclaration: InternalModuleDeclaration
|
||||
) {
|
||||
@@ -48,6 +51,7 @@ export default class CartModuleService implements ICartModuleService {
|
||||
this.cartService_ = cartService
|
||||
this.addressService_ = addressService
|
||||
this.lineItemService_ = lineItemService
|
||||
this.shippingMethodService_ = shippingMethodService
|
||||
}
|
||||
|
||||
__joinerConfig(): ModuleJoinerConfig {
|
||||
@@ -252,6 +256,25 @@ export default class CartModuleService implements ICartModuleService {
|
||||
)
|
||||
}
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
async listShippingMethods(
|
||||
filters: CartTypes.FilterableShippingMethodProps = {},
|
||||
config: FindConfig<CartTypes.CartShippingMethodDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<CartTypes.CartShippingMethodDTO[]> {
|
||||
const methods = await this.shippingMethodService_.list(
|
||||
filters,
|
||||
config,
|
||||
sharedContext
|
||||
)
|
||||
|
||||
return await this.baseRepository_.serialize<
|
||||
CartTypes.CartShippingMethodDTO[]
|
||||
>(methods, {
|
||||
populate: true,
|
||||
})
|
||||
}
|
||||
|
||||
addLineItems(
|
||||
data: CartTypes.CreateLineItemForCartDTO
|
||||
): Promise<CartTypes.CartLineItemDTO>
|
||||
@@ -527,4 +550,112 @@ export default class CartModuleService implements ICartModuleService {
|
||||
const addressIds = Array.isArray(ids) ? ids : [ids]
|
||||
await this.addressService_.delete(addressIds, sharedContext)
|
||||
}
|
||||
|
||||
async addShippingMethods(
|
||||
data: CartTypes.CreateShippingMethodDTO
|
||||
): Promise<CartTypes.CartShippingMethodDTO>
|
||||
async addShippingMethods(
|
||||
data: CartTypes.CreateShippingMethodDTO[]
|
||||
): Promise<CartTypes.CartShippingMethodDTO[]>
|
||||
async addShippingMethods(
|
||||
cartId: string,
|
||||
methods: CartTypes.CreateShippingMethodDTO[],
|
||||
sharedContext?: Context
|
||||
): Promise<CartTypes.CartShippingMethodDTO[]>
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
async addShippingMethods(
|
||||
cartIdOrData:
|
||||
| string
|
||||
| CartTypes.CreateShippingMethodDTO[]
|
||||
| CartTypes.CreateShippingMethodDTO,
|
||||
data?: CartTypes.CreateShippingMethodDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<
|
||||
CartTypes.CartShippingMethodDTO[] | CartTypes.CartShippingMethodDTO
|
||||
> {
|
||||
let methods: ShippingMethod[] = []
|
||||
if (isString(cartIdOrData)) {
|
||||
methods = await this.addShippingMethods_(
|
||||
cartIdOrData,
|
||||
data as CartTypes.CreateShippingMethodDTO[],
|
||||
sharedContext
|
||||
)
|
||||
} else {
|
||||
const data = Array.isArray(cartIdOrData) ? cartIdOrData : [cartIdOrData]
|
||||
methods = await this.addShippingMethodsBulk_(data, sharedContext)
|
||||
}
|
||||
|
||||
return await this.baseRepository_.serialize<
|
||||
CartTypes.CartShippingMethodDTO[]
|
||||
>(methods, {
|
||||
populate: true,
|
||||
})
|
||||
}
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
protected async addShippingMethods_(
|
||||
cartId: string,
|
||||
data: CartTypes.CreateShippingMethodDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<ShippingMethod[]> {
|
||||
const cart = await this.retrieve(cartId, { select: ["id"] }, sharedContext)
|
||||
|
||||
const methods = data.map((method) => {
|
||||
return {
|
||||
...method,
|
||||
cart_id: cart.id,
|
||||
}
|
||||
})
|
||||
|
||||
return await this.addShippingMethodsBulk_(methods, sharedContext)
|
||||
}
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
protected async addShippingMethodsBulk_(
|
||||
data: CartTypes.CreateShippingMethodDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<ShippingMethod[]> {
|
||||
return await this.shippingMethodService_.create(data, sharedContext)
|
||||
}
|
||||
|
||||
async removeShippingMethods(
|
||||
methodIds: string[],
|
||||
sharedContext?: Context
|
||||
): Promise<void>
|
||||
async removeShippingMethods(
|
||||
methodIds: string,
|
||||
sharedContext?: Context
|
||||
): Promise<void>
|
||||
async removeShippingMethods(
|
||||
selector: Partial<CartTypes.CartShippingMethodDTO>,
|
||||
sharedContext?: Context
|
||||
): Promise<void>
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
async removeShippingMethods(
|
||||
methodIdsOrSelector:
|
||||
| string
|
||||
| string[]
|
||||
| Partial<CartTypes.CartShippingMethodDTO>,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<void> {
|
||||
let toDelete: string[] = []
|
||||
if (isObject(methodIdsOrSelector)) {
|
||||
const methods = await this.listShippingMethods(
|
||||
{
|
||||
...(methodIdsOrSelector as Partial<CartTypes.CartShippingMethodDTO>),
|
||||
},
|
||||
{},
|
||||
sharedContext
|
||||
)
|
||||
|
||||
toDelete = methods.map((m) => m.id)
|
||||
} else {
|
||||
toDelete = Array.isArray(methodIdsOrSelector)
|
||||
? methodIdsOrSelector
|
||||
: [methodIdsOrSelector]
|
||||
}
|
||||
await this.shippingMethodService_.delete(toDelete, sharedContext)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ export default class CartService<
|
||||
update: UpdateCartDTO
|
||||
}
|
||||
>(Cart)<TEntity> {
|
||||
constructor({ cartRepository }: InjectedDependencies) {
|
||||
constructor(container: InjectedDependencies) {
|
||||
// @ts-ignore
|
||||
super(...arguments)
|
||||
}
|
||||
|
||||
@@ -2,4 +2,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"
|
||||
export { default as ShippingMethodService } from "./shipping-method"
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { DAL } from "@medusajs/types"
|
||||
import { ModulesSdkUtils } from "@medusajs/utils"
|
||||
import { LineItem } from "@models"
|
||||
import { CreateLineItemDTO, UpdateLineItemDTO } from "../types"
|
||||
import { CreateLineItemDTO, UpdateLineItemDTO } from "@types"
|
||||
|
||||
type InjectedDependencies = {
|
||||
lineItemRepository: DAL.RepositoryService
|
||||
@@ -15,4 +15,9 @@ export default class LineItemService<
|
||||
create: CreateLineItemDTO
|
||||
update: UpdateLineItemDTO
|
||||
}
|
||||
>(LineItem)<TEntity> {}
|
||||
>(LineItem)<TEntity> {
|
||||
constructor(container: InjectedDependencies) {
|
||||
// @ts-ignore
|
||||
super(...arguments)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
import { DAL } from "@medusajs/types"
|
||||
import { ModulesSdkUtils } from "@medusajs/utils"
|
||||
import { ShippingMethod } from "@models"
|
||||
import { CreateShippingMethodDTO, UpdateShippingMethodDTO } from "../types"
|
||||
|
||||
type InjectedDependencies = {
|
||||
shippingMethodRepository: DAL.RepositoryService
|
||||
}
|
||||
|
||||
export default class ShippingMethodService<
|
||||
TEntity extends ShippingMethod = ShippingMethod
|
||||
> extends ModulesSdkUtils.abstractServiceFactory<
|
||||
InjectedDependencies,
|
||||
{
|
||||
create: CreateShippingMethodDTO
|
||||
update: UpdateShippingMethodDTO
|
||||
}
|
||||
>(ShippingMethod)<TEntity> {
|
||||
constructor(container: InjectedDependencies) {
|
||||
// @ts-ignore
|
||||
super(...arguments)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user