appcontex
This commit is contained in:
18
web/check_db.js
Normal file
18
web/check_db.js
Normal file
@@ -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();
|
||||
@@ -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)),
|
||||
|
||||
17
web/src/lib/check_db.ts
Normal file
17
web/src/lib/check_db.ts
Normal file
@@ -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();
|
||||
Reference in New Issue
Block a user