diff --git a/src/lib/submissionItemsService.ts b/src/lib/submissionItemsService.ts index c95bab15..e4530121 100644 --- a/src/lib/submissionItemsService.ts +++ b/src/lib/submissionItemsService.ts @@ -240,6 +240,13 @@ export async function approveSubmissionItems( /** * Create version history for approved submission item + * + * NOTE: Versions are now created automatically via database triggers. + * This function is no longer needed since the relational versioning system + * handles version creation automatically when entities are inserted/updated. + * + * The trigger `create_relational_version()` reads session variables set by + * the edge function and creates versions in the appropriate `*_versions` table. */ async function createVersionForApprovedItem( itemType: string, @@ -248,50 +255,14 @@ async function createVersionForApprovedItem( submissionId: string, isEdit: boolean ): Promise { - const { captureCurrentState, createEntityVersion } = await import('./versioningHelpers'); - - // Map item_type to entity_type - let entityType: 'park' | 'ride' | 'company' | 'ride_model'; - switch (itemType) { - case 'park': - entityType = 'park'; - break; - case 'ride': - entityType = 'ride'; - break; - case 'manufacturer': - case 'operator': - case 'property_owner': - case 'designer': - entityType = 'company'; - break; - case 'ride_model': - entityType = 'ride_model'; - break; - default: - console.warn(`Unknown entity type for versioning: ${itemType}`); - return; - } - - // Capture current state - const currentState = await captureCurrentState(entityType, entityId); - if (!currentState) { - console.warn(`Failed to capture state for ${entityType} ${entityId}`); - return; - } - - // Create version - await createEntityVersion({ - entityType, - entityId, - versionData: currentState, - changedBy: userId, - changeReason: isEdit - ? `Approved edit from submission #${submissionId.slice(0, 8)}` - : `Created via submission #${submissionId.slice(0, 8)}`, - submissionId, - changeType: isEdit ? 'updated' : 'created', - }); + // No-op: Versions are created automatically by triggers + // The edge function sets: + // - app.current_user_id = original submitter + // - app.submission_id = submission ID + // Then the trigger creates the version automatically + console.debug( + `Version will be created automatically by trigger for ${itemType} ${entityId}` + ); } /** diff --git a/supabase/migrations/20251015181416_8351b769-6a25-4893-be6b-d6e3aec3fb8d.sql b/supabase/migrations/20251015181416_8351b769-6a25-4893-be6b-d6e3aec3fb8d.sql new file mode 100644 index 00000000..8fde611f --- /dev/null +++ b/supabase/migrations/20251015181416_8351b769-6a25-4893-be6b-d6e3aec3fb8d.sql @@ -0,0 +1,15 @@ +-- Enable RLS on the archive table +ALTER TABLE public.entity_versions_archive ENABLE ROW LEVEL SECURITY; + +-- Create RLS policies for the archive table (same as original) +CREATE POLICY "Moderators can view all archived versions" + ON public.entity_versions_archive + FOR SELECT + TO authenticated + USING (is_moderator(auth.uid())); + +CREATE POLICY "Public can view current archived versions" + ON public.entity_versions_archive + FOR SELECT + TO authenticated + USING (is_current = true); \ No newline at end of file