feat: Use tag ids instead of values wherever possible (#8394)

This commit is contained in:
Stevche Radevski
2024-08-02 09:22:03 +02:00
committed by GitHub
parent 71f0d24359
commit 3a068c6b27
26 changed files with 259 additions and 229 deletions
@@ -1,4 +1,8 @@
import { IProductModuleService, ProductCategoryDTO } from "@medusajs/types"
import {
IProductModuleService,
ProductCategoryDTO,
ProductTagDTO,
} from "@medusajs/types"
import { kebabCase, Modules, ProductStatus } from "@medusajs/utils"
import {
Product,
@@ -104,6 +108,11 @@ moduleIntegrationTestRunner<IProductModuleService>({
categories.push(await service.createProductCategories(entry))
}
const tags: ProductTagDTO[] = []
for (const entry of tagsData) {
tags.push(await service.createProductTags(entry))
}
productCategoryOne = categories[0]
productCategoryTwo = categories[1]
@@ -125,7 +134,7 @@ moduleIntegrationTestRunner<IProductModuleService>({
status: ProductStatus.PUBLISHED,
categories: [{ id: productCategoryOne.id }],
collection_id: productCollectionOne.id,
tags: tagsData,
tags: [{ id: tags[0].id }],
options: [
{
title: "size",
@@ -437,6 +446,8 @@ moduleIntegrationTestRunner<IProductModuleService>({
value: "tag 2",
}
await service.createProductTags(newTagData)
const updateData = {
id: productTwo.id,
categories: [
@@ -446,7 +457,7 @@ moduleIntegrationTestRunner<IProductModuleService>({
],
collection_id: productCollectionTwo.id,
type_id: productTypeTwo.id,
tags: [newTagData],
tags: [{ id: newTagData.id }],
}
await service.upsertProducts([updateData])
@@ -1,3 +1,2 @@
export { default as ProductService } from "./product"
export { default as ProductCategoryService } from "./product-category"
export { default as ProductModuleService } from "./product-module-service"
@@ -18,7 +18,7 @@ import {
ProductType,
ProductVariant,
} from "@models"
import { ProductCategoryService, ProductService } from "@services"
import { ProductCategoryService } from "@services"
import {
arrayDifference,
@@ -55,7 +55,7 @@ import { joinerConfig } from "./../joiner-config"
type InjectedDependencies = {
baseRepository: DAL.RepositoryService
productService: ProductService
productService: ModulesSdkTypes.IMedusaInternalService<any, any>
productVariantService: ModulesSdkTypes.IMedusaInternalService<any, any>
productTagService: ModulesSdkTypes.IMedusaInternalService<any>
productCategoryService: ProductCategoryService
@@ -102,7 +102,7 @@ export default class ProductModuleService
implements ProductTypes.IProductModuleService
{
protected baseRepository_: DAL.RepositoryService
protected readonly productService_: ProductService
protected readonly productService_: ModulesSdkTypes.IMedusaInternalService<Product>
protected readonly productVariantService_: ModulesSdkTypes.IMedusaInternalService<ProductVariant>
protected readonly productCategoryService_: ProductCategoryService
protected readonly productTagService_: ModulesSdkTypes.IMedusaInternalService<ProductTag>
@@ -1609,26 +1609,6 @@ export default class ProductModuleService
productData.discountable = false
}
if (productData.tags?.length && productData.tags.some((t) => !t.id)) {
const dbTags = await this.productTagService_.list(
{
value: productData.tags
.map((t) => t.value)
.filter((v) => !!v) as string[],
},
{ take: null },
sharedContext
)
productData.tags = productData.tags.map((tag) => {
const dbTag = dbTags.find((t) => t.value === tag.value)
return {
...tag,
...(dbTag ? { id: dbTag.id } : {}),
}
})
}
if (productData.options?.length) {
const dbOptions = await this.productOptionService_.list(
{ product_id: productData.id },
@@ -1652,6 +1632,13 @@ export default class ProductModuleService
})
}
if (productData.tag_ids) {
;(productData as any).tags = productData.tag_ids.map((cid) => ({
id: cid,
}))
delete productData.tag_ids
}
if (productData.category_ids) {
;(productData as any).categories = productData.category_ids.map(
(cid) => ({
@@ -1,80 +0,0 @@
import {
Context,
DAL,
FilterableProductProps,
FindConfig,
ProductTypes,
} from "@medusajs/types"
import { InjectManager, MedusaContext, ModulesSdkUtils } from "@medusajs/utils"
import { Product } from "@models"
type InjectedDependencies = {
productRepository: DAL.RepositoryService
}
type NormalizedFilterableProductProps = ProductTypes.FilterableProductProps & {
categories?: {
id: string | { $in: string[] }
}
}
export default class ProductService extends ModulesSdkUtils.MedusaInternalService<
InjectedDependencies,
Product
>(Product) {
protected readonly productRepository_: DAL.RepositoryService<Product>
constructor({ productRepository }: InjectedDependencies) {
// @ts-ignore
// eslint-disable-next-line prefer-rest-params
super(...arguments)
this.productRepository_ = productRepository
}
@InjectManager("productRepository_")
async list(
filters: ProductTypes.FilterableProductProps = {},
config: FindConfig<Product> = {},
@MedusaContext() sharedContext: Context = {}
): Promise<Product[]> {
return await super.list(
ProductService.normalizeFilters(filters),
config,
sharedContext
)
}
@InjectManager("productRepository_")
async listAndCount(
filters: ProductTypes.FilterableProductProps = {},
config: FindConfig<any> = {},
@MedusaContext() sharedContext: Context = {}
): Promise<[Product[], number]> {
return await super.listAndCount(
ProductService.normalizeFilters(filters),
config,
sharedContext
)
}
protected static normalizeFilters(
filters: FilterableProductProps = {}
): NormalizedFilterableProductProps {
const normalized = filters as NormalizedFilterableProductProps
if (normalized.category_id) {
if (Array.isArray(normalized.category_id)) {
normalized.categories = {
id: { $in: normalized.category_id },
}
} else {
normalized.categories = {
id: normalized.category_id as string,
}
}
delete normalized.category_id
}
return normalized
}
}