category
This commit is contained in:
@@ -6,16 +6,18 @@ import { CategoryRepository } from '../repositories/category.repository';
|
||||
import { EntityManager } from '@mikro-orm/postgresql';
|
||||
import { RequiredEntityData, FilterQuery } from '@mikro-orm/core';
|
||||
import { Product } from '../entities/product.entity';
|
||||
import { CategoryMessage, ProductMessage, RestMessage } from 'src/common/enums/message.enum';
|
||||
import { CategoryMessage, ProductMessage } from 'src/common/enums/message.enum';
|
||||
import { Favorite } from '../entities/favorite.entity';
|
||||
import { Variant } from '../entities/variant.entity';
|
||||
import { ShopService } from 'src/modules/shops/providers/shops.service';
|
||||
import { CategoryService } from './category.service';
|
||||
|
||||
@Injectable()
|
||||
export class ProductService {
|
||||
constructor(
|
||||
private readonly productRepository: ProductRepository,
|
||||
private readonly categoryRepository: CategoryRepository,
|
||||
private readonly categoryService: CategoryService,
|
||||
private readonly shopService: ShopService,
|
||||
private readonly em: EntityManager,
|
||||
) { }
|
||||
@@ -23,13 +25,8 @@ export class ProductService {
|
||||
async create(shopId: string, dto: CreateProductDto) {
|
||||
const { categoryId, variants, ...rest } = dto;
|
||||
const shop = await this.shopService.findOrFail(shopId);
|
||||
if (!shop) {
|
||||
throw new NotFoundException(RestMessage.NOT_FOUND);
|
||||
}
|
||||
const category = await this.categoryRepository.findOne({ id: categoryId, shop: { id: shopId } });
|
||||
if (!category) {
|
||||
throw new NotFoundException(CategoryMessage.NOT_FOUND);
|
||||
}
|
||||
|
||||
const category = await this.categoryService.findOrFail(categoryId)
|
||||
|
||||
const product = await this.em.transactional(async em => {
|
||||
// prepare data with defaults for required fields so repository.create typing is satisfied
|
||||
@@ -50,8 +47,6 @@ export class ProductService {
|
||||
|
||||
const product = em.create(Product, data);
|
||||
|
||||
em.persist(product)
|
||||
|
||||
for (const variant of variants) {
|
||||
const variantRecord = em.create(Variant, {
|
||||
product,
|
||||
@@ -179,7 +174,7 @@ export class ProductService {
|
||||
|
||||
return product;
|
||||
}
|
||||
|
||||
|
||||
|
||||
async remove(shopId: string, id: string): Promise<void> {
|
||||
const product = await this.findOrFail(id)
|
||||
@@ -216,7 +211,7 @@ export class ProductService {
|
||||
* Helper
|
||||
*/
|
||||
async findOrFail(productId: string) {
|
||||
const product = await this.productRepository.findOne({ id: productId, isActive: true },
|
||||
const product = await this.productRepository.findOne({ id: productId },
|
||||
{ populate: ['category', 'variants', 'shop'] });
|
||||
|
||||
if (!product) throw new NotFoundException(ProductMessage.NOT_FOUND);
|
||||
|
||||
Reference in New Issue
Block a user