Fix admin consulting page query
Change from relationship query to manual join to avoid foreign key issues with profiles table 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -74,16 +74,38 @@ export default function AdminConsulting() {
|
|||||||
}, [user, isAdmin, authLoading]);
|
}, [user, isAdmin, authLoading]);
|
||||||
|
|
||||||
const fetchSlots = async () => {
|
const fetchSlots = async () => {
|
||||||
const { data, error } = await supabase
|
// First fetch slots
|
||||||
|
const { data: slotsData, error: slotsError } = await supabase
|
||||||
.from('consulting_slots')
|
.from('consulting_slots')
|
||||||
.select(`
|
.select('*')
|
||||||
*,
|
|
||||||
profiles:user_id (name, email)
|
|
||||||
`)
|
|
||||||
.order('date', { ascending: false })
|
.order('date', { ascending: false })
|
||||||
.order('start_time', { ascending: true });
|
.order('start_time', { ascending: true });
|
||||||
|
|
||||||
if (!error && data) setSlots(data);
|
if (slotsError || !slotsData) {
|
||||||
|
console.error('Error fetching slots:', slotsError);
|
||||||
|
setLoading(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Then fetch profiles for each user_id
|
||||||
|
const userIds = [...new Set(slotsData.map(s => s.user_id))];
|
||||||
|
const { data: profilesData } = await supabase
|
||||||
|
.from('profiles')
|
||||||
|
.select('id, name, email')
|
||||||
|
.in('id', userIds);
|
||||||
|
|
||||||
|
// Create a map for quick lookup
|
||||||
|
const profilesMap = new Map(
|
||||||
|
(profilesData || []).map(p => [p.id, { name: p.name, email: p.email }])
|
||||||
|
);
|
||||||
|
|
||||||
|
// Combine slots with profile data
|
||||||
|
const slotsWithProfiles = slotsData.map(slot => ({
|
||||||
|
...slot,
|
||||||
|
profiles: profilesMap.get(slot.user_id) || null
|
||||||
|
}));
|
||||||
|
|
||||||
|
setSlots(slotsWithProfiles);
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user