From fb36c5f4c0a4c219725f8384b8c2d2df41386b05 Mon Sep 17 00:00:00 2001 From: morteza-mortezai Date: Tue, 10 Feb 2026 16:36:02 +0330 Subject: [PATCH] bug --- .../products/providers/product.service.ts | 30 ++++++++++++++----- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/src/modules/products/providers/product.service.ts b/src/modules/products/providers/product.service.ts index 7ad35a8..d4020f0 100644 --- a/src/modules/products/providers/product.service.ts +++ b/src/modules/products/providers/product.service.ts @@ -193,18 +193,32 @@ export class ProductService { async toggleFavorite(userId: string, productId: string) { - const favorite = await this.em.findOne(Favorite, { user: { id: userId }, product: { id: productId } }); + const user = await this.em.findOne(User, userId); + + const product = await this.em.findOne(Product, productId); + + const favorite = await this.em.findOne(Favorite, { user, product }); + if (favorite) { - return await this.em.removeAndFlush(favorite); + await this.em.removeAndFlush(favorite); + return { message: 'Favorite removed successfully' }; + } + + if (!user) { + throw new Error('User not found'); + } + + if (!product) { + throw new Error('Product not found'); } - const userRef = this.em.getReference(User, userId); - const productRef = this.em.getReference(Product, productId); const newFavorite = this.em.create(Favorite, { - user: userRef, - product: productRef, + user, + product, }); - return await this.em.persistAndFlush(newFavorite); + + await this.em.persistAndFlush(newFavorite); + return { message: 'Favorite added successfully' }; } @@ -224,7 +238,7 @@ export class ProductService { } async findOrFailVariant(variantId: string) { - const variant = await this.em.findOne(Variant, { id: variantId } ,{ populate: ['product','product.shop',] }); + const variant = await this.em.findOne(Variant, { id: variantId }, { populate: ['product', 'product.shop',] }); if (!variant) throw new NotFoundException('Variant not found');