feat: Implement shop ID association for 'barbearia' users and refine shop data queries.
This commit is contained in:
@@ -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