feat: Revamp of product categories (#7695)
* feat: Normalize the categories interface to match standards * feat: Revamp the product category implementation * fix: Adjustments to code and tests around product categories
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import {
|
||||
CreateProductCategoryDTO,
|
||||
IProductModuleService,
|
||||
} from "@medusajs/types"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
type CreateProductCategoriesStepInput = {
|
||||
product_categories: CreateProductCategoryDTO[]
|
||||
}
|
||||
|
||||
export const createProductCategoriesStepId = "create-product-categories"
|
||||
export const createProductCategoriesStep = createStep(
|
||||
createProductCategoriesStepId,
|
||||
async (data: CreateProductCategoriesStepInput, { container }) => {
|
||||
const service = container.resolve<IProductModuleService>(
|
||||
ModuleRegistrationName.PRODUCT
|
||||
)
|
||||
|
||||
const created = await service.createCategories(data.product_categories)
|
||||
|
||||
return new StepResponse(
|
||||
created,
|
||||
created.map((c) => c.id)
|
||||
)
|
||||
},
|
||||
async (createdIds, { container }) => {
|
||||
if (!createdIds?.length) {
|
||||
return
|
||||
}
|
||||
|
||||
const service = container.resolve<IProductModuleService>(
|
||||
ModuleRegistrationName.PRODUCT
|
||||
)
|
||||
|
||||
await service.deleteCategories(createdIds)
|
||||
}
|
||||
)
|
||||
@@ -1,35 +0,0 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import {
|
||||
CreateProductCategoryDTO,
|
||||
IProductModuleService,
|
||||
} from "@medusajs/types"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
type CreateProductCategoryStepInput = {
|
||||
product_category: CreateProductCategoryDTO
|
||||
}
|
||||
|
||||
export const createProductCategoryStepId = "create-product-category"
|
||||
export const createProductCategoryStep = createStep(
|
||||
createProductCategoryStepId,
|
||||
async (data: CreateProductCategoryStepInput, { container }) => {
|
||||
const service = container.resolve<IProductModuleService>(
|
||||
ModuleRegistrationName.PRODUCT
|
||||
)
|
||||
|
||||
const created = await service.createCategory(data.product_category)
|
||||
|
||||
return new StepResponse(created, created.id)
|
||||
},
|
||||
async (createdId, { container }) => {
|
||||
if (!createdId) {
|
||||
return
|
||||
}
|
||||
|
||||
const service = container.resolve<IProductModuleService>(
|
||||
ModuleRegistrationName.PRODUCT
|
||||
)
|
||||
|
||||
await service.deleteCategory(createdId)
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,27 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { IProductModuleService } from "@medusajs/types"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
export const deleteProductCategoriesStepId = "delete-product-categories"
|
||||
export const deleteProductCategoriesStep = createStep(
|
||||
deleteProductCategoriesStepId,
|
||||
async (ids: string[], { container }) => {
|
||||
const service = container.resolve<IProductModuleService>(
|
||||
ModuleRegistrationName.PRODUCT
|
||||
)
|
||||
|
||||
await service.softDeleteCategories(ids)
|
||||
return new StepResponse(void 0, ids)
|
||||
},
|
||||
async (prevIds, { container }) => {
|
||||
if (!prevIds?.length) {
|
||||
return
|
||||
}
|
||||
|
||||
const service = container.resolve<IProductModuleService>(
|
||||
ModuleRegistrationName.PRODUCT
|
||||
)
|
||||
|
||||
await service.restoreCategories(prevIds)
|
||||
}
|
||||
)
|
||||
@@ -1,28 +0,0 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { IProductModuleService } from "@medusajs/types"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
export const deleteProductCategoryStepId = "delete-product-category"
|
||||
export const deleteProductCategoryStep = createStep(
|
||||
deleteProductCategoryStepId,
|
||||
async (id: string, { container }) => {
|
||||
const service = container.resolve<IProductModuleService>(
|
||||
ModuleRegistrationName.PRODUCT
|
||||
)
|
||||
|
||||
await service.deleteCategory(id)
|
||||
return new StepResponse(void 0, id)
|
||||
},
|
||||
async (prevId, { container }) => {
|
||||
if (!prevId) {
|
||||
return
|
||||
}
|
||||
|
||||
const service = container.resolve<IProductModuleService>(
|
||||
ModuleRegistrationName.PRODUCT
|
||||
)
|
||||
|
||||
// TODO: There is no soft delete support for categories yet
|
||||
// await service.restoreCategory(prevId)
|
||||
}
|
||||
)
|
||||
@@ -1,3 +1,3 @@
|
||||
export * from "./create-product-category"
|
||||
export * from "./update-product-category"
|
||||
export * from "./delete-product-category"
|
||||
export * from "./create-product-categories"
|
||||
export * from "./update-product-categories"
|
||||
export * from "./delete-product-categories"
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import {
|
||||
FilterableProductCategoryProps,
|
||||
IProductModuleService,
|
||||
UpdateProductCategoryDTO,
|
||||
} from "@medusajs/types"
|
||||
import { getSelectsAndRelationsFromObjectArray } from "@medusajs/utils"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
type UpdateProductCategoriesStepInput = {
|
||||
selector: FilterableProductCategoryProps
|
||||
update: UpdateProductCategoryDTO
|
||||
}
|
||||
|
||||
export const updateProductCategoriesStepId = "update-product-categories"
|
||||
export const updateProductCategoriesStep = createStep(
|
||||
updateProductCategoriesStepId,
|
||||
async (data: UpdateProductCategoriesStepInput, { container }) => {
|
||||
const service = container.resolve<IProductModuleService>(
|
||||
ModuleRegistrationName.PRODUCT
|
||||
)
|
||||
|
||||
const { selects, relations } = getSelectsAndRelationsFromObjectArray([
|
||||
data.update,
|
||||
])
|
||||
|
||||
const prevData = await service.listCategories(data.selector, {
|
||||
select: selects,
|
||||
relations,
|
||||
})
|
||||
|
||||
const productCategories = await service.updateCategories(
|
||||
data.selector,
|
||||
data.update
|
||||
)
|
||||
return new StepResponse(productCategories, prevData)
|
||||
},
|
||||
async (prevData, { container }) => {
|
||||
if (!prevData?.length) {
|
||||
return
|
||||
}
|
||||
|
||||
const service = container.resolve<IProductModuleService>(
|
||||
ModuleRegistrationName.PRODUCT
|
||||
)
|
||||
|
||||
await service.upsertCategories(prevData)
|
||||
}
|
||||
)
|
||||
@@ -1,61 +0,0 @@
|
||||
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,
|
||||
})
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,14 @@
|
||||
import { ProductCategoryWorkflow } from "@medusajs/types"
|
||||
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
|
||||
import { createProductCategoriesStep } from "../steps"
|
||||
|
||||
type WorkflowInputData =
|
||||
ProductCategoryWorkflow.CreateProductCategoriesWorkflowInput
|
||||
|
||||
export const createProductCategoriesWorkflowId = "create-product-categories"
|
||||
export const createProductCategoriesWorkflow = createWorkflow(
|
||||
createProductCategoriesWorkflowId,
|
||||
(input: WorkflowData<WorkflowInputData>) => {
|
||||
return createProductCategoriesStep(input)
|
||||
}
|
||||
)
|
||||
@@ -1,16 +0,0 @@
|
||||
import { ProductCategoryWorkflow } from "@medusajs/types"
|
||||
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
|
||||
import { createProductCategoryStep } from "../steps"
|
||||
|
||||
type WorkflowInputData =
|
||||
ProductCategoryWorkflow.CreateProductCategoryWorkflowInput
|
||||
|
||||
export const createProductCategoryWorkflowId = "create-product-category"
|
||||
export const createProductCategoryWorkflow = createWorkflow(
|
||||
createProductCategoryWorkflowId,
|
||||
(input: WorkflowData<WorkflowInputData>) => {
|
||||
const category = createProductCategoryStep(input)
|
||||
|
||||
return category
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,10 @@
|
||||
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
|
||||
import { deleteProductCategoriesStep } from "../steps"
|
||||
|
||||
export const deleteProductCategoriesWorkflowId = "delete-product-categories"
|
||||
export const deleteProductCategoriesWorkflow = createWorkflow(
|
||||
deleteProductCategoriesWorkflowId,
|
||||
(input: WorkflowData<string[]>) => {
|
||||
return deleteProductCategoriesStep(input)
|
||||
}
|
||||
)
|
||||
@@ -1,10 +0,0 @@
|
||||
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
|
||||
import { deleteProductCategoryStep } from "../steps"
|
||||
|
||||
export const deleteProductCategoryWorkflowId = "delete-product-category"
|
||||
export const deleteProductCategoryWorkflow = createWorkflow(
|
||||
deleteProductCategoryWorkflowId,
|
||||
(input: WorkflowData<string>) => {
|
||||
return deleteProductCategoryStep(input)
|
||||
}
|
||||
)
|
||||
@@ -1,3 +1,3 @@
|
||||
export * from "./create-product-category"
|
||||
export * from "./update-product-category"
|
||||
export * from "./delete-product-category"
|
||||
export * from "./create-product-categories"
|
||||
export * from "./update-product-categories"
|
||||
export * from "./delete-product-categories"
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
import { ProductCategoryWorkflow } from "@medusajs/types"
|
||||
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
|
||||
import { updateProductCategoriesStep } from "../steps"
|
||||
|
||||
type WorkflowInputData =
|
||||
ProductCategoryWorkflow.UpdateProductCategoriesWorkflowInput
|
||||
|
||||
export const updateProductCategoriesWorkflowId = "update-product-categories"
|
||||
export const updateProductCategoriesWorkflow = createWorkflow(
|
||||
updateProductCategoriesWorkflowId,
|
||||
(input: WorkflowData<WorkflowInputData>) => {
|
||||
return updateProductCategoriesStep(input)
|
||||
}
|
||||
)
|
||||
@@ -1,16 +0,0 @@
|
||||
import { ProductCategoryWorkflow } from "@medusajs/types"
|
||||
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
|
||||
import { updateProductCategoryStep } from "../steps"
|
||||
|
||||
type WorkflowInputData =
|
||||
ProductCategoryWorkflow.UpdateProductCategoryWorkflowInput
|
||||
|
||||
export const updateProductCategoryWorkflowId = "update-product-category"
|
||||
export const updateProductCategoryWorkflow = createWorkflow(
|
||||
updateProductCategoryWorkflowId,
|
||||
(input: WorkflowData<WorkflowInputData>) => {
|
||||
const category = updateProductCategoryStep(input)
|
||||
|
||||
return category
|
||||
}
|
||||
)
|
||||
Reference in New Issue
Block a user