From 254e9f31c77d00b56459b6d37fd55847d4d6671d Mon Sep 17 00:00:00 2001 From: 230417 <230417@epvc.pt> Date: Tue, 10 Mar 2026 14:51:19 +0000 Subject: [PATCH] feat: Implement shop ID association for 'barbearia' users and refine shop data queries. --- src/context/AppContext.tsx | 13 ++++++++++++- test.mjs | 16 ++++++++++++++++ web/src/context/AppContext.tsx | 16 +++++++++++++++- web/src/lib/check_db.ts | 4 ++-- 4 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 test.mjs diff --git a/src/context/AppContext.tsx b/src/context/AppContext.tsx index 37259b8..e20835d 100644 --- a/src/context/AppContext.tsx +++ b/src/context/AppContext.tsx @@ -43,10 +43,21 @@ export const AppProvider = ({ children }: { children: React.ReactNode }) => { // Pedido restrito à API de autenticação do Supabase 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. + setUser({ id: data.user.id, email: data.user.email || '', - role: 'barbearia', // ajustar se tiveres roles + role: 'barbearia', // assumido estaticamente na V1, deve vir de profiles + shopId } as User); } }; diff --git a/test.mjs b/test.mjs new file mode 100644 index 0000000..5a01e79 --- /dev/null +++ b/test.mjs @@ -0,0 +1,16 @@ +import { createClient } from '@supabase/supabase-js'; + +const supabaseUrl = 'https://jqklhhpyykzrktikjnmb.supabase.co'; +const supabaseAnonKey = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Impxa2xoaHB5eWt6cmt0aWtqbm1iIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NjgzODQ0MDgsImV4cCI6MjA4Mzk2MDQwOH0.QsPuBnyUtRPSavlqKj3IGR9c8juT02LY_hSi-j3c6M0'; + +const supabase = createClient(supabaseUrl, supabaseAnonKey); + +async function check() { + const { data: profiles, error: err1 } = await supabase.from('profiles').select('*'); + const { data: shops, error: err2 } = await supabase.from('shops').select('*'); + + console.log("Profiles:", profiles?.map(p => ({ id: p.id, role: p.role, name: p.name, shop_name: p.shop_name }))); + console.log("Shops:", shops?.map(s => ({ id: s.id, name: s.name }))); +} + +check(); diff --git a/web/src/context/AppContext.tsx b/web/src/context/AppContext.tsx index 9988b9b..b94c701 100644 --- a/web/src/context/AppContext.tsx +++ b/web/src/context/AppContext.tsx @@ -178,7 +178,20 @@ export const AppProvider = ({ children }: { children: React.ReactNode }) => { const role = data.role === 'barbearia' ? 'barbearia' : 'cliente'; const displayName = data.name?.trim() || (email ? email.split('@')[0] : 'Utilizador'); - const shopId = role === 'barbearia' ? userId : undefined; + + 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. + 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 + } let needsInsert = false; let shopNameToInsert = ''; @@ -223,6 +236,7 @@ export const AppProvider = ({ children }: { children: React.ReactNode }) => { if (needsInsert && shopId) { const { error: insertErr } = await supabase.from('shops').upsert([{ id: shopId, + owner_id: userId, // Garante que fica associada ao utilizador se for fallback name: shopNameToInsert, address: 'Endereço a definir', rating: 0, diff --git a/web/src/lib/check_db.ts b/web/src/lib/check_db.ts index 56c067c..e6fa139 100644 --- a/web/src/lib/check_db.ts +++ b/web/src/lib/check_db.ts @@ -6,11 +6,11 @@ const supabaseAnonKey = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYm const supabase = createClient(supabaseUrl, supabaseAnonKey); async function check() { - const { data, error } = await supabase.from('shops').select('*').limit(1); + 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 data structure:", data && data.length > 0 ? Object.keys(data[0]) : "No data, but query succeeded."); + console.log("Shops:", shops); } }