forked from yoone/API
1
0
Fork 0

fix(订单服务): 修复产品分类可能为空的错误并优化产品查询

修复订单服务中产品分类可能为空导致的错误,改为可选链操作
优化产品服务中组件查询逻辑,使用sku查询并添加关联关系
移除产品服务中不必要的组件查询逻辑
This commit is contained in:
tikkhun 2026-01-29 09:19:16 +08:00
parent f37de5ac32
commit 0b211628f3
2 changed files with 5 additions and 8 deletions

View File

@ -740,6 +740,7 @@ export class OrderService {
const orderSales: OrderSale[] = componentDetails.map(({product, parentProduct, quantity}) => { const orderSales: OrderSale[] = componentDetails.map(({product, parentProduct, quantity}) => {
if (!product) return null if (!product) return null
console.log('product',product)
const attrsObj = this.productService.getAttributesObject(product.attributes) const attrsObj = this.productService.getAttributesObject(product.attributes)
const orderSale = plainToClass(OrderSale, { const orderSale = plainToClass(OrderSale, {
orderId: orderItem.orderId, orderId: orderItem.orderId,
@ -758,7 +759,7 @@ export class OrderService {
flavor: attrsObj?.['flavor']?.name, flavor: attrsObj?.['flavor']?.name,
humidity: attrsObj?.['humidity']?.name, humidity: attrsObj?.['humidity']?.name,
size: attrsObj?.['size']?.name, size: attrsObj?.['size']?.name,
category: product.category.name, category: product.category?.name,
}); });
return orderSale return orderSale
}).filter(v => v !== null) }).filter(v => v !== null)

View File

@ -1786,7 +1786,7 @@ export class ProductService {
} }
} }
// 获取库存单品列表 // 获取库存单品列表
async getComponentDetailFromSiteSku(siteProduct: { sku: string, name: string }, site: Site): Promise<{ product: Product,parentProduct?: Product, quantity: number }[]> { async getComponentDetailFromSiteSku(siteProduct: { sku: string, name?: string }, site: Site): Promise<{ product: Product,parentProduct?: Product, quantity: number }[]> {
if (!siteProduct.sku) { if (!siteProduct.sku) {
throw new Error('siteSku 不能为空') throw new Error('siteSku 不能为空')
} }
@ -1805,7 +1805,8 @@ export class ProductService {
return await Promise.all(product.components.map(async comp => { return await Promise.all(product.components.map(async comp => {
return { return {
product: await this.productModel.findOne({ product: await this.productModel.findOne({
where: { id: comp.productId }, where: { sku: comp.sku },
relations: ['category', 'attributes', 'attributes.dict', 'components']
}), }),
parentProduct: product, // 这里得记录一下他的爸爸用来记录 parentProduct: product, // 这里得记录一下他的爸爸用来记录
quantity: comp.quantity, quantity: comp.quantity,
@ -2133,11 +2134,6 @@ export class ProductService {
component.sku = product.sku; component.sku = product.sku;
component.quantity = 1; component.quantity = 1;
product.components = [component]; product.components = [component];
} else {
// 混装商品返回持久化的 SKU 组成
product.components = await this.productStockComponentModel.find({
where: { productId: product.id },
});
} }
return product; return product;