Chore/rm main entity concept (#7709)

**What**
Update the `MedusaService` class, factory and types to remove the concept of main modules. The idea being that all method will be explicitly named and suffixes to represent the object you are trying to manipulate.
This pr also includes various fixes in different modules

Co-authored-by: Stevche Radevski <4820812+sradevski@users.noreply.github.com>
Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
Adrien de Peretti
2024-06-19 13:02:16 +00:00
committed by GitHub
co-authored by Stevche Radevski Oli Juhl
parent 2895ccfba8
commit 48963f55ef
533 changed files with 6469 additions and 9769 deletions
@@ -8,7 +8,7 @@ import {
ProductTypes,
} from "@medusajs/types"
import {
Image,
Image as ProductImage,
Product,
ProductCategory,
ProductCollection,
@@ -31,15 +31,13 @@ import {
kebabCase,
MedusaContext,
MedusaError,
ModulesSdkUtils,
MedusaService,
ProductStatus,
promiseAll,
removeUndefined,
toHandle,
} from "@medusajs/utils"
import {
ProductCategoryEventData,
ProductCategoryEvents,
ProductCollectionEventData,
ProductCollectionEvents,
ProductEventData,
@@ -69,81 +67,53 @@ type InjectedDependencies = {
eventBusModuleService?: IEventBusModuleService
}
const generateMethodForModels = {
ProductCategory: { singular: "Category", plural: "Categories" },
ProductCollection: { singular: "Collection", plural: "Collections" },
ProductOption: { singular: "Option", plural: "Options" },
ProductTag: { singular: "Tag", plural: "Tags" },
ProductType: { singular: "Type", plural: "Types" },
ProductVariant: { singular: "Variant", plural: "Variants" },
}
export default class ProductModuleService<
TProduct extends Product = Product,
TProductVariant extends ProductVariant = ProductVariant,
TProductTag extends ProductTag = ProductTag,
TProductCollection extends ProductCollection = ProductCollection,
TProductCategory extends ProductCategory = ProductCategory,
TProductImage extends Image = Image,
TProductType extends ProductType = ProductType,
TProductOption extends ProductOption = ProductOption,
TProductOptionValue extends ProductOptionValue = ProductOptionValue
>
extends ModulesSdkUtils.MedusaService<
ProductTypes.ProductDTO,
{
ProductCategory: {
dto: ProductTypes.ProductCategoryDTO
singular: "Category"
plural: "Categories"
}
ProductCollection: {
dto: ProductTypes.ProductCollectionDTO
singular: "Collection"
plural: "Collections"
}
ProductOption: {
dto: ProductTypes.ProductOptionDTO
singular: "Option"
plural: "Options"
}
ProductTag: {
dto: ProductTypes.ProductTagDTO
singular: "Tag"
plural: "Tags"
}
ProductType: {
dto: ProductTypes.ProductTypeDTO
singular: "Type"
plural: "Types"
}
ProductVariant: {
dto: ProductTypes.ProductVariantDTO
singular: "Variant"
plural: "Variants"
}
export default class ProductModuleService
extends MedusaService<{
Product: {
dto: ProductTypes.ProductDTO
}
>(Product, generateMethodForModels, entityNameToLinkableKeysMap)
ProductCategory: {
dto: ProductTypes.ProductCategoryDTO
}
ProductCollection: {
dto: ProductTypes.ProductCollectionDTO
}
ProductOption: {
dto: ProductTypes.ProductOptionDTO
}
ProductTag: {
dto: ProductTypes.ProductTagDTO
}
ProductType: {
dto: ProductTypes.ProductTypeDTO
}
ProductVariant: {
dto: ProductTypes.ProductVariantDTO
}
}>(
{
Product,
ProductCategory,
ProductCollection,
ProductOption,
ProductTag,
ProductType,
ProductVariant,
},
entityNameToLinkableKeysMap
)
implements ProductTypes.IProductModuleService
{
protected baseRepository_: DAL.RepositoryService
protected readonly productService_: ProductService<TProduct>
// eslint-disable-next-line max-len
protected readonly productVariantService_: ModulesSdkTypes.IMedusaInternalService<TProductVariant>
// eslint-disable-next-line max-len
protected readonly productCategoryService_: ProductCategoryService<TProductCategory>
// eslint-disable-next-line max-len
protected readonly productTagService_: ModulesSdkTypes.IMedusaInternalService<TProductTag>
// eslint-disable-next-line max-len
protected readonly productCollectionService_: ModulesSdkTypes.IMedusaInternalService<TProductCollection>
// eslint-disable-next-line max-len
protected readonly productImageService_: ModulesSdkTypes.IMedusaInternalService<TProductImage>
// eslint-disable-next-line max-len
protected readonly productTypeService_: ModulesSdkTypes.IMedusaInternalService<TProductType>
// eslint-disable-next-line max-len
protected readonly productOptionService_: ModulesSdkTypes.IMedusaInternalService<TProductOption>
// eslint-disable-next-line max-len
protected readonly productOptionValueService_: ModulesSdkTypes.IMedusaInternalService<TProductOptionValue>
protected readonly productService_: ProductService<Product>
protected readonly productVariantService_: ModulesSdkTypes.IMedusaInternalService<ProductVariant>
protected readonly productCategoryService_: ProductCategoryService<ProductCategory>
protected readonly productTagService_: ModulesSdkTypes.IMedusaInternalService<ProductTag>
protected readonly productCollectionService_: ModulesSdkTypes.IMedusaInternalService<ProductCollection>
protected readonly productImageService_: ModulesSdkTypes.IMedusaInternalService<ProductImage>
protected readonly productTypeService_: ModulesSdkTypes.IMedusaInternalService<ProductType>
protected readonly productOptionService_: ModulesSdkTypes.IMedusaInternalService<ProductOption>
protected readonly productOptionValueService_: ModulesSdkTypes.IMedusaInternalService<ProductOptionValue>
protected readonly eventBusModuleService_?: IEventBusModuleService
constructor(
@@ -185,18 +155,18 @@ export default class ProductModuleService<
// TODO: Add options validation, among other things
// @ts-ignore
createVariants(
createProductVariants(
data: ProductTypes.CreateProductVariantDTO[],
sharedContext?: Context
): Promise<ProductTypes.ProductVariantDTO[]>
createVariants(
createProductVariants(
data: ProductTypes.CreateProductVariantDTO,
sharedContext?: Context
): Promise<ProductTypes.ProductVariantDTO>
@InjectManager("baseRepository_")
@EmitEvents()
async createVariants(
async createProductVariants(
data:
| ProductTypes.CreateProductVariantDTO[]
| ProductTypes.CreateProductVariantDTO,
@@ -254,18 +224,18 @@ export default class ProductModuleService<
return createdVariants
}
async upsertVariants(
async upsertProductVariants(
data: ProductTypes.UpsertProductVariantDTO[],
sharedContext?: Context
): Promise<ProductTypes.ProductVariantDTO[]>
async upsertVariants(
async upsertProductVariants(
data: ProductTypes.UpsertProductVariantDTO,
sharedContext?: Context
): Promise<ProductTypes.ProductVariantDTO>
@InjectTransactionManager("baseRepository_")
@EmitEvents()
async upsertVariants(
async upsertProductVariants(
data:
| ProductTypes.UpsertProductVariantDTO[]
| ProductTypes.UpsertProductVariantDTO,
@@ -300,12 +270,12 @@ export default class ProductModuleService<
}
// @ts-ignore
updateVariants(
updateProductVariants(
id: string,
data: ProductTypes.UpdateProductVariantDTO,
sharedContext?: Context
): Promise<ProductTypes.ProductVariantDTO>
updateVariants(
updateProductVariants(
selector: ProductTypes.FilterableProductVariantProps,
data: ProductTypes.UpdateProductVariantDTO,
sharedContext?: Context
@@ -313,7 +283,7 @@ export default class ProductModuleService<
@InjectManager("baseRepository_")
@EmitEvents()
async updateVariants(
async updateProductVariants(
idOrSelector: string | ProductTypes.FilterableProductVariantProps,
data: ProductTypes.UpdateProductVariantDTO,
@MedusaContext() sharedContext: Context = {}
@@ -349,7 +319,7 @@ export default class ProductModuleService<
protected async updateVariants_(
data: UpdateProductVariantInput[],
@MedusaContext() sharedContext: Context = {}
): Promise<TProductVariant[]> {
): Promise<ProductVariant[]> {
// Validation step
const variantIdsToUpdate = data.map(({ id }) => id)
const variants = await this.productVariantService_.list(
@@ -415,18 +385,18 @@ export default class ProductModuleService<
}
// @ts-ignore
createTags(
createProductTags(
data: ProductTypes.CreateProductTagDTO[],
sharedContext?: Context
): Promise<ProductTypes.ProductTagDTO[]>
createTags(
createProductTags(
data: ProductTypes.CreateProductTagDTO,
sharedContext?: Context
): Promise<ProductTypes.ProductTagDTO>
@InjectManager("baseRepository_")
@EmitEvents()
async createTags(
async createProductTags(
data: ProductTypes.CreateProductTagDTO[] | ProductTypes.CreateProductTagDTO,
@MedusaContext() sharedContext: Context = {}
): Promise<ProductTypes.ProductTagDTO[] | ProductTypes.ProductTagDTO> {
@@ -446,18 +416,18 @@ export default class ProductModuleService<
return Array.isArray(data) ? createdTags : createdTags[0]
}
async upsertTags(
async upsertProductTags(
data: ProductTypes.UpsertProductTagDTO[],
sharedContext?: Context
): Promise<ProductTypes.ProductTagDTO[]>
async upsertTags(
async upsertProductTags(
data: ProductTypes.UpsertProductTagDTO,
sharedContext?: Context
): Promise<ProductTypes.ProductTagDTO>
@InjectTransactionManager("baseRepository_")
@EmitEvents()
async upsertTags(
async upsertProductTags(
data: ProductTypes.UpsertProductTagDTO[] | ProductTypes.UpsertProductTagDTO,
@MedusaContext() sharedContext: Context = {}
): Promise<ProductTypes.ProductTagDTO[] | ProductTypes.ProductTagDTO> {
@@ -494,12 +464,12 @@ export default class ProductModuleService<
}
// @ts-ignore
updateTags(
updateProductTags(
id: string,
data: ProductTypes.UpdateProductTagDTO,
sharedContext?: Context
): Promise<ProductTypes.ProductTagDTO>
updateTags(
updateProductTags(
selector: ProductTypes.FilterableProductTagProps,
data: ProductTypes.UpdateProductTagDTO,
sharedContext?: Context
@@ -507,7 +477,7 @@ export default class ProductModuleService<
@InjectManager("baseRepository_")
@EmitEvents()
async updateTags(
async updateProductTags(
idOrSelector: string | ProductTypes.FilterableProductTagProps,
data: ProductTypes.UpdateProductTagDTO,
@MedusaContext() sharedContext: Context = {}
@@ -548,17 +518,17 @@ export default class ProductModuleService<
}
// @ts-ignore
createTypes(
createProductTypes(
data: ProductTypes.CreateProductTypeDTO[],
sharedContext?: Context
): Promise<ProductTypes.ProductTypeDTO[]>
createTypes(
createProductTypes(
data: ProductTypes.CreateProductTypeDTO,
sharedContext?: Context
): Promise<ProductTypes.ProductTypeDTO>
@InjectManager("baseRepository_")
async createTypes(
async createProductTypes(
data:
| ProductTypes.CreateProductTypeDTO[]
| ProductTypes.CreateProductTypeDTO,
@@ -575,17 +545,17 @@ export default class ProductModuleService<
return Array.isArray(data) ? createdTypes : createdTypes[0]
}
async upsertTypes(
async upsertProductTypes(
data: ProductTypes.UpsertProductTypeDTO[],
sharedContext?: Context
): Promise<ProductTypes.ProductTypeDTO[]>
async upsertTypes(
async upsertProductTypes(
data: ProductTypes.UpsertProductTypeDTO,
sharedContext?: Context
): Promise<ProductTypes.ProductTypeDTO>
@InjectTransactionManager("baseRepository_")
async upsertTypes(
async upsertProductTypes(
data:
| ProductTypes.UpsertProductTypeDTO[]
| ProductTypes.UpsertProductTypeDTO,
@@ -616,19 +586,19 @@ export default class ProductModuleService<
}
// @ts-ignore
updateTypes(
updateProductTypes(
id: string,
data: ProductTypes.UpdateProductTypeDTO,
sharedContext?: Context
): Promise<ProductTypes.ProductTypeDTO>
updateTypes(
updateProductTypes(
selector: ProductTypes.FilterableProductTypeProps,
data: ProductTypes.UpdateProductTypeDTO,
sharedContext?: Context
): Promise<ProductTypes.ProductTypeDTO[]>
@InjectManager("baseRepository_")
async updateTypes(
async updateProductTypes(
idOrSelector: string | ProductTypes.FilterableProductTypeProps,
data: ProductTypes.UpdateProductTypeDTO,
@MedusaContext() sharedContext: Context = {}
@@ -664,17 +634,17 @@ export default class ProductModuleService<
}
// @ts-ignore
createOptions(
createProductOptions(
data: ProductTypes.CreateProductOptionDTO[],
sharedContext?: Context
): Promise<ProductTypes.ProductOptionDTO[]>
createOptions(
createProductOptions(
data: ProductTypes.CreateProductOptionDTO,
sharedContext?: Context
): Promise<ProductTypes.ProductOptionDTO>
@InjectManager("baseRepository_")
async createOptions(
async createProductOptions(
data:
| ProductTypes.CreateProductOptionDTO[]
| ProductTypes.CreateProductOptionDTO,
@@ -718,17 +688,17 @@ export default class ProductModuleService<
)
}
async upsertOptions(
async upsertProductOptions(
data: ProductTypes.UpsertProductOptionDTO[],
sharedContext?: Context
): Promise<ProductTypes.ProductOptionDTO[]>
async upsertOptions(
async upsertProductOptions(
data: ProductTypes.UpsertProductOptionDTO,
sharedContext?: Context
): Promise<ProductTypes.ProductOptionDTO>
@InjectTransactionManager("baseRepository_")
async upsertOptions(
async upsertProductOptions(
data:
| ProductTypes.UpsertProductOptionDTO[]
| ProductTypes.UpsertProductOptionDTO,
@@ -761,19 +731,19 @@ export default class ProductModuleService<
}
// @ts-ignore
updateOptions(
updateProductOptions(
id: string,
data: ProductTypes.UpdateProductOptionDTO,
sharedContext?: Context
): Promise<ProductTypes.ProductOptionDTO>
updateOptions(
updateProductOptions(
selector: ProductTypes.FilterableProductOptionProps,
data: ProductTypes.UpdateProductOptionDTO,
sharedContext?: Context
): Promise<ProductTypes.ProductOptionDTO[]>
@InjectManager("baseRepository_")
async updateOptions(
async updateProductOptions(
idOrSelector: string | ProductTypes.FilterableProductOptionProps,
data: ProductTypes.UpdateProductOptionDTO,
@MedusaContext() sharedContext: Context = {}
@@ -878,17 +848,17 @@ export default class ProductModuleService<
}
// @ts-ignore
createCollections(
createProductCollections(
data: ProductTypes.CreateProductCollectionDTO[],
sharedContext?: Context
): Promise<ProductTypes.ProductCollectionDTO[]>
createCollections(
createProductCollections(
data: ProductTypes.CreateProductCollectionDTO,
sharedContext?: Context
): Promise<ProductTypes.ProductCollectionDTO>
@InjectManager("baseRepository_")
async createCollections(
async createProductCollections(
data:
| ProductTypes.CreateProductCollectionDTO[]
| ProductTypes.CreateProductCollectionDTO,
@@ -918,7 +888,7 @@ export default class ProductModuleService<
async createCollections_(
data: ProductTypes.CreateProductCollectionDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<TProductCollection[]> {
): Promise<ProductCollection[]> {
const normalizedInput = data.map(
ProductModuleService.normalizeCreateProductCollectionInput
)
@@ -935,16 +905,16 @@ export default class ProductModuleService<
return productCollections
}
async upsertCollections(
async upsertProductCollections(
data: ProductTypes.UpsertProductCollectionDTO[],
sharedContext?: Context
): Promise<ProductTypes.ProductCollectionDTO[]>
async upsertCollections(
async upsertProductCollections(
data: ProductTypes.UpsertProductCollectionDTO,
sharedContext?: Context
): Promise<ProductTypes.ProductCollectionDTO>
@InjectTransactionManager("baseRepository_")
async upsertCollections(
async upsertProductCollections(
data:
| ProductTypes.UpsertProductCollectionDTO[]
| ProductTypes.UpsertProductCollectionDTO,
@@ -998,19 +968,19 @@ export default class ProductModuleService<
}
// @ts-ignore
updateCollections(
updateProductCollections(
id: string,
data: ProductTypes.UpdateProductCollectionDTO,
sharedContext?: Context
): Promise<ProductTypes.ProductCollectionDTO>
updateCollections(
updateProductCollections(
selector: ProductTypes.FilterableProductCollectionProps,
data: ProductTypes.UpdateProductCollectionDTO,
sharedContext?: Context
): Promise<ProductTypes.ProductCollectionDTO[]>
@InjectManager("baseRepository_")
async updateCollections(
async updateProductCollections(
idOrSelector: string | ProductTypes.FilterableProductCollectionProps,
data: ProductTypes.UpdateProductCollectionDTO,
@MedusaContext() sharedContext: Context = {}
@@ -1061,7 +1031,7 @@ export default class ProductModuleService<
protected async updateCollections_(
data: UpdateCollectionInput[],
@MedusaContext() sharedContext: Context = {}
): Promise<TProductCollection[]> {
): Promise<ProductCollection[]> {
const normalizedInput = data.map(
ProductModuleService.normalizeUpdateProductCollectionInput
) as UpdateCollectionInput[]
@@ -1075,7 +1045,7 @@ export default class ProductModuleService<
sharedContext
)
const collections: TProductCollection[] = []
const collections: ProductCollection[] = []
const updateSelectorAndData = updatedCollections.flatMap(
(collectionData) => {
@@ -1114,7 +1084,7 @@ export default class ProductModuleService<
collections.push({
...collectionData,
products: productsToUpdate ?? [],
})
} as ProductCollection)
return result
}
@@ -1125,18 +1095,18 @@ export default class ProductModuleService<
}
// @ts-ignore
createCategories(
createProductCategories(
data: ProductTypes.CreateProductCategoryDTO[],
sharedContext?: Context
): Promise<ProductTypes.ProductCategoryDTO[]>
createCategories(
createProductCategories(
data: ProductTypes.CreateProductCategoryDTO,
sharedContext?: Context
): Promise<ProductTypes.ProductCategoryDTO>
@InjectManager("baseRepository_")
@EmitEvents()
async createCategories(
async createProductCategories(
data:
| ProductTypes.CreateProductCategoryDTO[]
| ProductTypes.CreateProductCategoryDTO,
@@ -1163,18 +1133,18 @@ export default class ProductModuleService<
return Array.isArray(data) ? createdCategories : createdCategories[0]
}
async upsertCategories(
async upsertProductCategories(
data: ProductTypes.UpsertProductCategoryDTO[],
sharedContext?: Context
): Promise<ProductTypes.ProductCategoryDTO[]>
async upsertCategories(
async upsertProductCategories(
data: ProductTypes.UpsertProductCategoryDTO,
sharedContext?: Context
): Promise<ProductTypes.ProductCategoryDTO>
@InjectTransactionManager("baseRepository_")
@EmitEvents()
async upsertCategories(
async upsertProductCategories(
data:
| ProductTypes.UpsertProductCategoryDTO[]
| ProductTypes.UpsertProductCategoryDTO,
@@ -1229,12 +1199,12 @@ export default class ProductModuleService<
}
// @ts-ignore
updateCategories(
updateProductCategories(
id: string,
data: ProductTypes.UpdateProductCategoryDTO,
sharedContext?: Context
): Promise<ProductTypes.ProductCategoryDTO>
updateCategories(
updateProductCategories(
selector: ProductTypes.FilterableProductTypeProps,
data: ProductTypes.UpdateProductCategoryDTO,
sharedContext?: Context
@@ -1242,7 +1212,7 @@ export default class ProductModuleService<
@InjectManager("baseRepository_")
@EmitEvents()
async updateCategories(
async updateProductCategories(
idOrSelector: string | ProductTypes.FilterableProductTypeProps,
data: ProductTypes.UpdateProductCategoryDTO,
@MedusaContext() sharedContext: Context = {}
@@ -1288,22 +1258,23 @@ export default class ProductModuleService<
return isString(idOrSelector) ? updatedCategories[0] : updatedCategories
}
create(
//@ts-expect-error
createProducts(
data: ProductTypes.CreateProductDTO[],
sharedContext?: Context
): Promise<ProductTypes.ProductDTO[]>
create(
createProducts(
data: ProductTypes.CreateProductDTO,
sharedContext?: Context
): Promise<ProductTypes.ProductDTO>
@InjectManager("baseRepository_")
async create(
async createProducts(
data: ProductTypes.CreateProductDTO[] | ProductTypes.CreateProductDTO,
@MedusaContext() sharedContext: Context = {}
): Promise<ProductTypes.ProductDTO[] | ProductTypes.ProductDTO> {
const input = Array.isArray(data) ? data : [data]
const products = await this.create_(input, sharedContext)
const products = await this.createProducts_(input, sharedContext)
const createdProducts = await this.baseRepository_.serialize<
ProductTypes.ProductDTO[]
@@ -1319,16 +1290,17 @@ export default class ProductModuleService<
return Array.isArray(data) ? createdProducts : createdProducts[0]
}
async upsert(
async upsertProducts(
data: ProductTypes.UpsertProductDTO[],
sharedContext?: Context
): Promise<ProductTypes.ProductDTO[]>
async upsert(
async upsertProducts(
data: ProductTypes.UpsertProductDTO,
sharedContext?: Context
): Promise<ProductTypes.ProductDTO>
@InjectTransactionManager("baseRepository_")
async upsert(
async upsertProducts(
data: ProductTypes.UpsertProductDTO[] | ProductTypes.UpsertProductDTO,
@MedusaContext() sharedContext: Context = {}
): Promise<ProductTypes.ProductDTO[] | ProductTypes.ProductDTO> {
@@ -1344,10 +1316,10 @@ export default class ProductModuleService<
let updated: Product[] = []
if (forCreate.length) {
created = await this.create_(forCreate, sharedContext)
created = await this.createProducts_(forCreate, sharedContext)
}
if (forUpdate.length) {
updated = await this.update_(forUpdate, sharedContext)
updated = await this.updateProducts_(forUpdate, sharedContext)
}
const result = [...created, ...updated]
@@ -1376,19 +1348,20 @@ export default class ProductModuleService<
return Array.isArray(data) ? allProducts : allProducts[0]
}
update(
// @ts-expect-error
updateProducts(
id: string,
data: ProductTypes.UpdateProductDTO,
sharedContext?: Context
): Promise<ProductTypes.ProductDTO>
update(
updateProducts(
selector: ProductTypes.FilterableProductProps,
data: ProductTypes.UpdateProductDTO,
sharedContext?: Context
): Promise<ProductTypes.ProductDTO[]>
@InjectManager("baseRepository_")
async update(
async updateProducts(
idOrSelector: string | ProductTypes.FilterableProductProps,
data: ProductTypes.UpdateProductDTO,
@MedusaContext() sharedContext: Context = {}
@@ -1412,7 +1385,7 @@ export default class ProductModuleService<
}))
}
const products = await this.update_(normalizedInput, sharedContext)
const products = await this.updateProducts_(normalizedInput, sharedContext)
const updatedProducts = await this.baseRepository_.serialize<
ProductTypes.ProductDTO[]
@@ -1429,10 +1402,10 @@ export default class ProductModuleService<
}
@InjectTransactionManager("baseRepository_")
protected async create_(
protected async createProducts_(
data: ProductTypes.CreateProductDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<TProduct[]> {
): Promise<Product[]> {
const normalizedInput = await promiseAll(
data.map(async (d) => {
const normalized = await this.normalizeCreateProductInput(
@@ -1495,10 +1468,10 @@ export default class ProductModuleService<
}
@InjectTransactionManager("baseRepository_")
protected async update_(
protected async updateProducts_(
data: UpdateProductInput[],
@MedusaContext() sharedContext: Context = {}
): Promise<TProduct[]> {
): Promise<Product[]> {
const normalizedInput = await promiseAll(
data.map(async (d) => {
const normalized = await this.normalizeUpdateProductInput(