diff --git a/web/src/context/AppContext.tsx b/web/src/context/AppContext.tsx index e33f8d6..af59b05 100644 --- a/web/src/context/AppContext.tsx +++ b/web/src/context/AppContext.tsx @@ -109,17 +109,24 @@ export const AppProvider = ({ children }: { children: React.ReactNode }) => { const { data: ordersData } = await supabase.from('orders').select('*'); const { data: waitlistData } = await supabase.from('waitlist').select('*'); const { data: notificationsData } = await supabase.from('notifications').select('*'); + const { data: reviewsData } = await supabase.from('reviews').select('shop_id, rating'); - const fetchedShops: BarberShop[] = shopsData.map((shop) => ({ - id: shop.id, - name: shop.name, - address: shop.address ?? '', - rating: shop.rating ?? 0, - imageUrl: shop.image_url ?? shop.imageUrl ?? undefined, - schedule: shop.schedule, - paymentMethods: shop.payment_methods, - socialNetworks: shop.social_networks, - contacts: shop.contacts, + const fetchedShops: BarberShop[] = shopsData.map((shop) => { + const shopReviews = (reviewsData ?? []).filter(r => r.shop_id === shop.id); + const avgRating = shopReviews.length > 0 + ? shopReviews.reduce((sum, r) => sum + r.rating, 0) / shopReviews.length + : (shop.rating ?? 0); + + return { + id: shop.id, + name: shop.name, + address: shop.address ?? '', + rating: avgRating, + imageUrl: shop.image_url ?? shop.imageUrl ?? undefined, + schedule: shop.schedule, + paymentMethods: shop.payment_methods, + socialNetworks: shop.social_networks, + contacts: shop.contacts, services: (servicesData ?? []) .filter((s) => s.shop_id === shop.id) .map((s) => ({ @@ -146,7 +153,8 @@ export const AppProvider = ({ children }: { children: React.ReactNode }) => { specialties: b.specialties ?? [], schedule: b.schedule ?? [], })), - })); + }; + }); const formattedAppointments: Appointment[] = (appointmentsData ?? []).map((a) => ({ id: a.id,