fix(medusa): fixes bug for mpath incorrectly updated for nested categories (#3311)
* chore: fix issue with mpath being incorrectly set * chore: address review changes
This commit is contained in:
@@ -121,7 +121,7 @@ class ProductCategoryService extends TransactionBaseService {
|
||||
|
||||
/**
|
||||
* Creates a product category
|
||||
* @param productCategory - params used to create
|
||||
* @param productCategoryInput - parameters to create a product category
|
||||
* @return created product category
|
||||
*/
|
||||
async create(
|
||||
@@ -129,6 +129,9 @@ class ProductCategoryService extends TransactionBaseService {
|
||||
): Promise<ProductCategory> {
|
||||
return await this.atomicPhase_(async (manager) => {
|
||||
const pcRepo = manager.withRepository(this.productCategoryRepo_)
|
||||
|
||||
await this.transformParentIdToEntity(productCategoryInput)
|
||||
|
||||
let productCategory = pcRepo.create(productCategoryInput)
|
||||
productCategory = await pcRepo.save(productCategory)
|
||||
|
||||
@@ -157,6 +160,8 @@ class ProductCategoryService extends TransactionBaseService {
|
||||
this.productCategoryRepo_
|
||||
)
|
||||
|
||||
await this.transformParentIdToEntity(productCategoryInput)
|
||||
|
||||
let productCategory = await this.retrieve(productCategoryId)
|
||||
|
||||
for (const key in productCategoryInput) {
|
||||
@@ -253,6 +258,34 @@ class ProductCategoryService extends TransactionBaseService {
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Accepts an input object and transforms product_category_id
|
||||
* into product_category entity.
|
||||
* @param productCategoryInput - params used to create/update
|
||||
* @return transformed productCategoryInput
|
||||
*/
|
||||
protected async transformParentIdToEntity(
|
||||
productCategoryInput:
|
||||
| CreateProductCategoryInput
|
||||
| UpdateProductCategoryInput
|
||||
): Promise<CreateProductCategoryInput | UpdateProductCategoryInput> {
|
||||
// Typeorm only updates mpath when the category entity of the parent
|
||||
// is passed into create/save. For this reason, everytime we create a
|
||||
// category, we must fetch the entity and push to create
|
||||
const parentCategoryId = productCategoryInput.parent_category_id
|
||||
|
||||
if (!parentCategoryId) {
|
||||
return productCategoryInput
|
||||
}
|
||||
|
||||
const parentCategory = await this.retrieve(parentCategoryId)
|
||||
|
||||
productCategoryInput.parent_category = parentCategory
|
||||
delete productCategoryInput.parent_category_id
|
||||
|
||||
return productCategoryInput
|
||||
}
|
||||
}
|
||||
|
||||
export default ProductCategoryService
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { Transform } from "class-transformer"
|
||||
import { IsNotEmpty, IsOptional, IsString, IsBoolean } from "class-validator"
|
||||
import { ProductCategory } from "../models"
|
||||
|
||||
export type CreateProductCategoryInput = {
|
||||
name: string
|
||||
@@ -7,6 +8,7 @@ export type CreateProductCategoryInput = {
|
||||
is_internal?: boolean
|
||||
is_active?: boolean
|
||||
parent_category_id?: string | null
|
||||
parent_category?: ProductCategory | null
|
||||
}
|
||||
|
||||
export type UpdateProductCategoryInput = {
|
||||
@@ -15,6 +17,7 @@ export type UpdateProductCategoryInput = {
|
||||
is_internal?: boolean
|
||||
is_active?: boolean
|
||||
parent_category_id?: string | null
|
||||
parent_category?: ProductCategory | null
|
||||
}
|
||||
|
||||
export class AdminProductCategoriesReqBase {
|
||||
|
||||
Reference in New Issue
Block a user