From 13a350676ca30e10075263d65e189b0114c043cb Mon Sep 17 00:00:00 2001 From: 230417 <230417@epvc.pt> Date: Tue, 10 Mar 2026 15:45:03 +0000 Subject: [PATCH] refactor: Retrieve shop ID from user profiles table instead of directly querying shops by owner ID, and update `check_db` for verification. --- src/context/AppContext.tsx | 15 ++++++------ web/src/context/AppContext.tsx | 13 +++------- web/src/lib/check_db.ts | 43 ++++++++++++++++++++++++++++++---- 3 files changed, 49 insertions(+), 22 deletions(-) diff --git a/src/context/AppContext.tsx b/src/context/AppContext.tsx index e20835d..4eb5c9a 100644 --- a/src/context/AppContext.tsx +++ b/src/context/AppContext.tsx @@ -44,14 +44,15 @@ export const AppProvider = ({ children }: { children: React.ReactNode }) => { const { data } = await supabase.auth.getUser(); if (data.user) { let shopId: string | undefined = undefined; - // tenta ir buscar a barbearia ligada ao user atual (owner_id) - const { data: existingShop } = await supabase - .from('shops') - .select('id') - .eq('owner_id', data.user.id) - .maybeSingle(); - shopId = existingShop?.id; // Não forçamos igual a userid. Ou existe, ou é undefined. + // Vai buscar o shop_id mapeado na tabela profiles + const { data: prof } = await supabase + .from('profiles') + .select('shop_id') + .eq('id', data.user.id) + .single(); + + shopId = prof?.shop_id || undefined; setUser({ id: data.user.id, diff --git a/web/src/context/AppContext.tsx b/web/src/context/AppContext.tsx index b94c701..5e70033 100644 --- a/web/src/context/AppContext.tsx +++ b/web/src/context/AppContext.tsx @@ -166,7 +166,7 @@ export const AppProvider = ({ children }: { children: React.ReactNode }) => { const applyProfile = async (userId: string, email?: string | null, userMetadata?: any) => { const { data, error } = await supabase .from('profiles') - .select('id,name,role,shop_name') + .select('id,name,role,shop_name,shop_id') .eq('id', userId) .single(); @@ -181,16 +181,9 @@ export const AppProvider = ({ children }: { children: React.ReactNode }) => { let shopId: string | undefined = undefined; - // Se for barbearia, vê se já existe a loja real associada ao owner_id na BD - // O Supabase tem um trigger que cria a loja automaticamente. + // O trigger de backend já popula a tabela profiles com o shop_id quando é barbearia if (role === 'barbearia') { - const { data: existingShop } = await supabase - .from('shops') - .select('id') - .eq('owner_id', userId) - .maybeSingle(); - - shopId = existingShop?.id || userId; // Fallback para userId se por algum motivo não encontrou + shopId = data.shop_id || userId; // Fallback extremo se o trigger falhar } let needsInsert = false; diff --git a/web/src/lib/check_db.ts b/web/src/lib/check_db.ts index e6fa139..27a2b4b 100644 --- a/web/src/lib/check_db.ts +++ b/web/src/lib/check_db.ts @@ -6,11 +6,44 @@ const supabaseAnonKey = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYm const supabase = createClient(supabaseUrl, supabaseAnonKey); async function check() { - const { data: shops, error } = await supabase.from('shops').select('id, name, owner_id'); - if (error) { - console.error("Error fetching shops:", error); - } else { - console.log("Shops:", shops); + const email = `test_shop_${Date.now()}@test.com`; + const password = 'Password123!'; + + 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" + } + } + }); + + 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); } }