fix product bug

This commit is contained in:
2026-02-09 13:06:22 +03:30
parent c9c42db698
commit a21d1f6004
@@ -11,6 +11,7 @@ import { Favorite } from '../entities/favorite.entity';
import { Variant } from '../entities/variant.entity'; import { Variant } from '../entities/variant.entity';
import { ShopService } from 'src/modules/shops/providers/shops.service'; import { ShopService } from 'src/modules/shops/providers/shops.service';
import { CategoryService } from './category.service'; import { CategoryService } from './category.service';
import { User } from 'src/modules/users/entities/user.entity';
@Injectable() @Injectable()
export class ProductService { export class ProductService {
@@ -127,7 +128,7 @@ export class ProductService {
if (variants) { if (variants) {
// Get existing variants for this product // Get existing variants for this product
const existingVariants = product.variants const existingVariants = product.variants.getItems();
// Create a map of existing variants by id for easy lookup // Create a map of existing variants by id for easy lookup
const existingVariantsMap = new Map(existingVariants.map((v: any) => [v.id, v])); const existingVariantsMap = new Map(existingVariants.map((v: any) => [v.id, v]));
@@ -192,20 +193,23 @@ export class ProductService {
async toggleFavorite(userId: string, productId: string) { async toggleFavorite(userId: string, productId: string) {
const favorite = await this.em.findOne(Favorite, { user: userId, product: productId }); const favorite = await this.em.findOne(Favorite, { user: { id: userId }, product: { id: productId } });
if (favorite) { if (favorite) {
return await this.em.removeAndFlush(favorite); return await this.em.removeAndFlush(favorite);
} }
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: userId, user: userRef,
product: productId, product: productRef,
}); });
return await this.em.persistAndFlush(newFavorite); return await this.em.persistAndFlush(newFavorite);
} }
async getMyFavorites(userId: string, shopId: string) { async getMyFavorites(userId: string, shopId: string) {
return this.em.find(Favorite, { user: userId, product: { shop: { id: shopId } } }, { populate: ['product'] }); return this.em.find(Favorite, { user: { id: userId }, product: { shop: { id: shopId } } }, { populate: ['product'] });
} }
/** /**
* Helper * Helper