bug
This commit is contained in:
@@ -193,18 +193,32 @@ export class ProductService {
|
|||||||
|
|
||||||
async toggleFavorite(userId: string, productId: string) {
|
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) {
|
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, {
|
const newFavorite = this.em.create(Favorite, {
|
||||||
user: userRef,
|
user,
|
||||||
product: productRef,
|
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) {
|
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');
|
if (!variant) throw new NotFoundException('Variant not found');
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user