Feat: Add product tag endpoints, move tests to HTTP folder (#7591)
* chore: Move product type tests to HTTP folder * feat: Add product tags endpoints and move tests to HTTP folder
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 createProductTagsStepId = "create-product-tags"
|
||||
export const createProductTagsStep = createStep(
|
||||
createProductTagsStepId,
|
||||
async (data: ProductTypes.CreateProductTagDTO[], { container }) => {
|
||||
const service = container.resolve<IProductModuleService>(
|
||||
ModuleRegistrationName.PRODUCT
|
||||
)
|
||||
|
||||
const created = await service.createTags(data)
|
||||
return new StepResponse(
|
||||
created,
|
||||
created.map((productTag) => productTag.id)
|
||||
)
|
||||
},
|
||||
async (createdIds, { container }) => {
|
||||
if (!createdIds?.length) {
|
||||
return
|
||||
}
|
||||
|
||||
const service = container.resolve<IProductModuleService>(
|
||||
ModuleRegistrationName.PRODUCT
|
||||
)
|
||||
|
||||
await service.deleteTags(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 deleteProductTagsStepId = "delete-product-tags"
|
||||
export const deleteProductTagsStep = createStep(
|
||||
deleteProductTagsStepId,
|
||||
async (ids: string[], { container }) => {
|
||||
const service = container.resolve<IProductModuleService>(
|
||||
ModuleRegistrationName.PRODUCT
|
||||
)
|
||||
|
||||
await service.softDeleteTags(ids)
|
||||
return new StepResponse(void 0, ids)
|
||||
},
|
||||
async (prevIds, { container }) => {
|
||||
if (!prevIds?.length) {
|
||||
return
|
||||
}
|
||||
|
||||
const service = container.resolve<IProductModuleService>(
|
||||
ModuleRegistrationName.PRODUCT
|
||||
)
|
||||
|
||||
await service.restoreTags(prevIds)
|
||||
}
|
||||
)
|
||||
@@ -16,3 +16,6 @@ export * from "./batch-link-products-collection"
|
||||
export * from "./create-product-types"
|
||||
export * from "./update-product-types"
|
||||
export * from "./delete-product-types"
|
||||
export * from "./create-product-tags"
|
||||
export * from "./update-product-tags"
|
||||
export * from "./delete-product-tags"
|
||||
|
||||
@@ -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 UpdateProductTagsStepInput = {
|
||||
selector: ProductTypes.FilterableProductTagProps
|
||||
update: ProductTypes.UpdateProductTagDTO
|
||||
}
|
||||
|
||||
export const updateProductTagsStepId = "update-product-tags"
|
||||
export const updateProductTagsStep = createStep(
|
||||
updateProductTagsStepId,
|
||||
async (data: UpdateProductTagsStepInput, { container }) => {
|
||||
const service = container.resolve<IProductModuleService>(
|
||||
ModuleRegistrationName.PRODUCT
|
||||
)
|
||||
|
||||
const { selects, relations } = getSelectsAndRelationsFromObjectArray([
|
||||
data.update,
|
||||
])
|
||||
|
||||
const prevData = await service.listTags(data.selector, {
|
||||
select: selects,
|
||||
relations,
|
||||
})
|
||||
|
||||
const productTags = await service.updateTags(data.selector, data.update)
|
||||
return new StepResponse(productTags, prevData)
|
||||
},
|
||||
async (prevData, { container }) => {
|
||||
if (!prevData?.length) {
|
||||
return
|
||||
}
|
||||
|
||||
const service = container.resolve<IProductModuleService>(
|
||||
ModuleRegistrationName.PRODUCT
|
||||
)
|
||||
|
||||
await service.upsertTags(prevData)
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,15 @@
|
||||
import { ProductTypes } from "@medusajs/types"
|
||||
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
|
||||
import { createProductTagsStep } from "../steps"
|
||||
|
||||
type WorkflowInput = { product_tags: ProductTypes.CreateProductTagDTO[] }
|
||||
|
||||
export const createProductTagsWorkflowId = "create-product-tags"
|
||||
export const createProductTagsWorkflow = createWorkflow(
|
||||
createProductTagsWorkflowId,
|
||||
(
|
||||
input: WorkflowData<WorkflowInput>
|
||||
): WorkflowData<ProductTypes.ProductTagDTO[]> => {
|
||||
return createProductTagsStep(input.product_tags)
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,12 @@
|
||||
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
|
||||
import { deleteProductTagsStep } from "../steps"
|
||||
|
||||
type WorkflowInput = { ids: string[] }
|
||||
|
||||
export const deleteProductTagsWorkflowId = "delete-product-tags"
|
||||
export const deleteProductTagsWorkflow = createWorkflow(
|
||||
deleteProductTagsWorkflowId,
|
||||
(input: WorkflowData<WorkflowInput>): WorkflowData<void> => {
|
||||
return deleteProductTagsStep(input.ids)
|
||||
}
|
||||
)
|
||||
@@ -5,15 +5,18 @@ export * from "./batch-products-in-category"
|
||||
export * from "./create-collections"
|
||||
export * from "./create-product-options"
|
||||
export * from "./create-product-types"
|
||||
export * from "./create-product-tags"
|
||||
export * from "./create-product-variants"
|
||||
export * from "./create-products"
|
||||
export * from "./delete-collections"
|
||||
export * from "./delete-product-options"
|
||||
export * from "./delete-product-types"
|
||||
export * from "./delete-product-tags"
|
||||
export * from "./delete-product-variants"
|
||||
export * from "./delete-products"
|
||||
export * from "./update-collections"
|
||||
export * from "./update-product-options"
|
||||
export * from "./update-product-types"
|
||||
export * from "./update-product-tags"
|
||||
export * from "./update-product-variants"
|
||||
export * from "./update-products"
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
import { ProductTypes } from "@medusajs/types"
|
||||
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
|
||||
import { updateProductTagsStep } from "../steps"
|
||||
|
||||
type UpdateProductTagsStepInput = {
|
||||
selector: ProductTypes.FilterableProductTypeProps
|
||||
update: ProductTypes.UpdateProductTypeDTO
|
||||
}
|
||||
|
||||
type WorkflowInput = UpdateProductTagsStepInput
|
||||
|
||||
export const updateProductTagsWorkflowId = "update-product-tags"
|
||||
export const updateProductTagsWorkflow = createWorkflow(
|
||||
updateProductTagsWorkflowId,
|
||||
(
|
||||
input: WorkflowData<WorkflowInput>
|
||||
): WorkflowData<ProductTypes.ProductTagDTO[]> => {
|
||||
return updateProductTagsStep(input)
|
||||
}
|
||||
)
|
||||
Reference in New Issue
Block a user