chore: fix interfaces for product module (#5387)
Some changes based on @shahednasser's PR - https://github.com/medusajs/medusa/pull/5341
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { DAL } from "@medusajs/types"
|
||||
import { DALUtils, generateEntityId } from "@medusajs/utils"
|
||||
import {
|
||||
BeforeCreate,
|
||||
@@ -14,7 +15,6 @@ import {
|
||||
} from "@mikro-orm/core"
|
||||
import { Product } from "./index"
|
||||
import ProductOptionValue from "./product-option-value"
|
||||
import { DAL } from "@medusajs/types"
|
||||
|
||||
type OptionalRelations =
|
||||
| "values"
|
||||
@@ -39,8 +39,9 @@ class ProductOption {
|
||||
@ManyToOne(() => Product, {
|
||||
index: "IDX_product_option_product_id",
|
||||
fieldName: "product_id",
|
||||
nullable: true,
|
||||
})
|
||||
product: Product
|
||||
product!: Product
|
||||
|
||||
@OneToMany(() => ProductOptionValue, (value) => value.option, {
|
||||
cascade: [Cascade.REMOVE, "soft-remove" as any],
|
||||
|
||||
@@ -1,12 +1,20 @@
|
||||
import { ProductCollection } from "@models"
|
||||
import { Context, DAL, ProductTypes } from "@medusajs/types"
|
||||
import { DALUtils, MedusaError } from "@medusajs/utils"
|
||||
import {
|
||||
LoadStrategy,
|
||||
FilterQuery as MikroFilterQuery,
|
||||
FindOptions as MikroOptions,
|
||||
LoadStrategy,
|
||||
} from "@mikro-orm/core"
|
||||
import { Context, DAL, ProductTypes } from "@medusajs/types"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import { DALUtils, MedusaError } from "@medusajs/utils"
|
||||
import { ProductCollection } from "@models"
|
||||
|
||||
type UpdateProductCollection = ProductTypes.UpdateProductCollectionDTO & {
|
||||
products?: string[]
|
||||
}
|
||||
|
||||
type CreateProductCollection = ProductTypes.CreateProductCollectionDTO & {
|
||||
products?: string[]
|
||||
}
|
||||
|
||||
// eslint-disable-next-line max-len
|
||||
export class ProductCollectionRepository extends DALUtils.MikroOrmBaseRepository {
|
||||
@@ -71,12 +79,18 @@ export class ProductCollectionRepository extends DALUtils.MikroOrmBaseRepository
|
||||
}
|
||||
|
||||
async create(
|
||||
data: ProductTypes.CreateProductCollectionDTO[],
|
||||
data: CreateProductCollection[],
|
||||
context: Context = {}
|
||||
): Promise<ProductCollection[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const productCollections = data.map((collectionData) => {
|
||||
if (collectionData.product_ids) {
|
||||
collectionData.products = collectionData.product_ids
|
||||
|
||||
delete collectionData.product_ids
|
||||
}
|
||||
|
||||
return manager.create(ProductCollection, collectionData)
|
||||
})
|
||||
|
||||
@@ -86,7 +100,7 @@ export class ProductCollectionRepository extends DALUtils.MikroOrmBaseRepository
|
||||
}
|
||||
|
||||
async update(
|
||||
data: ProductTypes.UpdateProductCollectionDTO[],
|
||||
data: UpdateProductCollection[],
|
||||
context: Context = {}
|
||||
): Promise<ProductCollection[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
@@ -119,6 +133,12 @@ export class ProductCollectionRepository extends DALUtils.MikroOrmBaseRepository
|
||||
)
|
||||
}
|
||||
|
||||
if (collectionData.product_ids) {
|
||||
collectionData.products = collectionData.product_ids
|
||||
|
||||
delete collectionData.product_ids
|
||||
}
|
||||
|
||||
return manager.assign(existingCollection, collectionData)
|
||||
})
|
||||
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import { Context, DAL, ProductTypes } from "@medusajs/types"
|
||||
import { DALUtils, MedusaError } from "@medusajs/utils"
|
||||
import {
|
||||
LoadStrategy,
|
||||
FilterQuery as MikroFilterQuery,
|
||||
FindOptions as MikroOptions,
|
||||
LoadStrategy,
|
||||
} from "@mikro-orm/core"
|
||||
import { Product, ProductOption } from "@models"
|
||||
import { Context, DAL, ProductTypes } from "@medusajs/types"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import { DALUtils, MedusaError } from "@medusajs/utils"
|
||||
import { Product, ProductOption } from "@models"
|
||||
|
||||
// eslint-disable-next-line max-len
|
||||
export class ProductOptionRepository extends DALUtils.MikroOrmAbstractBaseRepository<ProductOption> {
|
||||
@@ -97,7 +97,7 @@ export class ProductOptionRepository extends DALUtils.MikroOrmAbstractBaseReposi
|
||||
if (productId) {
|
||||
const product = existingProductsMap.get(productId)
|
||||
|
||||
optionData.product = product
|
||||
optionData.product_id = product?.id
|
||||
}
|
||||
|
||||
return manager.create(ProductOption, optionData)
|
||||
|
||||
@@ -446,7 +446,11 @@ export default class ProductModuleService<
|
||||
sharedContext
|
||||
)
|
||||
|
||||
return JSON.parse(JSON.stringify(productOptions))
|
||||
return await this.baseRepository_.serialize<
|
||||
ProductTypes.ProductOptionDTO[]
|
||||
>(productOptions, {
|
||||
populate: true,
|
||||
})
|
||||
}
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
||||
@@ -459,7 +463,11 @@ export default class ProductModuleService<
|
||||
sharedContext
|
||||
)
|
||||
|
||||
return JSON.parse(JSON.stringify(productOptions))
|
||||
return await this.baseRepository_.serialize<
|
||||
ProductTypes.ProductOptionDTO[]
|
||||
>(productOptions, {
|
||||
populate: true,
|
||||
})
|
||||
}
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
||||
|
||||
Reference in New Issue
Block a user