feat: Implement shop ID association for 'barbearia' users and refine shop data queries.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
};
|
||||
|
||||
16
test.mjs
Normal file
16
test.mjs
Normal file
@@ -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();
|
||||
@@ -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,
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user