mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-20 11:51:14 -05:00
feat: Implement contact submission archiving and deletion
This commit is contained in:
@@ -498,6 +498,13 @@ export type Database = {
|
|||||||
referencedRelation: "contact_submissions"
|
referencedRelation: "contact_submissions"
|
||||||
referencedColumns: ["id"]
|
referencedColumns: ["id"]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
foreignKeyName: "fk_submission_cascade"
|
||||||
|
columns: ["submission_id"]
|
||||||
|
isOneToOne: false
|
||||||
|
referencedRelation: "contact_submissions"
|
||||||
|
referencedColumns: ["id"]
|
||||||
|
},
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
contact_rate_limits: {
|
contact_rate_limits: {
|
||||||
@@ -524,6 +531,8 @@ export type Database = {
|
|||||||
contact_submissions: {
|
contact_submissions: {
|
||||||
Row: {
|
Row: {
|
||||||
admin_notes: string | null
|
admin_notes: string | null
|
||||||
|
archived_at: string | null
|
||||||
|
archived_by: string | null
|
||||||
assigned_to: string | null
|
assigned_to: string | null
|
||||||
category: string
|
category: string
|
||||||
created_at: string
|
created_at: string
|
||||||
@@ -549,6 +558,8 @@ export type Database = {
|
|||||||
}
|
}
|
||||||
Insert: {
|
Insert: {
|
||||||
admin_notes?: string | null
|
admin_notes?: string | null
|
||||||
|
archived_at?: string | null
|
||||||
|
archived_by?: string | null
|
||||||
assigned_to?: string | null
|
assigned_to?: string | null
|
||||||
category: string
|
category: string
|
||||||
created_at?: string
|
created_at?: string
|
||||||
@@ -574,6 +585,8 @@ export type Database = {
|
|||||||
}
|
}
|
||||||
Update: {
|
Update: {
|
||||||
admin_notes?: string | null
|
admin_notes?: string | null
|
||||||
|
archived_at?: string | null
|
||||||
|
archived_by?: string | null
|
||||||
assigned_to?: string | null
|
assigned_to?: string | null
|
||||||
category?: string
|
category?: string
|
||||||
created_at?: string
|
created_at?: string
|
||||||
|
|||||||
@@ -21,6 +21,10 @@ import {
|
|||||||
User,
|
User,
|
||||||
Award,
|
Award,
|
||||||
TrendingUp,
|
TrendingUp,
|
||||||
|
Archive,
|
||||||
|
ArchiveRestore,
|
||||||
|
Trash2,
|
||||||
|
AlertTriangle,
|
||||||
} from 'lucide-react';
|
} from 'lucide-react';
|
||||||
import { supabase } from '@/integrations/supabase/client';
|
import { supabase } from '@/integrations/supabase/client';
|
||||||
import { Button } from '@/components/ui/button';
|
import { Button } from '@/components/ui/button';
|
||||||
@@ -48,6 +52,17 @@ import {
|
|||||||
DialogHeader,
|
DialogHeader,
|
||||||
DialogTitle,
|
DialogTitle,
|
||||||
} from '@/components/ui/dialog';
|
} from '@/components/ui/dialog';
|
||||||
|
import {
|
||||||
|
AlertDialog,
|
||||||
|
AlertDialogAction,
|
||||||
|
AlertDialogCancel,
|
||||||
|
AlertDialogContent,
|
||||||
|
AlertDialogDescription,
|
||||||
|
AlertDialogFooter,
|
||||||
|
AlertDialogHeader,
|
||||||
|
AlertDialogTitle,
|
||||||
|
AlertDialogTrigger,
|
||||||
|
} from '@/components/ui/alert-dialog';
|
||||||
import { Badge } from '@/components/ui/badge';
|
import { Badge } from '@/components/ui/badge';
|
||||||
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs';
|
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs';
|
||||||
import { ScrollArea } from '@/components/ui/scroll-area';
|
import { ScrollArea } from '@/components/ui/scroll-area';
|
||||||
@@ -93,6 +108,8 @@ interface ContactSubmission {
|
|||||||
last_admin_response_at: string | null;
|
last_admin_response_at: string | null;
|
||||||
response_count: number;
|
response_count: number;
|
||||||
ticket_number: string;
|
ticket_number: string;
|
||||||
|
archived_at: string | null;
|
||||||
|
archived_by: string | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface EmailThread {
|
interface EmailThread {
|
||||||
@@ -123,16 +140,24 @@ export default function AdminContact() {
|
|||||||
const [copiedTicket, setCopiedTicket] = useState<string | null>(null);
|
const [copiedTicket, setCopiedTicket] = useState<string | null>(null);
|
||||||
const [activeTab, setActiveTab] = useState<string>('details');
|
const [activeTab, setActiveTab] = useState<string>('details');
|
||||||
const [replyStatus, setReplyStatus] = useState<string>('');
|
const [replyStatus, setReplyStatus] = useState<string>('');
|
||||||
|
const [showArchived, setShowArchived] = useState(false);
|
||||||
|
|
||||||
// Fetch contact submissions
|
// Fetch contact submissions
|
||||||
const { data: submissions, isLoading } = useQuery({
|
const { data: submissions, isLoading } = useQuery({
|
||||||
queryKey: ['admin-contact-submissions', statusFilter, categoryFilter, searchQuery],
|
queryKey: ['admin-contact-submissions', statusFilter, categoryFilter, searchQuery, showArchived],
|
||||||
queryFn: async () => {
|
queryFn: async () => {
|
||||||
let query = supabase
|
let query = supabase
|
||||||
.from('contact_submissions')
|
.from('contact_submissions')
|
||||||
.select('*')
|
.select('*')
|
||||||
.order('created_at', { ascending: false });
|
.order('created_at', { ascending: false });
|
||||||
|
|
||||||
|
// Filter archived based on toggle
|
||||||
|
if (showArchived) {
|
||||||
|
query = query.not('archived_at', 'is', null);
|
||||||
|
} else {
|
||||||
|
query = query.is('archived_at', null);
|
||||||
|
}
|
||||||
|
|
||||||
if (statusFilter !== 'all') {
|
if (statusFilter !== 'all') {
|
||||||
query = query.eq('status', statusFilter);
|
query = query.eq('status', statusFilter);
|
||||||
}
|
}
|
||||||
@@ -290,6 +315,73 @@ export default function AdminContact() {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Archive submission mutation
|
||||||
|
const archiveSubmissionMutation = useMutation({
|
||||||
|
mutationFn: async (id: string) => {
|
||||||
|
const { data: { user } } = await supabase.auth.getUser();
|
||||||
|
const { error } = await supabase
|
||||||
|
.from('contact_submissions')
|
||||||
|
.update({
|
||||||
|
archived_at: new Date().toISOString(),
|
||||||
|
archived_by: user?.id
|
||||||
|
})
|
||||||
|
.eq('id', id);
|
||||||
|
|
||||||
|
if (error) throw error;
|
||||||
|
},
|
||||||
|
onSuccess: () => {
|
||||||
|
queryClient.invalidateQueries({ queryKey: ['admin-contact-submissions'] });
|
||||||
|
handleSuccess('Archived', 'Contact submission has been archived');
|
||||||
|
setSelectedSubmission(null);
|
||||||
|
},
|
||||||
|
onError: (error) => {
|
||||||
|
handleError(error, { action: 'archive_contact_submission' });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Unarchive submission mutation
|
||||||
|
const unarchiveSubmissionMutation = useMutation({
|
||||||
|
mutationFn: async (id: string) => {
|
||||||
|
const { error } = await supabase
|
||||||
|
.from('contact_submissions')
|
||||||
|
.update({
|
||||||
|
archived_at: null,
|
||||||
|
archived_by: null
|
||||||
|
})
|
||||||
|
.eq('id', id);
|
||||||
|
|
||||||
|
if (error) throw error;
|
||||||
|
},
|
||||||
|
onSuccess: () => {
|
||||||
|
queryClient.invalidateQueries({ queryKey: ['admin-contact-submissions'] });
|
||||||
|
handleSuccess('Restored', 'Contact submission has been restored from archive');
|
||||||
|
setSelectedSubmission(null);
|
||||||
|
},
|
||||||
|
onError: (error) => {
|
||||||
|
handleError(error, { action: 'unarchive_contact_submission' });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Delete submission mutation
|
||||||
|
const deleteSubmissionMutation = useMutation({
|
||||||
|
mutationFn: async (id: string) => {
|
||||||
|
const { error } = await supabase
|
||||||
|
.from('contact_submissions')
|
||||||
|
.delete()
|
||||||
|
.eq('id', id);
|
||||||
|
|
||||||
|
if (error) throw error;
|
||||||
|
},
|
||||||
|
onSuccess: () => {
|
||||||
|
queryClient.invalidateQueries({ queryKey: ['admin-contact-submissions'] });
|
||||||
|
handleSuccess('Deleted', 'Contact submission has been permanently deleted');
|
||||||
|
setSelectedSubmission(null);
|
||||||
|
},
|
||||||
|
onError: (error) => {
|
||||||
|
handleError(error, { action: 'delete_contact_submission' });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
const handleUpdateStatus = (status: string) => {
|
const handleUpdateStatus = (status: string) => {
|
||||||
if (!selectedSubmission) return;
|
if (!selectedSubmission) return;
|
||||||
|
|
||||||
@@ -426,9 +518,10 @@ export default function AdminContact() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const stats = {
|
const stats = {
|
||||||
pending: submissions?.filter((s) => s.status === 'pending').length || 0,
|
pending: submissions?.filter((s) => s.status === 'pending' && !s.archived_at).length || 0,
|
||||||
inProgress: submissions?.filter((s) => s.status === 'in_progress').length || 0,
|
inProgress: submissions?.filter((s) => s.status === 'in_progress' && !s.archived_at).length || 0,
|
||||||
resolved: submissions?.filter((s) => s.status === 'resolved').length || 0,
|
resolved: submissions?.filter((s) => s.status === 'resolved' && !s.archived_at).length || 0,
|
||||||
|
archived: submissions?.filter((s) => s.archived_at !== null).length || 0,
|
||||||
total: submissions?.length || 0,
|
total: submissions?.length || 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -442,6 +535,14 @@ export default function AdminContact() {
|
|||||||
Manage and respond to user contact form submissions
|
Manage and respond to user contact form submissions
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<Button
|
||||||
|
variant={showArchived ? "default" : "outline"}
|
||||||
|
onClick={() => setShowArchived(!showArchived)}
|
||||||
|
>
|
||||||
|
{showArchived ? <ArchiveRestore className="h-4 w-4 mr-2" /> : <Archive className="h-4 w-4 mr-2" />}
|
||||||
|
{showArchived ? 'View Active' : 'View Archived'}
|
||||||
|
</Button>
|
||||||
<Button
|
<Button
|
||||||
variant="outline"
|
variant="outline"
|
||||||
size="icon"
|
size="icon"
|
||||||
@@ -452,9 +553,10 @@ export default function AdminContact() {
|
|||||||
<RefreshCw className={`h-4 w-4 ${isLoading ? 'animate-spin' : ''}`} />
|
<RefreshCw className={`h-4 w-4 ${isLoading ? 'animate-spin' : ''}`} />
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
{/* Stats Cards */}
|
{/* Stats Cards */}
|
||||||
<div className="grid grid-cols-1 md:grid-cols-4 gap-4 mb-8">
|
<div className="grid grid-cols-1 md:grid-cols-5 gap-4 mb-8">
|
||||||
<Card>
|
<Card>
|
||||||
<CardHeader className="pb-3">
|
<CardHeader className="pb-3">
|
||||||
<CardDescription>Pending</CardDescription>
|
<CardDescription>Pending</CardDescription>
|
||||||
@@ -473,6 +575,12 @@ export default function AdminContact() {
|
|||||||
<CardTitle className="text-3xl">{stats.resolved}</CardTitle>
|
<CardTitle className="text-3xl">{stats.resolved}</CardTitle>
|
||||||
</CardHeader>
|
</CardHeader>
|
||||||
</Card>
|
</Card>
|
||||||
|
<Card>
|
||||||
|
<CardHeader className="pb-3">
|
||||||
|
<CardDescription>Archived</CardDescription>
|
||||||
|
<CardTitle className="text-3xl">{stats.archived}</CardTitle>
|
||||||
|
</CardHeader>
|
||||||
|
</Card>
|
||||||
<Card>
|
<Card>
|
||||||
<CardHeader className="pb-3">
|
<CardHeader className="pb-3">
|
||||||
<CardDescription>Total</CardDescription>
|
<CardDescription>Total</CardDescription>
|
||||||
@@ -574,6 +682,12 @@ export default function AdminContact() {
|
|||||||
</Badge>
|
</Badge>
|
||||||
{getStatusBadge(submission.status)}
|
{getStatusBadge(submission.status)}
|
||||||
<Badge variant="outline">{getCategoryLabel(submission.category)}</Badge>
|
<Badge variant="outline">{getCategoryLabel(submission.category)}</Badge>
|
||||||
|
{submission.archived_at && (
|
||||||
|
<Badge variant="outline" className="bg-muted">
|
||||||
|
<Archive className="h-3 w-3 mr-1" />
|
||||||
|
Archived
|
||||||
|
</Badge>
|
||||||
|
)}
|
||||||
{submission.response_count > 0 && (
|
{submission.response_count > 0 && (
|
||||||
<Badge variant="secondary" className="text-xs">
|
<Badge variant="secondary" className="text-xs">
|
||||||
📧 {submission.response_count} {submission.response_count === 1 ? 'reply' : 'replies'}
|
📧 {submission.response_count} {submission.response_count === 1 ? 'reply' : 'replies'}
|
||||||
@@ -815,6 +929,71 @@ export default function AdminContact() {
|
|||||||
Save Notes & Status
|
Save Notes & Status
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{/* Danger Zone - Archive/Delete */}
|
||||||
|
<div className="mt-8 pt-6 border-t border-destructive/20">
|
||||||
|
<h4 className="text-sm font-semibold mb-3 text-destructive flex items-center gap-2">
|
||||||
|
<AlertTriangle className="h-4 w-4" />
|
||||||
|
Danger Zone
|
||||||
|
</h4>
|
||||||
|
<div className="flex items-center gap-3">
|
||||||
|
{!showArchived ? (
|
||||||
|
<>
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
onClick={() => archiveSubmissionMutation.mutate(selectedSubmission.id)}
|
||||||
|
disabled={archiveSubmissionMutation.isPending}
|
||||||
|
className="flex-1"
|
||||||
|
>
|
||||||
|
{archiveSubmissionMutation.isPending && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
|
||||||
|
<Archive className="mr-2 h-4 w-4" />
|
||||||
|
Archive Submission
|
||||||
|
</Button>
|
||||||
|
<AlertDialog>
|
||||||
|
<AlertDialogTrigger asChild>
|
||||||
|
<Button
|
||||||
|
variant="destructive"
|
||||||
|
className="flex-1"
|
||||||
|
>
|
||||||
|
<Trash2 className="mr-2 h-4 w-4" />
|
||||||
|
Permanent Delete
|
||||||
|
</Button>
|
||||||
|
</AlertDialogTrigger>
|
||||||
|
<AlertDialogContent>
|
||||||
|
<AlertDialogHeader>
|
||||||
|
<AlertDialogTitle>Are you absolutely sure?</AlertDialogTitle>
|
||||||
|
<AlertDialogDescription>
|
||||||
|
This will permanently delete the contact submission and all associated email threads.
|
||||||
|
This action cannot be undone. Consider archiving instead.
|
||||||
|
</AlertDialogDescription>
|
||||||
|
</AlertDialogHeader>
|
||||||
|
<AlertDialogFooter>
|
||||||
|
<AlertDialogCancel>Cancel</AlertDialogCancel>
|
||||||
|
<AlertDialogAction
|
||||||
|
onClick={() => deleteSubmissionMutation.mutate(selectedSubmission.id)}
|
||||||
|
className="bg-destructive text-destructive-foreground hover:bg-destructive/90"
|
||||||
|
>
|
||||||
|
{deleteSubmissionMutation.isPending && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
|
||||||
|
Yes, Delete Permanently
|
||||||
|
</AlertDialogAction>
|
||||||
|
</AlertDialogFooter>
|
||||||
|
</AlertDialogContent>
|
||||||
|
</AlertDialog>
|
||||||
|
</>
|
||||||
|
) : (
|
||||||
|
<Button
|
||||||
|
variant="default"
|
||||||
|
onClick={() => unarchiveSubmissionMutation.mutate(selectedSubmission.id)}
|
||||||
|
disabled={unarchiveSubmissionMutation.isPending}
|
||||||
|
className="flex-1"
|
||||||
|
>
|
||||||
|
{unarchiveSubmissionMutation.isPending && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
|
||||||
|
<ArchiveRestore className="mr-2 h-4 w-4" />
|
||||||
|
Restore from Archive
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</TabsContent>
|
</TabsContent>
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
-- Add archive support columns to contact_submissions
|
||||||
|
ALTER TABLE contact_submissions
|
||||||
|
ADD COLUMN archived_at timestamp with time zone,
|
||||||
|
ADD COLUMN archived_by uuid REFERENCES auth.users(id);
|
||||||
|
|
||||||
|
-- Add comments for clarity
|
||||||
|
COMMENT ON COLUMN contact_submissions.archived_at IS 'Timestamp when submission was archived (soft deleted)';
|
||||||
|
COMMENT ON COLUMN contact_submissions.archived_by IS 'User who archived this submission';
|
||||||
|
|
||||||
|
-- Index for performance on archived queries
|
||||||
|
CREATE INDEX idx_contact_submissions_archived ON contact_submissions(archived_at)
|
||||||
|
WHERE archived_at IS NOT NULL;
|
||||||
|
|
||||||
|
-- Add FK constraint to ensure email threads are deleted with submission (cascade)
|
||||||
|
ALTER TABLE contact_email_threads
|
||||||
|
ADD CONSTRAINT fk_submission_cascade
|
||||||
|
FOREIGN KEY (submission_id)
|
||||||
|
REFERENCES contact_submissions(id)
|
||||||
|
ON DELETE CASCADE;
|
||||||
|
|
||||||
|
-- Add comment on constraint
|
||||||
|
COMMENT ON CONSTRAINT fk_submission_cascade ON contact_email_threads IS
|
||||||
|
'Cascade delete email threads when parent submission is deleted';
|
||||||
|
|
||||||
|
-- Allow moderators to DELETE contact submissions (hard delete with MFA)
|
||||||
|
CREATE POLICY "Moderators can delete contact submissions" ON contact_submissions
|
||||||
|
FOR DELETE
|
||||||
|
TO authenticated
|
||||||
|
USING (
|
||||||
|
is_moderator(auth.uid()) AND has_aal2()
|
||||||
|
);
|
||||||
Reference in New Issue
Block a user