refactor: Retrieve shop ID from user profiles table instead of directly querying shops by owner ID, and update check_db for verification.

This commit is contained in:
2026-03-10 15:45:03 +00:00
parent 254e9f31c7
commit 13a350676c
3 changed files with 49 additions and 22 deletions

View File

@@ -44,14 +44,15 @@ export const AppProvider = ({ children }: { children: React.ReactNode }) => {
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.
// Vai buscar o shop_id mapeado na tabela profiles
const { data: prof } = await supabase
.from('profiles')
.select('shop_id')
.eq('id', data.user.id)
.single();
shopId = prof?.shop_id || undefined;
setUser({
id: data.user.id,

View File

@@ -166,7 +166,7 @@ export const AppProvider = ({ children }: { children: React.ReactNode }) => {
const applyProfile = async (userId: string, email?: string | null, userMetadata?: any) => {
const { data, error } = await supabase
.from('profiles')
.select('id,name,role,shop_name')
.select('id,name,role,shop_name,shop_id')
.eq('id', userId)
.single();
@@ -181,16 +181,9 @@ export const AppProvider = ({ children }: { children: React.ReactNode }) => {
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.
// O trigger de backend já popula a tabela profiles com o shop_id quando é barbearia
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
shopId = data.shop_id || userId; // Fallback extremo se o trigger falhar
}
let needsInsert = false;

View File

@@ -6,11 +6,44 @@ const supabaseAnonKey = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYm
const supabase = createClient(supabaseUrl, supabaseAnonKey);
async function check() {
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:", shops);
const email = `test_shop_${Date.now()}@test.com`;
const password = 'Password123!';
console.log("Signing up:", email);
const { data: authData, error: authErr } = await supabase.auth.signUp({
email,
password,
options: {
data: {
name: "Test Shop User",
role: "barbearia",
shopName: "Test Shop Automatica",
shop_name: "Test Shop Automatica"
}
}
});
if (authErr) {
console.error("Signup err:", authErr);
return;
}
const userId = authData.user?.id;
console.log("Created user ID:", userId);
// Wait for triggers to complete
await new Promise(r => setTimeout(r, 2000));
const { data: profiles, error: profErr } = await supabase.from('profiles').select('*').eq('id', userId ?? '');
console.log("Created profile:", profiles, profErr || "");
const { data: shops, error: shopErr } = await supabase.from('shops').select('*').eq('owner_id', userId ?? '');
console.log("Created shop (by owner_id):", shops, shopErr || "");
// check if profiles has a shop_id that we can lookup
if (profiles && profiles.length > 0 && profiles[0].shop_id) {
const { data: shopsByProfId } = await supabase.from('shops').select('*').eq('id', profiles[0].shop_id);
console.log("Created shop (by profile shop_id):", shopsByProfId);
}
}