product populate
This commit is contained in:
@@ -41,12 +41,12 @@ import { FormBuilderModule } from '../form-builder/form-builder.module';
|
||||
// AttributeValueService
|
||||
],
|
||||
exports: [
|
||||
ProductService,
|
||||
ProductRepository,
|
||||
CategoryRepository,
|
||||
CategoryService,
|
||||
// AttributeRepository,
|
||||
// AttributeService,
|
||||
ProductService
|
||||
],
|
||||
})
|
||||
export class ProductModule { }
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { BadRequestException, forwardRef, Inject, Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { CreateproductDto } from '../dto/create-product.dto';
|
||||
import { FindproductsDto } from '../dto/find-products.dto';
|
||||
import { ProductRepository } from '../repositories/product.repository';
|
||||
@@ -8,6 +8,9 @@ import { Product } from '../entities/product.entity';
|
||||
import { CategoryMessage, productMessage } from 'src/common/enums/message.enum';
|
||||
import { CategoryRepository } from '../repositories/category.repository';
|
||||
import { FieldService } from 'src/modules/form-builder/provider/field.service';
|
||||
import { FieldRepository } from 'src/modules/form-builder/repository/field.repository';
|
||||
import { Field } from 'src/modules/form-builder/entities/field.entity';
|
||||
import { Category } from '../entities/category.entity';
|
||||
|
||||
@Injectable()
|
||||
export class ProductService {
|
||||
@@ -15,7 +18,7 @@ export class ProductService {
|
||||
constructor(
|
||||
private readonly productRepository: ProductRepository,
|
||||
private readonly categoryRepository: CategoryRepository,
|
||||
// private readonly fieldService: FieldService,
|
||||
private readonly fieldRepository: FieldRepository,
|
||||
private readonly em: EntityManager,
|
||||
) { }
|
||||
|
||||
@@ -73,12 +76,29 @@ export class ProductService {
|
||||
return this.productRepository.findAllPaginated(dto);
|
||||
}
|
||||
|
||||
async findById(productId: string): Promise<Product> {
|
||||
async findById(productId: string): Promise<
|
||||
Omit<Product, 'attributes'> & {
|
||||
attributes: Field[];
|
||||
category: Category;
|
||||
}
|
||||
> {
|
||||
const product = await this.productRepository.findOne({ id: productId },
|
||||
{ populate: ['category', 'attributes', 'attributes.values'] });
|
||||
{ populate: ['category'] });
|
||||
|
||||
if (!product) throw new NotFoundException(productMessage.NOT_FOUND);
|
||||
return product;
|
||||
|
||||
const attributes = product?.attributes
|
||||
|
||||
let fields: Field[] = []
|
||||
|
||||
if (attributes && attributes.length) {
|
||||
fields = await this.fieldRepository.find({
|
||||
id: { $in: attributes }
|
||||
},
|
||||
{ populate: ['options'] })
|
||||
}
|
||||
|
||||
return { ...product, attributes: fields };
|
||||
}
|
||||
|
||||
// async update( id: string, dto: Partial<CreateproductDto>): Promise<Product> {
|
||||
|
||||
@@ -45,7 +45,7 @@ export class ProductRepository extends EntityRepository<Product> {
|
||||
limit,
|
||||
offset,
|
||||
orderBy: { [orderBy]: order.toLowerCase() as 'asc' | 'desc' },
|
||||
populate: ['attributes', 'attributes.values'],
|
||||
populate: [],
|
||||
});
|
||||
|
||||
const totalPages = Math.ceil(total / limit);
|
||||
|
||||
Reference in New Issue
Block a user