Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Make tags unique, clean up searchability of product entities #7014

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 22 additions & 20 deletions integration-tests/api/__tests__/admin/product.js
Original file line number Diff line number Diff line change
Expand Up @@ -658,8 +658,7 @@ medusaIntegrationTestRunner({
}
})

// TODO: Enforce tag uniqueness in product module
it.skip("returns a list of products with tags", async () => {
it("returns a list of products with tags", async () => {
const response = await api.get(
`/admin/products?tags[]=${baseProduct.tags[0].id}`,
adminHeaders
Expand All @@ -668,26 +667,25 @@ medusaIntegrationTestRunner({
expect(response.status).toEqual(200)
expect(response.data.products).toHaveLength(2)
expect(response.data.products).toEqual(
expect.arrayContaining(
[
expect.objectContaining({
id: baseProduct.id,
tags: [
expect.objectContaining({ id: baseProduct.tags[0].id }),
],
}),
],
expect.arrayContaining([
expect.objectContaining({
id: baseProduct.id,
tags: expect.arrayContaining([
expect.objectContaining({ id: baseProduct.tags[0].id }),
]),
}),
expect.objectContaining({
id: publishedProduct.id,
// It should be the same tag instance in both products
tags: [expect.objectContaining({ id: baseProduct.tags[0].id })],
})
)
tags: expect.arrayContaining([
expect.objectContaining({ id: baseProduct.tags[0].id }),
]),
}),
])
)
})

// TODO: Enforce tag uniqueness in product module
it.skip("returns a list of products with tags in a collection", async () => {
it("returns a list of products with tags in a collection", async () => {
const response = await api.get(
`/admin/products?collection_id[]=${baseCollection.id}&tags[]=${baseProduct.tags[0].id}`,
adminHeaders
Expand All @@ -700,7 +698,9 @@ medusaIntegrationTestRunner({
expect.objectContaining({
id: baseProduct.id,
collection_id: baseCollection.id,
tags: [expect.objectContaining({ id: baseProduct.tags[0].id })],
tags: expect.arrayContaining([
expect.objectContaining({ id: baseProduct.tags[0].id }),
]),
}),
])
)
Expand Down Expand Up @@ -2655,8 +2655,7 @@ medusaIntegrationTestRunner({
expect(response2.data.id).toEqual(res.data.product.id)
})

// TODO: We just need to return the correct error message
it.skip("should fail when creating a product with a handle that already exists", async () => {
it("should fail when creating a product with a handle that already exists", async () => {
// Lets try to create a product with same handle as deleted one
const payload = {
title: baseProduct.title,
Expand All @@ -2675,7 +2674,10 @@ medusaIntegrationTestRunner({
await api.post("/admin/products", payload, adminHeaders)
} catch (error) {
expect(error.response.data.message).toMatch(
"Product with handle test-product already exists."
breaking(
() => "Product with handle base-product already exists.",
() => "Product with handle: base-product already exists."
)
)
}
})
Expand Down
1 change: 1 addition & 0 deletions packages/medusa/src/api-v2/admin/products/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export const AdminGetProductOptionsParams = createFindParams({
limit: 50,
}).merge(
z.object({
q: z.string().optional(),
id: z.union([z.string(), z.array(z.string())]).optional(),
title: z.string().optional(),
$and: z.lazy(() => AdminGetProductsParams.array()).optional(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const productsData = [
tags: [
{
id: "tag-3",
value: "Germany",
value: "Netherlands",
},
],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,7 @@ moduleIntegrationTestRunner({

const productTwoData = buildProductAndRelationsData({
collection_id: productCollectionTwo.id,
tags: [],
})

await service.create([productOneData, productTwoData])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ moduleIntegrationTestRunner({

const productOptions = await service.list(
{
title: "US%",
q: "US%",
},
{
relations: ["product"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ moduleIntegrationTestRunner({
})

it("list product tags by value matching string", async () => {
const tagsResults = await service.list({ value: "united kingdom" })
const tagsResults = await service.list({ q: "united kingdom" })

expect(tagsResults).toEqual([
expect.objectContaining({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { SqlEntityManager } from "@mikro-orm/postgresql"
import { createProductCategories } from "../../../__fixtures__/product-category"
import { Modules } from "@medusajs/modules-sdk"
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
import { ProductTag } from "../../../../src/models"

jest.setTimeout(30000)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ export class InitialSetup20240315083440 extends Migration {

this.addSql('create table if not exists "product_tag" ("id" text not null, "value" text not null, "metadata" jsonb null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "product_tag_pkey" primary key ("id"));');

// TODO: We need to modify upsertWithReplace to handle unique constraints
// this.addSql('create unique index if not exists "IDX_tag_value_unique" on "product_tag" (value) where deleted_at is null;')
this.addSql('create unique index if not exists "IDX_tag_value_unique" on "product_tag" (value) where deleted_at is null;')
this.addSql('create index if not exists "IDX_product_tag_deleted_at" on "product_tag" ("deleted_at");');

this.addSql('create table if not exists "product_type" ("id" text not null, "value" text not null, "metadata" json null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "product_type_pkey" primary key ("id"));');
Expand Down
2 changes: 2 additions & 0 deletions packages/product/src/models/product-option.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
DALUtils,
Searchable,
createPsqlIndexStatementHelper,
generateEntityId,
} from "@medusajs/utils"
Expand Down Expand Up @@ -35,6 +36,7 @@ class ProductOption {
@PrimaryKey({ columnType: "text" })
id!: string

@Searchable()
@Property({ columnType: "text" })
title: string

Expand Down
2 changes: 2 additions & 0 deletions packages/product/src/models/product-tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {

import {
DALUtils,
Searchable,
createPsqlIndexStatementHelper,
generateEntityId,
} from "@medusajs/utils"
Expand All @@ -33,6 +34,7 @@ class ProductTag {
@PrimaryKey({ columnType: "text" })
id!: string

@Searchable()
@Property({ columnType: "text" })
value: string

Expand Down
2 changes: 2 additions & 0 deletions packages/product/src/models/product-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {

import {
DALUtils,
Searchable,
createPsqlIndexStatementHelper,
generateEntityId,
} from "@medusajs/utils"
Expand All @@ -30,6 +31,7 @@ class ProductType {
@PrimaryKey({ columnType: "text" })
id!: string

@Searchable()
@Property({ columnType: "text" })
value: string

Expand Down
3 changes: 3 additions & 0 deletions packages/product/src/models/product-variant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,15 @@ class ProductVariant {
@Property({ columnType: "text", nullable: true })
sku?: string | null

@Searchable()
@Property({ columnType: "text", nullable: true })
barcode?: string | null

@Searchable()
@Property({ columnType: "text", nullable: true })
ean?: string | null

@Searchable()
@Property({ columnType: "text", nullable: true })
upc?: string | null

Expand Down
1 change: 1 addition & 0 deletions packages/product/src/models/product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class Product {
@Property({ columnType: "text" })
handle?: string

@Searchable()
@Property({ columnType: "text", nullable: true })
subtitle?: string | null

Expand Down
4 changes: 0 additions & 4 deletions packages/product/src/services/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
export { default as ProductService } from "./product"
export { default as ProductCategoryService } from "./product-category"
export { default as ProductCollectionService } from "./product-collection"
export { default as ProductModuleService } from "./product-module-service"
export { default as ProductTagService } from "./product-tag"
export { default as ProductTypeService } from "./product-type"
export { default as ProductOptionService } from "./product-option"
63 changes: 0 additions & 63 deletions packages/product/src/services/product-collection.ts

This file was deleted.

Loading
Loading