diff --git a/src/context/AppContext.tsx b/src/context/AppContext.tsx index 4eb5c9a..3822687 100644 --- a/src/context/AppContext.tsx +++ b/src/context/AppContext.tsx @@ -94,13 +94,23 @@ export const AppProvider = ({ children }: { children: React.ReactNode }) => { return; } - // Associar serviços às respetivas shops, simulando um INNER JOIN nativo do SQL + // Query 3: Obtém a listagem global de Barbeiros (tabela 'barbers') + const { data: barbersData, error: barbersError } = await supabase + .from('barbers') + .select('*'); + + if (barbersError) { + console.error("Erro ao buscar barbers:", barbersError); + return; + } + + // Associar serviços e barbeiros às respetivas shops, simulando um INNER JOIN nativo do SQL const shopsWithServices = shopsData.map((shop) => ({ ...shop, // Relaciona a 'foreign key' (shop_id) com o resgistro primário (shop.id) services: servicesData.filter((s) => s.shop_id === shop.id), products: [], - barbers: [], + barbers: barbersData.filter((b) => b.shop_id === shop.id), })); console.log("Shops carregadas:", shopsWithServices); diff --git a/web/src/context/AppContext.tsx b/web/src/context/AppContext.tsx index 5e70033..8206874 100644 --- a/web/src/context/AppContext.tsx +++ b/web/src/context/AppContext.tsx @@ -33,15 +33,15 @@ type AppContextValue = State & { placeOrder: (customerId: string, shopId?: string) => Order | null; updateAppointmentStatus: (id: string, status: Appointment['status']) => void; updateOrderStatus: (id: string, status: Order['status']) => void; - addService: (shopId: string, service: Omit) => void; - updateService: (shopId: string, service: Service) => void; - deleteService: (shopId: string, serviceId: string) => void; - addProduct: (shopId: string, product: Omit) => void; - updateProduct: (shopId: string, product: Product) => void; - deleteProduct: (shopId: string, productId: string) => void; - addBarber: (shopId: string, barber: Omit) => void; - updateBarber: (shopId: string, barber: Barber) => void; - deleteBarber: (shopId: string, barberId: string) => void; + addService: (shopId: string, service: Omit) => Promise; + updateService: (shopId: string, service: Service) => Promise; + deleteService: (shopId: string, serviceId: string) => Promise; + addProduct: (shopId: string, product: Omit) => Promise; + updateProduct: (shopId: string, product: Product) => Promise; + deleteProduct: (shopId: string, productId: string) => Promise; + addBarber: (shopId: string, barber: Omit) => Promise; + updateBarber: (shopId: string, barber: Barber) => Promise; + deleteBarber: (shopId: string, barberId: string) => Promise; updateShopDetails: (shopId: string, payload: Partial) => Promise; refreshShops: () => Promise; shopsReady: boolean; @@ -94,6 +94,7 @@ export const AppProvider = ({ children }: { children: React.ReactNode }) => { } const { data: servicesData } = await supabase.from('services').select('*'); + const { data: barbersData } = await supabase.from('barbers').select('*'); const fetchedShops: BarberShop[] = shopsData.map((shop) => ({ id: shop.id, @@ -111,7 +112,14 @@ export const AppProvider = ({ children }: { children: React.ReactNode }) => { barberIds: s.barber_ids ?? [], })), products: [], - barbers: [], + barbers: (barbersData ?? []) + .filter((b) => b.shop_id === shop.id) + .map((b) => ({ + id: b.id, + name: b.name, + specialties: b.specialties ?? [], + schedule: b.schedule ?? [], + })), })); setState((s) => { @@ -391,82 +399,70 @@ export const AppProvider = ({ children }: { children: React.ReactNode }) => { setState((s) => ({ ...s, orders: s.orders.map((o) => (o.id === id ? { ...o, status } : o)) })); }; - const addService: AppContextValue['addService'] = (shopId, service) => { - const entry: Service = { ...service, id: nanoid() }; - setState((s) => ({ - ...s, - shops: s.shops.map((shop) => (shop.id === shopId ? { ...shop, services: [...shop.services, entry] } : shop)), - })); + const addService: AppContextValue['addService'] = async (shopId, service) => { + const { error } = await supabase.from('services').insert([ + { shop_id: shopId, name: service.name, price: service.price, duration: service.duration } + ]); + if (error) console.error("Erro addService:", error); + else await refreshShops(); }; - const updateService: AppContextValue['updateService'] = (shopId, service) => { - setState((s) => ({ - ...s, - shops: s.shops.map((shop) => - shop.id === shopId ? { ...shop, services: shop.services.map((sv) => (sv.id === service.id ? service : sv)) } : shop - ), - })); + const updateService: AppContextValue['updateService'] = async (shopId, service) => { + const { error } = await supabase.from('services').update({ + name: service.name, price: service.price, duration: service.duration + }).eq('id', service.id); + if (error) console.error("Erro updateService:", error); + else await refreshShops(); }; - const deleteService: AppContextValue['deleteService'] = (shopId, serviceId) => { - setState((s) => ({ - ...s, - shops: s.shops.map((shop) => - shop.id === shopId ? { ...shop, services: shop.services.filter((sv) => sv.id !== serviceId) } : shop - ), - })); + const deleteService: AppContextValue['deleteService'] = async (shopId, serviceId) => { + const { error } = await supabase.from('services').delete().eq('id', serviceId); + if (error) console.error("Erro deleteService:", error); + else await refreshShops(); }; - const addProduct: AppContextValue['addProduct'] = (shopId, product) => { - const entry: Product = { ...product, id: nanoid() }; - setState((s) => ({ - ...s, - shops: s.shops.map((shop) => (shop.id === shopId ? { ...shop, products: [...shop.products, entry] } : shop)), - })); + const addProduct: AppContextValue['addProduct'] = async (shopId, product) => { + const { error } = await supabase.from('products').insert([ + { shop_id: shopId, name: product.name, price: product.price, stock: product.stock } + ]); + if (error) console.error("Erro addProduct:", error); + else await refreshShops(); }; - const updateProduct: AppContextValue['updateProduct'] = (shopId, product) => { - setState((s) => ({ - ...s, - shops: s.shops.map((shop) => - shop.id === shopId ? { ...shop, products: shop.products.map((p) => (p.id === product.id ? product : p)) } : shop - ), - })); + const updateProduct: AppContextValue['updateProduct'] = async (shopId, product) => { + const { error } = await supabase.from('products').update({ + name: product.name, price: product.price, stock: product.stock + }).eq('id', product.id); + if (error) console.error("Erro updateProduct:", error); + else await refreshShops(); }; - const deleteProduct: AppContextValue['deleteProduct'] = (shopId, productId) => { - setState((s) => ({ - ...s, - shops: s.shops.map((shop) => - shop.id === shopId ? { ...shop, products: shop.products.filter((p) => p.id !== productId) } : shop - ), - })); + const deleteProduct: AppContextValue['deleteProduct'] = async (shopId, productId) => { + const { error } = await supabase.from('products').delete().eq('id', productId); + if (error) console.error("Erro deleteProduct:", error); + else await refreshShops(); }; - const addBarber: AppContextValue['addBarber'] = (shopId, barber) => { - const entry: Barber = { ...barber, id: nanoid() }; - setState((s) => ({ - ...s, - shops: s.shops.map((shop) => (shop.id === shopId ? { ...shop, barbers: [...shop.barbers, entry] } : shop)), - })); + const addBarber: AppContextValue['addBarber'] = async (shopId, barber) => { + const { error } = await supabase.from('barbers').insert([ + { shop_id: shopId, name: barber.name, specialties: barber.specialties } + ]); + if (error) console.error("Erro addBarber:", error); + else await refreshShops(); }; - const updateBarber: AppContextValue['updateBarber'] = (shopId, barber) => { - setState((s) => ({ - ...s, - shops: s.shops.map((shop) => - shop.id === shopId ? { ...shop, barbers: shop.barbers.map((b) => (b.id === barber.id ? barber : b)) } : shop - ), - })); + const updateBarber: AppContextValue['updateBarber'] = async (shopId, barber) => { + const { error } = await supabase.from('barbers').update({ + name: barber.name, specialties: barber.specialties + }).eq('id', barber.id); + if (error) console.error("Erro updateBarber:", error); + else await refreshShops(); }; - const deleteBarber: AppContextValue['deleteBarber'] = (shopId, barberId) => { - setState((s) => ({ - ...s, - shops: s.shops.map((shop) => - shop.id === shopId ? { ...shop, barbers: shop.barbers.filter((b) => b.id !== barberId) } : shop - ), - })); + const deleteBarber: AppContextValue['deleteBarber'] = async (shopId, barberId) => { + const { error } = await supabase.from('barbers').delete().eq('id', barberId); + if (error) console.error("Erro deleteBarber:", error); + else await refreshShops(); }; const updateShopDetails: AppContextValue['updateShopDetails'] = async (shopId, payload) => { diff --git a/web/src/lib/check_db.ts b/web/src/lib/check_db.ts index 27a2b4b..f0fad01 100644 --- a/web/src/lib/check_db.ts +++ b/web/src/lib/check_db.ts @@ -6,45 +6,15 @@ const supabaseAnonKey = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYm const supabase = createClient(supabaseUrl, supabaseAnonKey); async function check() { - const email = `test_shop_${Date.now()}@test.com`; - const password = 'Password123!'; + console.log("Checking columns..."); + const { data: bCols } = await supabase.from('barbers').select('*').limit(1); + console.log("Barbers columns:", bCols ? Object.keys(bCols[0] || {}) : "No data"); - console.log("Signing up:", email); - const { data: authData, error: authErr } = await supabase.auth.signUp({ - email, - password, - options: { - data: { - name: "Test Shop User", - role: "barbearia", - shopName: "Test Shop Automatica", - shop_name: "Test Shop Automatica" - } - } - }); + const { data: sCols } = await supabase.from('services').select('*').limit(1); + console.log("Services columns:", sCols ? Object.keys(sCols[0] || {}) : "No data"); - if (authErr) { - console.error("Signup err:", authErr); - return; - } - - const userId = authData.user?.id; - console.log("Created user ID:", userId); - - // Wait for triggers to complete - await new Promise(r => setTimeout(r, 2000)); - - const { data: profiles, error: profErr } = await supabase.from('profiles').select('*').eq('id', userId ?? ''); - console.log("Created profile:", profiles, profErr || ""); - - const { data: shops, error: shopErr } = await supabase.from('shops').select('*').eq('owner_id', userId ?? ''); - console.log("Created shop (by owner_id):", shops, shopErr || ""); - - // check if profiles has a shop_id that we can lookup - if (profiles && profiles.length > 0 && profiles[0].shop_id) { - const { data: shopsByProfId } = await supabase.from('shops').select('*').eq('id', profiles[0].shop_id); - console.log("Created shop (by profile shop_id):", shopsByProfId); - } + const { data: pCols } = await supabase.from('products').select('*').limit(1); + console.log("Products columns:", pCols ? Object.keys(pCols[0] || {}) : "No data"); } check();