-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(medusa): Nested Categories Admin Update Endpoint (#2986)
What: Introduces an admin endpoint that allows a user to update a product category Why: This is part of a greater goal of allowing products to be added to multiple categories. How: - Creates a route on the admin scope to update category - Creates a method in product category services to update a category RESOLVES CORE-956
- Loading branch information
Showing
7 changed files
with
327 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@medusajs/medusa": patch | ||
--- | ||
|
||
feat(medusa): added admin endpoint to update product categories |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
packages/medusa/src/api/routes/admin/product-categories/update-product-category.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
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" | ||
* summary: "Update a Product Category" | ||
* description: "Updates a Product Category." | ||
* 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: | ||
* schema: | ||
* $ref: "#/components/schemas/AdminPostProductCategoriesCategoryReq" | ||
* x-codeSamples: | ||
* - lang: JavaScript | ||
* label: JS Client | ||
* source: | | ||
* import Medusa from "@medusajs/medusa-js" | ||
* const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 }) | ||
* // must be previously logged in or use api token | ||
* medusa.admin.productCategories.update(categoryId, { | ||
* name: 'Skinny Jeans' | ||
* }) | ||
* .then(({ productCategory }) => { | ||
* console.log(productCategory.id); | ||
* }); | ||
* - lang: Shell | ||
* label: cURL | ||
* source: | | ||
* curl --location --request POST 'https://medusa-url.com/admin/product-categories/{id}' \ | ||
* --header 'Authorization: Bearer {api_token}' \ | ||
* --header 'Content-Type: application/json' \ | ||
* --data-raw '{ | ||
* "name": "Skinny Jeans" | ||
* }' | ||
* security: | ||
* - api_token: [] | ||
* - cookie_auth: [] | ||
* tags: | ||
* - Product Category | ||
* responses: | ||
* "200": | ||
* description: OK | ||
* content: | ||
* application/json: | ||
* schema: | ||
* type: object | ||
* properties: | ||
* productCategory: | ||
* $ref: "#/components/schemas/ProductCategory" | ||
* "400": | ||
* $ref: "#/components/responses/400_error" | ||
* "401": | ||
* $ref: "#/components/responses/unauthorized" | ||
* "404": | ||
* $ref: "#/components/responses/not_found_error" | ||
* "409": | ||
* $ref: "#/components/responses/invalid_state_error" | ||
* "422": | ||
* $ref: "#/components/responses/invalid_request_error" | ||
* "500": | ||
* $ref: "#/components/responses/500_error" | ||
*/ | ||
export default async (req: Request, res: Response) => { | ||
const { id } = req.params | ||
const { validatedBody } = req as { | ||
validatedBody: AdminPostProductCategoriesCategoryReq | ||
} | ||
|
||
const productCategoryService: ProductCategoryService = req.scope.resolve( | ||
"productCategoryService" | ||
) | ||
|
||
const manager: EntityManager = req.scope.resolve("manager") | ||
const updated = await manager.transaction(async (transactionManager) => { | ||
return await productCategoryService | ||
.withTransaction(transactionManager) | ||
.update(id, validatedBody) | ||
}) | ||
|
||
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: 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 | ||
*/ | ||
// eslint-disable-next-line max-len | ||
export class AdminPostProductCategoriesCategoryReq extends AdminProductCategoriesReqBase { | ||
@IsString() | ||
@IsOptional() | ||
name?: string | ||
} | ||
|
||
export class AdminPostProductCategoriesCategoryParams extends FindParams {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters