feat: update shop rating calculation to use aggregated reviews from database

This commit is contained in:
2026-04-28 16:47:33 +01:00
parent 2212f17206
commit f3cb32a39b

View File

@@ -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,