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:
co-authored by
Philip Korsholm
parent
44dbe55f50
commit
ee42b60a20
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/medusa": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
feat(medusa): add or remove categories from products
|
||||||
@@ -3,6 +3,7 @@
|
|||||||
exports[`/admin/products GET /admin/products returns a list of products with only giftcard in list 1`] = `
|
exports[`/admin/products GET /admin/products returns a list of products with only giftcard in list 1`] = `
|
||||||
Array [
|
Array [
|
||||||
Object {
|
Object {
|
||||||
|
"categories": Array [],
|
||||||
"collection": null,
|
"collection": null,
|
||||||
"collection_id": null,
|
"collection_id": null,
|
||||||
"created_at": Any<String>,
|
"created_at": Any<String>,
|
||||||
@@ -111,6 +112,7 @@ Array [
|
|||||||
|
|
||||||
exports[`/admin/products POST /admin/products creates a product 1`] = `
|
exports[`/admin/products POST /admin/products creates a product 1`] = `
|
||||||
Object {
|
Object {
|
||||||
|
"categories": Array [],
|
||||||
"collection": Object {
|
"collection": Object {
|
||||||
"created_at": Any<String>,
|
"created_at": Any<String>,
|
||||||
"deleted_at": null,
|
"deleted_at": null,
|
||||||
@@ -314,6 +316,7 @@ Object {
|
|||||||
|
|
||||||
exports[`/admin/products POST /admin/products updates a product (update prices, tags, update status, delete collection, delete type, replaces images) 1`] = `
|
exports[`/admin/products POST /admin/products updates a product (update prices, tags, update status, delete collection, delete type, replaces images) 1`] = `
|
||||||
Object {
|
Object {
|
||||||
|
"categories": Array [],
|
||||||
"collection": null,
|
"collection": null,
|
||||||
"collection_id": null,
|
"collection_id": null,
|
||||||
"created_at": Any<String>,
|
"created_at": Any<String>,
|
||||||
|
|||||||
@@ -6,6 +6,8 @@ const { initDb, useDb } = require("../../../helpers/use-db")
|
|||||||
|
|
||||||
const adminSeeder = require("../../helpers/admin-seeder")
|
const adminSeeder = require("../../helpers/admin-seeder")
|
||||||
const productSeeder = require("../../helpers/product-seeder")
|
const productSeeder = require("../../helpers/product-seeder")
|
||||||
|
const { Product, ProductCategory } = require("@medusajs/medusa")
|
||||||
|
|
||||||
const {
|
const {
|
||||||
ProductVariant,
|
ProductVariant,
|
||||||
ProductOptionValue,
|
ProductOptionValue,
|
||||||
@@ -17,12 +19,14 @@ const priceListSeeder = require("../../helpers/price-list-seeder")
|
|||||||
const {
|
const {
|
||||||
simpleProductFactory,
|
simpleProductFactory,
|
||||||
simpleDiscountFactory,
|
simpleDiscountFactory,
|
||||||
|
simpleProductCategoryFactory,
|
||||||
} = require("../../factories")
|
} = require("../../factories")
|
||||||
const { DiscountRuleType, AllocationType } = require("@medusajs/medusa/dist")
|
const { DiscountRuleType, AllocationType } = require("@medusajs/medusa/dist")
|
||||||
const { IdMap } = require("medusa-test-utils")
|
const { IdMap } = require("medusa-test-utils")
|
||||||
|
|
||||||
jest.setTimeout(50000)
|
jest.setTimeout(50000)
|
||||||
|
|
||||||
|
const testProductId = "test-product"
|
||||||
const adminHeaders = {
|
const adminHeaders = {
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: "Bearer test_token",
|
Authorization: "Bearer test_token",
|
||||||
@@ -1426,6 +1430,7 @@ describe("/admin/products", () => {
|
|||||||
],
|
],
|
||||||
type: null,
|
type: null,
|
||||||
collection: null,
|
collection: null,
|
||||||
|
categories: [],
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
@@ -1456,6 +1461,141 @@ describe("/admin/products", () => {
|
|||||||
})
|
})
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe("Categories", () => {
|
||||||
|
let categoryWithProduct, categoryWithoutProduct
|
||||||
|
const categoryWithProductId = "category-with-product-id"
|
||||||
|
const categoryWithoutProductId = "category-without-product-id"
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
const manager = dbConnection.manager
|
||||||
|
categoryWithProduct = await manager.create(ProductCategory, {
|
||||||
|
id: categoryWithProductId,
|
||||||
|
name: "category with Product",
|
||||||
|
products: [{ id: testProductId }],
|
||||||
|
})
|
||||||
|
await manager.save(categoryWithProduct)
|
||||||
|
|
||||||
|
categoryWithoutProduct = await manager.create(ProductCategory, {
|
||||||
|
id: categoryWithoutProductId,
|
||||||
|
name: "category without product",
|
||||||
|
})
|
||||||
|
await manager.save(categoryWithoutProduct)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("creates a product with categories associated to it", async () => {
|
||||||
|
const api = useApi()
|
||||||
|
|
||||||
|
const payload = {
|
||||||
|
title: "Test",
|
||||||
|
description: "test-product-description",
|
||||||
|
categories: [{ id: categoryWithProductId }, { id: categoryWithoutProductId }]
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await api
|
||||||
|
.post("/admin/products", payload, adminHeaders)
|
||||||
|
.catch(e => e)
|
||||||
|
|
||||||
|
expect(response.status).toEqual(200)
|
||||||
|
expect(response.data.product).toEqual(
|
||||||
|
expect.objectContaining({
|
||||||
|
categories: [
|
||||||
|
expect.objectContaining({
|
||||||
|
id: categoryWithProductId,
|
||||||
|
}),
|
||||||
|
expect.objectContaining({
|
||||||
|
id: categoryWithoutProductId,
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
})
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("throws error when creating a product with invalid category ID", async () => {
|
||||||
|
const api = useApi()
|
||||||
|
const categoryNotFoundId = "category-doesnt-exist"
|
||||||
|
|
||||||
|
const payload = {
|
||||||
|
title: "Test",
|
||||||
|
description: "test-product-description",
|
||||||
|
categories: [{ id: categoryNotFoundId }]
|
||||||
|
}
|
||||||
|
|
||||||
|
const error = await api
|
||||||
|
.post("/admin/products", payload, adminHeaders)
|
||||||
|
.catch(e => e)
|
||||||
|
|
||||||
|
expect(error.response.status).toEqual(404)
|
||||||
|
expect(error.response.data.type).toEqual("not_found")
|
||||||
|
expect(error.response.data.message).toEqual(`Product_category with product_category_id ${categoryNotFoundId} does not exist.`)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("updates a product's categories", async () => {
|
||||||
|
const api = useApi()
|
||||||
|
|
||||||
|
const payload = {
|
||||||
|
categories: [{ id: categoryWithoutProductId }],
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await api
|
||||||
|
.post(`/admin/products/${testProductId}`, payload, adminHeaders)
|
||||||
|
|
||||||
|
expect(response.status).toEqual(200)
|
||||||
|
expect(response.data.product).toEqual(
|
||||||
|
expect.objectContaining({
|
||||||
|
id: testProductId,
|
||||||
|
handle: "test-product",
|
||||||
|
categories: [
|
||||||
|
expect.objectContaining({
|
||||||
|
id: categoryWithoutProductId,
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
})
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("remove all categories of a product", async () => {
|
||||||
|
const api = useApi()
|
||||||
|
const category = await simpleProductCategoryFactory(
|
||||||
|
dbConnection,
|
||||||
|
{
|
||||||
|
id: "existing-category",
|
||||||
|
name: "existing category",
|
||||||
|
products: [{ id: "test-product" }]
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
const payload = {
|
||||||
|
categories: [],
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await api
|
||||||
|
.post("/admin/products/test-product", payload, adminHeaders)
|
||||||
|
|
||||||
|
expect(response.status).toEqual(200)
|
||||||
|
expect(response.data.product).toEqual(
|
||||||
|
expect.objectContaining({
|
||||||
|
id: "test-product",
|
||||||
|
categories: [],
|
||||||
|
})
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("throws error if product categories input is incorreect", async () => {
|
||||||
|
const api = useApi()
|
||||||
|
const payload = {
|
||||||
|
categories: [{ incorrect: "test-category-d2B" }],
|
||||||
|
}
|
||||||
|
|
||||||
|
const error = await api
|
||||||
|
.post("/admin/products/test-product", payload, adminHeaders)
|
||||||
|
.catch(e => e)
|
||||||
|
|
||||||
|
expect(error.response.status).toEqual(400)
|
||||||
|
expect(error.response.data.type).toEqual("invalid_data")
|
||||||
|
expect(error.response.data.message).toEqual("property incorrect should not exist, id must be a string")
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("DELETE /admin/products/:id/options/:option_id", () => {
|
describe("DELETE /admin/products/:id/options/:option_id", () => {
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ export const simpleProductCategoryFactory = async (
|
|||||||
data: Partial<ProductCategory> = {}
|
data: Partial<ProductCategory> = {}
|
||||||
): Promise<ProductCategory> => {
|
): Promise<ProductCategory> => {
|
||||||
const manager = connection.manager
|
const manager = connection.manager
|
||||||
const address = manager.create(ProductCategory, data)
|
const category = manager.create(ProductCategory, data)
|
||||||
|
|
||||||
return await manager.save(address)
|
return await manager.save(category)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -65,6 +65,7 @@ describe("GET /admin/products/:id", () => {
|
|||||||
"tags",
|
"tags",
|
||||||
"type",
|
"type",
|
||||||
"collection",
|
"collection",
|
||||||
|
"categories",
|
||||||
"sales_channels",
|
"sales_channels",
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ import {
|
|||||||
} from "../../../../services"
|
} from "../../../../services"
|
||||||
import {
|
import {
|
||||||
ProductSalesChannelReq,
|
ProductSalesChannelReq,
|
||||||
|
ProductProductCategoryReq,
|
||||||
ProductTagReq,
|
ProductTagReq,
|
||||||
ProductTypeReq,
|
ProductTypeReq,
|
||||||
} from "../../../../types/product"
|
} from "../../../../types/product"
|
||||||
@@ -340,6 +341,16 @@ class ProductVariantReq {
|
|||||||
* id:
|
* id:
|
||||||
* description: The ID of an existing Sales channel.
|
* description: The ID of an existing Sales channel.
|
||||||
* type: string
|
* 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:
|
* options:
|
||||||
* description: The Options that the Product should have. These define on which properties the Product's Product Variants will differ.
|
* description: The Options that the Product should have. These define on which properties the Product's Product Variants will differ.
|
||||||
* type: array
|
* type: array
|
||||||
@@ -527,6 +538,12 @@ export class AdminPostProductsReq {
|
|||||||
])
|
])
|
||||||
sales_channels?: ProductSalesChannelReq[]
|
sales_channels?: ProductSalesChannelReq[]
|
||||||
|
|
||||||
|
@IsOptional()
|
||||||
|
@Type(() => ProductProductCategoryReq)
|
||||||
|
@ValidateNested({ each: true })
|
||||||
|
@IsArray()
|
||||||
|
categories?: ProductProductCategoryReq[]
|
||||||
|
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
@Type(() => ProductOptionReq)
|
@Type(() => ProductOptionReq)
|
||||||
@ValidateNested({ each: true })
|
@ValidateNested({ each: true })
|
||||||
|
|||||||
@@ -100,6 +100,7 @@ export const defaultAdminProductRelations = [
|
|||||||
"tags",
|
"tags",
|
||||||
"type",
|
"type",
|
||||||
"collection",
|
"collection",
|
||||||
|
"categories",
|
||||||
]
|
]
|
||||||
|
|
||||||
export const defaultAdminProductFields: (keyof Product)[] = [
|
export const defaultAdminProductFields: (keyof Product)[] = [
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ import {
|
|||||||
ProductSalesChannelReq,
|
ProductSalesChannelReq,
|
||||||
ProductTagReq,
|
ProductTagReq,
|
||||||
ProductTypeReq,
|
ProductTypeReq,
|
||||||
|
ProductProductCategoryReq,
|
||||||
} from "../../../../types/product"
|
} from "../../../../types/product"
|
||||||
|
|
||||||
import { Type } from "class-transformer"
|
import { Type } from "class-transformer"
|
||||||
@@ -278,6 +279,16 @@ class ProductVariantReq {
|
|||||||
* id:
|
* id:
|
||||||
* description: The ID of an existing Sales channel.
|
* description: The ID of an existing Sales channel.
|
||||||
* type: string
|
* 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:
|
* variants:
|
||||||
* description: A list of Product Variants to create with the Product.
|
* description: A list of Product Variants to create with the Product.
|
||||||
* type: array
|
* type: array
|
||||||
@@ -459,6 +470,12 @@ export class AdminPostProductsProductReq {
|
|||||||
])
|
])
|
||||||
sales_channels?: ProductSalesChannelReq[] | null
|
sales_channels?: ProductSalesChannelReq[] | null
|
||||||
|
|
||||||
|
@IsOptional()
|
||||||
|
@Type(() => ProductProductCategoryReq)
|
||||||
|
@ValidateNested({ each: true })
|
||||||
|
@IsArray()
|
||||||
|
categories?: ProductProductCategoryReq[]
|
||||||
|
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
@Type(() => ProductVariantReq)
|
@Type(() => ProductVariantReq)
|
||||||
@ValidateNested({ each: true })
|
@ValidateNested({ each: true })
|
||||||
|
|||||||
@@ -56,11 +56,11 @@ export class ProductCategory extends SoftDeletableEntity {
|
|||||||
@JoinTable({
|
@JoinTable({
|
||||||
name: "product_category_product",
|
name: "product_category_product",
|
||||||
joinColumn: {
|
joinColumn: {
|
||||||
name: "product_id",
|
name: "product_category_id",
|
||||||
referencedColumnName: "id",
|
referencedColumnName: "id",
|
||||||
},
|
},
|
||||||
inverseJoinColumn: {
|
inverseJoinColumn: {
|
||||||
name: "product_category_id",
|
name: "product_id",
|
||||||
referencedColumnName: "id",
|
referencedColumnName: "id",
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
@@ -122,6 +122,12 @@ export class ProductCategory extends SoftDeletableEntity {
|
|||||||
* parent_category:
|
* parent_category:
|
||||||
* description: A product category object. Available if the relation `parent_category` is expanded.
|
* description: A product category object. Available if the relation `parent_category` is expanded.
|
||||||
* type: object
|
* 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:
|
* created_at:
|
||||||
* type: string
|
* type: string
|
||||||
* description: "The date with timezone at which the resource was created."
|
* description: "The date with timezone at which the resource was created."
|
||||||
|
|||||||
@@ -82,11 +82,11 @@ export class Product extends SoftDeletableEntity {
|
|||||||
@JoinTable({
|
@JoinTable({
|
||||||
name: "product_category_product",
|
name: "product_category_product",
|
||||||
joinColumn: {
|
joinColumn: {
|
||||||
name: "product_category_id",
|
name: "product_id",
|
||||||
referencedColumnName: "id",
|
referencedColumnName: "id",
|
||||||
},
|
},
|
||||||
inverseJoinColumn: {
|
inverseJoinColumn: {
|
||||||
name: "product_id",
|
name: "product_category_id",
|
||||||
referencedColumnName: "id",
|
referencedColumnName: "id",
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
@@ -320,6 +320,12 @@ export class Product extends SoftDeletableEntity {
|
|||||||
* items:
|
* items:
|
||||||
* type: object
|
* type: object
|
||||||
* description: A sales channel 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:
|
* created_at:
|
||||||
* type: string
|
* type: string
|
||||||
* description: "The date with timezone at which the resource was created."
|
* description: "The date with timezone at which the resource was created."
|
||||||
|
|||||||
@@ -12,12 +12,14 @@ import {
|
|||||||
ProductType,
|
ProductType,
|
||||||
ProductVariant,
|
ProductVariant,
|
||||||
SalesChannel,
|
SalesChannel,
|
||||||
|
ProductCategory,
|
||||||
} from "../models"
|
} from "../models"
|
||||||
import { ImageRepository } from "../repositories/image"
|
import { ImageRepository } from "../repositories/image"
|
||||||
import {
|
import {
|
||||||
FindWithoutRelationsOptions,
|
FindWithoutRelationsOptions,
|
||||||
ProductRepository,
|
ProductRepository,
|
||||||
} from "../repositories/product"
|
} from "../repositories/product"
|
||||||
|
import { ProductCategoryRepository } from "../repositories/product-category"
|
||||||
import { ProductOptionRepository } from "../repositories/product-option"
|
import { ProductOptionRepository } from "../repositories/product-option"
|
||||||
import { ProductTagRepository } from "../repositories/product-tag"
|
import { ProductTagRepository } from "../repositories/product-tag"
|
||||||
import { ProductTypeRepository } from "../repositories/product-type"
|
import { ProductTypeRepository } from "../repositories/product-type"
|
||||||
@@ -42,6 +44,7 @@ type InjectedDependencies = {
|
|||||||
productTypeRepository: typeof ProductTypeRepository
|
productTypeRepository: typeof ProductTypeRepository
|
||||||
productTagRepository: typeof ProductTagRepository
|
productTagRepository: typeof ProductTagRepository
|
||||||
imageRepository: typeof ImageRepository
|
imageRepository: typeof ImageRepository
|
||||||
|
productCategoryRepository: typeof ProductCategoryRepository
|
||||||
productVariantService: ProductVariantService
|
productVariantService: ProductVariantService
|
||||||
searchService: SearchService
|
searchService: SearchService
|
||||||
eventBusService: EventBusService
|
eventBusService: EventBusService
|
||||||
@@ -58,6 +61,8 @@ class ProductService extends TransactionBaseService {
|
|||||||
protected readonly productTypeRepository_: typeof ProductTypeRepository
|
protected readonly productTypeRepository_: typeof ProductTypeRepository
|
||||||
protected readonly productTagRepository_: typeof ProductTagRepository
|
protected readonly productTagRepository_: typeof ProductTagRepository
|
||||||
protected readonly imageRepository_: typeof ImageRepository
|
protected readonly imageRepository_: typeof ImageRepository
|
||||||
|
// eslint-disable-next-line max-len
|
||||||
|
protected readonly productCategoryRepository_: typeof ProductCategoryRepository
|
||||||
protected readonly productVariantService_: ProductVariantService
|
protected readonly productVariantService_: ProductVariantService
|
||||||
protected readonly searchService_: SearchService
|
protected readonly searchService_: SearchService
|
||||||
protected readonly eventBus_: EventBusService
|
protected readonly eventBus_: EventBusService
|
||||||
@@ -79,6 +84,7 @@ class ProductService extends TransactionBaseService {
|
|||||||
productVariantService,
|
productVariantService,
|
||||||
productTypeRepository,
|
productTypeRepository,
|
||||||
productTagRepository,
|
productTagRepository,
|
||||||
|
productCategoryRepository,
|
||||||
imageRepository,
|
imageRepository,
|
||||||
searchService,
|
searchService,
|
||||||
featureFlagRouter,
|
featureFlagRouter,
|
||||||
@@ -92,6 +98,7 @@ class ProductService extends TransactionBaseService {
|
|||||||
this.productVariantRepository_ = productVariantRepository
|
this.productVariantRepository_ = productVariantRepository
|
||||||
this.eventBus_ = eventBusService
|
this.eventBus_ = eventBusService
|
||||||
this.productVariantService_ = productVariantService
|
this.productVariantService_ = productVariantService
|
||||||
|
this.productCategoryRepository_ = productCategoryRepository
|
||||||
this.productTypeRepository_ = productTypeRepository
|
this.productTypeRepository_ = productTypeRepository
|
||||||
this.productTagRepository_ = productTagRepository
|
this.productTagRepository_ = productTagRepository
|
||||||
this.imageRepository_ = imageRepository
|
this.imageRepository_ = imageRepository
|
||||||
@@ -393,6 +400,7 @@ class ProductService extends TransactionBaseService {
|
|||||||
type,
|
type,
|
||||||
images,
|
images,
|
||||||
sales_channels: salesChannels,
|
sales_channels: salesChannels,
|
||||||
|
categories: categories,
|
||||||
...rest
|
...rest
|
||||||
} = productObject
|
} = 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 = await productRepo.save(product)
|
||||||
|
|
||||||
product.options = await Promise.all(
|
product.options = await Promise.all(
|
||||||
@@ -513,6 +532,7 @@ class ProductService extends TransactionBaseService {
|
|||||||
tags,
|
tags,
|
||||||
type,
|
type,
|
||||||
sales_channels: salesChannels,
|
sales_channels: salesChannels,
|
||||||
|
categories: categories,
|
||||||
...rest
|
...rest
|
||||||
} = update
|
} = update
|
||||||
|
|
||||||
@@ -536,6 +556,17 @@ class ProductService extends TransactionBaseService {
|
|||||||
product.tags = await productTagRepo.upsertTags(tags)
|
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 (
|
if (
|
||||||
this.featureFlagRouter_.isFeatureEnabled(SalesChannelFeatureFlag.key)
|
this.featureFlagRouter_.isFeatureEnabled(SalesChannelFeatureFlag.key)
|
||||||
) {
|
) {
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import {
|
|||||||
ProductOptionValue,
|
ProductOptionValue,
|
||||||
ProductStatus,
|
ProductStatus,
|
||||||
SalesChannel,
|
SalesChannel,
|
||||||
|
ProductCategory,
|
||||||
} from "../models"
|
} from "../models"
|
||||||
import { FeatureFlagDecorators } from "../utils/feature-flag-decorators"
|
import { FeatureFlagDecorators } from "../utils/feature-flag-decorators"
|
||||||
import { optionalBooleanMapper } from "../utils/validators/is-boolean"
|
import { optionalBooleanMapper } from "../utils/validators/is-boolean"
|
||||||
@@ -74,6 +75,10 @@ export class FilterableProductProps {
|
|||||||
@FeatureFlagDecorators(SalesChannelFeatureFlag.key, [IsOptional(), IsArray()])
|
@FeatureFlagDecorators(SalesChannelFeatureFlag.key, [IsOptional(), IsArray()])
|
||||||
sales_channel_id?: string[]
|
sales_channel_id?: string[]
|
||||||
|
|
||||||
|
@IsArray()
|
||||||
|
@IsOptional()
|
||||||
|
category_id?: string[]
|
||||||
|
|
||||||
@IsString()
|
@IsString()
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
discount_condition_id?: string
|
discount_condition_id?: string
|
||||||
@@ -101,6 +106,7 @@ export type ProductSelector =
|
|||||||
discount_condition_id?: string
|
discount_condition_id?: string
|
||||||
price_list_id?: string[] | FindOperator<PriceList>
|
price_list_id?: string[] | FindOperator<PriceList>
|
||||||
sales_channel_id?: string[] | FindOperator<SalesChannel>
|
sales_channel_id?: string[] | FindOperator<SalesChannel>
|
||||||
|
category_id?: string[] | FindOperator<ProductCategory>
|
||||||
})
|
})
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -124,6 +130,7 @@ export type CreateProductInput = {
|
|||||||
options?: CreateProductProductOption[]
|
options?: CreateProductProductOption[]
|
||||||
variants?: CreateProductProductVariantInput[]
|
variants?: CreateProductProductVariantInput[]
|
||||||
sales_channels?: CreateProductProductSalesChannelInput[] | null
|
sales_channels?: CreateProductProductSalesChannelInput[] | null
|
||||||
|
categories?: CreateProductProductCategoryInput[] | null
|
||||||
weight?: number
|
weight?: number
|
||||||
length?: number
|
length?: number
|
||||||
height?: number
|
height?: number
|
||||||
@@ -145,6 +152,10 @@ export type CreateProductProductSalesChannelInput = {
|
|||||||
id: string
|
id: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type CreateProductProductCategoryInput = {
|
||||||
|
id: string
|
||||||
|
}
|
||||||
|
|
||||||
export type CreateProductProductTypeInput = {
|
export type CreateProductProductTypeInput = {
|
||||||
id?: string
|
id?: string
|
||||||
value: string
|
value: string
|
||||||
@@ -226,6 +237,11 @@ export class ProductSalesChannelReq {
|
|||||||
id: string
|
id: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class ProductProductCategoryReq {
|
||||||
|
@IsString()
|
||||||
|
id: string
|
||||||
|
}
|
||||||
|
|
||||||
export class ProductTagReq {
|
export class ProductTagReq {
|
||||||
@IsString()
|
@IsString()
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
|
|||||||
Reference in New Issue
Block a user