feat: Update the signatures for variants and options to follow convention (#6748)

This commit is contained in:
Stevche Radevski authored and GitHub committed 2024-03-20 11:31:54 +00:00
1 parent a859b7895e
commit 05e857d256
9 files changed
+566 -109

No files matched your search

+7
View File
@@ -0,0 +1,7 @@
---
"@medusajs/core-flows": minor
"@medusajs/product": minor
"@medusajs/types": minor
---
Updated the signature of the product module variant and options endpoints to follow our conventions
@@ -27,7 +27,7 @@ export async function updateProductVariants({
} }
if (updateVariantsData.length) { if (updateVariantsData.length) {
await productModuleService.updateVariants(updateVariantsData) await productModuleService.upsertVariants(updateVariantsData)
} }
return productsVariants return productsVariants
@@ -25,9 +25,10 @@ export const updateProductOptionsStep = createStep(
relations, relations,
}) })
// TODO: We need to update the module's signature const productOptions = await service.updateOptions(
// const productOptions = await service.updateOptions(data.selector, data.update) data.selector,
const productOptions = [] data.update
)
return new StepResponse(productOptions, prevData) return new StepResponse(productOptions, prevData)
}, },
async (prevData, { container }) => { async (prevData, { container }) => {
@@ -39,11 +40,6 @@ export const updateProductOptionsStep = createStep(
ModuleRegistrationName.PRODUCT ModuleRegistrationName.PRODUCT
) )
// TODO: We need to update the module's signature await service.upsertOptions(prevData)
// await service.upsertOptions(
// prevData.map((r) => ({
// ...r,
// }))
// )
} }
) )
@@ -25,9 +25,10 @@ export const updateProductVariantsStep = createStep(
relations, relations,
}) })
// TODO: We need to update the module's signature const productVariants = await service.updateVariants(
// const productVariants = await service.updateVariants(data.selector, data.update) data.selector,
const productVariants = [] data.update
)
return new StepResponse(productVariants, prevData) return new StepResponse(productVariants, prevData)
}, },
async (prevData, { container }) => { async (prevData, { container }) => {
@@ -39,11 +40,6 @@ export const updateProductVariantsStep = createStep(
ModuleRegistrationName.PRODUCT ModuleRegistrationName.PRODUCT
) )
// TODO: We need to update the module's signature await service.upsertVariants(prevData)
// await service.upsertVariants(
// prevData.map((r) => ({
// ...r,
// }))
// )
} }
) )
@@ -242,7 +242,7 @@ moduleIntegrationTestRunner({
const optionId = "option-1" const optionId = "option-1"
it("should update the title of the option successfully", async () => { it("should update the title of the option successfully", async () => {
await service.updateOptions([ await service.upsertOptions([
{ {
id: optionId, id: optionId,
title: "new test", title: "new test",
@@ -258,7 +258,7 @@ moduleIntegrationTestRunner({
let error let error
try { try {
await service.updateOptions([ await service.upsertOptions([
{ {
id: "does-not-exist", id: "does-not-exist",
}, },
@@ -32,7 +32,6 @@ import {
arrayDifference, arrayDifference,
InjectManager, InjectManager,
InjectTransactionManager, InjectTransactionManager,
isPresent,
isString, isString,
kebabCase, kebabCase,
MedusaContext, MedusaContext,
@@ -50,6 +49,7 @@ import {
ProductCollectionEventData, ProductCollectionEventData,
ProductCollectionEvents, ProductCollectionEvents,
UpdateProductVariantInput, UpdateProductVariantInput,
UpdateProductOptionInput,
} from "../types" } from "../types"
import { entityNameToLinkableKeysMap, joinerConfig } from "./../joiner-config" import { entityNameToLinkableKeysMap, joinerConfig } from "./../joiner-config"
import { ProductStatus } from "@medusajs/utils" import { ProductStatus } from "@medusajs/utils"
@@ -197,58 +197,158 @@ export default class ProductModuleService<
return joinerConfig return joinerConfig
} }
// TODO: Add options validation, among other things
createVariants(
data: ProductTypes.CreateProductVariantDTO[],
sharedContext?: Context
): Promise<ProductTypes.ProductVariantDTO[]>
createVariants(
data: ProductTypes.CreateProductVariantDTO,
sharedContext?: Context
): Promise<ProductTypes.ProductVariantDTO>
@InjectManager("baseRepository_")
async createVariants( async createVariants(
data:
| ProductTypes.CreateProductVariantDTO[]
| ProductTypes.CreateProductVariantDTO,
@MedusaContext() sharedContext: Context = {}
): Promise<
ProductTypes.ProductVariantDTO[] | ProductTypes.ProductVariantDTO
> {
const input = Array.isArray(data) ? data : [data]
const variants = await this.createVariants_(input, sharedContext)
const createdVariants = await this.baseRepository_.serialize<
ProductTypes.ProductVariantDTO[]
>(variants, { populate: true })
return Array.isArray(data) ? createdVariants : createdVariants[0]
}
@InjectTransactionManager("baseRepository_")
protected async createVariants_(
data: ProductTypes.CreateProductVariantDTO[], data: ProductTypes.CreateProductVariantDTO[],
@MedusaContext() sharedContext: Context = {} @MedusaContext() sharedContext: Context = {}
): Promise<ProductTypes.ProductVariantDTO[]> { ): Promise<ProductVariant[]> {
const productVariantsWithOptions = await promiseAll( if (data.some((v) => !v.product_id)) {
data.map(async (variant) => { throw new MedusaError(
if (!isPresent(variant.options)) { MedusaError.Types.INVALID_DATA,
return variant "Tried to create variants without specifying a product_id"
)
} }
const productOptions = await this.productOptionService_.list( const productOptions = await this.productOptionService_.list(
{ product_id: variant.product_id }, {
product_id: [...new Set<string>(data.map((v) => v.product_id!))],
},
{ {
take: null, take: null,
}, },
sharedContext sharedContext
) )
return ProductModuleService.assignOptionsToVariants( const productVariantsWithOptions =
[variant], ProductModuleService.assignOptionsToVariants(data, productOptions)
productOptions
)[0]
})
)
const productVariants = await this.productVariantService_.create( return await this.productVariantService_.create(
productVariantsWithOptions, productVariantsWithOptions,
sharedContext sharedContext
) )
return await this.baseRepository_.serialize<
ProductTypes.ProductVariantDTO[]
>(productVariants)
} }
async upsertVariants(
data: ProductTypes.UpsertProductVariantDTO[],
sharedContext?: Context
): Promise<ProductTypes.ProductVariantDTO[]>
async upsertVariants(
data: ProductTypes.UpsertProductVariantDTO,
sharedContext?: Context
): Promise<ProductTypes.ProductVariantDTO>
@InjectTransactionManager("baseRepository_")
async upsertVariants(
data:
| ProductTypes.UpsertProductVariantDTO[]
| ProductTypes.UpsertProductVariantDTO,
@MedusaContext() sharedContext: Context = {}
): Promise<
ProductTypes.ProductVariantDTO[] | ProductTypes.ProductVariantDTO
> {
const input = Array.isArray(data) ? data : [data]
const forUpdate = input.filter(
(variant): variant is UpdateProductVariantInput => !!variant.id
)
const forCreate = input.filter(
(variant): variant is ProductTypes.CreateProductVariantDTO => !variant.id
)
let created: ProductVariant[] = []
let updated: ProductVariant[] = []
if (forCreate.length) {
created = await this.createVariants_(forCreate, sharedContext)
}
if (forUpdate.length) {
updated = await this.updateVariants_(forUpdate, sharedContext)
}
const result = [...created, ...updated]
const allVariants = await this.baseRepository_.serialize<
ProductTypes.ProductVariantDTO[] | ProductTypes.ProductVariantDTO
>(result)
return Array.isArray(data) ? allVariants : allVariants[0]
}
updateVariants(
id: string,
data: ProductTypes.UpdateProductVariantDTO,
sharedContext?: Context
): Promise<ProductTypes.ProductVariantDTO>
updateVariants(
selector: ProductTypes.FilterableProductVariantProps,
data: ProductTypes.UpdateProductVariantDTO,
sharedContext?: Context
): Promise<ProductTypes.ProductVariantDTO[]>
@InjectManager("baseRepository_") @InjectManager("baseRepository_")
async updateVariants( async updateVariants(
data: ProductTypes.UpdateProductVariantDTO[], idOrSelector: string | ProductTypes.FilterableProductVariantProps,
data: ProductTypes.UpdateProductVariantDTO,
@MedusaContext() sharedContext: Context = {} @MedusaContext() sharedContext: Context = {}
): Promise<ProductTypes.ProductVariantDTO[]> { ): Promise<
const productVariants = await this.updateVariants_(data, sharedContext) ProductTypes.ProductVariantDTO[] | ProductTypes.ProductVariantDTO
> {
let normalizedInput: UpdateProductVariantInput[] = []
if (isString(idOrSelector)) {
normalizedInput = [{ id: idOrSelector, ...data }]
} else {
const variants = await this.productVariantService_.list(
idOrSelector,
{},
sharedContext
)
normalizedInput = variants.map((variant) => ({
id: variant.id,
...data,
}))
}
const variants = await this.updateVariants_(normalizedInput, sharedContext)
const updatedVariants = await this.baseRepository_.serialize< const updatedVariants = await this.baseRepository_.serialize<
ProductTypes.ProductVariantDTO[] ProductTypes.ProductVariantDTO[]
>(productVariants) >(variants)
return updatedVariants return isString(idOrSelector) ? updatedVariants[0] : updatedVariants
} }
@InjectTransactionManager("baseRepository_") @InjectTransactionManager("baseRepository_")
protected async updateVariants_( protected async updateVariants_(
data: ProductTypes.UpdateProductVariantDTO[], data: UpdateProductVariantInput[],
@MedusaContext() sharedContext: Context = {} @MedusaContext() sharedContext: Context = {}
): Promise<TProductVariant[]> { ): Promise<TProductVariant[]> {
// Validation step // Validation step
@@ -280,7 +380,7 @@ export default class ProductModuleService<
const productOptions = await this.productOptionService_.list( const productOptions = await this.productOptionService_.list(
{ {
product_id: Array.from( product_id: Array.from(
new Set(variantsWithProductId.map((v) => v.product_id)) new Set(variantsWithProductId.map((v) => v.product_id!))
), ),
}, },
{ take: null }, { take: null },
@@ -294,8 +394,8 @@ export default class ProductModuleService<
) )
} }
@InjectManager("baseRepository_") @InjectTransactionManager("baseRepository_")
async diffVariants_( protected async diffVariants_(
data: UpdateProductVariantInput[], data: UpdateProductVariantInput[],
productOptions: ProductOption[], productOptions: ProductOption[],
@MedusaContext() sharedContext: Context = {} @MedusaContext() sharedContext: Context = {}
@@ -427,11 +527,45 @@ export default class ProductModuleService<
}) })
} }
@InjectTransactionManager("baseRepository_") createOptions(
data: ProductTypes.CreateProductOptionDTO[],
sharedContext?: Context
): Promise<ProductTypes.ProductOptionDTO[]>
createOptions(
data: ProductTypes.CreateProductOptionDTO,
sharedContext?: Context
): Promise<ProductTypes.ProductOptionDTO>
@InjectManager("baseRepository_")
async createOptions( async createOptions(
data:
| ProductTypes.CreateProductOptionDTO[]
| ProductTypes.CreateProductOptionDTO,
@MedusaContext() sharedContext: Context = {}
): Promise<ProductTypes.ProductOptionDTO[] | ProductTypes.ProductOptionDTO> {
const input = Array.isArray(data) ? data : [data]
const options = await this.createOptions_(input, sharedContext)
const createdOptions = await this.baseRepository_.serialize<
ProductTypes.ProductOptionDTO[]
>(options, { populate: true })
return Array.isArray(data) ? createdOptions : createdOptions[0]
}
@InjectTransactionManager("baseRepository_")
protected async createOptions_(
data: ProductTypes.CreateProductOptionDTO[], data: ProductTypes.CreateProductOptionDTO[],
@MedusaContext() sharedContext: Context = {} @MedusaContext() sharedContext: Context = {}
) { ): Promise<ProductOption[]> {
if (data.some((v) => !v.product_id)) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
"Tried to create options without specifying a product_id"
)
}
const normalizedInput = data.map((opt) => { const normalizedInput = data.map((opt) => {
return { return {
...opt, ...opt,
@@ -441,21 +575,102 @@ export default class ProductModuleService<
} }
}) })
const productOptions = await this.productOptionService_.create( return await this.productOptionService_.create(
normalizedInput, normalizedInput,
sharedContext sharedContext
) )
}
return await this.baseRepository_.serialize< async upsertOptions(
data: ProductTypes.UpsertProductOptionDTO[],
sharedContext?: Context
): Promise<ProductTypes.ProductOptionDTO[]>
async upsertOptions(
data: ProductTypes.UpsertProductOptionDTO,
sharedContext?: Context
): Promise<ProductTypes.ProductOptionDTO>
@InjectTransactionManager("baseRepository_")
async upsertOptions(
data:
| ProductTypes.UpsertProductOptionDTO[]
| ProductTypes.UpsertProductOptionDTO,
@MedusaContext() sharedContext: Context = {}
): Promise<ProductTypes.ProductOptionDTO[] | ProductTypes.ProductOptionDTO> {
const input = Array.isArray(data) ? data : [data]
const forUpdate = input.filter(
(option): option is UpdateProductOptionInput => !!option.id
)
const forCreate = input.filter(
(option): option is ProductTypes.CreateProductOptionDTO => !option.id
)
let created: ProductOption[] = []
let updated: ProductOption[] = []
if (forCreate.length) {
created = await this.createOptions_(forCreate, sharedContext)
}
if (forUpdate.length) {
updated = await this.updateOptions_(forUpdate, sharedContext)
}
const result = [...created, ...updated]
const allOptions = await this.baseRepository_.serialize<
ProductTypes.ProductOptionDTO[] | ProductTypes.ProductOptionDTO
>(result)
return Array.isArray(data) ? allOptions : allOptions[0]
}
updateOptions(
id: string,
data: ProductTypes.UpdateProductOptionDTO,
sharedContext?: Context
): Promise<ProductTypes.ProductOptionDTO>
updateOptions(
selector: ProductTypes.FilterableProductOptionProps,
data: ProductTypes.UpdateProductOptionDTO,
sharedContext?: Context
): Promise<ProductTypes.ProductOptionDTO[]>
@InjectManager("baseRepository_")
async updateOptions(
idOrSelector: string | ProductTypes.FilterableProductOptionProps,
data: ProductTypes.UpdateProductOptionDTO,
@MedusaContext() sharedContext: Context = {}
): Promise<ProductTypes.ProductOptionDTO[] | ProductTypes.ProductOptionDTO> {
let normalizedInput: UpdateProductOptionInput[] = []
if (isString(idOrSelector)) {
normalizedInput = [{ id: idOrSelector, ...data }]
} else {
const options = await this.productOptionService_.list(
idOrSelector,
{},
sharedContext
)
normalizedInput = options.map((option) => ({
id: option.id,
...data,
}))
}
const options = await this.updateOptions_(normalizedInput, sharedContext)
const updatedOptions = await this.baseRepository_.serialize<
ProductTypes.ProductOptionDTO[] ProductTypes.ProductOptionDTO[]
>(productOptions) >(options)
return isString(idOrSelector) ? updatedOptions[0] : updatedOptions
} }
@InjectTransactionManager("baseRepository_") @InjectTransactionManager("baseRepository_")
async updateOptions( protected async updateOptions_(
data: ProductTypes.UpdateProductOptionDTO[], data: UpdateProductOptionInput[],
@MedusaContext() sharedContext: Context = {} @MedusaContext() sharedContext: Context = {}
) { ): Promise<ProductOption[]> {
// Validation step
const normalizedInput = data.map((opt) => { const normalizedInput = data.map((opt) => {
return { return {
...opt, ...opt,
@@ -466,7 +681,7 @@ export default class ProductModuleService<
}), }),
} }
: {}), : {}),
} as ProductTypes.UpdateProductOptionDTO } as UpdateProductOptionInput
}) })
if (normalizedInput.some((option) => !option.id)) { if (normalizedInput.some((option) => !option.id)) {
@@ -481,15 +696,13 @@ export default class ProductModuleService<
sharedContext sharedContext
) )
return await this.baseRepository_.serialize< return productOptions
ProductTypes.ProductOptionDTO[]
>(productOptions)
} }
// TODO: Do validation // TODO: Do validation
@InjectTransactionManager("baseRepository_") @InjectTransactionManager("baseRepository_")
async diffOptions_( protected async diffOptions_(
data: ProductTypes.UpdateProductOptionDTO[], data: UpdateProductOptionInput[],
@MedusaContext() sharedContext: Context = {} @MedusaContext() sharedContext: Context = {}
) { ) {
const toCreate = data.filter((o) => !o.id) const toCreate = data.filter((o) => !o.id)
@@ -740,7 +953,7 @@ export default class ProductModuleService<
} }
@InjectTransactionManager("baseRepository_") @InjectTransactionManager("baseRepository_")
async updateCollections_( protected async updateCollections_(
data: UpdateCollectionInput[], data: UpdateCollectionInput[],
@MedusaContext() sharedContext: Context = {} @MedusaContext() sharedContext: Context = {}
): Promise<TProductCollection[]> { ): Promise<TProductCollection[]> {
+6 -1
View File
@@ -54,5 +54,10 @@ export type UpdateCollectionInput = ProductTypes.UpdateProductCollectionDTO & {
} }
export type UpdateProductVariantInput = ProductTypes.UpdateProductVariantDTO & { export type UpdateProductVariantInput = ProductTypes.UpdateProductVariantDTO & {
product_id: string id: string
product_id?: string
}
export type UpdateProductOptionInput = ProductTypes.UpdateProductOptionDTO & {
id: string
} }
+5 -7
View File
@@ -1018,8 +1018,11 @@ export interface CreateProductOptionDTO {
product_id?: string product_id?: string
} }
export interface UpsertProductOptionDTO extends UpdateProductOptionDTO {
id?: string
}
export interface UpdateProductOptionDTO { export interface UpdateProductOptionDTO {
id: string
title?: string title?: string
values?: string[] | { value: string }[] values?: string[] | { value: string }[]
product_id?: string product_id?: string
@@ -1109,8 +1112,7 @@ export interface CreateProductVariantDTO {
metadata?: Record<string, unknown> metadata?: Record<string, unknown>
} }
export interface UpsertProductVariantDTO export interface UpsertProductVariantDTO extends UpdateProductVariantDTO {
extends Omit<UpdateProductVariantDTO, "id"> {
/** /**
* The ID of the product variant to update. * The ID of the product variant to update.
*/ */
@@ -1123,10 +1125,6 @@ export interface UpsertProductVariantDTO
* The data to update in a product variant. The `id` is used to identify which product variant to update. * The data to update in a product variant. The `id` is used to identify which product variant to update.
*/ */
export interface UpdateProductVariantDTO { export interface UpdateProductVariantDTO {
/**
* The ID of the product variant to update.
*/
id: string
/** /**
* The tile of the product variant. * The tile of the product variant.
*/ */
+280 -38
View File
@@ -30,6 +30,8 @@ import {
UpdateProductVariantDTO, UpdateProductVariantDTO,
UpsertProductCollectionDTO, UpsertProductCollectionDTO,
UpsertProductDTO, UpsertProductDTO,
UpsertProductOptionDTO,
UpsertProductVariantDTO,
} from "./common" } from "./common"
import { FindConfig } from "../common" import { FindConfig } from "../common"
@@ -1256,25 +1258,25 @@ export interface IProductModuleService extends IModuleService {
* *
* @param {CreateProductOptionDTO[]} data - The product options to be created. * @param {CreateProductOptionDTO[]} data - The product options to be created.
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module. * @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {ProductOptionDTO[]} The list of created product options. * @returns {Promise<ProductOptionDTO[]>} The list of created product options.
* *
* @example * @example
* import { * import {
* initialize as initializeProductModule, * initialize as initializeProductModule,
* } from "@medusajs/product" * } from "@medusajs/product"
* *
* async function createProductOption (title: string, productId: string) { * async function createOptions (title: string) {
* const productModule = await initializeProductModule() * const productModule = await initializeProductModule()
* *
* const productOptions = await productModule.createOptions([ * const options = await productModule.createOptions([
* { * {
* title, * title
* product_id: productId
* } * }
* ]) * ])
* *
* // do something with the product options or return them * // do something with the product options or return them
* } * }
*
*/ */
createOptions( createOptions(
data: CreateProductOptionDTO[], data: CreateProductOptionDTO[],
@@ -1282,32 +1284,150 @@ export interface IProductModuleService extends IModuleService {
): Promise<ProductOptionDTO[]> ): Promise<ProductOptionDTO[]>
/** /**
* This method is used to update existing product options. * This method is used to create a product option.
* *
* @param {UpdateProductOptionDTO[]} data - The product options to be updated, each holding the attributes that should be updated in the product option. * @param {CreateProductOptionDTO} data - The product option to be created.
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module. * @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {ProductOptionDTO[]} The list of updated product options. * @returns {Promise<ProductOptionDTO>} The created product option.
* *
* @example * @example
* import { * import {
* initialize as initializeProductModule, * initialize as initializeProductModule,
* } from "@medusajs/product" * } from "@medusajs/product"
* *
* async function updateProductOption (id: string, title: string) { * async function createOption (title: string) {
* const productModule = await initializeProductModule() * const productModule = await initializeProductModule()
* *
* const productOptions = await productModule.updateOptions([ * const option = await productModule.createOptions(
* {
* title
* }
* )
*
* // do something with the product option or return them
* }
*
*/
createOptions(
data: CreateProductOptionDTO,
sharedContext?: Context
): Promise<ProductOptionDTO>
/**
* This method updates existing options, or creates new ones if they don't exist.
*
* @param {UpsertProductOptionDTO[]} data - The attributes to update or create for each option.
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<ProductOptionDTO[]>} The updated and created options.
*
* @example
* import {
* initialize as initializeProductModule,
* } from "@medusajs/product"
*
* async function upsertOptions (title: string) {
* const productModule = await initializeProductModule()
*
* const createdOptions = await productModule.upsertOptions([
* { * {
* id,
* title * title
* } * }
* ]) * ])
* *
* // do something with the product options or return them * // do something with the options or return them
* }
*/
upsertOptions(
data: UpsertProductOptionDTO[],
sharedContext?: Context
): Promise<ProductOptionDTO[]>
/**
* This method updates an existing option, or creates a new one if it doesn't exist.
*
* @param {UpsertProductOptionDTO} data - The attributes to update or create for the option.
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<ProductOptionDTO>} The updated or created option.
*
* @example
* import {
* initialize as initializeProductModule,
* } from "@medusajs/product"
*
* async function upsertOption (title: string) {
* const productModule = await initializeProductModule()
*
* const createdOption = await productModule.upsertOptions(
* {
* title
* }
* )
*
* // do something with the option or return it
* }
*/
upsertOptions(
data: UpsertProductOptionDTO,
sharedContext?: Context
): Promise<ProductOptionDTO>
/**
* This method is used to update a option.
*
* @param {string} id - The ID of the option to be updated.
* @param {UpdateProductOptionDTO} data - The attributes of the option to be updated
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<ProductOptionDTO>} The updated option.
*
* @example
* import {
* initialize as initializeProductModule,
* } from "@medusajs/product"
*
* async function updateOption (id: string, title: string) {
* const productModule = await initializeProductModule()
*
* const option = await productModule.updateOptions(id, {
* title
* }
* )
*
* // do something with the option or return it
* } * }
*/ */
updateOptions( updateOptions(
data: UpdateProductOptionDTO[], id: string,
data: UpdateProductOptionDTO,
sharedContext?: Context
): Promise<ProductOptionDTO>
/**
* This method is used to update a list of options determined by the selector filters.
*
* @param {FilterableProductOptionProps} selector - The filters that will determine which options will be updated.
* @param {UpdateProductOptionDTO} data - The attributes to be updated on the selected options
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<ProductOptionDTO[]>} The updated options.
*
* @example
* import {
* initialize as initializeProductModule,
* } from "@medusajs/product"
*
* async function updateOptions(ids: string[], title: string) {
* const productModule = await initializeProductModule()
*
* const options = await productModule.updateOptions({id: ids}, {
* title
* }
* )
*
* // do something with the options or return them
* }
*/
updateOptions(
selector: FilterableProductOptionProps,
data: UpdateProductOptionDTO,
sharedContext?: Context sharedContext?: Context
): Promise<ProductOptionDTO[]> ): Promise<ProductOptionDTO[]>
@@ -1560,58 +1680,180 @@ export interface IProductModuleService extends IModuleService {
): Promise<ProductVariantDTO[]> ): Promise<ProductVariantDTO[]>
/** /**
* This method is used to update a product's variants. * This method is used to create product variants.
* *
* @param {UpdateProductVariantDTO[]} data - The product variants to update. * @param {CreateProductVariantDTO[]} data - The product variants to be created.
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module. * @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<ProductVariantDTO[]>} The updated product variants's details. * @returns {Promise<ProductVariantDTO[]>} The list of created product variants.
* *
* @example * @example
* import { * import {
* initialize as initializeProductModule, * initialize as initializeProductModule,
* } from "@medusajs/product" * } from "@medusajs/product"
* import {
* UpdateProductVariantDTO
* } from "@medusajs/product/dist/types/services/product-variant"
* *
* async function updateProductVariants (items: UpdateProductVariantDTO[]) { * async function createVariants (title: string) {
* const productModule = await initializeProductModule() * const productModule = await initializeProductModule()
* *
* const productVariants = await productModule.updateVariants(items) * const variants = await productModule.createVariants([
* {
* title
* }
* ])
* *
* // do something with the product variants or return them * // do something with the product variants or return them
* } * }
*
*/ */
updateVariants( createVariants(
data: UpdateProductVariantDTO[], data: CreateProductVariantDTO[],
sharedContext?: Context sharedContext?: Context
): Promise<ProductVariantDTO[]> ): Promise<ProductVariantDTO[]>
/** /**
* This method is used to create variants for a product. * This method is used to create a product variant.
* *
* @param {CreateProductVariantDTO[]} data - The product variants to create. * @param {CreateProductVariantDTO} data - The product variant to be created.
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module. * @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<ProductVariantDTO[]>} The created product variants' details. * @returns {Promise<ProductVariantDTO>} The created product variant.
* *
* @example * @example
* import { * import {
* initialize as initializeProductModule, * initialize as initializeProductModule,
* } from "@medusajs/product" * } from "@medusajs/product"
* *
* async function createProductVariants (items: { * async function createVariant (title: string) {
* product_id: string,
* title: string
* }[]) {
* const productModule = await initializeProductModule() * const productModule = await initializeProductModule()
* *
* const productVariants = await productModule.createVariants(items) * const variant = await productModule.createVariants(
* * {
* // do something with the product variants or return them * title
* } * }
* )
*
* // do something with the product variant or return them
* }
*
*/ */
createVariants( createVariants(
data: CreateProductVariantDTO[], data: CreateProductVariantDTO,
sharedContext?: Context
): Promise<ProductVariantDTO>
/**
* This method updates existing variants, or creates new ones if they don't exist.
*
* @param {UpsertProductVariantDTO[]} data - The attributes to update or create for each variant.
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<ProductVariantDTO[]>} The updated and created variants.
*
* @example
* import {
* initialize as initializeProductModule,
* } from "@medusajs/product"
*
* async function upsertVariants (title: string) {
* const productModule = await initializeProductModule()
*
* const createdVariants = await productModule.upsertVariants([
* {
* title
* }
* ])
*
* // do something with the variants or return them
* }
*/
upsertVariants(
data: UpsertProductVariantDTO[],
sharedContext?: Context
): Promise<ProductVariantDTO[]>
/**
* This method updates an existing variant, or creates a new one if it doesn't exist.
*
* @param {UpsertProductVariantDTO} data - The attributes to update or create for the variant.
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<ProductVariantDTO>} The updated or created variant.
*
* @example
* import {
* initialize as initializeProductModule,
* } from "@medusajs/product"
*
* async function upsertVariant (title: string) {
* const productModule = await initializeProductModule()
*
* const createdVariant = await productModule.upsertVariants(
* {
* title
* }
* )
*
* // do something with the variant or return it
* }
*/
upsertVariants(
data: UpsertProductVariantDTO,
sharedContext?: Context
): Promise<ProductVariantDTO>
/**
* This method is used to update a variant.
*
* @param {string} id - The ID of the variant to be updated.
* @param {UpdateProductVariantDTO} data - The attributes of the variant to be updated
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<ProductVariantDTO>} The updated variant.
*
* @example
* import {
* initialize as initializeProductModule,
* } from "@medusajs/product"
*
* async function updateVariant (id: string, title: string) {
* const productModule = await initializeProductModule()
*
* const variant = await productModule.updateVariants(id, {
* title
* }
* )
*
* // do something with the variant or return it
* }
*/
updateVariants(
id: string,
data: UpdateProductVariantDTO,
sharedContext?: Context
): Promise<ProductVariantDTO>
/**
* This method is used to update a list of variants determined by the selector filters.
*
* @param {FilterableProductVariantProps} selector - The filters that will determine which variants will be updated.
* @param {UpdateProductVariantDTO} data - The attributes to be updated on the selected variants
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<ProductVariantDTO[]>} The updated variants.
*
* @example
* import {
* initialize as initializeProductModule,
* } from "@medusajs/product"
*
* async function updateVariants(ids: string[], title: string) {
* const productModule = await initializeProductModule()
*
* const variants = await productModule.updateVariants({id: ids}, {
* title
* }
* )
*
* // do something with the variants or return them
* }
*/
updateVariants(
selector: FilterableProductVariantProps,
data: UpdateProductVariantDTO,
sharedContext?: Context sharedContext?: Context
): Promise<ProductVariantDTO[]> ): Promise<ProductVariantDTO[]>
@@ -2147,10 +2389,10 @@ export interface IProductModuleService extends IModuleService {
* initialize as initializeProductModule, * initialize as initializeProductModule,
* } from "@medusajs/product" * } from "@medusajs/product"
* *
* async function upserCollections (title: string) { * async function upsertCollections (title: string) {
* const productModule = await initializeProductModule() * const productModule = await initializeProductModule()
* *
* const createdCollections = await productModule.upsert([ * const createdCollections = await productModule.upsertCollections([
* { * {
* title * title
* } * }
@@ -2176,10 +2418,10 @@ export interface IProductModuleService extends IModuleService {
* initialize as initializeProductModule, * initialize as initializeProductModule,
* } from "@medusajs/product" * } from "@medusajs/product"
* *
* async function upserCollection (title: string) { * async function upsertCollection (title: string) {
* const productModule = await initializeProductModule() * const productModule = await initializeProductModule()
* *
* const createdCollection = await productModule.upsert( * const createdCollection = await productModule.upsertCollection(
* { * {
* title * title
* } * }