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:
Stevche Radevski
2024-06-04 10:56:22 +02:00
committed by GitHub
parent ce40fe88f5
commit 0929c4f457
24 changed files with 805 additions and 399 deletions
@@ -251,12 +251,9 @@ moduleIntegrationTestRunner({
const tagId = "tag-1"
it("should update the value of the tag successfully", async () => {
await service.updateTags([
{
id: tagId,
value: "UK",
},
])
await service.updateTags(tagId, {
value: "UK",
})
const productTag = await service.retrieveTag(tagId)
@@ -267,18 +264,15 @@ moduleIntegrationTestRunner({
let error
try {
await service.updateTags([
{
id: "does-not-exist",
value: "UK",
},
])
await service.updateTags("does-not-exist", {
value: "UK",
})
} catch (e) {
error = e
}
expect(error.message).toEqual(
'ProductTag with id "does-not-exist" not found'
"ProductTag with id: does-not-exist was not found"
)
})
})
@@ -1,8 +1,7 @@
import { Modules } from "@medusajs/modules-sdk"
import { IProductModuleService } from "@medusajs/types"
import { IProductModuleService, ModulesSdkTypes } from "@medusajs/types"
import { ProductStatus } from "@medusajs/utils"
import { Product } from "@models"
import { ProductTagService } from "@services"
import { Product, ProductTag } from "@models"
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
import { createProductAndTags } from "../../../__fixtures__/product"
@@ -16,7 +15,7 @@ moduleIntegrationTestRunner({
}: SuiteOptions<IProductModuleService>) => {
describe("ProductTag Service", () => {
let data!: Product[]
let service: ProductTagService
let service: ModulesSdkTypes.InternalModuleService<ProductTag>
beforeEach(() => {
service = medusaApp.modules["productService"].productTagService_
@@ -47,6 +47,7 @@ import {
UpdateProductInput,
UpdateProductOptionInput,
UpdateProductVariantInput,
UpdateTagInput,
UpdateTypeInput,
} from "../types"
import { entityNameToLinkableKeysMap, joinerConfig } from "./../joiner-config"
@@ -384,30 +385,114 @@ export default class ProductModuleService<
)
}
@InjectTransactionManager("baseRepository_")
async createTags(
createTags(
data: ProductTypes.CreateProductTagDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<ProductTypes.ProductTagDTO[]> {
const productTags = await this.productTagService_.create(
data,
sharedContext
)
sharedContext?: Context
): Promise<ProductTypes.ProductTagDTO[]>
createTags(
data: ProductTypes.CreateProductTagDTO,
sharedContext?: Context
): Promise<ProductTypes.ProductTagDTO>
return await this.baseRepository_.serialize(productTags)
@InjectManager("baseRepository_")
async createTags(
data: ProductTypes.CreateProductTagDTO[] | ProductTypes.CreateProductTagDTO,
@MedusaContext() sharedContext: Context = {}
): Promise<ProductTypes.ProductTagDTO[] | ProductTypes.ProductTagDTO> {
const input = Array.isArray(data) ? data : [data]
const tags = await this.productTagService_.create(input, sharedContext)
const createdTags = await this.baseRepository_.serialize<
ProductTypes.ProductTagDTO[]
>(tags)
return Array.isArray(data) ? createdTags : createdTags[0]
}
async upsertTags(
data: ProductTypes.UpsertProductTagDTO[],
sharedContext?: Context
): Promise<ProductTypes.ProductTagDTO[]>
async upsertTags(
data: ProductTypes.UpsertProductTagDTO,
sharedContext?: Context
): Promise<ProductTypes.ProductTagDTO>
@InjectTransactionManager("baseRepository_")
async updateTags(
data: ProductTypes.UpdateProductTagDTO[],
async upsertTags(
data: ProductTypes.UpsertProductTagDTO[] | ProductTypes.UpsertProductTagDTO,
@MedusaContext() sharedContext: Context = {}
): Promise<ProductTypes.ProductTagDTO[]> {
const productTags = await this.productTagService_.update(
data,
): Promise<ProductTypes.ProductTagDTO[] | ProductTypes.ProductTagDTO> {
const input = Array.isArray(data) ? data : [data]
const forUpdate = input.filter((tag): tag is UpdateTagInput => !!tag.id)
const forCreate = input.filter(
(tag): tag is ProductTypes.CreateProductTagDTO => !tag.id
)
let created: ProductTag[] = []
let updated: ProductTag[] = []
if (forCreate.length) {
created = await this.productTagService_.create(forCreate, sharedContext)
}
if (forUpdate.length) {
updated = await this.productTagService_.update(forUpdate, sharedContext)
}
const result = [...created, ...updated]
const allTags = await this.baseRepository_.serialize<
ProductTypes.ProductTagDTO[] | ProductTypes.ProductTagDTO
>(result)
return Array.isArray(data) ? allTags : allTags[0]
}
updateTags(
id: string,
data: ProductTypes.UpdateProductTagDTO,
sharedContext?: Context
): Promise<ProductTypes.ProductTagDTO>
updateTags(
selector: ProductTypes.FilterableProductTagProps,
data: ProductTypes.UpdateProductTagDTO,
sharedContext?: Context
): Promise<ProductTypes.ProductTagDTO[]>
@InjectManager("baseRepository_")
async updateTags(
idOrSelector: string | ProductTypes.FilterableProductTagProps,
data: ProductTypes.UpdateProductTagDTO,
@MedusaContext() sharedContext: Context = {}
): Promise<ProductTypes.ProductTagDTO[] | ProductTypes.ProductTagDTO> {
let normalizedInput: UpdateTagInput[] = []
if (isString(idOrSelector)) {
// Check if the tag exists in the first place
await this.productTagService_.retrieve(idOrSelector, {}, sharedContext)
normalizedInput = [{ id: idOrSelector, ...data }]
} else {
const tags = await this.productTagService_.list(
idOrSelector,
{},
sharedContext
)
normalizedInput = tags.map((tag) => ({
id: tag.id,
...data,
}))
}
const tags = await this.productTagService_.update(
normalizedInput,
sharedContext
)
return await this.baseRepository_.serialize(productTags)
const updatedTags = await this.baseRepository_.serialize<
ProductTypes.ProductTagDTO[]
>(tags)
return isString(idOrSelector) ? updatedTags[0] : updatedTags
}
createTypes(
@@ -57,6 +57,10 @@ export type UpdateTypeInput = ProductTypes.UpdateProductTypeDTO & {
id: string
}
export type UpdateTagInput = ProductTypes.UpdateProductTagDTO & {
id: string
}
export type UpdateProductVariantInput = ProductTypes.UpdateProductVariantDTO & {
id: string
product_id?: string | null