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