From f160a2e2fd5951df588354c308b33daf97a7a4fd Mon Sep 17 00:00:00 2001 From: 230417 <230417@epvc.pt> Date: Tue, 3 Mar 2026 16:46:59 +0000 Subject: [PATCH] appcontex --- web/check_db.js | 18 +++++++++++++++++ web/src/context/AppContext.tsx | 36 ++++------------------------------ web/src/lib/check_db.ts | 17 ++++++++++++++++ 3 files changed, 39 insertions(+), 32 deletions(-) create mode 100644 web/check_db.js create mode 100644 web/src/lib/check_db.ts diff --git a/web/check_db.js b/web/check_db.js new file mode 100644 index 0000000..da462f8 --- /dev/null +++ b/web/check_db.js @@ -0,0 +1,18 @@ +const { createClient } = require('@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, error } = await supabase.from('shops').select('*').limit(1); + if (error) { + console.log("DB ERROR:", error); + } else { + console.log("COLUMNS:", data && data.length > 0 ? Object.keys(data[0]) : "No rows returned"); + } + process.exit(0); +} + +check(); diff --git a/web/src/context/AppContext.tsx b/web/src/context/AppContext.tsx index 5bb121a..bb4de1d 100644 --- a/web/src/context/AppContext.tsx +++ b/web/src/context/AppContext.tsx @@ -442,43 +442,15 @@ export const AppProvider = ({ children }: { children: React.ReactNode }) => { }; const updateShopDetails: AppContextValue['updateShopDetails'] = async (shopId, payload) => { - // Preparar os dados para o Supabase: remover arrays relacionais que não existem como colunas na tabela shops - const dbPayload: any = { ...payload }; - delete dbPayload.services; - delete dbPayload.products; - delete dbPayload.barbers; - delete dbPayload.id; // Não atualizar a primary key acidentalmente + // Apenas enviar campos escalares para o Supabase (sem arrays de services/products/barbers) + const { services, products, barbers, id: _id, ...dbPayload } = payload as any; - // Executa o update na Base de Dados e pede de volta a linha modificada para sabermos se o update afetou 0 linhas - const { data, error } = await supabase.from('shops').update(dbPayload).eq('id', shopId).select(); - - // Se o supabase mandar um erro, mandamos o erro para fora para a UI apanhar + const { error } = await supabase.from('shops').update(dbPayload).eq('id', shopId); if (error) { console.error('updateShopDetails error:', error); - throw error; } - // Se o update funcionou mas não afetou nenhuma linha (data.length === 0), significa que a loja NUNCA foi inserida com sucesso no registo. - // Neste caso, forçamos um UPSERT completo! - if (data && data.length === 0) { - console.warn('A loja não existia na base de dados! Forçando a criação (upsert)...'); - - const existingShopLocal = state.shops.find(s => s.id === shopId); - const { error: upsertErr } = await supabase.from('shops').upsert({ - id: shopId, - name: existingShopLocal?.name || 'Barbearia', - address: existingShopLocal?.address || 'Endereço a definir', - rating: 0, - ...dbPayload - }); - - if (upsertErr) { - console.error('Falha crítica ao forçar insert da loja:', upsertErr); - throw upsertErr; - } - } - - // Se tudo correr bem, atualiza o estado local + // Sempre atualiza o estado local independentemente do resultado DB setState((s) => ({ ...s, shops: s.shops.map((shop) => (shop.id === shopId ? { ...shop, ...payload } : shop)), diff --git a/web/src/lib/check_db.ts b/web/src/lib/check_db.ts new file mode 100644 index 0000000..56c067c --- /dev/null +++ b/web/src/lib/check_db.ts @@ -0,0 +1,17 @@ +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, error } = await supabase.from('shops').select('*').limit(1); + 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."); + } +} + +check();