fix product bug
This commit is contained in:
@@ -11,6 +11,7 @@ import { Favorite } from '../entities/favorite.entity';
|
||||
import { Variant } from '../entities/variant.entity';
|
||||
import { ShopService } from 'src/modules/shops/providers/shops.service';
|
||||
import { CategoryService } from './category.service';
|
||||
import { User } from 'src/modules/users/entities/user.entity';
|
||||
|
||||
@Injectable()
|
||||
export class ProductService {
|
||||
@@ -127,7 +128,7 @@ export class ProductService {
|
||||
|
||||
if (variants) {
|
||||
// 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
|
||||
const existingVariantsMap = new Map(existingVariants.map((v: any) => [v.id, v]));
|
||||
@@ -192,20 +193,23 @@ export class ProductService {
|
||||
|
||||
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) {
|
||||
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, {
|
||||
user: userId,
|
||||
product: productId,
|
||||
user: userRef,
|
||||
product: productRef,
|
||||
});
|
||||
return await this.em.persistAndFlush(newFavorite);
|
||||
}
|
||||
|
||||
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user