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:
1 parent
ce40fe88f5
commit
0929c4f457
24 files changed
+805
-399
No files matched your search
@@ -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)
|
||||
}
|
||||
)
|
||||
@@ -31,6 +31,7 @@ import {
|
||||
UpsertProductCollectionDTO,
|
||||
UpsertProductDTO,
|
||||
UpsertProductOptionDTO,
|
||||
UpsertProductTagDTO,
|
||||
UpsertProductTypeDTO,
|
||||
UpsertProductVariantDTO,
|
||||
} from "./common"
|
||||
@@ -512,19 +513,16 @@ export interface IProductModuleService extends IModuleService {
|
||||
): Promise<[ProductTagDTO[], number]>
|
||||
|
||||
/**
|
||||
* This method is used to create product tags.
|
||||
* This method is used to create a product tag.
|
||||
*
|
||||
* @param {CreateProductTagDTO[]} data - The product tags to create.
|
||||
* @param {CreateProductTagDTO[]} data - The product tags to be created.
|
||||
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
|
||||
* @returns {Promise<ProductTagDTO[]>} The list of product tags.
|
||||
* @return {Promise<ProductTagDTO[]>} The list of created product tags.
|
||||
*
|
||||
* @example
|
||||
* const tags = await productModuleService.createTags([
|
||||
* const productTags = await productModuleService.createTags([
|
||||
* {
|
||||
* value: "Clothes",
|
||||
* },
|
||||
* {
|
||||
* value: "Accessories",
|
||||
* value: "digital",
|
||||
* },
|
||||
* ])
|
||||
*/
|
||||
@@ -534,29 +532,111 @@ export interface IProductModuleService extends IModuleService {
|
||||
): Promise<ProductTagDTO[]>
|
||||
|
||||
/**
|
||||
* This method is used to update existing product tags.
|
||||
* This method is used to create a product tag.
|
||||
*
|
||||
* @param {UpdateProductTagDTO[]} data - The product tags to be updated, each having the attributes that should be updated in a product tag.
|
||||
* @param {CreateProductTagDTO} data - The product tag to be created.
|
||||
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
|
||||
* @returns {Promise<ProductTagDTO[]>} The list of updated product tags.
|
||||
* @returns {Promise<ProductTagDTO>} The created product tag.
|
||||
*
|
||||
* @example
|
||||
* const productTags = await productModule.updateTags([
|
||||
* const productTag = await productModuleService.createTags({
|
||||
* value: "digital",
|
||||
* })
|
||||
*
|
||||
*/
|
||||
createTags(
|
||||
data: CreateProductTagDTO,
|
||||
sharedContext?: Context
|
||||
): Promise<ProductTagDTO>
|
||||
|
||||
/**
|
||||
* This method updates existing tags, or creates new ones if they don't exist.
|
||||
*
|
||||
* @param {UpsertProductTagDTO[]} data - The attributes to update or create for each tag.
|
||||
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
|
||||
* @returns {Promise<ProductTagDTO[]>} The updated and created tags.
|
||||
*
|
||||
* @example
|
||||
* const productTags = await productModuleService.upsertTags([
|
||||
* {
|
||||
* id,
|
||||
* value
|
||||
* }
|
||||
* id: "ptag_123",
|
||||
* metadata: {
|
||||
* test: true,
|
||||
* },
|
||||
* },
|
||||
* {
|
||||
* value: "Digital",
|
||||
* },
|
||||
* ])
|
||||
*/
|
||||
upsertTags(
|
||||
data: UpsertProductTagDTO[],
|
||||
sharedContext?: Context
|
||||
): Promise<ProductTagDTO[]>
|
||||
|
||||
/**
|
||||
* This method updates an existing tag, or creates a new one if it doesn't exist.
|
||||
*
|
||||
* @ignore
|
||||
* @param {UpsertProductTagDTO} data - The attributes to update or create for the tag.
|
||||
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
|
||||
* @returns {Promise<ProductTagDTO>} The updated or created tag.
|
||||
*
|
||||
* @privateRemarks
|
||||
* This method needs an update as it doesn't allow passing an ID of the tag to update
|
||||
* So, for now, we've added the `@\ignore` tag to not show it in the generated docs.
|
||||
* Once fixed, the `@\ignore` tag (and this comment) can be removed safely.
|
||||
* @example
|
||||
* const productTag = await productModuleService.upsertTags({
|
||||
* id: "ptag_123",
|
||||
* metadata: {
|
||||
* test: true,
|
||||
* },
|
||||
* })
|
||||
*/
|
||||
upsertTags(
|
||||
data: UpsertProductTagDTO,
|
||||
sharedContext?: Context
|
||||
): Promise<ProductTagDTO>
|
||||
|
||||
/**
|
||||
* This method is used to update a tag.
|
||||
*
|
||||
* @param {string} id - The ID of the tag to be updated.
|
||||
* @param {UpdateProductTagDTO} data - The attributes of the tag to be updated
|
||||
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
|
||||
* @returns {Promise<ProductTagDTO>} The updated tag.
|
||||
*
|
||||
* @example
|
||||
* const productTag = await productModuleService.updateTags(
|
||||
* "ptag_123",
|
||||
* {
|
||||
* value: "Digital",
|
||||
* }
|
||||
* )
|
||||
*/
|
||||
updateTags(
|
||||
data: UpdateProductTagDTO[],
|
||||
id: string,
|
||||
data: UpdateProductTagDTO,
|
||||
sharedContext?: Context
|
||||
): Promise<ProductTagDTO>
|
||||
|
||||
/**
|
||||
* This method is used to update a list of tags matching the specified filters.
|
||||
*
|
||||
* @param {FilterableProductTagProps} selector - The filters specifying which tags to update.
|
||||
* @param {UpdateProductTagDTO} data - The attributes to be updated on the selected tags
|
||||
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
|
||||
* @returns {Promise<ProductTagDTO[]>} The updated tags.
|
||||
*
|
||||
* @example
|
||||
* const productTags = await productModuleService.updateTags(
|
||||
* {
|
||||
* id: ["ptag_123", "ptag_321"],
|
||||
* },
|
||||
* {
|
||||
* value: "Digital",
|
||||
* }
|
||||
* )
|
||||
*/
|
||||
updateTags(
|
||||
selector: FilterableProductTagProps,
|
||||
data: UpdateProductTagDTO,
|
||||
sharedContext?: Context
|
||||
): Promise<ProductTagDTO[]>
|
||||
|
||||
@@ -575,6 +655,58 @@ export interface IProductModuleService extends IModuleService {
|
||||
*/
|
||||
deleteTags(productTagIds: string[], sharedContext?: Context): Promise<void>
|
||||
|
||||
/**
|
||||
* This method is used to delete tags. Unlike the {@link delete} method, this method won't completely remove the tag. It can still be accessed or retrieved using methods like {@link retrieve} if you pass the `withDeleted` property to the `config` object parameter.
|
||||
*
|
||||
* The soft-deleted tags can be restored using the {@link restore} method.
|
||||
*
|
||||
* @param {string[]} tagIds - The IDs of the tags to soft-delete.
|
||||
* @param {SoftDeleteReturn<TReturnableLinkableKeys>} config -
|
||||
* Configurations determining which relations to soft delete along with the each of the tags. You can pass to its `returnLinkableKeys`
|
||||
* property any of the tag'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 tag entity's relations, and its value is an array of strings, each being the ID of a record associated with the tag through this relation.
|
||||
*
|
||||
* If there are no related records, the promise resolved to `void`.
|
||||
*
|
||||
* @example
|
||||
* await productModuleService.softDeleteTags([
|
||||
* "ptag_123",
|
||||
* "ptag_321",
|
||||
* ])
|
||||
*/
|
||||
softDeleteTags<TReturnableLinkableKeys extends string = string>(
|
||||
tagIds: string[],
|
||||
config?: SoftDeleteReturn<TReturnableLinkableKeys>,
|
||||
sharedContext?: Context
|
||||
): Promise<Record<string, string[]> | void>
|
||||
|
||||
/**
|
||||
* This method is used to restore tags which were deleted using the {@link softDelete} method.
|
||||
*
|
||||
* @param {string[]} tagIds - The IDs of the tags to restore.
|
||||
* @param {RestoreReturn<TReturnableLinkableKeys>} config -
|
||||
* Configurations determining which relations to restore along with each of the tags. You can pass to its `returnLinkableKeys`
|
||||
* property any of the tag'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 tag entity's relations, and its value is an array of strings, each being the ID of the record associated with the tag through this relation.
|
||||
*
|
||||
* If there are no related records that were restored, the promise resolved to `void`.
|
||||
*
|
||||
* @example
|
||||
* await productModuleService.restoreTags([
|
||||
* "ptag_123",
|
||||
* "ptag_321",
|
||||
* ])
|
||||
*/
|
||||
restoreTags<TReturnableLinkableKeys extends string = string>(
|
||||
tagIds: string[],
|
||||
config?: RestoreReturn<TReturnableLinkableKeys>,
|
||||
sharedContext?: Context
|
||||
): Promise<Record<string, string[]> | void>
|
||||
|
||||
/**
|
||||
* This method is used to retrieve a product type by its ID.
|
||||
*
|
||||
|
||||
Reference in new issue
Block a user