feat(medusa): add or remove categories from products (#3114)

* wip

* chore: fix issues with join table

* chore: fix issues

* chore: fix ordering issue on random failing test

* chore: revert table name

* chore: added oas for category

* chore: update categories for a product

* chore: add remove category test

* chore: added changeset

* chore: address review comments

* Apply suggestions from code review

Co-authored-by: Philip Korsholm <88927411+pKorsholm@users.noreply.github.com>

Co-authored-by: Philip Korsholm <88927411+pKorsholm@users.noreply.github.com>
This commit is contained in:
Riqwan Thamir
2023-01-27 15:25:46 +01:00
committed by GitHub
co-authored by Philip Korsholm
parent 44dbe55f50
commit ee42b60a20
12 changed files with 249 additions and 6 deletions
@@ -65,6 +65,7 @@ describe("GET /admin/products/:id", () => {
"tags",
"type",
"collection",
"categories",
"sales_channels",
],
}
@@ -17,6 +17,7 @@ import {
} from "../../../../services"
import {
ProductSalesChannelReq,
ProductProductCategoryReq,
ProductTagReq,
ProductTypeReq,
} from "../../../../types/product"
@@ -340,6 +341,16 @@ class ProductVariantReq {
* id:
* description: The ID of an existing Sales channel.
* type: string
* categories:
* description: "Categories to add the Product to."
* type: array
* items:
* required:
* - id
* properties:
* id:
* description: The ID of a Product Category.
* type: string
* options:
* description: The Options that the Product should have. These define on which properties the Product's Product Variants will differ.
* type: array
@@ -527,6 +538,12 @@ export class AdminPostProductsReq {
])
sales_channels?: ProductSalesChannelReq[]
@IsOptional()
@Type(() => ProductProductCategoryReq)
@ValidateNested({ each: true })
@IsArray()
categories?: ProductProductCategoryReq[]
@IsOptional()
@Type(() => ProductOptionReq)
@ValidateNested({ each: true })
@@ -100,6 +100,7 @@ export const defaultAdminProductRelations = [
"tags",
"type",
"collection",
"categories",
]
export const defaultAdminProductFields: (keyof Product)[] = [
@@ -17,6 +17,7 @@ import {
ProductSalesChannelReq,
ProductTagReq,
ProductTypeReq,
ProductProductCategoryReq,
} from "../../../../types/product"
import { Type } from "class-transformer"
@@ -278,6 +279,16 @@ class ProductVariantReq {
* id:
* description: The ID of an existing Sales channel.
* type: string
* categories:
* description: "Categories to add the Product to."
* type: array
* items:
* required:
* - id
* properties:
* id:
* description: The ID of a Product Category.
* type: string
* variants:
* description: A list of Product Variants to create with the Product.
* type: array
@@ -459,6 +470,12 @@ export class AdminPostProductsProductReq {
])
sales_channels?: ProductSalesChannelReq[] | null
@IsOptional()
@Type(() => ProductProductCategoryReq)
@ValidateNested({ each: true })
@IsArray()
categories?: ProductProductCategoryReq[]
@IsOptional()
@Type(() => ProductVariantReq)
@ValidateNested({ each: true })
@@ -56,11 +56,11 @@ export class ProductCategory extends SoftDeletableEntity {
@JoinTable({
name: "product_category_product",
joinColumn: {
name: "product_id",
name: "product_category_id",
referencedColumnName: "id",
},
inverseJoinColumn: {
name: "product_category_id",
name: "product_id",
referencedColumnName: "id",
},
})
@@ -122,6 +122,12 @@ export class ProductCategory extends SoftDeletableEntity {
* parent_category:
* description: A product category object. Available if the relation `parent_category` is expanded.
* type: object
* products:
* description: products associated with category. Available if the relation `products` is expanded.
* type: array
* items:
* type: object
* description: A product object.
* created_at:
* type: string
* description: "The date with timezone at which the resource was created."
+8 -2
View File
@@ -82,11 +82,11 @@ export class Product extends SoftDeletableEntity {
@JoinTable({
name: "product_category_product",
joinColumn: {
name: "product_category_id",
name: "product_id",
referencedColumnName: "id",
},
inverseJoinColumn: {
name: "product_id",
name: "product_category_id",
referencedColumnName: "id",
},
})
@@ -320,6 +320,12 @@ export class Product extends SoftDeletableEntity {
* items:
* type: object
* description: A sales channel object.
* categories:
* description: The product's associated categories. Available if the relation `categories` is expanded.
* type: array
* items:
* type: object
* description: A category object.
* created_at:
* type: string
* description: "The date with timezone at which the resource was created."
+31
View File
@@ -12,12 +12,14 @@ import {
ProductType,
ProductVariant,
SalesChannel,
ProductCategory,
} from "../models"
import { ImageRepository } from "../repositories/image"
import {
FindWithoutRelationsOptions,
ProductRepository,
} from "../repositories/product"
import { ProductCategoryRepository } from "../repositories/product-category"
import { ProductOptionRepository } from "../repositories/product-option"
import { ProductTagRepository } from "../repositories/product-tag"
import { ProductTypeRepository } from "../repositories/product-type"
@@ -42,6 +44,7 @@ type InjectedDependencies = {
productTypeRepository: typeof ProductTypeRepository
productTagRepository: typeof ProductTagRepository
imageRepository: typeof ImageRepository
productCategoryRepository: typeof ProductCategoryRepository
productVariantService: ProductVariantService
searchService: SearchService
eventBusService: EventBusService
@@ -58,6 +61,8 @@ class ProductService extends TransactionBaseService {
protected readonly productTypeRepository_: typeof ProductTypeRepository
protected readonly productTagRepository_: typeof ProductTagRepository
protected readonly imageRepository_: typeof ImageRepository
// eslint-disable-next-line max-len
protected readonly productCategoryRepository_: typeof ProductCategoryRepository
protected readonly productVariantService_: ProductVariantService
protected readonly searchService_: SearchService
protected readonly eventBus_: EventBusService
@@ -79,6 +84,7 @@ class ProductService extends TransactionBaseService {
productVariantService,
productTypeRepository,
productTagRepository,
productCategoryRepository,
imageRepository,
searchService,
featureFlagRouter,
@@ -92,6 +98,7 @@ class ProductService extends TransactionBaseService {
this.productVariantRepository_ = productVariantRepository
this.eventBus_ = eventBusService
this.productVariantService_ = productVariantService
this.productCategoryRepository_ = productCategoryRepository
this.productTypeRepository_ = productTypeRepository
this.productTagRepository_ = productTagRepository
this.imageRepository_ = imageRepository
@@ -393,6 +400,7 @@ class ProductService extends TransactionBaseService {
type,
images,
sales_channels: salesChannels,
categories: categories,
...rest
} = productObject
@@ -433,6 +441,17 @@ class ProductService extends TransactionBaseService {
}
}
if (isDefined(categories)) {
product.categories = []
if (categories?.length) {
const categoryIds = categories.map((c) => c.id)
const categoryRecords = categoryIds.map((id) => ({ id } as ProductCategory))
product.categories = categoryRecords
}
}
product = await productRepo.save(product)
product.options = await Promise.all(
@@ -513,6 +532,7 @@ class ProductService extends TransactionBaseService {
tags,
type,
sales_channels: salesChannels,
categories: categories,
...rest
} = update
@@ -536,6 +556,17 @@ class ProductService extends TransactionBaseService {
product.tags = await productTagRepo.upsertTags(tags)
}
if (isDefined(categories)) {
product.categories = []
if (categories?.length) {
const categoryIds = categories.map((c) => c.id)
const categoryRecords = categoryIds.map((id) => ({ id } as ProductCategory))
product.categories = categoryRecords
}
}
if (
this.featureFlagRouter_.isFeatureEnabled(SalesChannelFeatureFlag.key)
) {
+16
View File
@@ -14,6 +14,7 @@ import {
ProductOptionValue,
ProductStatus,
SalesChannel,
ProductCategory,
} from "../models"
import { FeatureFlagDecorators } from "../utils/feature-flag-decorators"
import { optionalBooleanMapper } from "../utils/validators/is-boolean"
@@ -74,6 +75,10 @@ export class FilterableProductProps {
@FeatureFlagDecorators(SalesChannelFeatureFlag.key, [IsOptional(), IsArray()])
sales_channel_id?: string[]
@IsArray()
@IsOptional()
category_id?: string[]
@IsString()
@IsOptional()
discount_condition_id?: string
@@ -101,6 +106,7 @@ export type ProductSelector =
discount_condition_id?: string
price_list_id?: string[] | FindOperator<PriceList>
sales_channel_id?: string[] | FindOperator<SalesChannel>
category_id?: string[] | FindOperator<ProductCategory>
})
/**
@@ -124,6 +130,7 @@ export type CreateProductInput = {
options?: CreateProductProductOption[]
variants?: CreateProductProductVariantInput[]
sales_channels?: CreateProductProductSalesChannelInput[] | null
categories?: CreateProductProductCategoryInput[] | null
weight?: number
length?: number
height?: number
@@ -145,6 +152,10 @@ export type CreateProductProductSalesChannelInput = {
id: string
}
export type CreateProductProductCategoryInput = {
id: string
}
export type CreateProductProductTypeInput = {
id?: string
value: string
@@ -226,6 +237,11 @@ export class ProductSalesChannelReq {
id: string
}
export class ProductProductCategoryReq {
@IsString()
id: string
}
export class ProductTagReq {
@IsString()
@IsOptional()