Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cart): Shipping methods #6101

Merged
merged 11 commits into from
Jan 18, 2024
4 changes: 2 additions & 2 deletions packages/cart/src/services/cart-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,10 @@ export default class CartModuleService implements ICartModuleService {

@InjectManager("baseRepository_")
async listShippingMethods(
filters = {},
filters: CartTypes.FilterableShippingMethodProps = {},
config: FindConfig<CartTypes.CartShippingMethodDTO> = {},
@MedusaContext() sharedContext: Context = {}
) {
): Promise<CartTypes.CartShippingMethodDTO[]> {
const methods = await this.shippingMethodService_.list(
filters,
config,
Expand Down
134 changes: 8 additions & 126 deletions packages/cart/src/services/shipping-method.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,6 @@
import {
CartShippingMethodDTO,
Context,
DAL,
// FilterableShippingMethodProps,
FindConfig,
} from "@medusajs/types"
import {
InjectManager,
InjectTransactionManager,
MedusaContext,
MedusaError,
ModulesSdkUtils,
retrieveEntity,
} from "@medusajs/utils"
import { DAL } from "@medusajs/types"
import { ModulesSdkUtils } from "@medusajs/utils"
import { ShippingMethod } from "@models"
import { ShippingMethodRepository } from "@repositories"
import { CreateShippingMethodDTO, UpdateShippingMethodDTO } from "../types"

type InjectedDependencies = {
Expand All @@ -23,114 +9,10 @@ type InjectedDependencies = {

export default class ShippingMethodService<
TEntity extends ShippingMethod = ShippingMethod
> {
protected readonly shippingMethodRepository_: DAL.RepositoryService<ShippingMethod>

constructor({ shippingMethodRepository }: InjectedDependencies) {
this.shippingMethodRepository_ = shippingMethodRepository
}

@InjectManager("shippingMethodRepository_")
async retrieve(
id: string,
config: FindConfig<CartShippingMethodDTO> = {},
@MedusaContext() sharedContext: Context = {}
): Promise<TEntity> {
return (await retrieveEntity<ShippingMethod, CartShippingMethodDTO>({
id: id,
entityName: ShippingMethod.name,
repository: this.shippingMethodRepository_,
config,
sharedContext,
})) as TEntity
}

@InjectManager("shippingMethodRepository_")
async list(
filters: any = {},
config: FindConfig<CartShippingMethodDTO> = {},
@MedusaContext() sharedContext: Context = {}
): Promise<TEntity[]> {
const queryOptions = ModulesSdkUtils.buildQuery<ShippingMethod>(
filters,
config
)

return (await this.shippingMethodRepository_.find(
queryOptions,
sharedContext
)) as TEntity[]
}

@InjectManager("shippingMethodRepository_")
async listAndCount(
filters: any = {},
config: FindConfig<CartShippingMethodDTO> = {},
@MedusaContext() sharedContext: Context = {}
): Promise<[TEntity[], number]> {
const queryOptions = ModulesSdkUtils.buildQuery<ShippingMethod>(
filters,
config
)

return (await this.shippingMethodRepository_.findAndCount(
queryOptions,
sharedContext
)) as [TEntity[], number]
}

@InjectTransactionManager("shippingMethodRepository_")
async create(
data: CreateShippingMethodDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<TEntity[]> {
return (await (
this.shippingMethodRepository_ as ShippingMethodRepository
).create(data, sharedContext)) as TEntity[]
> extends ModulesSdkUtils.abstractServiceFactory<
InjectedDependencies,
{
create: CreateShippingMethodDTO
update: UpdateShippingMethodDTO
}

@InjectTransactionManager("shippingMethodRepository_")
async update(
data: UpdateShippingMethodDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<TEntity[]> {
const existingMethods = await this.list(
{
id: data.map((d) => d.id),
},
{},
sharedContext
)

const existingMethodsMap = new Map(
existingMethods.map<[string, ShippingMethod]>((sm) => [sm.id, sm])
)

const updates: UpdateShippingMethodDTO[] = []

for (const update of data) {
const shippingMethod = existingMethodsMap.get(update.id)

if (!shippingMethod) {
throw new MedusaError(
MedusaError.Types.NOT_FOUND,
`Shipping method with id "${update.id}" not found`
)
}

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

return (await (
this.shippingMethodRepository_ as ShippingMethodRepository
).update(updates, sharedContext)) as TEntity[]
}

@InjectTransactionManager("shippingMethodRepository_")
async delete(
ids: string[],
@MedusaContext() sharedContext: Context = {}
): Promise<void> {
await this.shippingMethodRepository_.delete(ids, sharedContext)
}
}
>(ShippingMethod)<TEntity> {}
8 changes: 8 additions & 0 deletions packages/types/src/cart/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,14 @@ export interface FilterableLineItemProps
product_id?: string | string[]
}

export interface FilterableShippingMethodProps
extends BaseFilterable<FilterableShippingMethodProps> {
id?: string | string[]
cart_id?: string | string[]
name?: string
shipping_option_id?: string | string[]
}

/**
* TODO: Remove this in favor of CartDTO, when module is released
* @deprecated Use CartDTO instead
Expand Down
7 changes: 7 additions & 0 deletions packages/types/src/cart/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
CartShippingMethodDTO,
FilterableAddressProps,
FilterableCartProps,
FilterableShippingMethodProps,
} from "./common"
import {
CreateAddressDTO,
Expand Down Expand Up @@ -105,6 +106,12 @@ export interface ICartModuleService extends IModuleService {
sharedContext?: Context
): Promise<void>

listShippingMethods(
filters: FilterableShippingMethodProps,
config: FindConfig<CartShippingMethodDTO>,
sharedContext: Context
): Promise<CartShippingMethodDTO[]>

addShippingMethods(
data: CreateShippingMethodDTO
): Promise<CartShippingMethodDTO>
Expand Down
Loading