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:
Stevche Radevski
2024-06-13 09:10:12 +02:00
committed by GitHub
parent fbd8eef18b
commit d862d03de0
35 changed files with 1135 additions and 874 deletions
@@ -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
}
)
@@ -104,9 +104,9 @@ export interface TreeRepositoryService<T = any>
context?: Context
): Promise<[T[], number]>
create(data: unknown, context?: Context): Promise<T>
create(data: unknown[], context?: Context): Promise<T[]>
delete(id: string, context?: Context): Promise<void>
delete(ids: string[], context?: Context): Promise<void>
}
/**
@@ -371,6 +371,13 @@ export interface CreateProductCategoryDTO {
metadata?: Record<string, unknown>
}
export interface UpsertProductCategoryDTO extends UpdateProductCategoryDTO {
/**
* The ID of the product category to update. If not provided, the product category is created. In this case, the `name` property is required.
*/
id?: string
}
/**
* @interface
*
+172 -22
View File
@@ -28,6 +28,7 @@ import {
UpdateProductTagDTO,
UpdateProductTypeDTO,
UpdateProductVariantDTO,
UpsertProductCategoryDTO,
UpsertProductCollectionDTO,
UpsertProductDTO,
UpsertProductOptionDTO,
@@ -2201,6 +2202,30 @@ export interface IProductModuleService extends IModuleService {
sharedContext?: Context
): Promise<[ProductCategoryDTO[], number]>
/**
* This method is used to create product categories.
*
* @param {CreateProductCategoryDTO[]} data - The product categories to be created.
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<ProductCategoryDTO[]>} The list of created product categories.
*
* @example
* const categories =
* await productModuleService.createCategories([
* {
* name: "Tools",
* },
* {
* name: "Clothing",
* },
* ])
*
*/
createCategories(
data: CreateProductCategoryDTO[],
sharedContext?: Context
): Promise<ProductCategoryDTO[]>
/**
* This method is used to create a product category.
*
@@ -2209,48 +2234,173 @@ export interface IProductModuleService extends IModuleService {
* @returns {Promise<ProductCategoryDTO>} The created product category.
*
* @example
* const category = await productModuleService.createCategory({
* name: "Shirts",
* parent_category_id: null,
* })
* const category =
* await productModuleService.createCategories({
* name: "Tools",
* })
*
*/
createCategory(
createCategories(
data: CreateProductCategoryDTO,
sharedContext?: Context
): Promise<ProductCategoryDTO>
/**
* This method is used to update a product category by its ID.
* This method updates existing categories, or creates new ones if they don't exist.
*
* @param {string} categoryId - The ID of the product category to update.
* @param {UpdateProductCategoryDTO} data - The attributes to update in th product category.
* @param {UpsertProductCategoryDTO[]} data - The attributes to update or create for each category.
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<ProductCategoryDTO>} The updated product category.
* @returns {Promise<ProductCategoryDTO[]>} The updated and created categories.
*
* @example
* const category = await productModuleService.updateCategory(
* "pcat_123",
* {
* name: "Shirts",
* }
* )
* const categories =
* await productModuleService.upsertCategories([
* {
* id: "pcat_123",
* name: "Clothing",
* },
* {
* name: "Tools",
* },
* ])
*/
updateCategory(
categoryId: string,
upsertCategories(
data: UpsertProductCategoryDTO[],
sharedContext?: Context
): Promise<ProductCategoryDTO[]>
/**
* This method updates an existing category, or creates a new one if it doesn't exist.
*
* @param {UpsertProductCategoryDTO} data - The attributes to update or create for the category.
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<ProductCategoryDTO>} The updated or created category.
*
* @example
* const category =
* await productModuleService.upsertCategories({
* id: "pcat_123",
* name: "Clothing",
* })
*/
upsertCategories(
data: UpsertProductCategoryDTO,
sharedContext?: Context
): Promise<ProductCategoryDTO>
/**
* This method is used to update a category.
*
* @param {string} id - The ID of the category to be updated.
* @param {UpdateProductCategoryDTO} data - The attributes of the category to be updated
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<ProductCategoryDTO>} The updated category.
*
* @example
* const category =
* await productModuleService.updateCategories("pcat_123", {
* title: "Tools",
* })
*/
updateCategories(
id: string,
data: UpdateProductCategoryDTO,
sharedContext?: Context
): Promise<ProductCategoryDTO>
/**
* This method is used to delete a product category by its ID.
* This method is used to update a list of categories matching the specified filters.
*
* @param {string} categoryId - The ID of the product category to delete.
* @param {FilterableProductCategoryProps} selector - The filters specifying which categories to update.
* @param {UpdateProductCategoryDTO} data - The attributes to be updated on the selected categories
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<void>} Resolves when the product category is successfully deleted.
* @returns {Promise<ProductCategoryDTO[]>} The updated categories.
*
* @example
* await productModuleService.deleteCategory("pcat_123")
* const categories =
* await productModuleService.updateCategories(
* {
* id: ["pcat_123", "pcat_321"],
* },
* {
* title: "Tools",
* }
* )
*/
deleteCategory(categoryId: string, sharedContext?: Context): Promise<void>
updateCategories(
selector: FilterableProductCategoryProps,
data: UpdateProductCategoryDTO,
sharedContext?: Context
): Promise<ProductCategoryDTO[]>
/**
* This method is used to delete categories by their ID.
*
* @param {string[]} productCategoryIds - The IDs of the product categories to be updated.
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<void>} Resolves when the product options are successfully deleted.
*
* @example
* await productModuleService.deleteCategories([
* "pcat_123",
* "pcat_321",
* ])
*
*/
deleteCategories(
productCategoryIds: string[],
sharedContext?: Context
): Promise<void>
/**
* This method is used to delete product categories. Unlike the {@link deleteCategories} method, this method won't completely remove the category. It can still be accessed or retrieved using methods like {@link retrieveCategories} if you pass the `withDeleted` property to the `config` object parameter.
*
* The soft-deleted categories can be restored using the {@link restoreCategories} method.
*
* @param {string[]} categoryIds - The IDs of the categories to soft-delete.
* @param {SoftDeleteReturn<TReturnableLinkableKeys>} config -
* Configurations determining which relations to soft delete along with the each of the categories. You can pass to its `returnLinkableKeys`
* property any of the category's relation attribute names.
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<Record<string, string[]> | void>}
* An object that includes the IDs of related records that were also soft deleted. The object's keys are the ID attribute names of the category entity's relations.
*
* If there are no related records, the promise resolved to `void`.
*
* @example
* await productModuleService.softDeleteCategories([
* "pcat_123",
* "pcat_321",
* ])
*/
softDeleteCategories<TReturnableLinkableKeys extends string = string>(
categoryIds: string[],
config?: SoftDeleteReturn<TReturnableLinkableKeys>,
sharedContext?: Context
): Promise<Record<string, string[]> | void>
/**
* This method is used to restore categories which were deleted using the {@link softDelete} method.
*
* @param {string[]} categoryIds - The IDs of the categories to restore.
* @param {RestoreReturn<TReturnableLinkableKeys>} config -
* Configurations determining which relations to restore along with each of the categories. You can pass to its `returnLinkableKeys`
* property any of the category's relation attribute names.
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<Record<string, string[]> | void>}
* An object that includes the IDs of related records that were restored. The object's keys are the ID attribute names of the product entity's relations.
*
* If there are no related records that were restored, the promise resolved to `void`.
*
* @example
* await productModuleService.restoreCategories([
* "pcat_123",
* "pcat_321",
* ])
*/
restoreCategories<TReturnableLinkableKeys extends string = string>(
categoryIds: string[],
config?: RestoreReturn<TReturnableLinkableKeys>,
sharedContext?: Context
): Promise<Record<string, string[]> | void>
}
@@ -1,19 +1,16 @@
import { LinkWorkflowInput } from "../../common"
import {
CreateProductCategoryDTO,
FilterableProductCategoryProps,
UpdateProductCategoryDTO,
} from "../../product"
export interface CreateProductCategoryWorkflowInput {
product_category: CreateProductCategoryDTO
export interface CreateProductCategoriesWorkflowInput {
product_categories: CreateProductCategoryDTO[]
}
// TODO: Should we converted to bulk update
export interface UpdateProductCategoryWorkflowInput {
// selector: FilterableProductCategoryProps
// data: UpdateProductCategoryDTO
id: string
data: UpdateProductCategoryDTO
export interface UpdateProductCategoriesWorkflowInput {
selector: FilterableProductCategoryProps
update: UpdateProductCategoryDTO
}
export interface BatchUpdateProductsOnCategoryWorkflowInput
@@ -241,11 +241,15 @@ export class MikroOrmBaseTreeRepository<
throw new Error("Method not implemented.")
}
create(data: unknown, context?: Context): Promise<T> {
create(data: unknown[], context?: Context): Promise<T[]> {
throw new Error("Method not implemented.")
}
delete(id: string, context?: Context): Promise<void> {
update(data: unknown[], context?: Context): Promise<T[]> {
throw new Error("Method not implemented.")
}
delete(ids: string[], context?: Context): Promise<void> {
throw new Error("Method not implemented.")
}
}
+10 -2
View File
@@ -6,8 +6,16 @@ const eventBaseNames: [
"productVariant",
"productOption",
"productType",
"productTag"
] = ["product", "productVariant", "productOption", "productType", "productTag"]
"productTag",
"productCategory"
] = [
"product",
"productVariant",
"productOption",
"productType",
"productTag",
"productCategory",
]
export const ProductEvents = buildEventNamesFromEntityName(
eventBaseNames,