feat(medusa,types,workflows,utils,product): PricingModule Integration of PriceLists into Core (#5536)

This commit is contained in:
Philip Korsholm
2023-11-21 17:42:37 +00:00
committed by GitHub
parent 9c7ac7332a
commit dc5750dd66
94 changed files with 5697 additions and 880 deletions
@@ -8,8 +8,11 @@ export class Migration20230719100648 extends Migration {
this.addSql(
'create index IF NOT EXISTS "IDX_product_category_path" on "product_category" ("mpath");'
)
this.addSql('DROP INDEX IF EXISTS "IDX_product_category_handle";')
this.addSql(
'alter table "product_category" add constraint "IDX_product_category_handle" unique ("handle");'
'alter table "product_category" ADD CONSTRAINT "IDX_product_category_handle" unique ("handle");'
)
this.addSql(
@@ -231,6 +231,82 @@ export default class ProductModuleService<
return [JSON.parse(JSON.stringify(variants)), count]
}
async createVariants(
data: ProductTypes.CreateProductVariantDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<ProductTypes.ProductVariantDTO[]> {
const productOptionIds = data
.map((pv) => (pv.options || []).map((opt) => opt.option_id!))
.flat()
const productOptions = await this.listOptions(
{ id: productOptionIds },
{},
sharedContext
)
const productOptionsMap = new Map<string, ProductTypes.ProductOptionDTO>(
productOptions.map((po) => [po.id, po])
)
const productVariantsMap = new Map<
string,
ProductTypes.CreateProductVariantDTO[]
>()
for (const productVariantData of data) {
productVariantData.options = productVariantData.options?.map((option) => {
const productOption = productOptionsMap.get(option.option_id!)
return {
option: productOption?.id,
value: option.value,
}
})
const productVariants = productVariantsMap.get(
productVariantData.product_id!
)
if (productVariants) {
productVariants.push(productVariantData)
} else {
productVariantsMap.set(productVariantData.product_id!, [
productVariantData,
])
}
}
const productVariants = (
await promiseAll(
[...productVariantsMap].map(async ([productId, variants]) => {
return await this.productVariantService_.create(
productId,
variants as unknown as ProductTypes.CreateProductVariantOnlyDTO[],
sharedContext
)
})
)
).flat()
return productVariants as unknown as ProductTypes.ProductVariantDTO[]
}
@InjectTransactionManager("baseRepository_")
async deleteVariants(
productVariantIds: string[],
@MedusaContext() sharedContext: Context = {}
): Promise<void> {
await this.productVariantService_.delete(productVariantIds, sharedContext)
await this.eventBusModuleService_?.emit<ProductEventData>(
productVariantIds.map((id) => ({
eventName: ProductEvents.PRODUCT_DELETED,
data: { id },
}))
)
}
@InjectManager("baseRepository_")
async retrieveTag(
tagId: string,
@@ -1,14 +1,14 @@
import { Product, ProductVariant } from "@models"
import { Context, DAL, FindConfig, ProductTypes } from "@medusajs/types"
import { ProductVariantRepository } from "@repositories"
import {
InjectManager,
InjectTransactionManager,
isString,
MedusaContext,
ModulesSdkUtils,
isString,
retrieveEntity,
} from "@medusajs/utils"
import { Product, ProductVariant } from "@models"
import { ProductVariantRepository } from "@repositories"
import { ProductVariantServiceTypes } from "../types/services"
import ProductService from "./product"
@@ -96,7 +96,7 @@ export default class ProductVariantService<
if (isString(productOrId)) {
product = await this.productService_.retrieve(
productOrId,
{},
{ relations: ["variants"] },
sharedContext
)
}
@@ -105,6 +105,8 @@ export default class ProductVariantService<
const data_ = [...data]
data_.forEach((variant) => {
delete variant?.product_id
Object.assign(variant, {
variant_rank: computedRank++,
product,