feat: Add v2 product types endpoints (#6880)
Also adjusts the product type module APIs to follow the conventions
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { IProductModuleService, ProductTypes } from "@medusajs/types"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
export const createProductTypesStepId = "create-product-types"
|
||||
export const createProductTypesStep = createStep(
|
||||
createProductTypesStepId,
|
||||
async (data: ProductTypes.CreateProductTypeDTO[], { container }) => {
|
||||
const service = container.resolve<IProductModuleService>(
|
||||
ModuleRegistrationName.PRODUCT
|
||||
)
|
||||
|
||||
const created = await service.createTypes(data)
|
||||
return new StepResponse(
|
||||
created,
|
||||
created.map((productType) => productType.id)
|
||||
)
|
||||
},
|
||||
async (createdIds, { container }) => {
|
||||
if (!createdIds?.length) {
|
||||
return
|
||||
}
|
||||
|
||||
const service = container.resolve<IProductModuleService>(
|
||||
ModuleRegistrationName.PRODUCT
|
||||
)
|
||||
|
||||
await service.deleteTypes(createdIds)
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,27 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { IProductModuleService } from "@medusajs/types"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
export const deleteProductTypesStepId = "delete-product-types"
|
||||
export const deleteProductTypesStep = createStep(
|
||||
deleteProductTypesStepId,
|
||||
async (ids: string[], { container }) => {
|
||||
const service = container.resolve<IProductModuleService>(
|
||||
ModuleRegistrationName.PRODUCT
|
||||
)
|
||||
|
||||
await service.softDeleteTypes(ids)
|
||||
return new StepResponse(void 0, ids)
|
||||
},
|
||||
async (prevIds, { container }) => {
|
||||
if (!prevIds?.length) {
|
||||
return
|
||||
}
|
||||
|
||||
const service = container.resolve<IProductModuleService>(
|
||||
ModuleRegistrationName.PRODUCT
|
||||
)
|
||||
|
||||
await service.restoreTypes(prevIds)
|
||||
}
|
||||
)
|
||||
@@ -13,3 +13,6 @@ export * from "./delete-product-variants"
|
||||
export * from "./create-collections"
|
||||
export * from "./update-collections"
|
||||
export * from "./delete-collections"
|
||||
export * from "./create-product-types"
|
||||
export * from "./update-product-types"
|
||||
export * from "./delete-product-types"
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { IProductModuleService, ProductTypes } from "@medusajs/types"
|
||||
import { getSelectsAndRelationsFromObjectArray } from "@medusajs/utils"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
type UpdateProductTypesStepInput = {
|
||||
selector: ProductTypes.FilterableProductTypeProps
|
||||
update: ProductTypes.UpdateProductTypeDTO
|
||||
}
|
||||
|
||||
export const updateProductTypesStepId = "update-product-types"
|
||||
export const updateProductTypesStep = createStep(
|
||||
updateProductTypesStepId,
|
||||
async (data: UpdateProductTypesStepInput, { container }) => {
|
||||
const service = container.resolve<IProductModuleService>(
|
||||
ModuleRegistrationName.PRODUCT
|
||||
)
|
||||
|
||||
const { selects, relations } = getSelectsAndRelationsFromObjectArray([
|
||||
data.update,
|
||||
])
|
||||
|
||||
const prevData = await service.listTypes(data.selector, {
|
||||
select: selects,
|
||||
relations,
|
||||
})
|
||||
|
||||
const productTypes = await service.updateTypes(data.selector, data.update)
|
||||
return new StepResponse(productTypes, prevData)
|
||||
},
|
||||
async (prevData, { container }) => {
|
||||
if (!prevData?.length) {
|
||||
return
|
||||
}
|
||||
|
||||
const service = container.resolve<IProductModuleService>(
|
||||
ModuleRegistrationName.PRODUCT
|
||||
)
|
||||
|
||||
await service.upsertTypes(prevData)
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,15 @@
|
||||
import { ProductTypes } from "@medusajs/types"
|
||||
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
|
||||
import { createProductTypesStep } from "../steps"
|
||||
|
||||
type WorkflowInput = { product_types: ProductTypes.CreateProductTypeDTO[] }
|
||||
|
||||
export const createProductTypesWorkflowId = "create-product-types"
|
||||
export const createProductTypesWorkflow = createWorkflow(
|
||||
createProductTypesWorkflowId,
|
||||
(
|
||||
input: WorkflowData<WorkflowInput>
|
||||
): WorkflowData<ProductTypes.ProductTypeDTO[]> => {
|
||||
return createProductTypesStep(input.product_types)
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,12 @@
|
||||
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
|
||||
import { deleteProductTypesStep } from "../steps"
|
||||
|
||||
type WorkflowInput = { ids: string[] }
|
||||
|
||||
export const deleteProductTypesWorkflowId = "delete-product-types"
|
||||
export const deleteProductTypesWorkflow = createWorkflow(
|
||||
deleteProductTypesWorkflowId,
|
||||
(input: WorkflowData<WorkflowInput>): WorkflowData<void> => {
|
||||
return deleteProductTypesStep(input.ids)
|
||||
}
|
||||
)
|
||||
@@ -10,3 +10,6 @@ export * from "./update-product-variants"
|
||||
export * from "./create-collections"
|
||||
export * from "./delete-collections"
|
||||
export * from "./update-collections"
|
||||
export * from "./create-product-types"
|
||||
export * from "./delete-product-types"
|
||||
export * from "./update-product-types"
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
import { ProductTypes } from "@medusajs/types"
|
||||
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
|
||||
import { updateProductTypesStep } from "../steps"
|
||||
|
||||
type UpdateProductTypesStepInput = {
|
||||
selector: ProductTypes.FilterableProductTypeProps
|
||||
update: ProductTypes.UpdateProductTypeDTO
|
||||
}
|
||||
|
||||
type WorkflowInput = UpdateProductTypesStepInput
|
||||
|
||||
export const updateProductTypesWorkflowId = "update-product-types"
|
||||
export const updateProductTypesWorkflow = createWorkflow(
|
||||
updateProductTypesWorkflowId,
|
||||
(
|
||||
input: WorkflowData<WorkflowInput>
|
||||
): WorkflowData<ProductTypes.ProductTypeDTO[]> => {
|
||||
return updateProductTypesStep(input)
|
||||
}
|
||||
)
|
||||
Reference in New Issue
Block a user