Update submission_metadata queries

Enhance frontend to correctly fetch entity names by joining with submission_metadata and filtering metadata_key = 'name'; replace incorrect submission_metadata(name) usage in systemActivityService.ts and Profile.tsx with proper inner join and metadata_value extraction.
This commit is contained in:
gpt-engineer-app[bot]
2025-11-12 01:53:51 +00:00
parent 78e29f9e49
commit 888ef0224a
3 changed files with 138 additions and 7 deletions

View File

@@ -368,7 +368,7 @@ export async function fetchSystemActivities(
} }
// Fetch submission reviews (approved/rejected submissions) // Fetch submission reviews (approved/rejected submissions)
// Note: Content is now in submission_metadata table, but entity_name is cached in view // Note: Content is now in submission_metadata table - need to join and filter properly
const { data: submissions, error: submissionsError } = await supabase const { data: submissions, error: submissionsError } = await supabase
.from('content_submissions') .from('content_submissions')
.select(` .select(`
@@ -377,8 +377,9 @@ export async function fetchSystemActivities(
status, status,
reviewer_id, reviewer_id,
reviewed_at, reviewed_at,
submission_metadata(name) submission_metadata!inner(metadata_value)
`) `)
.eq('submission_metadata.metadata_key', 'name')
.not('reviewed_at', 'is', null) .not('reviewed_at', 'is', null)
.in('status', ['approved', 'rejected', 'partially_approved']) .in('status', ['approved', 'rejected', 'partially_approved'])
.order('reviewed_at', { ascending: false }) .order('reviewed_at', { ascending: false })
@@ -415,10 +416,10 @@ export async function fetchSystemActivities(
); );
for (const submission of submissions) { for (const submission of submissions) {
// Get name from submission_metadata // Get name from submission_metadata - extract metadata_value from the joined result
const metadata = submission.submission_metadata as any; const metadata = submission.submission_metadata as any;
const entityName = Array.isArray(metadata) && metadata.length > 0 const entityName = Array.isArray(metadata) && metadata.length > 0
? metadata[0]?.name ? metadata[0]?.metadata_value
: undefined; : undefined;
const submissionItem = itemsMap.get(submission.id); const submissionItem = itemsMap.get(submission.id);

View File

@@ -291,8 +291,9 @@ export default function Profile() {
submission_type, submission_type,
status, status,
created_at, created_at,
submission_metadata(name) submission_metadata!inner(metadata_value)
`) `)
.eq('submission_metadata.metadata_key', 'name')
.eq('user_id', userId) .eq('user_id', userId)
.order('created_at', { ascending: false }) .order('created_at', { ascending: false })
.limit(10); .limit(10);
@@ -310,10 +311,10 @@ export default function Profile() {
const enrichedSubmissions = await Promise.all((submissions || []).map(async (sub) => { const enrichedSubmissions = await Promise.all((submissions || []).map(async (sub) => {
const enriched: any = { ...sub }; const enriched: any = { ...sub };
// Get name from submission_metadata // Get name from submission_metadata - extract metadata_value from the joined result
const metadata = sub.submission_metadata as any; const metadata = sub.submission_metadata as any;
enriched.name = Array.isArray(metadata) && metadata.length > 0 enriched.name = Array.isArray(metadata) && metadata.length > 0
? metadata[0]?.name ? metadata[0]?.metadata_value
: undefined; : undefined;
// For photo submissions, get photo count and preview // For photo submissions, get photo count and preview

View File

@@ -0,0 +1,129 @@
-- Fix get_recent_additions: Remove created_by joins for tables without created_by column
-- Only entity_timeline_events has created_by column, not parks/rides/companies/ride_models/locations
CREATE OR REPLACE FUNCTION public.get_recent_additions(limit_count integer DEFAULT 50)
RETURNS TABLE(entity_id uuid, entity_type text, entity_name text, entity_slug text, park_slug text, image_url text, created_at timestamp with time zone, created_by_id uuid, created_by_username text, created_by_avatar text)
LANGUAGE plpgsql
STABLE SECURITY DEFINER
SET search_path TO 'public'
AS $function$
BEGIN
RETURN QUERY
SELECT * FROM (
-- Parks - FIXED: Removed created_by join (parks table doesn't have created_by column)
SELECT
p.id as entity_id,
'park'::text as entity_type,
p.name as entity_name,
p.slug as entity_slug,
NULL::text as park_slug,
p.card_image_url as image_url,
p.created_at,
NULL::uuid as created_by_id,
NULL::text as created_by_username,
NULL::text as created_by_avatar
FROM parks p
UNION ALL
-- Rides - FIXED: Removed created_by join (rides table doesn't have created_by column)
SELECT
r.id as entity_id,
'ride'::text as entity_type,
r.name as entity_name,
r.slug as entity_slug,
pk.slug as park_slug,
r.card_image_url as image_url,
r.created_at,
NULL::uuid as created_by_id,
NULL::text as created_by_username,
NULL::text as created_by_avatar
FROM rides r
LEFT JOIN parks pk ON pk.id = r.park_id
UNION ALL
-- Companies - FIXED: Removed created_by join (companies table doesn't have created_by column)
SELECT
c.id as entity_id,
'company'::text as entity_type,
c.name as entity_name,
c.slug as entity_slug,
NULL::text as park_slug,
c.card_image_url as image_url,
c.created_at,
NULL::uuid as created_by_id,
NULL::text as created_by_username,
NULL::text as created_by_avatar
FROM companies c
UNION ALL
-- Ride Models - FIXED: Removed created_by join (ride_models table doesn't have created_by column)
SELECT
rm.id as entity_id,
'ride_model'::text as entity_type,
rm.name as entity_name,
rm.slug as entity_slug,
NULL::text as park_slug,
rm.card_image_url as image_url,
rm.created_at,
NULL::uuid as created_by_id,
NULL::text as created_by_username,
NULL::text as created_by_avatar
FROM ride_models rm
UNION ALL
-- Locations - FIXED: Removed created_by join (locations table doesn't have created_by column)
SELECT
l.id as entity_id,
'location'::text as entity_type,
COALESCE(l.city || ', ' || l.country, l.country, 'Location') as entity_name,
NULL::text as entity_slug,
NULL::text as park_slug,
NULL::text as image_url,
l.created_at,
NULL::uuid as created_by_id,
NULL::text as created_by_username,
NULL::text as created_by_avatar
FROM locations l
UNION ALL
-- Timeline Events - KEPT: This table has created_by column
SELECT
te.id as entity_id,
'timeline_event'::text as entity_type,
te.event_title as entity_name,
NULL::text as entity_slug,
NULL::text as park_slug,
NULL::text as image_url,
te.created_at,
te.created_by as created_by_id,
prof.username as created_by_username,
prof.avatar_url as created_by_avatar
FROM entity_timeline_events te
LEFT JOIN profiles prof ON prof.user_id = te.created_by
UNION ALL
-- Photos - KEPT: This table has submitted_by column
SELECT
p.id as entity_id,
'photo'::text as entity_type,
COALESCE(p.title, 'Photo') as entity_name,
NULL::text as entity_slug,
NULL::text as park_slug,
p.cloudflare_image_url as image_url,
p.created_at as created_at,
p.submitted_by as created_by_id,
prof.username as created_by_username,
prof.avatar_url as created_by_avatar
FROM photos p
LEFT JOIN profiles prof ON prof.user_id = p.submitted_by
) combined
ORDER BY created_at DESC
LIMIT limit_count;
END;
$function$;