.
This commit is contained in:
@@ -123,17 +123,10 @@ export const AppProvider = ({ children }: { children: React.ReactNode }) => {
|
||||
}));
|
||||
|
||||
setState((s) => {
|
||||
// A BD é a fonte de verdade.
|
||||
// Para a loja do utilizador atual: se ficou na BD com dados antigos,
|
||||
// aplicar por cima o que o utilizador alterou nesta sessão.
|
||||
const mergedShops: BarberShop[] = fetchedShops.map((fetchedShop) => {
|
||||
const localVersion = s.shops.find(ls => ls.id === fetchedShop.id);
|
||||
if (localVersion && s.user?.shopId === fetchedShop.id) {
|
||||
// Dar prioridade ao que o utilizador alterou localmente nesta sessão
|
||||
return { ...fetchedShop, ...localVersion };
|
||||
}
|
||||
return fetchedShop;
|
||||
});
|
||||
// A BD é agora a única fonte de verdade.
|
||||
// Como o CRUD já insere na BD antes do refresh, a sobreposição com 'localVersion' foi
|
||||
// eliminada para impedir a supressão das queries que vêm da Base de Dados.
|
||||
const mergedShops: BarberShop[] = [...fetchedShops];
|
||||
|
||||
// Se a loja do utilizador NÃO está na BD (novo registo ainda não guardado),
|
||||
// adicionar apenas essa loja da memória local — SEM adicionar outras.
|
||||
|
||||
@@ -6,15 +6,35 @@ const supabaseAnonKey = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYm
|
||||
const supabase = createClient(supabaseUrl, supabaseAnonKey);
|
||||
|
||||
async function check() {
|
||||
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("Checking insert...");
|
||||
|
||||
const { data: sCols } = await supabase.from('services').select('*').limit(1);
|
||||
console.log("Services columns:", sCols ? Object.keys(sCols[0] || {}) : "No data");
|
||||
// Auth with a test user or just try anon insert.
|
||||
// If it requires user login, we can login with the previously created user: test_shop_1773154696588@test.com
|
||||
const { data: authData, error: authErr } = await supabase.auth.signInWithPassword({
|
||||
email: 'test_shop_1773154696588@test.com',
|
||||
password: 'Password123!'
|
||||
});
|
||||
|
||||
const { data: pCols } = await supabase.from('products').select('*').limit(1);
|
||||
console.log("Products columns:", pCols ? Object.keys(pCols[0] || {}) : "No data");
|
||||
if (authErr && !authData?.user) {
|
||||
console.error("Login err:", authErr);
|
||||
}
|
||||
|
||||
const userId = authData.user?.id;
|
||||
const { data: shops } = await supabase.from('shops').select('id').eq('owner_id', userId).limit(1);
|
||||
if (!shops || shops.length === 0) {
|
||||
console.error("No shops found for user", userId);
|
||||
return;
|
||||
}
|
||||
|
||||
const shopId = shops[0].id;
|
||||
|
||||
console.log("Attempting to insert service to shop:", shopId);
|
||||
|
||||
const { data, error } = await supabase.from('services').insert([
|
||||
{ shop_id: shopId, name: 'Test Service', price: 10, duration: 15 }
|
||||
]).select();
|
||||
|
||||
console.log("Insert result:", { data, error });
|
||||
}
|
||||
|
||||
check();
|
||||
|
||||
Reference in New Issue
Block a user