feat: Implement missing methods in product module and make more tests pass (#6650)
The 2 bigger remaining tasks are: 1. handling prices for variants 2. Handling options (breaking change) After that all tests should pass on both v1 and v2
This commit is contained in:
30
packages/core-flows/src/product/steps/create-collections.ts
Normal file
30
packages/core-flows/src/product/steps/create-collections.ts
Normal file
@@ -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 createCollectionsStepId = "create-collections"
|
||||
export const createCollectionsStep = createStep(
|
||||
createCollectionsStepId,
|
||||
async (data: ProductTypes.CreateProductCollectionDTO[], { container }) => {
|
||||
const service = container.resolve<IProductModuleService>(
|
||||
ModuleRegistrationName.PRODUCT
|
||||
)
|
||||
|
||||
const created = await service.createCollections(data)
|
||||
return new StepResponse(
|
||||
created,
|
||||
created.map((collection) => collection.id)
|
||||
)
|
||||
},
|
||||
async (createdIds, { container }) => {
|
||||
if (!createdIds?.length) {
|
||||
return
|
||||
}
|
||||
|
||||
const service = container.resolve<IProductModuleService>(
|
||||
ModuleRegistrationName.PRODUCT
|
||||
)
|
||||
|
||||
await service.deleteCollections(createdIds)
|
||||
}
|
||||
)
|
||||
27
packages/core-flows/src/product/steps/delete-collections.ts
Normal file
27
packages/core-flows/src/product/steps/delete-collections.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { IProductModuleService } from "@medusajs/types"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
export const deleteCollectionsStepId = "delete-collections"
|
||||
export const deleteCollectionsStep = createStep(
|
||||
deleteCollectionsStepId,
|
||||
async (ids: string[], { container }) => {
|
||||
const service = container.resolve<IProductModuleService>(
|
||||
ModuleRegistrationName.PRODUCT
|
||||
)
|
||||
|
||||
await service.softDeleteCollections(ids)
|
||||
return new StepResponse(void 0, ids)
|
||||
},
|
||||
async (prevIds, { container }) => {
|
||||
if (!prevIds?.length) {
|
||||
return
|
||||
}
|
||||
|
||||
const service = container.resolve<IProductModuleService>(
|
||||
ModuleRegistrationName.PRODUCT
|
||||
)
|
||||
|
||||
await service.restoreCollections(prevIds)
|
||||
}
|
||||
)
|
||||
@@ -7,3 +7,6 @@ export * from "./delete-product-options"
|
||||
export * from "./create-product-variants"
|
||||
export * from "./update-product-variants"
|
||||
export * from "./delete-product-variants"
|
||||
export * from "./create-collections"
|
||||
export * from "./update-collections"
|
||||
export * from "./delete-collections"
|
||||
|
||||
49
packages/core-flows/src/product/steps/update-collections.ts
Normal file
49
packages/core-flows/src/product/steps/update-collections.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
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 UpdateCollectionsStepInput = {
|
||||
selector: ProductTypes.FilterableProductCollectionProps
|
||||
update: ProductTypes.UpdateProductCollectionDTO
|
||||
}
|
||||
|
||||
export const updateCollectionsStepId = "update-collections"
|
||||
export const updateCollectionsStep = createStep(
|
||||
updateCollectionsStepId,
|
||||
async (data: UpdateCollectionsStepInput, { container }) => {
|
||||
const service = container.resolve<IProductModuleService>(
|
||||
ModuleRegistrationName.PRODUCT
|
||||
)
|
||||
|
||||
const { selects, relations } = getSelectsAndRelationsFromObjectArray([
|
||||
data.update,
|
||||
])
|
||||
|
||||
const prevData = await service.listCollections(data.selector, {
|
||||
select: selects,
|
||||
relations,
|
||||
})
|
||||
|
||||
const collections = await service.updateCollections(
|
||||
data.selector,
|
||||
data.update
|
||||
)
|
||||
return new StepResponse(collections, prevData)
|
||||
},
|
||||
async (prevData, { container }) => {
|
||||
if (!prevData?.length) {
|
||||
return
|
||||
}
|
||||
|
||||
const service = container.resolve<IProductModuleService>(
|
||||
ModuleRegistrationName.PRODUCT
|
||||
)
|
||||
|
||||
await service.upsertCollections(
|
||||
prevData.map((r) => ({
|
||||
...(r as unknown as ProductTypes.UpdateProductCollectionDTO),
|
||||
}))
|
||||
)
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,15 @@
|
||||
import { ProductTypes } from "@medusajs/types"
|
||||
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
|
||||
import { createCollectionsStep } from "../steps"
|
||||
|
||||
type WorkflowInput = { collections: ProductTypes.CreateProductCollectionDTO[] }
|
||||
|
||||
export const createCollectionsWorkflowId = "create-collections"
|
||||
export const createCollectionsWorkflow = createWorkflow(
|
||||
createCollectionsWorkflowId,
|
||||
(
|
||||
input: WorkflowData<WorkflowInput>
|
||||
): WorkflowData<ProductTypes.ProductCollectionDTO[]> => {
|
||||
return createCollectionsStep(input.collections)
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,12 @@
|
||||
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
|
||||
import { deleteCollectionsStep } from "../steps"
|
||||
|
||||
type WorkflowInput = { ids: string[] }
|
||||
|
||||
export const deleteCollectionsWorkflowId = "delete-collections"
|
||||
export const deleteCollectionsWorkflow = createWorkflow(
|
||||
deleteCollectionsWorkflowId,
|
||||
(input: WorkflowData<WorkflowInput>): WorkflowData<void> => {
|
||||
return deleteCollectionsStep(input.ids)
|
||||
}
|
||||
)
|
||||
@@ -7,3 +7,6 @@ export * from "./update-product-options"
|
||||
export * from "./create-product-variants"
|
||||
export * from "./delete-product-variants"
|
||||
export * from "./update-product-variants"
|
||||
export * from "./create-collections"
|
||||
export * from "./delete-collections"
|
||||
export * from "./update-collections"
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
import { ProductTypes } from "@medusajs/types"
|
||||
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
|
||||
import { updateCollectionsStep } from "../steps"
|
||||
|
||||
type UpdateCollectionsStepInput = {
|
||||
selector: ProductTypes.FilterableProductCollectionProps
|
||||
update: ProductTypes.UpdateProductCollectionDTO
|
||||
}
|
||||
|
||||
type WorkflowInput = UpdateCollectionsStepInput
|
||||
|
||||
export const updateCollectionsWorkflowId = "update-collections"
|
||||
export const updateCollectionsWorkflow = createWorkflow(
|
||||
updateCollectionsWorkflowId,
|
||||
(
|
||||
input: WorkflowData<WorkflowInput>
|
||||
): WorkflowData<ProductTypes.ProductCollectionDTO[]> => {
|
||||
return updateCollectionsStep(input)
|
||||
}
|
||||
)
|
||||
Reference in New Issue
Block a user