Skip to content

Commit

Permalink
chore: added trasnform query, minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
riqwan committed Jan 11, 2023
1 parent f3e91e0 commit 8e109a0
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 13 deletions.
6 changes: 4 additions & 2 deletions integration-tests/api/__tests__/admin/product-category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ describe("/admin/product-categories", () => {
const [process, connection] = await startServerWithEnvironment({
cwd,
env: { MEDUSA_FF_PRODUCT_CATEGORIES: true },
verbose: true
})
dbConnection = connection
medusaProcess = process
Expand Down Expand Up @@ -454,7 +453,10 @@ describe("/admin/product-categories", () => {
is_active: true,
created_at: expect.any(String),
updated_at: expect.any(String),
parent_category_id: productCategory2.id,
parent_category: expect.objectContaining({
id: productCategory2.id,
}),
category_children: []
}),
})
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import createProductCategory, {

import updateProductCategory, {
AdminPostProductCategoriesCategoryReq,
AdminPostProductCategoriesCategoryParams,
} from "./update-product-category"

const route = Router()
Expand Down Expand Up @@ -65,6 +66,11 @@ export default (app) => {

route.post(
"/:id",
transformQuery(AdminPostProductCategoriesCategoryParams, {
defaultFields: defaultProductCategoryFields,
defaultRelations: defaultAdminProductCategoryRelations,
isList: false,
}),
transformBody(AdminPostProductCategoriesCategoryReq),
middlewares.wrap(updateProductCategory)
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { IsOptional, IsString } from "class-validator"
import { Request, Response } from "express"
import { EntityManager } from "typeorm"

import { ProductCategoryService } from "../../../../services"
import { AdminProductCategoriesReqBase } from "../../../../types/product-category"

import { FindParams } from "../../../../types/common"
/**
* @oas [post] /product-categories/{id}
* operationId: "PostProductCategoriesCategory"
Expand All @@ -12,6 +13,8 @@ import { AdminProductCategoriesReqBase } from "../../../../types/product-categor
* x-authenticated: true
* parameters:
* - (path) id=* {string} The ID of the Product Category.
* - (query) expand {string} (Comma separated) Which fields should be expanded in each product category.
* - (query) fields {string} (Comma separated) Which fields should be retrieved in each product category.
* requestBody:
* content:
* application/json:
Expand Down Expand Up @@ -84,24 +87,31 @@ export default async (req: Request, res: Response) => {
.update(id, validatedBody)
})

const productCategory = await productCategoryService.retrieve(updated.id)
const productCategory = await productCategoryService.retrieve(
updated.id,
req.retrieveConfig,
)

res.status(200).json({ product_category: productCategory })
}

/**
* @schema AdminPostProductCategoriesCategoryReq
* type: object
* properties:
* name:
* type: string
* description: The name to identify the Product Category by.
* handle:
* type: string
* description: An optional handle to be used in slugs, if none is provided we will kebab-case the name.
* metadata:
* description: An optional set of key-value pairs to hold additional information.
* type: object
* description: A handle to be used in slugs.
* is_internal:
* type: boolean
* description: A flag to make product category an internal category for admins
* is_active:
* type: boolean
* description: A flag to make product category visible/hidden in the store front
* parent_category_id:
* type: string
* description: The ID of the parent product category
*/
export class AdminPostProductCategoriesCategoryReq extends AdminProductCategoriesReqBase {
@IsString()
Expand All @@ -112,3 +122,5 @@ export class AdminPostProductCategoriesCategoryReq extends AdminProductCategorie
@IsOptional()
handle?: string
}

export class AdminPostProductCategoriesCategoryParams extends FindParams {}
4 changes: 2 additions & 2 deletions packages/medusa/src/services/product-category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { FindConfig, Selector, QuerySelector } from "../types/common"
import { buildQuery } from "../utils"
import {
CreateProductCategoryInput,
UpdateProductCategory,
UpdateProductCategoryInput,
} from "../types/product-category"

type InjectedDependencies = {
Expand Down Expand Up @@ -127,7 +127,7 @@ class ProductCategoryService extends TransactionBaseService {
*/
async update(
productCategoryId: string,
productCategoryUpdate: UpdateProductCategory
productCategoryUpdate: UpdateProductCategoryInput
): Promise<ProductCategory> {
return await this.atomicPhase_(async (manager) => {
const productCategoryRepo = manager.getCustomRepository(
Expand Down
2 changes: 1 addition & 1 deletion packages/medusa/src/types/product-category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export type CreateProductCategoryInput = {
parent_category_id?: string | null
}

export type UpdateProductCategory = {
export type UpdateProductCategoryInput = {
name?: string
handle?: string
is_internal?: boolean
Expand Down

0 comments on commit 8e109a0

Please sign in to comment.