Approve database migration

This commit is contained in:
gpt-engineer-app[bot]
2025-10-15 19:29:06 +00:00
parent 359a156260
commit 444634dc85
9 changed files with 1020 additions and 0 deletions

View File

@@ -290,6 +290,9 @@ serve(async (req) => {
await deletePhoto(supabase, resolvedData);
entityId = resolvedData.photo_id;
break;
case 'timeline_event':
entityId = await createTimelineEvent(supabase, resolvedData, submitterId, authenticatedUserId, submissionId);
break;
default:
throw new Error(`Unknown item type: ${item.item_type}`);
}
@@ -872,3 +875,42 @@ async function deletePhoto(supabase: any, data: any): Promise<void> {
if (error) throw new Error(`Failed to delete photo: ${error.message}`);
}
async function createTimelineEvent(
supabase: any,
data: any,
submitterId: string,
approvingUserId: string,
submissionId: string
): Promise<string> {
console.log('Creating timeline event');
const eventData = {
entity_id: data.entity_id,
entity_type: data.entity_type,
event_type: data.event_type,
event_date: data.event_date,
event_date_precision: data.event_date_precision,
title: data.title,
description: data.description,
from_value: data.from_value,
to_value: data.to_value,
from_entity_id: data.from_entity_id,
to_entity_id: data.to_entity_id,
from_location_id: data.from_location_id,
to_location_id: data.to_location_id,
is_public: data.is_public ?? true,
created_by: submitterId,
approved_by: approvingUserId,
submission_id: submissionId,
};
const { data: event, error } = await supabase
.from('entity_timeline_events')
.insert(eventData)
.select('id')
.single();
if (error) throw new Error(`Failed to create timeline event: ${error.message}`);
return event.id;
}

View File

@@ -0,0 +1,66 @@
-- Create entity_timeline_events table for historical milestones
CREATE TABLE public.entity_timeline_events (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
entity_id UUID NOT NULL,
entity_type TEXT NOT NULL CHECK (entity_type IN ('park', 'ride', 'company')),
event_type TEXT NOT NULL CHECK (event_type IN (
'name_change',
'operator_change',
'owner_change',
'location_change',
'status_change',
'closure',
'reopening',
'renovation',
'expansion',
'acquisition',
'milestone',
'other'
)),
event_date DATE NOT NULL,
event_date_precision TEXT DEFAULT 'day' CHECK (event_date_precision IN ('day', 'month', 'year')),
title TEXT NOT NULL,
description TEXT,
-- Type-specific relational data (NO JSON!)
from_value TEXT,
to_value TEXT,
from_entity_id UUID,
to_entity_id UUID,
from_location_id UUID REFERENCES public.locations(id),
to_location_id UUID REFERENCES public.locations(id),
-- Metadata
is_public BOOLEAN NOT NULL DEFAULT true,
display_order INTEGER DEFAULT 0,
created_by UUID REFERENCES auth.users(id),
approved_by UUID,
submission_id UUID REFERENCES public.content_submissions(id),
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now()
);
-- Create indexes for performance
CREATE INDEX idx_timeline_events_entity ON public.entity_timeline_events(entity_type, entity_id);
CREATE INDEX idx_timeline_events_date ON public.entity_timeline_events(event_date DESC);
CREATE INDEX idx_timeline_events_submission ON public.entity_timeline_events(submission_id) WHERE submission_id IS NOT NULL;
CREATE INDEX idx_timeline_events_created_by ON public.entity_timeline_events(created_by);
-- Enable RLS
ALTER TABLE public.entity_timeline_events ENABLE ROW LEVEL SECURITY;
-- Public can view approved public events
CREATE POLICY "Public can view public timeline events"
ON public.entity_timeline_events FOR SELECT
USING (is_public = true AND approved_by IS NOT NULL);
-- Users can view their own pending submissions
CREATE POLICY "Users can view their own timeline submissions"
ON public.entity_timeline_events FOR SELECT
USING (created_by = auth.uid() AND approved_by IS NULL);
-- Service role can manage (for approval process)
CREATE POLICY "Service role can manage timeline events"
ON public.entity_timeline_events FOR ALL
USING (auth.jwt() ->> 'role' = 'service_role');