Skip to content

Commit

Permalink
chore: make handle optional, create before insert
Browse files Browse the repository at this point in the history
  • Loading branch information
riqwan committed Jan 11, 2023
1 parent 107f28d commit 7311999
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import { FindParams } from "../../../../types/common"
* // must be previously logged in or use api token
* medusa.admin.productCategories.create({
* name: 'Jeans',
* handle: 'jeans',
* })
* .then(({ productCategory }) => {
* console.log(productCategory.id);
Expand All @@ -42,7 +41,6 @@ import { FindParams } from "../../../../types/common"
* --header 'Content-Type: application/json' \
* --data-raw '{
* "name": "Jeans",
* "handle": "jeans",
* }'
* security:
* - api_token: []
Expand Down Expand Up @@ -101,7 +99,6 @@ export default async (req: Request, res: Response) => {
* type: object
* required:
* - name
* - handle
* properties:
* name:
* type: string
Expand All @@ -124,10 +121,6 @@ export class AdminPostProductCategoriesReq extends AdminProductCategoriesReqBase
@IsString()
@IsNotEmpty()
name: string

@IsString()
@IsNotEmpty()
handle: string
}

export class AdminPostProductCategoriesParams extends FindParams {}
6 changes: 5 additions & 1 deletion packages/medusa/src/models/product-category.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { generateEntityId } from "../utils/generate-entity-id"
import { SoftDeletableEntity } from "../interfaces/models/soft-deletable-entity"
import { kebabCase } from "lodash"
import {
BeforeInsert,
Index,
Expand Down Expand Up @@ -49,6 +50,10 @@ export class ProductCategory extends SoftDeletableEntity {
@BeforeInsert()
private beforeInsert(): void {
this.id = generateEntityId(this.id, "pcat")

if (!this.handle) {
this.handle = kebabCase(this.name)
}
}
}

Expand All @@ -60,7 +65,6 @@ export class ProductCategory extends SoftDeletableEntity {
* type: object
* required:
* - name
* - handle
* properties:
* id:
* type: string
Expand Down
3 changes: 1 addition & 2 deletions packages/medusa/src/services/__tests__/product-category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,11 @@ describe("ProductCategoryService", () => {
})

it("successfully creates a product category", async () => {
await productCategoryService.create({ name: "jeans", handle: "jeans" })
await productCategoryService.create({ name: "jeans" })

expect(productCategoryRepository.create).toHaveBeenCalledTimes(1)
expect(productCategoryRepository.create).toHaveBeenCalledWith({
name: "jeans",
handle: "jeans"
})
})
})
Expand Down
7 changes: 6 additions & 1 deletion packages/medusa/src/types/product-category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ import { IsNotEmpty, IsOptional, IsString, IsBoolean } from "class-validator"

export type CreateProductCategoryInput = {
name: string
handle: string
handle?: string
is_internal?: boolean
is_active?: boolean
parent_category_id?: string | null
}

export class AdminProductCategoriesReqBase {
@IsString()
@IsNotEmpty()
handle?: string

@IsBoolean()
@IsOptional()
is_internal?: boolean
Expand All @@ -18,6 +22,7 @@ export class AdminProductCategoriesReqBase {
@IsOptional()
is_active?: boolean

@IsString()
@IsOptional()
@Transform(({ value }) => {
return value === "null" ? null : value
Expand Down

0 comments on commit 7311999

Please sign in to comment.