feat: Add v2 product types endpoints (#6880)
Also adjusts the product type module APIs to follow the conventions
This commit is contained in:
1 parent
1bcb13f892
commit
1a48fe0282
21 files changed
+833
-41
No files matched your search
+7
-13
@@ -211,12 +211,9 @@ moduleIntegrationTestRunner({
|
||||
const typeId = "type-1"
|
||||
|
||||
it("should update the value of the type successfully", async () => {
|
||||
await service.updateTypes([
|
||||
{
|
||||
id: typeId,
|
||||
value: "UK",
|
||||
},
|
||||
])
|
||||
await service.updateTypes(typeId, {
|
||||
value: "UK",
|
||||
})
|
||||
|
||||
const productType = await service.retrieveType(typeId)
|
||||
|
||||
@@ -227,18 +224,15 @@ moduleIntegrationTestRunner({
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.updateTypes([
|
||||
{
|
||||
id: "does-not-exist",
|
||||
value: "UK",
|
||||
},
|
||||
])
|
||||
await service.updateTypes("does-not-exist", {
|
||||
value: "UK",
|
||||
})
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
'ProductType with id "does-not-exist" not found'
|
||||
"ProductType with id: does-not-exist was not found"
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -51,6 +51,7 @@ import {
|
||||
UpdateProductInput,
|
||||
UpdateProductOptionInput,
|
||||
UpdateProductVariantInput,
|
||||
UpdateTypeInput,
|
||||
} from "../types"
|
||||
import { entityNameToLinkableKeysMap, joinerConfig } from "./../joiner-config"
|
||||
|
||||
@@ -425,34 +426,118 @@ export default class ProductModuleService<
|
||||
return await this.baseRepository_.serialize(productTags)
|
||||
}
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
async createTypes(
|
||||
createTypes(
|
||||
data: ProductTypes.CreateProductTypeDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<ProductTypes.ProductTypeDTO[]> {
|
||||
const productTypes = await this.productTypeService_.create(
|
||||
data,
|
||||
sharedContext
|
||||
)
|
||||
sharedContext?: Context
|
||||
): Promise<ProductTypes.ProductTypeDTO[]>
|
||||
createTypes(
|
||||
data: ProductTypes.CreateProductTypeDTO,
|
||||
sharedContext?: Context
|
||||
): Promise<ProductTypes.ProductTypeDTO>
|
||||
|
||||
return await this.baseRepository_.serialize(productTypes, {
|
||||
populate: true,
|
||||
})
|
||||
@InjectManager("baseRepository_")
|
||||
async createTypes(
|
||||
data:
|
||||
| ProductTypes.CreateProductTypeDTO[]
|
||||
| ProductTypes.CreateProductTypeDTO,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<ProductTypes.ProductTypeDTO[] | ProductTypes.ProductTypeDTO> {
|
||||
const input = Array.isArray(data) ? data : [data]
|
||||
|
||||
const types = await this.productTypeService_.create(input, sharedContext)
|
||||
|
||||
const createdTypes = await this.baseRepository_.serialize<
|
||||
ProductTypes.ProductTypeDTO[]
|
||||
>(types)
|
||||
|
||||
return Array.isArray(data) ? createdTypes : createdTypes[0]
|
||||
}
|
||||
|
||||
async upsertTypes(
|
||||
data: ProductTypes.UpsertProductTypeDTO[],
|
||||
sharedContext?: Context
|
||||
): Promise<ProductTypes.ProductTypeDTO[]>
|
||||
async upsertTypes(
|
||||
data: ProductTypes.UpsertProductTypeDTO,
|
||||
sharedContext?: Context
|
||||
): Promise<ProductTypes.ProductTypeDTO>
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
async updateTypes(
|
||||
data: ProductTypes.UpdateProductTypeDTO[],
|
||||
async upsertTypes(
|
||||
data:
|
||||
| ProductTypes.UpsertProductTypeDTO[]
|
||||
| ProductTypes.UpsertProductTypeDTO,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<ProductTypes.ProductTypeDTO[]> {
|
||||
const productTypes = await this.productTypeService_.update(
|
||||
data,
|
||||
): Promise<ProductTypes.ProductTypeDTO[] | ProductTypes.ProductTypeDTO> {
|
||||
const input = Array.isArray(data) ? data : [data]
|
||||
const forUpdate = input.filter((type): type is UpdateTypeInput => !!type.id)
|
||||
const forCreate = input.filter(
|
||||
(type): type is ProductTypes.CreateProductTypeDTO => !type.id
|
||||
)
|
||||
|
||||
let created: ProductType[] = []
|
||||
let updated: ProductType[] = []
|
||||
|
||||
if (forCreate.length) {
|
||||
created = await this.productTypeService_.create(forCreate, sharedContext)
|
||||
}
|
||||
if (forUpdate.length) {
|
||||
updated = await this.productTypeService_.update(forUpdate, sharedContext)
|
||||
}
|
||||
|
||||
const result = [...created, ...updated]
|
||||
const allTypes = await this.baseRepository_.serialize<
|
||||
ProductTypes.ProductTypeDTO[] | ProductTypes.ProductTypeDTO
|
||||
>(result)
|
||||
|
||||
return Array.isArray(data) ? allTypes : allTypes[0]
|
||||
}
|
||||
|
||||
updateTypes(
|
||||
id: string,
|
||||
data: ProductTypes.UpdateProductTypeDTO,
|
||||
sharedContext?: Context
|
||||
): Promise<ProductTypes.ProductTypeDTO>
|
||||
updateTypes(
|
||||
selector: ProductTypes.FilterableProductTypeProps,
|
||||
data: ProductTypes.UpdateProductTypeDTO,
|
||||
sharedContext?: Context
|
||||
): Promise<ProductTypes.ProductTypeDTO[]>
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
async updateTypes(
|
||||
idOrSelector: string | ProductTypes.FilterableProductTypeProps,
|
||||
data: ProductTypes.UpdateProductTypeDTO,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<ProductTypes.ProductTypeDTO[] | ProductTypes.ProductTypeDTO> {
|
||||
let normalizedInput: UpdateTypeInput[] = []
|
||||
if (isString(idOrSelector)) {
|
||||
// Check if the type exists in the first place
|
||||
await this.productTypeService_.retrieve(idOrSelector, {}, sharedContext)
|
||||
normalizedInput = [{ id: idOrSelector, ...data }]
|
||||
} else {
|
||||
const types = await this.productTypeService_.list(
|
||||
idOrSelector,
|
||||
{},
|
||||
sharedContext
|
||||
)
|
||||
|
||||
normalizedInput = types.map((type) => ({
|
||||
id: type.id,
|
||||
...data,
|
||||
}))
|
||||
}
|
||||
|
||||
const types = await this.productTypeService_.update(
|
||||
normalizedInput,
|
||||
sharedContext
|
||||
)
|
||||
|
||||
return await this.baseRepository_.serialize(productTypes, {
|
||||
populate: true,
|
||||
})
|
||||
const updatedTypes = await this.baseRepository_.serialize<
|
||||
ProductTypes.ProductTypeDTO[]
|
||||
>(types)
|
||||
|
||||
return isString(idOrSelector) ? updatedTypes[0] : updatedTypes
|
||||
}
|
||||
|
||||
createOptions(
|
||||
|
||||
@@ -53,6 +53,10 @@ export type UpdateCollectionInput = ProductTypes.UpdateProductCollectionDTO & {
|
||||
id: string
|
||||
}
|
||||
|
||||
export type UpdateTypeInput = ProductTypes.UpdateProductTypeDTO & {
|
||||
id: string
|
||||
}
|
||||
|
||||
export type UpdateProductVariantInput = ProductTypes.UpdateProductVariantDTO & {
|
||||
id: string
|
||||
product_id?: string | null
|
||||
|
||||
Reference in new issue
Block a user