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(medusa): Add metadata to Product Category #5599

Merged
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
6 changes: 6 additions & 0 deletions .changeset/nine-fishes-matter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@medusajs/medusa": patch
"@medusajs/client-types": patch
---

feat(medusa): Add `metadata` to Product Category
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,8 @@ export interface AdminPostProductCategoriesCategoryReq {
* The rank of the category in the tree node (starting from 0)
*/
rank?: number
/**
* An optional set of key-value pairs to hold additional information.
*/
metadata?: Record<string, any>
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@ export interface AdminPostProductCategoriesReq {
* The ID of the parent product category
*/
parent_category_id?: string
/**
* An optional set of key-value pairs to hold additional information.
*/
metadata?: Record<string, any>
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,8 @@ export interface ProductCategory {
* The date with timezone at which the resource was updated.
*/
updated_at: string
/**
* An optional key-value map with additional details
*/
metadata: Record<string, any> | null
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IsNotEmpty, IsString } from "class-validator"
import { IsNotEmpty, IsString, IsObject, IsOptional } from "class-validator"
import { Request, Response } from "express"
import { EntityManager } from "typeorm"

Expand Down Expand Up @@ -122,12 +122,22 @@ export default async (req: Request, res: Response) => {
* parent_category_id:
* type: string
* description: The ID of the parent product category
* metadata:
* description: An optional set of key-value pairs to hold additional information.
* type: object
* externalDocs:
* description: "Learn about the metadata attribute, and how to delete and update it."
* url: "https://docs.medusajs.com/development/entities/overview#metadata-attribute"
*/
// eslint-disable-next-line max-len
export class AdminPostProductCategoriesReq extends AdminProductCategoriesReqBase {
@IsString()
@IsNotEmpty()
name: string

@IsObject()
@IsOptional()
metadata?: Record<string, unknown>
}

export class AdminPostProductCategoriesParams extends FindParams {}
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ export const defaultProductCategoryFields = [
"parent_category_id",
"created_at",
"updated_at",
"metadata",
]

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { IsOptional, IsString, IsInt, Min, IsNotEmpty } from "class-validator"
import {
IsOptional,
IsString,
IsInt,
Min,
IsNotEmpty,
IsObject,
} from "class-validator"
import { Request, Response } from "express"
import { EntityManager } from "typeorm"

Expand Down Expand Up @@ -123,6 +130,12 @@ export default async (req: Request, res: Response) => {
* rank:
* type: number
* description: The rank of the category in the tree node (starting from 0)
* metadata:
* description: An optional set of key-value pairs to hold additional information.
* type: object
* externalDocs:
* description: "Learn about the metadata attribute, and how to delete and update it."
* url: "https://docs.medusajs.com/development/entities/overview#metadata-attribute"
*/
// eslint-disable-next-line max-len
export class AdminPostProductCategoriesCategoryReq extends AdminProductCategoriesReqBase {
Expand All @@ -140,6 +153,10 @@ export class AdminPostProductCategoriesCategoryReq extends AdminProductCategorie
@IsNotEmpty()
@Min(0)
rank?: number

@IsObject()
@IsOptional()
metadata?: Record<string, unknown>
}

export class AdminPostProductCategoriesCategoryParams extends FindParams {}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export const defaultStoreProductCategoryFields = [
"created_at",
"updated_at",
"rank",
"metadata",
]

export const allowedStoreProductCategoryFields = [
Expand All @@ -71,6 +72,7 @@ export const allowedStoreProductCategoryFields = [
"created_at",
"updated_at",
"rank",
"metadata",
]

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { MigrationInterface, QueryRunner } from "typeorm"

export class AddMetadataToProductCategory1699564794649 implements MigrationInterface {

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "product_category" ADD COLUMN "metadata" jsonb NULL;`
)
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "product_category" DROP COLUMN "metadata"`
)
}

}
13 changes: 13 additions & 0 deletions packages/medusa/src/models/product-category.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { generateEntityId } from "../utils/generate-entity-id"
import { BaseEntity } from "../interfaces/models/base-entity"
import { kebabCase } from "lodash"
import { DbAwareColumn } from "../utils/db-aware-column"
import { Product } from "."
import {
BeforeInsert,
Expand Down Expand Up @@ -64,6 +65,9 @@ export class ProductCategory extends BaseEntity {
@Column({ nullable: false, default: 0 })
rank: number

@DbAwareColumn({ type: "jsonb", nullable: true })
metadata: Record<string, unknown>

@ManyToMany(() => Product, { cascade: ["remove", "soft-remove"] })
@JoinTable({
name: ProductCategory.productCategoryProductJoinTable,
Expand Down Expand Up @@ -105,6 +109,7 @@ export class ProductCategory extends BaseEntity {
* - id
* - is_active
* - is_internal
* - metadata
* - mpath
* - name
* - parent_category_id
Expand Down Expand Up @@ -173,4 +178,12 @@ export class ProductCategory extends BaseEntity {
* description: The date with timezone at which the resource was updated.
* type: string
* format: date-time
* metadata:
* description: An optional key-value map with additional details
* nullable: true
* type: object
* example: {car: "white"}
* externalDocs:
* description: "Learn about the metadata attribute, and how to delete and update it."
* url: "https://docs.medusajs.com/development/entities/overview#metadata-attribute"
*/
8 changes: 7 additions & 1 deletion packages/medusa/src/services/product-category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
tempReorderRank,
UpdateProductCategoryInput,
} from "../types/product-category"
import { buildQuery, nullableValue } from "../utils"
import { buildQuery, nullableValue, setMetadata } from "../utils"

type InjectedDependencies = {
manager: EntityManager
Expand Down Expand Up @@ -229,6 +229,12 @@ class ProductCategoryService extends TransactionBaseService {
this.productCategoryRepo_
)

const { metadata, ...rest } = productCategoryInput

if (metadata) {
productCategory.metadata = setMetadata(productCategory, metadata)
}

const conditions = this.fetchReorderConditions(
productCategory,
productCategoryInput
Expand Down
1 change: 1 addition & 0 deletions packages/medusa/src/types/product-category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type ProductCategoryInput = {
parent_category_id?: string | null
parent_category?: ProductCategory | null
rank?: number
metadata?: Record<string, unknown>
}

export type CreateProductCategoryInput = ProductCategoryInput & {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ required:
- id
- is_active
- is_internal
- metadata
- mpath
- name
- parent_category_id
Expand Down Expand Up @@ -84,3 +85,13 @@ properties:
description: The date with timezone at which the resource was updated.
type: string
format: date-time
metadata:
description: An optional key-value map with additional details
nullable: true
type: object
example:
car: white
externalDocs:
description: Learn about the metadata attribute, and how to delete and update it.
url: >-
https://docs.medusajs.com/development/entities/overview#metadata-attribute
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ required:
- id
- is_active
- is_internal
- metadata
- mpath
- name
- parent_category_id
Expand Down Expand Up @@ -84,3 +85,13 @@ properties:
description: The date with timezone at which the resource was updated.
type: string
format: date-time
metadata:
description: An optional key-value map with additional details
nullable: true
type: object
example:
car: white
externalDocs:
description: Learn about the metadata attribute, and how to delete and update it.
url: >-
https://docs.medusajs.com/development/entities/overview#metadata-attribute