diff --git a/src/modules/products/controllers/category.controller.ts b/src/modules/products/controllers/category.controller.ts index 83d52c0..49ce116 100644 --- a/src/modules/products/controllers/category.controller.ts +++ b/src/modules/products/controllers/category.controller.ts @@ -31,7 +31,7 @@ export class CategoryController { @ApiParam({ name: 'slug', required: true, description: 'Shop Slug' }) @ApiOkResponse({ description: 'List of all categories for the shop' }) findAllByRestaurant(@Param('slug') slug: string) { - return this.categoryService.findAllByRestaurant(slug); + return this.categoryService.findAllByShop(slug); } /*** Admin ***/ @@ -51,7 +51,7 @@ export class CategoryController { @ApiBearerAuth() @Get('admin/categories') findAll(@ShopId() restId: string) { - return this.categoryService.findAllByRestaurantId(restId); + return this.categoryService.findAllByShopId(restId); } @Permissions(Permission.MANAGE_CATEGORIES) diff --git a/src/modules/products/entities/category.entity.ts b/src/modules/products/entities/category.entity.ts index b2b0487..8b93768 100644 --- a/src/modules/products/entities/category.entity.ts +++ b/src/modules/products/entities/category.entity.ts @@ -7,6 +7,9 @@ import { Shop } from '../../shops/entities/shop.entity'; @Index({ properties: ['shop', 'isActive'] }) @Index({ properties: ['isActive'] }) export class Category extends BaseEntity { + @ManyToOne(() => Category, { nullable: true }) + parent?: Category + @Property() title!: string; diff --git a/src/modules/products/providers/category.service.ts b/src/modules/products/providers/category.service.ts index 96d750c..b018e73 100644 --- a/src/modules/products/providers/category.service.ts +++ b/src/modules/products/providers/category.service.ts @@ -15,8 +15,8 @@ export class CategoryService { private readonly em: EntityManager, ) { } - async create(restId: string, dto: CreateCategoryDto): Promise { - const shop = await this.em.findOne(Shop, { id: restId }); + async create(shopId: string, dto: CreateCategoryDto): Promise { + const shop = await this.em.findOne(Shop, { id: shopId }); if (!shop) { throw new NotFoundException(RestMessage.NOT_FOUND); } @@ -32,7 +32,7 @@ export class CategoryService { return category; } - async findAllByRestaurant(slug: string): Promise { + async findAllByShop(slug: string): Promise { const shop = await this.em.findOne(Shop, { slug }); if (!shop || !shop.id) { throw new NotFoundException(RestMessage.NOT_FOUND); @@ -41,14 +41,14 @@ export class CategoryService { { shop: shop, isActive: true }, { orderBy: { order: 'ASC' } }); } - async findAllByRestaurantId(restId: string): Promise { + async findAllByShopId(shopId: string): Promise { return this.categoryRepository.find( - { shop: { id: restId } }, + { shop: { id: shopId } }, { orderBy: { order: 'asc' } }); } - async findOne(restId: string, id: string): Promise { - const cat = await this.categoryRepository.findOne({ id, shop: { id: restId } }, { populate: ['products'] }); + async findOne(shopId: string, id: string): Promise { + const cat = await this.categoryRepository.findOne({ id, shop: { id: shopId } }, { populate: ['products'] }); if (!cat) throw new NotFoundException(CategoryMessage.NOT_FOUND); return { @@ -64,16 +64,16 @@ export class CategoryService { } - async update(restId: string, id: string, dto: UpdateCategoryDto): Promise { - const cat = await this.categoryRepository.findOne({ id, shop: { id: restId } }); + async update(shopId: string, id: string, dto: UpdateCategoryDto): Promise { + const cat = await this.categoryRepository.findOne({ id, shop: { id: shopId } }); if (!cat) throw new NotFoundException(CategoryMessage.NOT_FOUND); this.em.assign(cat, dto); await this.em.persistAndFlush(cat); return cat; } - async remove(restId: string, id: string): Promise { - const cat = await this.categoryRepository.findOne({ id, shop: { id: restId } }); + async remove(shopId: string, id: string): Promise { + const cat = await this.categoryRepository.findOne({ id, shop: { id: shopId } }); if (!cat) throw new NotFoundException(CategoryMessage.NOT_FOUND); cat.deletedAt = new Date(); await this.em.persistAndFlush(cat); diff --git a/src/modules/products/providers/product.service.ts b/src/modules/products/providers/product.service.ts index 69ee8b1..ccc8e57 100644 --- a/src/modules/products/providers/product.service.ts +++ b/src/modules/products/providers/product.service.ts @@ -71,8 +71,8 @@ export class ProductService { return product; } - findAll(restId: string, dto: FindProductsDto) { - return this.productRepository.findAllPaginated(restId, dto); + findAll(shopId: string, dto: FindProductsDto) { + return this.productRepository.findAllPaginated(shopId, dto); } @@ -113,14 +113,15 @@ export class ProductService { }); } - async update(restId: string, productId: string, dto: Partial): Promise { + + async update(shopId: string, productId: string, dto: Partial): Promise { const { categoryId, variants, ...rest } = dto; const product = await this.findOrFail(productId) // attach new categories if provided (adds, does not attempt to preserve/remove existing ones) if (categoryId) { - const category = await this.categoryRepository.findOne({ id: categoryId, shop: { id: restId } }); + const category = await this.categoryRepository.findOne({ id: categoryId, shop: { id: shopId } }); if (!category) { throw new NotFoundException(CategoryMessage.NOT_FOUND); @@ -178,6 +179,7 @@ export class ProductService { return product; } + async remove(shopId: string, id: string): Promise { const product = await this.findOrFail(id) @@ -207,8 +209,8 @@ export class ProductService { } - async getMyFavorites(userId: string, restId: string) { - return this.em.find(Favorite, { user: userId, product: { shop: { id: restId } } }, { populate: ['product'] }); + async getMyFavorites(userId: string, shopId: string) { + return this.em.find(Favorite, { user: userId, product: { shop: { id: shopId } } }, { populate: ['product'] }); } /** * Helper