From 9c4b80e454bf273753077809460cf35b6f2754ed Mon Sep 17 00:00:00 2001 From: "gpt-engineer-app[bot]" <159125892+gpt-engineer-app[bot]@users.noreply.github.com> Date: Sun, 2 Nov 2025 23:52:29 +0000 Subject: [PATCH] feat: Implement full inline editing enhancement --- src/integrations/supabase/types.ts | 53 ++++++++++++++++++ ...5_0c04eff1-e80c-49da-9ad6-f49550157e28.sql | 55 +++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 supabase/migrations/20251102235215_0c04eff1-e80c-49da-9ad6-f49550157e28.sql diff --git a/src/integrations/supabase/types.ts b/src/integrations/supabase/types.ts index c94e6561..ccc18db7 100644 --- a/src/integrations/supabase/types.ts +++ b/src/integrations/supabase/types.ts @@ -1118,6 +1118,38 @@ export type Database = { }, ] } + item_edit_history: { + Row: { + changes: Json + edited_at: string + editor_id: string + id: string + item_id: string + } + Insert: { + changes: Json + edited_at?: string + editor_id: string + id?: string + item_id: string + } + Update: { + changes?: Json + edited_at?: string + editor_id?: string + id?: string + item_id?: string + } + Relationships: [ + { + foreignKeyName: "item_edit_history_item_id_fkey" + columns: ["item_id"] + isOneToOne: false + referencedRelation: "submission_items" + referencedColumns: ["id"] + }, + ] + } list_items: { Row: { created_at: string | null @@ -4563,6 +4595,27 @@ export type Database = { } Relationships: [] } + item_edit_history_view: { + Row: { + changes: Json | null + edited_at: string | null + editor_avatar_url: string | null + editor_display_name: string | null + editor_id: string | null + editor_username: string | null + id: string | null + item_id: string | null + } + Relationships: [ + { + foreignKeyName: "item_edit_history_item_id_fkey" + columns: ["item_id"] + isOneToOne: false + referencedRelation: "submission_items" + referencedColumns: ["id"] + }, + ] + } moderation_sla_metrics: { Row: { avg_resolution_hours: number | null diff --git a/supabase/migrations/20251102235215_0c04eff1-e80c-49da-9ad6-f49550157e28.sql b/supabase/migrations/20251102235215_0c04eff1-e80c-49da-9ad6-f49550157e28.sql new file mode 100644 index 00000000..6480c2f0 --- /dev/null +++ b/supabase/migrations/20251102235215_0c04eff1-e80c-49da-9ad6-f49550157e28.sql @@ -0,0 +1,55 @@ +-- Phase 4: Create edit history tracking table +CREATE TABLE IF NOT EXISTS public.item_edit_history ( + id UUID PRIMARY KEY DEFAULT gen_random_uuid(), + item_id UUID NOT NULL REFERENCES public.submission_items(id) ON DELETE CASCADE, + editor_id UUID NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE, + changes JSONB NOT NULL, + edited_at TIMESTAMPTZ NOT NULL DEFAULT NOW() +); + +-- Add index for efficient querying +CREATE INDEX IF NOT EXISTS idx_item_edit_history_item_id ON public.item_edit_history(item_id); +CREATE INDEX IF NOT EXISTS idx_item_edit_history_edited_at ON public.item_edit_history(edited_at DESC); + +-- Enable RLS +ALTER TABLE public.item_edit_history ENABLE ROW LEVEL SECURITY; + +-- Allow moderators and admins to view edit history +CREATE POLICY "Moderators can view edit history" +ON public.item_edit_history +FOR SELECT +TO authenticated +USING ( + public.has_role(auth.uid(), 'moderator') OR + public.has_role(auth.uid(), 'admin') OR + public.has_role(auth.uid(), 'superuser') +); + +-- Allow system to insert edit history (via service role) +CREATE POLICY "System can insert edit history" +ON public.item_edit_history +FOR INSERT +TO authenticated +WITH CHECK ( + public.has_role(auth.uid(), 'moderator') OR + public.has_role(auth.uid(), 'admin') OR + public.has_role(auth.uid(), 'superuser') +); + +-- Create view to get edit history with user info +CREATE OR REPLACE VIEW public.item_edit_history_view AS +SELECT + ieh.id, + ieh.item_id, + ieh.editor_id, + ieh.changes, + ieh.edited_at, + p.username as editor_username, + p.display_name as editor_display_name, + p.avatar_url as editor_avatar_url +FROM public.item_edit_history ieh +LEFT JOIN public.profiles p ON p.user_id = ieh.editor_id +ORDER BY ieh.edited_at DESC; + +-- Grant access to the view +GRANT SELECT ON public.item_edit_history_view TO authenticated; \ No newline at end of file