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
parent 44dbe55f50
commit ee42b60a20
12 changed files with 249 additions and 6 deletions

View File

@@ -3,6 +3,7 @@
exports[`/admin/products GET /admin/products returns a list of products with only giftcard in list 1`] = `
Array [
Object {
"categories": Array [],
"collection": null,
"collection_id": null,
"created_at": Any<String>,
@@ -111,6 +112,7 @@ Array [
exports[`/admin/products POST /admin/products creates a product 1`] = `
Object {
"categories": Array [],
"collection": Object {
"created_at": Any<String>,
"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`] = `
Object {
"categories": Array [],
"collection": null,
"collection_id": null,
"created_at": Any<String>,

View File

@@ -6,6 +6,8 @@ const { initDb, useDb } = require("../../../helpers/use-db")
const adminSeeder = require("../../helpers/admin-seeder")
const productSeeder = require("../../helpers/product-seeder")
const { Product, ProductCategory } = require("@medusajs/medusa")
const {
ProductVariant,
ProductOptionValue,
@@ -17,12 +19,14 @@ const priceListSeeder = require("../../helpers/price-list-seeder")
const {
simpleProductFactory,
simpleDiscountFactory,
simpleProductCategoryFactory,
} = require("../../factories")
const { DiscountRuleType, AllocationType } = require("@medusajs/medusa/dist")
const { IdMap } = require("medusa-test-utils")
jest.setTimeout(50000)
const testProductId = "test-product"
const adminHeaders = {
headers: {
Authorization: "Bearer test_token",
@@ -1426,6 +1430,7 @@ describe("/admin/products", () => {
],
type: 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", () => {