This commit is contained in:
2026-02-10 16:36:02 +03:30
parent 15bf2209d3
commit fb36c5f4c0
@@ -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');