feat: Update product category (#7092)

* feat: Update product category

* Disable V2 FF in product category tes

* clean up
This commit is contained in:
Oli Juhl
2024-04-22 12:30:27 +02:00
committed by GitHub
parent 5c71e8304d
commit 67a21c3e45
15 changed files with 383 additions and 110 deletions
@@ -0,0 +1,61 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import {
IProductModuleService,
UpdateProductCategoryDTO,
} from "@medusajs/types"
import { getSelectsAndRelationsFromObjectArray } from "@medusajs/utils"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
type UpdateProductCategoryStepInput = {
id: string
data: UpdateProductCategoryDTO
}
export const updateProductCategoryStepId = "update-product-category"
export const updateProductCategoryStep = createStep(
updateProductCategoryStepId,
async (data: UpdateProductCategoryStepInput, { container }) => {
const service = container.resolve<IProductModuleService>(
ModuleRegistrationName.PRODUCT
)
const { selects, relations } = getSelectsAndRelationsFromObjectArray([
data.data,
])
const prevData = await service.listCategories(
{ id: data.id },
{
select: selects,
relations,
}
)
const updated = await service.updateCategory(data.id, data.data)
return new StepResponse(updated, prevData)
},
async (prevData, { container }) => {
if (!prevData?.length) {
return
}
const service = container.resolve<IProductModuleService>(
ModuleRegistrationName.PRODUCT
)
// TODO: Should be removed when bulk update is implemented
const category = prevData[0]
await service.updateCategory(category.id, {
name: category.name,
description: category.description,
is_active: category.is_active,
is_internal: category.is_internal,
rank: category.rank,
handle: category.handle,
metadata: category.metadata,
parent_category_id: category.parent_category_id,
})
}
)
@@ -1 +1,2 @@
export * from "./create-product-category"
export * from "./update-product-category"
@@ -0,0 +1,16 @@
import { ProductCategoryWorkflow } from "@medusajs/types"
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { updateProductCategoryStep } from "../steps/update-product-category"
type WorkflowInputData =
ProductCategoryWorkflow.UpdateProductCategoryWorkflowInput
export const updateProductCategoryWorkflowId = "update-product-category"
export const updateProductCategoryWorkflow = createWorkflow(
updateProductCategoryWorkflowId,
(input: WorkflowData<WorkflowInputData>) => {
const category = updateProductCategoryStep(input)
return category
}
)