chore: Internal medusa service proper typings with DML (#7792)

This commit is contained in:
Adrien de Peretti
2024-06-21 12:36:54 +02:00
committed by GitHub
parent 944051a951
commit 90e6ca0e9e
29 changed files with 198 additions and 142 deletions
@@ -8,11 +8,9 @@ type InjectedDependencies = {
inventoryLevelRepository: InventoryLevelRepository
}
export default class InventoryLevelService<
TEntity extends InventoryLevel = InventoryLevel
> extends ModulesSdkUtils.MedusaInternalService<InjectedDependencies>(
export default class InventoryLevelService extends ModulesSdkUtils.MedusaInternalService<InjectedDependencies>(
InventoryLevel
)<TEntity> {
)<InventoryLevel> {
protected readonly inventoryLevelRepository: InventoryLevelRepository
constructor(container: InjectedDependencies) {
@@ -30,7 +30,7 @@ import { IInventoryService } from "@medusajs/types/dist/inventory"
type InjectedDependencies = {
baseRepository: DAL.RepositoryService
inventoryItemService: ModulesSdkTypes.IMedusaInternalService<any>
inventoryLevelService: InventoryLevelService<any>
inventoryLevelService: InventoryLevelService
reservationItemService: ModulesSdkTypes.IMedusaInternalService<any>
}
@@ -59,7 +59,7 @@ export default class InventoryModuleService
protected readonly inventoryItemService_: ModulesSdkTypes.IMedusaInternalService<InventoryItem>
protected readonly reservationItemService_: ModulesSdkTypes.IMedusaInternalService<ReservationItem>
protected readonly inventoryLevelService_: InventoryLevelService<InventoryLevel>
protected readonly inventoryLevelService_: InventoryLevelService
constructor(
{
@@ -20,12 +20,10 @@ type InjectedDependencies = {
orderChangeRepository: DAL.RepositoryService
}
export default class OrderChangeService<
TEntity extends OrderChange = OrderChange
> extends ModulesSdkUtils.MedusaInternalService<InjectedDependencies>(
export default class OrderChangeService extends ModulesSdkUtils.MedusaInternalService<InjectedDependencies>(
OrderChange
)<TEntity> {
protected readonly orderChangeRepository_: RepositoryService<TEntity>
)<OrderChange> {
protected readonly orderChangeRepository_: RepositoryService<OrderChange>
constructor(container: InjectedDependencies) {
// @ts-ignore
@@ -38,7 +36,7 @@ export default class OrderChangeService<
orderId: string | string[],
config: FindConfig<TEntityMethod> = {},
@MedusaContext() sharedContext: Context = {}
): Promise<TEntity[]> {
): Promise<OrderChange[]> {
const allChanges = await super.list(
{ order_id: orderId },
config ?? {
@@ -67,7 +65,7 @@ export default class OrderChangeService<
}
}
let orderChange!: TEntity
let orderChange!: OrderChange
if (allChanges?.length > 0) {
if (this.isActive(allChanges[0])) {
orderChange = allChanges[0]
@@ -81,7 +79,7 @@ export default class OrderChangeService<
const relations = deduplicate([...(config.relations ?? []), "actions"])
config.relations = relations
const queryConfig = ModulesSdkUtils.buildQuery<TEntity>(
const queryConfig = ModulesSdkUtils.buildQuery<OrderChange>(
{
id: lastChanges,
order: {
@@ -104,20 +102,20 @@ export default class OrderChangeService<
}
async create(
data: Partial<TEntity>[],
data: Partial<OrderChange>[],
sharedContext?: Context
): Promise<TEntity[]>
): Promise<OrderChange[]>
async create(
data: Partial<TEntity>,
data: Partial<OrderChange>,
sharedContext?: Context
): Promise<TEntity>
): Promise<OrderChange>
@InjectTransactionManager("orderChangeRepository_")
async create(
data: Partial<TEntity>[] | Partial<TEntity>,
data: Partial<OrderChange>[] | Partial<OrderChange>,
@MedusaContext() sharedContext: Context = {}
): Promise<TEntity[] | TEntity> {
): Promise<OrderChange[] | OrderChange> {
const dataArr = Array.isArray(data) ? data : [data]
const activeOrderEdit = await this.listCurrentOrderChange(
dataArr.map((d) => d.order_id!),
@@ -78,7 +78,7 @@ import OrderService from "./order-service"
type InjectedDependencies = {
baseRepository: DAL.RepositoryService
orderService: OrderService<any>
orderService: OrderService
addressService: ModulesSdkTypes.IMedusaInternalService<any>
lineItemService: ModulesSdkTypes.IMedusaInternalService<any>
shippingMethodAdjustmentService: ModulesSdkTypes.IMedusaInternalService<any>
@@ -87,7 +87,7 @@ type InjectedDependencies = {
lineItemTaxLineService: ModulesSdkTypes.IMedusaInternalService<any>
shippingMethodTaxLineService: ModulesSdkTypes.IMedusaInternalService<any>
transactionService: ModulesSdkTypes.IMedusaInternalService<any>
orderChangeService: OrderChangeService<any>
orderChangeService: OrderChangeService
orderChangeActionService: ModulesSdkTypes.IMedusaInternalService<any>
orderItemService: ModulesSdkTypes.IMedusaInternalService<any>
orderSummaryService: ModulesSdkTypes.IMedusaInternalService<any>
@@ -169,7 +169,7 @@ export default class OrderModuleService<
implements IOrderModuleService
{
protected baseRepository_: DAL.RepositoryService
protected orderService_: OrderService<TOrder>
protected orderService_: OrderService
protected addressService_: ModulesSdkTypes.IMedusaInternalService<TAddress>
protected lineItemService_: ModulesSdkTypes.IMedusaInternalService<TLineItem>
protected shippingMethodAdjustmentService_: ModulesSdkTypes.IMedusaInternalService<TShippingMethodAdjustment>
@@ -178,7 +178,7 @@ export default class OrderModuleService<
protected lineItemTaxLineService_: ModulesSdkTypes.IMedusaInternalService<TLineItemTaxLine>
protected shippingMethodTaxLineService_: ModulesSdkTypes.IMedusaInternalService<TShippingMethodTaxLine>
protected transactionService_: ModulesSdkTypes.IMedusaInternalService<TTransaction>
protected orderChangeService_: OrderChangeService<TOrderChange>
protected orderChangeService_: OrderChangeService
protected orderChangeActionService_: ModulesSdkTypes.IMedusaInternalService<TOrderChangeAction>
protected orderItemService_: ModulesSdkTypes.IMedusaInternalService<TOrderItem>
protected orderSummaryService_: ModulesSdkTypes.IMedusaInternalService<TOrderSummary>
@@ -17,12 +17,10 @@ type InjectedDependencies = {
orderRepository: DAL.RepositoryService
}
export default class OrderService<
TEntity extends Order = Order
> extends ModulesSdkUtils.MedusaInternalService<InjectedDependencies>(
export default class OrderService extends ModulesSdkUtils.MedusaInternalService<InjectedDependencies>(
Order
)<TEntity> {
protected readonly orderRepository_: RepositoryService<TEntity>
)<Order> {
protected readonly orderRepository_: RepositoryService<Order>
constructor(container: InjectedDependencies) {
// @ts-ignore
@@ -36,8 +34,8 @@ export default class OrderService<
version: number,
config: FindConfig<TEntityMethod> = {},
@MedusaContext() sharedContext: Context = {}
): Promise<TEntity> {
const queryConfig = ModulesSdkUtils.buildQuery<TEntity>(
): Promise<Order> {
const queryConfig = ModulesSdkUtils.buildQuery<Order>(
{ id, items: { version } },
{ ...config, take: 1 }
)
@@ -7,11 +7,9 @@ type InjectedDependencies = {
priceListRepository: DAL.RepositoryService
}
export default class PriceListService<
TEntity extends PriceList = PriceList
> extends ModulesSdkUtils.MedusaInternalService<InjectedDependencies>(
export default class PriceListService extends ModulesSdkUtils.MedusaInternalService<InjectedDependencies>(
PriceList
)<TEntity> {
)<PriceList> {
constructor(container: InjectedDependencies) {
// @ts-ignore
super(...arguments)
@@ -20,32 +18,32 @@ export default class PriceListService<
create(
data: ServiceTypes.CreatePriceListDTO[],
sharedContext?: Context
): Promise<TEntity[]>
): Promise<PriceList[]>
create(
data: ServiceTypes.CreatePriceListDTO,
sharedContext?: Context
): Promise<TEntity>
): Promise<PriceList>
async create(
data: ServiceTypes.CreatePriceListDTO | ServiceTypes.CreatePriceListDTO[],
sharedContext?: Context
): Promise<TEntity | TEntity[]> {
): Promise<PriceList | PriceList[]> {
const data_ = Array.isArray(data) ? data : [data]
const priceLists = this.normalizePriceListDate(data_)
return await super.create(priceLists, sharedContext)
}
// @ts-ignore
update(data: any[], sharedContext?: Context): Promise<TEntity[]>
update(data: any[], sharedContext?: Context): Promise<PriceList[]>
// @ts-ignore
update(data: any, sharedContext?: Context): Promise<TEntity>
update(data: any, sharedContext?: Context): Promise<PriceList>
// TODO: Add support for selector? and then rm ts ignore
// @ts-ignore
async update(
data: ServiceTypes.UpdatePriceListDTO | ServiceTypes.UpdatePriceListDTO[],
sharedContext?: Context
): Promise<TEntity | TEntity[]> {
): Promise<PriceList | PriceList[]> {
const data_ = Array.isArray(data) ? data : [data]
const priceLists = this.normalizePriceListDate(data_)
return await super.update(priceLists, sharedContext)
@@ -58,11 +58,11 @@ type InjectedDependencies = {
baseRepository: DAL.RepositoryService
pricingRepository: PricingRepositoryService
priceSetService: ModulesSdkTypes.IMedusaInternalService<any>
ruleTypeService: RuleTypeService<any>
ruleTypeService: RuleTypeService
priceRuleService: ModulesSdkTypes.IMedusaInternalService<any>
priceSetRuleTypeService: ModulesSdkTypes.IMedusaInternalService<any>
priceService: ModulesSdkTypes.IMedusaInternalService<any>
priceListService: PriceListService<any>
priceListService: PriceListService
priceListRuleService: ModulesSdkTypes.IMedusaInternalService<any>
priceListRuleValueService: ModulesSdkTypes.IMedusaInternalService<any>
}
@@ -99,12 +99,12 @@ export default class PricingModuleService
{
protected baseRepository_: DAL.RepositoryService
protected readonly pricingRepository_: PricingRepositoryService
protected readonly ruleTypeService_: RuleTypeService<RuleType>
protected readonly ruleTypeService_: RuleTypeService
protected readonly priceSetService_: ModulesSdkTypes.IMedusaInternalService<PriceSet>
protected readonly priceRuleService_: ModulesSdkTypes.IMedusaInternalService<PriceRule>
protected readonly priceSetRuleTypeService_: ModulesSdkTypes.IMedusaInternalService<PriceSetRuleType>
protected readonly priceService_: ModulesSdkTypes.IMedusaInternalService<Price>
protected readonly priceListService_: PriceListService<PriceList>
protected readonly priceListService_: PriceListService
protected readonly priceListRuleService_: ModulesSdkTypes.IMedusaInternalService<PriceListRule>
protected readonly priceListRuleValueService_: ModulesSdkTypes.IMedusaInternalService<PriceListRuleValue>
@@ -11,12 +11,10 @@ type InjectedDependencies = {
ruleTypeRepository: DAL.RepositoryService
}
export default class RuleTypeService<
TEntity extends RuleType = RuleType
> extends ModulesSdkUtils.MedusaInternalService<InjectedDependencies>(
export default class RuleTypeService extends ModulesSdkUtils.MedusaInternalService<InjectedDependencies>(
RuleType
)<TEntity> {
protected readonly ruleTypeRepository_: DAL.RepositoryService<TEntity>
)<RuleType> {
protected readonly ruleTypeRepository_: DAL.RepositoryService<RuleType>
constructor({ ruleTypeRepository }: InjectedDependencies) {
// @ts-ignore
@@ -27,17 +25,17 @@ export default class RuleTypeService<
create(
data: PricingTypes.CreateRuleTypeDTO,
sharedContext: Context
): Promise<TEntity>
): Promise<RuleType>
create(
data: PricingTypes.CreateRuleTypeDTO[],
sharedContext: Context
): Promise<TEntity[]>
): Promise<RuleType[]>
@InjectTransactionManager("ruleTypeRepository_")
async create(
data: PricingTypes.CreateRuleTypeDTO | PricingTypes.CreateRuleTypeDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<TEntity | TEntity[]> {
): Promise<RuleType | RuleType[]> {
const data_ = Array.isArray(data) ? data : [data]
validateRuleAttributes(data_.map((d) => d.rule_attribute))
return await super.create(data, sharedContext)
@@ -47,12 +45,12 @@ export default class RuleTypeService<
update(
data: PricingTypes.UpdateRuleTypeDTO[],
sharedContext: Context
): Promise<TEntity[]>
): Promise<RuleType[]>
// @ts-ignore
update(
data: PricingTypes.UpdateRuleTypeDTO,
sharedContext: Context
): Promise<TEntity>
): Promise<RuleType>
@InjectTransactionManager("ruleTypeRepository_")
// TODO: add support for selector? and then rm ts ignore
@@ -60,7 +58,7 @@ export default class RuleTypeService<
async update(
data: PricingTypes.UpdateRuleTypeDTO | PricingTypes.UpdateRuleTypeDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<TEntity | TEntity[]> {
): Promise<RuleType | RuleType[]> {
const data_ = Array.isArray(data) ? data : [data]
validateRuleAttributes(data_.map((d) => d.rule_attribute))
return await super.update(data, sharedContext)
@@ -15,9 +15,7 @@ import { UpdateCategoryInput } from "@types"
type InjectedDependencies = {
productCategoryRepository: DAL.TreeRepositoryService
}
export default class ProductCategoryService<
TEntity extends ProductCategory = ProductCategory
> {
export default class ProductCategoryService {
protected readonly productCategoryRepository_: DAL.TreeRepositoryService
constructor({ productCategoryRepository }: InjectedDependencies) {
@@ -30,7 +28,7 @@ export default class ProductCategoryService<
productCategoryId: string,
config: FindConfig<ProductTypes.ProductCategoryDTO> = {},
@MedusaContext() sharedContext: Context = {}
): Promise<TEntity> {
): Promise<ProductCategory> {
if (!isDefined(productCategoryId)) {
throw new MedusaError(
MedusaError.Types.NOT_FOUND,
@@ -64,7 +62,7 @@ export default class ProductCategoryService<
)
}
return productCategories[0] as TEntity
return productCategories[0] as ProductCategory
}
@InjectManager("productCategoryRepository_")
@@ -72,7 +70,7 @@ export default class ProductCategoryService<
filters: ProductTypes.FilterableProductCategoryProps = {},
config: FindConfig<ProductTypes.ProductCategoryDTO> = {},
@MedusaContext() sharedContext: Context = {}
): Promise<TEntity[]> {
): Promise<ProductCategory[]> {
const transformOptions = {
includeDescendantsTree: filters?.include_descendants_tree || false,
includeAncestorsTree: filters?.include_ancestors_tree || false,
@@ -101,7 +99,7 @@ export default class ProductCategoryService<
queryOptions,
transformOptions,
sharedContext
)) as TEntity[]
)) as ProductCategory[]
}
@InjectManager("productCategoryRepository_")
@@ -109,7 +107,7 @@ export default class ProductCategoryService<
filters: ProductTypes.FilterableProductCategoryProps = {},
config: FindConfig<ProductTypes.ProductCategoryDTO> = {},
@MedusaContext() sharedContext: Context = {}
): Promise<[TEntity[], number]> {
): Promise<[ProductCategory[], number]> {
const transformOptions = {
includeDescendantsTree: filters?.include_descendants_tree || false,
includeAncestorsTree: filters?.include_ancestors_tree || false,
@@ -138,27 +136,27 @@ export default class ProductCategoryService<
queryOptions,
transformOptions,
sharedContext
)) as [TEntity[], number]
)) as [ProductCategory[], number]
}
@InjectTransactionManager("productCategoryRepository_")
async create(
data: ProductTypes.CreateProductCategoryDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<TEntity[]> {
): Promise<ProductCategory[]> {
return (await (
this.productCategoryRepository_ as unknown as ProductCategoryRepository
).create(data, sharedContext)) as TEntity[]
).create(data, sharedContext)) as ProductCategory[]
}
@InjectTransactionManager("productCategoryRepository_")
async update(
data: UpdateCategoryInput[],
@MedusaContext() sharedContext: Context = {}
): Promise<TEntity[]> {
): Promise<ProductCategory[]> {
return (await (
this.productCategoryRepository_ as unknown as ProductCategoryRepository
).update(data, sharedContext)) as TEntity[]
).update(data, sharedContext)) as ProductCategory[]
}
@InjectTransactionManager("productCategoryRepository_")
@@ -55,10 +55,10 @@ import { entityNameToLinkableKeysMap, joinerConfig } from "./../joiner-config"
type InjectedDependencies = {
baseRepository: DAL.RepositoryService
productService: ProductService<any>
productService: ProductService
productVariantService: ModulesSdkTypes.IMedusaInternalService<any, any>
productTagService: ModulesSdkTypes.IMedusaInternalService<any>
productCategoryService: ProductCategoryService<any>
productCategoryService: ProductCategoryService
productCollectionService: ModulesSdkTypes.IMedusaInternalService<any>
productImageService: ModulesSdkTypes.IMedusaInternalService<any>
productTypeService: ModulesSdkTypes.IMedusaInternalService<any>
@@ -105,9 +105,9 @@ export default class ProductModuleService
implements ProductTypes.IProductModuleService
{
protected baseRepository_: DAL.RepositoryService
protected readonly productService_: ProductService<Product>
protected readonly productService_: ProductService
protected readonly productVariantService_: ModulesSdkTypes.IMedusaInternalService<ProductVariant>
protected readonly productCategoryService_: ProductCategoryService<ProductCategory>
protected readonly productCategoryService_: ProductCategoryService
protected readonly productTagService_: ModulesSdkTypes.IMedusaInternalService<ProductTag>
protected readonly productCollectionService_: ModulesSdkTypes.IMedusaInternalService<ProductCollection>
protected readonly productImageService_: ModulesSdkTypes.IMedusaInternalService<ProductImage>
@@ -18,12 +18,10 @@ type NormalizedFilterableProductProps = ProductTypes.FilterableProductProps & {
}
}
export default class ProductService<
TEntity extends Product = Product
> extends ModulesSdkUtils.MedusaInternalService<InjectedDependencies>(
export default class ProductService extends ModulesSdkUtils.MedusaInternalService<InjectedDependencies>(
Product
)<TEntity> {
protected readonly productRepository_: DAL.RepositoryService<TEntity>
)<Product> {
protected readonly productRepository_: DAL.RepositoryService<Product>
constructor({ productRepository }: InjectedDependencies) {
// @ts-ignore
@@ -36,9 +34,9 @@ export default class ProductService<
@InjectManager("productRepository_")
async list(
filters: ProductTypes.FilterableProductProps = {},
config: FindConfig<TEntity> = {},
config: FindConfig<Product> = {},
@MedusaContext() sharedContext: Context = {}
): Promise<TEntity[]> {
): Promise<Product[]> {
return await super.list(
ProductService.normalizeFilters(filters),
config,
@@ -51,7 +49,7 @@ export default class ProductService<
filters: ProductTypes.FilterableProductProps = {},
config: FindConfig<any> = {},
@MedusaContext() sharedContext: Context = {}
): Promise<[TEntity[], number]> {
): Promise<[Product[], number]> {
return await super.listAndCount(
ProductService.normalizeFilters(filters),
config,