mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-21 21:11:22 -05:00
Reverted to commit cd76e30ed9
This commit is contained in:
@@ -4,10 +4,8 @@ import { Button } from '@/components/ui/button';
|
|||||||
import { RadioGroup, RadioGroupItem } from '@/components/ui/radio-group';
|
import { RadioGroup, RadioGroupItem } from '@/components/ui/radio-group';
|
||||||
import { Label } from '@/components/ui/label';
|
import { Label } from '@/components/ui/label';
|
||||||
import { Alert, AlertDescription } from '@/components/ui/alert';
|
import { Alert, AlertDescription } from '@/components/ui/alert';
|
||||||
import { AlertCircle, Loader2 } from 'lucide-react';
|
import { AlertCircle } from 'lucide-react';
|
||||||
import { toast } from 'sonner';
|
import { type DependencyConflict, type SubmissionItemWithDeps } from '@/lib/submissionItemsService';
|
||||||
import { type DependencyConflict, type SubmissionItemWithDeps, resolveConflicts } from '@/lib/submissionItemsService';
|
|
||||||
import { useAuth } from '@/hooks/useAuth';
|
|
||||||
|
|
||||||
interface ConflictResolutionDialogProps {
|
interface ConflictResolutionDialogProps {
|
||||||
open: boolean;
|
open: boolean;
|
||||||
@@ -24,9 +22,7 @@ export function ConflictResolutionDialog({
|
|||||||
items,
|
items,
|
||||||
onResolve,
|
onResolve,
|
||||||
}: ConflictResolutionDialogProps) {
|
}: ConflictResolutionDialogProps) {
|
||||||
const { user } = useAuth();
|
|
||||||
const [resolutions, setResolutions] = useState<Record<string, string>>({});
|
const [resolutions, setResolutions] = useState<Record<string, string>>({});
|
||||||
const [isProcessing, setIsProcessing] = useState(false);
|
|
||||||
|
|
||||||
const handleResolutionChange = (itemId: string, action: string) => {
|
const handleResolutionChange = (itemId: string, action: string) => {
|
||||||
setResolutions(prev => ({ ...prev, [itemId]: action }));
|
setResolutions(prev => ({ ...prev, [itemId]: action }));
|
||||||
@@ -36,36 +32,10 @@ export function ConflictResolutionDialog({
|
|||||||
conflict => resolutions[conflict.itemId]
|
conflict => resolutions[conflict.itemId]
|
||||||
);
|
);
|
||||||
|
|
||||||
const handleApply = async () => {
|
const handleApply = () => {
|
||||||
if (!user) {
|
// TODO: Apply resolutions
|
||||||
toast.error('You must be logged in to resolve conflicts');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
setIsProcessing(true);
|
|
||||||
|
|
||||||
try {
|
|
||||||
const { updatedItems, newConflicts } = await resolveConflicts(
|
|
||||||
conflicts,
|
|
||||||
resolutions,
|
|
||||||
items,
|
|
||||||
user.id
|
|
||||||
);
|
|
||||||
|
|
||||||
if (newConflicts.length > 0) {
|
|
||||||
toast.error(`Resolution completed with ${newConflicts.length} remaining conflicts`);
|
|
||||||
} else {
|
|
||||||
toast.success('All conflicts resolved successfully');
|
|
||||||
}
|
|
||||||
|
|
||||||
onResolve();
|
onResolve();
|
||||||
onOpenChange(false);
|
onOpenChange(false);
|
||||||
} catch (error) {
|
|
||||||
console.error('Error resolving conflicts:', error);
|
|
||||||
toast.error(error instanceof Error ? error.message : 'Failed to resolve conflicts');
|
|
||||||
} finally {
|
|
||||||
setIsProcessing(false);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -113,12 +83,11 @@ export function ConflictResolutionDialog({
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<DialogFooter>
|
<DialogFooter>
|
||||||
<Button variant="outline" onClick={() => onOpenChange(false)} disabled={isProcessing}>
|
<Button variant="outline" onClick={() => onOpenChange(false)}>
|
||||||
Cancel
|
Cancel
|
||||||
</Button>
|
</Button>
|
||||||
<Button onClick={handleApply} disabled={!allConflictsResolved || isProcessing}>
|
<Button onClick={handleApply} disabled={!allConflictsResolved}>
|
||||||
{isProcessing && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
|
Apply & Approve
|
||||||
{isProcessing ? 'Processing...' : 'Apply & Approve'}
|
|
||||||
</Button>
|
</Button>
|
||||||
</DialogFooter>
|
</DialogFooter>
|
||||||
</DialogContent>
|
</DialogContent>
|
||||||
|
|||||||
@@ -1,215 +0,0 @@
|
|||||||
import { useState } from 'react';
|
|
||||||
import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog';
|
|
||||||
import { Sheet, SheetContent, SheetHeader, SheetTitle } from '@/components/ui/sheet';
|
|
||||||
import { Input } from '@/components/ui/input';
|
|
||||||
import { Label } from '@/components/ui/label';
|
|
||||||
import { Button } from '@/components/ui/button';
|
|
||||||
import { ParkForm } from '@/components/admin/ParkForm';
|
|
||||||
import { RideForm } from '@/components/admin/RideForm';
|
|
||||||
import { ManufacturerForm } from '@/components/admin/ManufacturerForm';
|
|
||||||
import { DesignerForm } from '@/components/admin/DesignerForm';
|
|
||||||
import { OperatorForm } from '@/components/admin/OperatorForm';
|
|
||||||
import { PropertyOwnerForm } from '@/components/admin/PropertyOwnerForm';
|
|
||||||
import { RideModelForm } from '@/components/admin/RideModelForm';
|
|
||||||
import { PhotoCaptionEditor } from '@/components/upload/PhotoCaptionEditor';
|
|
||||||
import { useIsMobile } from '@/hooks/use-mobile';
|
|
||||||
import { editSubmissionItem } from '@/lib/submissionItemsService';
|
|
||||||
import { useAuth } from '@/hooks/useAuth';
|
|
||||||
import { toast } from '@/hooks/use-toast';
|
|
||||||
import type { SubmissionItemWithDeps } from '@/lib/submissionItemsService';
|
|
||||||
|
|
||||||
interface ItemEditDialogProps {
|
|
||||||
open: boolean;
|
|
||||||
onOpenChange: (open: boolean) => void;
|
|
||||||
item: SubmissionItemWithDeps | null;
|
|
||||||
onEditComplete: () => void;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function ItemEditDialog({ open, onOpenChange, item, onEditComplete }: ItemEditDialogProps) {
|
|
||||||
const isMobile = useIsMobile();
|
|
||||||
const { user } = useAuth();
|
|
||||||
const [isSubmitting, setIsSubmitting] = useState(false);
|
|
||||||
|
|
||||||
if (!item) return null;
|
|
||||||
|
|
||||||
const handleFormSubmit = async (data: any) => {
|
|
||||||
if (!user) {
|
|
||||||
toast({
|
|
||||||
title: "Authentication Required",
|
|
||||||
description: "You must be logged in to edit items.",
|
|
||||||
variant: "destructive"
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
setIsSubmitting(true);
|
|
||||||
try {
|
|
||||||
await editSubmissionItem(item.id, data, user.id);
|
|
||||||
|
|
||||||
toast({
|
|
||||||
title: "Item Updated",
|
|
||||||
description: "The submission item has been updated successfully."
|
|
||||||
});
|
|
||||||
|
|
||||||
onOpenChange(false);
|
|
||||||
onEditComplete();
|
|
||||||
} catch (error: any) {
|
|
||||||
toast({
|
|
||||||
title: "Error",
|
|
||||||
description: error.message || "Failed to update submission item.",
|
|
||||||
variant: "destructive"
|
|
||||||
});
|
|
||||||
} finally {
|
|
||||||
setIsSubmitting(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleCancel = () => {
|
|
||||||
if (!isSubmitting) {
|
|
||||||
onOpenChange(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const renderForm = () => {
|
|
||||||
const itemData = item.item_data as any;
|
|
||||||
|
|
||||||
switch (item.item_type) {
|
|
||||||
case 'park':
|
|
||||||
return (
|
|
||||||
<ParkForm
|
|
||||||
initialData={itemData}
|
|
||||||
onSubmit={handleFormSubmit}
|
|
||||||
onCancel={handleCancel}
|
|
||||||
isEditing={true}
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
|
|
||||||
case 'ride':
|
|
||||||
return (
|
|
||||||
<RideForm
|
|
||||||
initialData={itemData}
|
|
||||||
onSubmit={handleFormSubmit}
|
|
||||||
onCancel={handleCancel}
|
|
||||||
isEditing={true}
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
|
|
||||||
case 'manufacturer':
|
|
||||||
return (
|
|
||||||
<ManufacturerForm
|
|
||||||
initialData={itemData}
|
|
||||||
onSubmit={handleFormSubmit}
|
|
||||||
onCancel={handleCancel}
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
|
|
||||||
case 'designer':
|
|
||||||
return (
|
|
||||||
<DesignerForm
|
|
||||||
initialData={itemData}
|
|
||||||
onSubmit={handleFormSubmit}
|
|
||||||
onCancel={handleCancel}
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
|
|
||||||
case 'operator':
|
|
||||||
return (
|
|
||||||
<OperatorForm
|
|
||||||
initialData={itemData}
|
|
||||||
onSubmit={handleFormSubmit}
|
|
||||||
onCancel={handleCancel}
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
|
|
||||||
case 'property_owner':
|
|
||||||
return (
|
|
||||||
<PropertyOwnerForm
|
|
||||||
initialData={itemData}
|
|
||||||
onSubmit={handleFormSubmit}
|
|
||||||
onCancel={handleCancel}
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
|
|
||||||
case 'ride_model':
|
|
||||||
return (
|
|
||||||
<RideModelForm
|
|
||||||
manufacturerName={itemData.manufacturer_name || 'Unknown'}
|
|
||||||
manufacturerId={itemData.manufacturer_id}
|
|
||||||
initialData={itemData}
|
|
||||||
onSubmit={handleFormSubmit}
|
|
||||||
onCancel={handleCancel}
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
|
|
||||||
case 'photo':
|
|
||||||
return (
|
|
||||||
<div className="space-y-4 p-4">
|
|
||||||
<div className="space-y-2">
|
|
||||||
<Label htmlFor="caption">Caption</Label>
|
|
||||||
<Input
|
|
||||||
id="caption"
|
|
||||||
defaultValue={itemData.caption || ''}
|
|
||||||
onChange={(e) => {
|
|
||||||
itemData.caption = e.target.value;
|
|
||||||
}}
|
|
||||||
placeholder="Enter photo caption"
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
{itemData.image_url && (
|
|
||||||
<img
|
|
||||||
src={itemData.image_url}
|
|
||||||
alt="Preview"
|
|
||||||
className="w-full rounded-md"
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
<div className="flex gap-2 justify-end">
|
|
||||||
<Button variant="outline" onClick={handleCancel}>Cancel</Button>
|
|
||||||
<Button onClick={() => handleFormSubmit(itemData)}>Save</Button>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
|
|
||||||
default:
|
|
||||||
return (
|
|
||||||
<div className="p-4 text-center text-muted-foreground">
|
|
||||||
Form not available for item type: {item.item_type}
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const content = (
|
|
||||||
<>
|
|
||||||
{isMobile ? (
|
|
||||||
<SheetHeader>
|
|
||||||
<SheetTitle>Edit {item.item_type.replace('_', ' ')}</SheetTitle>
|
|
||||||
</SheetHeader>
|
|
||||||
) : (
|
|
||||||
<DialogHeader>
|
|
||||||
<DialogTitle>Edit {item.item_type.replace('_', ' ')}</DialogTitle>
|
|
||||||
</DialogHeader>
|
|
||||||
)}
|
|
||||||
<div className="mt-4 max-h-[calc(100vh-150px)] overflow-y-auto">
|
|
||||||
{renderForm()}
|
|
||||||
</div>
|
|
||||||
</>
|
|
||||||
);
|
|
||||||
|
|
||||||
if (isMobile) {
|
|
||||||
return (
|
|
||||||
<Sheet open={open} onOpenChange={onOpenChange}>
|
|
||||||
<SheetContent side="bottom" className="h-[95vh]">
|
|
||||||
{content}
|
|
||||||
</SheetContent>
|
|
||||||
</Sheet>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<Dialog open={open} onOpenChange={onOpenChange}>
|
|
||||||
<DialogContent className="max-w-4xl max-h-[90vh] overflow-y-auto">
|
|
||||||
{content}
|
|
||||||
</DialogContent>
|
|
||||||
</Dialog>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -141,11 +141,6 @@ export function ItemReviewCard({ item, onEdit, onStatusChange }: ItemReviewCardP
|
|||||||
<Edit className="w-3 h-3" />
|
<Edit className="w-3 h-3" />
|
||||||
</Button>
|
</Button>
|
||||||
)}
|
)}
|
||||||
{item.original_data && (
|
|
||||||
<Badge variant="secondary" className="text-xs">
|
|
||||||
Edited
|
|
||||||
</Badge>
|
|
||||||
)}
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</CardHeader>
|
</CardHeader>
|
||||||
|
|||||||
@@ -27,7 +27,6 @@ import { DependencyVisualizer } from './DependencyVisualizer';
|
|||||||
import { ConflictResolutionDialog } from './ConflictResolutionDialog';
|
import { ConflictResolutionDialog } from './ConflictResolutionDialog';
|
||||||
import { EscalationDialog } from './EscalationDialog';
|
import { EscalationDialog } from './EscalationDialog';
|
||||||
import { RejectionDialog } from './RejectionDialog';
|
import { RejectionDialog } from './RejectionDialog';
|
||||||
import { ItemEditDialog } from './ItemEditDialog';
|
|
||||||
|
|
||||||
interface SubmissionReviewManagerProps {
|
interface SubmissionReviewManagerProps {
|
||||||
submissionId: string;
|
submissionId: string;
|
||||||
@@ -49,8 +48,6 @@ export function SubmissionReviewManager({
|
|||||||
const [showConflictDialog, setShowConflictDialog] = useState(false);
|
const [showConflictDialog, setShowConflictDialog] = useState(false);
|
||||||
const [showEscalationDialog, setShowEscalationDialog] = useState(false);
|
const [showEscalationDialog, setShowEscalationDialog] = useState(false);
|
||||||
const [showRejectionDialog, setShowRejectionDialog] = useState(false);
|
const [showRejectionDialog, setShowRejectionDialog] = useState(false);
|
||||||
const [showEditDialog, setShowEditDialog] = useState(false);
|
|
||||||
const [editingItem, setEditingItem] = useState<SubmissionItemWithDeps | null>(null);
|
|
||||||
const [activeTab, setActiveTab] = useState<'items' | 'dependencies'>('items');
|
const [activeTab, setActiveTab] = useState<'items' | 'dependencies'>('items');
|
||||||
|
|
||||||
const { toast } = useToast();
|
const { toast } = useToast();
|
||||||
@@ -243,12 +240,6 @@ export function SubmissionReviewManager({
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleEditComplete = async () => {
|
|
||||||
// Reload items after edit
|
|
||||||
await loadSubmissionItems();
|
|
||||||
setEditingItem(null);
|
|
||||||
};
|
|
||||||
|
|
||||||
const pendingCount = items.filter(item => item.status === 'pending').length;
|
const pendingCount = items.filter(item => item.status === 'pending').length;
|
||||||
const selectedCount = selectedItemIds.size;
|
const selectedCount = selectedItemIds.size;
|
||||||
|
|
||||||
@@ -301,13 +292,6 @@ export function SubmissionReviewManager({
|
|||||||
)}
|
)}
|
||||||
onReject={handleReject}
|
onReject={handleReject}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<ItemEditDialog
|
|
||||||
open={showEditDialog}
|
|
||||||
onOpenChange={setShowEditDialog}
|
|
||||||
item={editingItem}
|
|
||||||
onEditComplete={handleEditComplete}
|
|
||||||
/>
|
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -347,10 +331,7 @@ export function SubmissionReviewManager({
|
|||||||
/>
|
/>
|
||||||
<ItemReviewCard
|
<ItemReviewCard
|
||||||
item={item}
|
item={item}
|
||||||
onEdit={() => {
|
onEdit={() => {/* TODO: Implement editing */}}
|
||||||
setEditingItem(item);
|
|
||||||
setShowEditDialog(true);
|
|
||||||
}}
|
|
||||||
onStatusChange={(status) => {/* TODO: Update status */}}
|
onStatusChange={(status) => {/* TODO: Update status */}}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -97,7 +97,7 @@ export async function detectDependencyConflicts(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Suggest linking to existing entity based on type
|
// Suggest linking to existing entity
|
||||||
if (parent.item_type === 'park') {
|
if (parent.item_type === 'park') {
|
||||||
const { data: parks } = await supabase
|
const { data: parks } = await supabase
|
||||||
.from('parks')
|
.from('parks')
|
||||||
@@ -112,35 +112,6 @@ export async function detectDependencyConflicts(
|
|||||||
entityId: park.id,
|
entityId: park.id,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
} else if (['manufacturer', 'operator', 'property_owner', 'designer'].includes(parent.item_type)) {
|
|
||||||
const { data: companies } = await supabase
|
|
||||||
.from('companies')
|
|
||||||
.select('id, name')
|
|
||||||
.eq('company_type', parent.item_type)
|
|
||||||
.ilike('name', `%${parent.item_data.name}%`)
|
|
||||||
.limit(3);
|
|
||||||
|
|
||||||
companies?.forEach(company => {
|
|
||||||
suggestions.push({
|
|
||||||
action: 'link_existing',
|
|
||||||
label: `Link to existing ${parent.item_type}: ${company.name}`,
|
|
||||||
entityId: company.id,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
} else if (parent.item_type === 'ride_model') {
|
|
||||||
const { data: models } = await supabase
|
|
||||||
.from('ride_models')
|
|
||||||
.select('id, name')
|
|
||||||
.ilike('name', `%${parent.item_data.name}%`)
|
|
||||||
.limit(3);
|
|
||||||
|
|
||||||
models?.forEach(model => {
|
|
||||||
suggestions.push({
|
|
||||||
action: 'link_existing',
|
|
||||||
label: `Link to existing ride model: ${model.name}`,
|
|
||||||
entityId: model.id,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
suggestions.push({
|
suggestions.push({
|
||||||
@@ -150,7 +121,7 @@ export async function detectDependencyConflicts(
|
|||||||
|
|
||||||
conflicts.push({
|
conflicts.push({
|
||||||
itemId: item.id,
|
itemId: item.id,
|
||||||
type: parent.status === 'rejected' ? 'rejected_parent' : 'missing_parent',
|
type: 'missing_parent',
|
||||||
message: `Cannot approve ${item.item_type} without its parent ${parent.item_type}`,
|
message: `Cannot approve ${item.item_type} without its parent ${parent.item_type}`,
|
||||||
suggestions,
|
suggestions,
|
||||||
});
|
});
|
||||||
@@ -158,301 +129,9 @@ export async function detectDependencyConflicts(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for circular dependencies
|
|
||||||
const circularDeps = detectCircularDependencies(items);
|
|
||||||
circularDeps.forEach(itemId => {
|
|
||||||
conflicts.push({
|
|
||||||
itemId,
|
|
||||||
type: 'circular_dependency',
|
|
||||||
message: 'Circular dependency detected in submission items',
|
|
||||||
suggestions: [
|
|
||||||
{
|
|
||||||
action: 'escalate',
|
|
||||||
label: 'Escalate for manual review',
|
|
||||||
},
|
|
||||||
],
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
return conflicts;
|
return conflicts;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Detect circular dependencies
|
|
||||||
*/
|
|
||||||
function detectCircularDependencies(items: SubmissionItemWithDeps[]): string[] {
|
|
||||||
const circular: string[] = [];
|
|
||||||
const visited = new Set<string>();
|
|
||||||
const recursionStack = new Set<string>();
|
|
||||||
|
|
||||||
function hasCycle(itemId: string): boolean {
|
|
||||||
visited.add(itemId);
|
|
||||||
recursionStack.add(itemId);
|
|
||||||
|
|
||||||
const item = items.find(i => i.id === itemId);
|
|
||||||
if (item?.depends_on) {
|
|
||||||
if (!visited.has(item.depends_on)) {
|
|
||||||
if (hasCycle(item.depends_on)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
} else if (recursionStack.has(item.depends_on)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
recursionStack.delete(itemId);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
items.forEach(item => {
|
|
||||||
if (!visited.has(item.id) && hasCycle(item.id)) {
|
|
||||||
circular.push(item.id);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return circular;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Link submission item to existing entity
|
|
||||||
*/
|
|
||||||
export async function linkToExistingEntity(
|
|
||||||
itemId: string,
|
|
||||||
entityId: string,
|
|
||||||
entityType: string
|
|
||||||
): Promise<void> {
|
|
||||||
const { data: item, error: fetchError } = await supabase
|
|
||||||
.from('submission_items')
|
|
||||||
.select('*')
|
|
||||||
.eq('id', itemId)
|
|
||||||
.single();
|
|
||||||
|
|
||||||
if (fetchError || !item) throw fetchError || new Error('Item not found');
|
|
||||||
|
|
||||||
// Update item_data to mark it as linked to existing entity
|
|
||||||
const currentItemData = (item.item_data as any) || {};
|
|
||||||
const updatedItemData = {
|
|
||||||
...currentItemData,
|
|
||||||
_linked_entity_id: entityId,
|
|
||||||
_skip_creation: true,
|
|
||||||
};
|
|
||||||
|
|
||||||
const { error: updateError } = await supabase
|
|
||||||
.from('submission_items')
|
|
||||||
.update({
|
|
||||||
item_data: updatedItemData,
|
|
||||||
approved_entity_id: entityId,
|
|
||||||
updated_at: new Date().toISOString(),
|
|
||||||
})
|
|
||||||
.eq('id', itemId);
|
|
||||||
|
|
||||||
if (updateError) throw updateError;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Resolve conflicts based on user selections
|
|
||||||
*/
|
|
||||||
export async function resolveConflicts(
|
|
||||||
conflicts: DependencyConflict[],
|
|
||||||
resolutions: Record<string, string>,
|
|
||||||
items: SubmissionItemWithDeps[],
|
|
||||||
userId: string
|
|
||||||
): Promise<{ updatedItems: SubmissionItemWithDeps[]; newConflicts: DependencyConflict[] }> {
|
|
||||||
if (!userId) {
|
|
||||||
throw new Error('User authentication required to resolve conflicts');
|
|
||||||
}
|
|
||||||
|
|
||||||
const updatedItems = [...items];
|
|
||||||
const newConflicts: DependencyConflict[] = [];
|
|
||||||
|
|
||||||
for (const conflict of conflicts) {
|
|
||||||
const resolution = resolutions[conflict.itemId];
|
|
||||||
if (!resolution) continue;
|
|
||||||
|
|
||||||
const suggestion = conflict.suggestions.find(s => s.action === resolution);
|
|
||||||
if (!suggestion) continue;
|
|
||||||
|
|
||||||
try {
|
|
||||||
switch (suggestion.action) {
|
|
||||||
case 'create_parent': {
|
|
||||||
// Add parent item to be approved along with child
|
|
||||||
const item = items.find(i => i.id === conflict.itemId);
|
|
||||||
if (item?.depends_on) {
|
|
||||||
const parent = items.find(i => i.id === item.depends_on);
|
|
||||||
if (parent && parent.status === 'pending') {
|
|
||||||
// Mark parent for approval (will be handled in approval flow)
|
|
||||||
const idx = updatedItems.findIndex(i => i.id === parent.id);
|
|
||||||
if (idx >= 0) {
|
|
||||||
updatedItems[idx] = {
|
|
||||||
...updatedItems[idx],
|
|
||||||
status: 'pending' as const
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
case 'link_existing': {
|
|
||||||
// Link to existing entity
|
|
||||||
if (suggestion.entityId) {
|
|
||||||
const item = items.find(i => i.id === conflict.itemId);
|
|
||||||
if (item?.depends_on) {
|
|
||||||
await linkToExistingEntity(item.depends_on, suggestion.entityId, item.item_type);
|
|
||||||
|
|
||||||
// Update local copy
|
|
||||||
const parentIdx = updatedItems.findIndex(i => i.id === item.depends_on);
|
|
||||||
if (parentIdx >= 0) {
|
|
||||||
updatedItems[parentIdx] = {
|
|
||||||
...updatedItems[parentIdx],
|
|
||||||
approved_entity_id: suggestion.entityId,
|
|
||||||
item_data: {
|
|
||||||
...updatedItems[parentIdx].item_data,
|
|
||||||
_linked_entity_id: suggestion.entityId,
|
|
||||||
_skip_creation: true,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
case 'escalate': {
|
|
||||||
// Escalate submission for admin review
|
|
||||||
const item = items.find(i => i.id === conflict.itemId);
|
|
||||||
if (item?.submission_id) {
|
|
||||||
await escalateSubmission(
|
|
||||||
item.submission_id,
|
|
||||||
`Dependency conflict: ${conflict.message}`,
|
|
||||||
userId
|
|
||||||
);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
case 'cascade_reject': {
|
|
||||||
// This would reject the item and its dependents
|
|
||||||
const item = items.find(i => i.id === conflict.itemId);
|
|
||||||
if (item) {
|
|
||||||
await rejectSubmissionItems(
|
|
||||||
[item],
|
|
||||||
'Rejected due to parent dependency conflict',
|
|
||||||
userId,
|
|
||||||
true
|
|
||||||
);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error(`Error resolving conflict for item ${conflict.itemId}:`, error);
|
|
||||||
newConflicts.push({
|
|
||||||
...conflict,
|
|
||||||
message: `Failed to resolve: ${error instanceof Error ? error.message : 'Unknown error'}`,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return { updatedItems, newConflicts };
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Edit a submission item
|
|
||||||
* Moderators can edit directly, regular users trigger auto-escalation
|
|
||||||
*/
|
|
||||||
export async function editSubmissionItem(
|
|
||||||
itemId: string,
|
|
||||||
newData: any,
|
|
||||||
userId: string
|
|
||||||
): Promise<void> {
|
|
||||||
// Check user permissions
|
|
||||||
const { data: userRoles } = await supabase
|
|
||||||
.from('user_roles')
|
|
||||||
.select('role')
|
|
||||||
.eq('user_id', userId);
|
|
||||||
|
|
||||||
const isModerator = userRoles?.some(r =>
|
|
||||||
['moderator', 'admin', 'superuser'].includes(r.role)
|
|
||||||
);
|
|
||||||
|
|
||||||
// Get current item
|
|
||||||
const { data: currentItem, error: fetchError } = await supabase
|
|
||||||
.from('submission_items')
|
|
||||||
.select('*')
|
|
||||||
.eq('id', itemId)
|
|
||||||
.single();
|
|
||||||
|
|
||||||
if (fetchError || !currentItem) {
|
|
||||||
throw new Error('Failed to fetch submission item');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Preserve original data if not already saved
|
|
||||||
const originalData = currentItem.original_data || currentItem.item_data;
|
|
||||||
|
|
||||||
if (isModerator) {
|
|
||||||
// Moderators can edit directly
|
|
||||||
const { error } = await supabase
|
|
||||||
.from('submission_items')
|
|
||||||
.update({
|
|
||||||
item_data: newData,
|
|
||||||
original_data: originalData,
|
|
||||||
updated_at: new Date().toISOString()
|
|
||||||
})
|
|
||||||
.eq('id', itemId);
|
|
||||||
|
|
||||||
if (error) {
|
|
||||||
console.error('Error updating submission item:', error);
|
|
||||||
throw new Error(`Failed to update item: ${error.message}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Log the edit
|
|
||||||
await supabase
|
|
||||||
.from('admin_audit_log')
|
|
||||||
.insert({
|
|
||||||
admin_user_id: userId,
|
|
||||||
target_user_id: currentItem.submission_id,
|
|
||||||
action: 'edit_submission_item',
|
|
||||||
details: {
|
|
||||||
item_id: itemId,
|
|
||||||
item_type: currentItem.item_type,
|
|
||||||
changes: newData
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
// Regular users trigger auto-escalation
|
|
||||||
const { data: submission } = await supabase
|
|
||||||
.from('content_submissions')
|
|
||||||
.select('id, status')
|
|
||||||
.eq('id', currentItem.submission_id)
|
|
||||||
.single();
|
|
||||||
|
|
||||||
if (submission && submission.status !== 'escalated') {
|
|
||||||
await escalateSubmission(
|
|
||||||
currentItem.submission_id,
|
|
||||||
`User requested edit for ${currentItem.item_type} item`,
|
|
||||||
userId
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update item with edit request
|
|
||||||
const { error } = await supabase
|
|
||||||
.from('submission_items')
|
|
||||||
.update({
|
|
||||||
item_data: newData,
|
|
||||||
original_data: originalData,
|
|
||||||
status: 'pending',
|
|
||||||
updated_at: new Date().toISOString()
|
|
||||||
})
|
|
||||||
.eq('id', itemId);
|
|
||||||
|
|
||||||
if (error) {
|
|
||||||
console.error('Error updating submission item:', error);
|
|
||||||
throw new Error(`Failed to update item: ${error.message}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update individual submission item status
|
* Update individual submission item status
|
||||||
*/
|
*/
|
||||||
@@ -482,47 +161,31 @@ export async function approveSubmissionItems(
|
|||||||
// Sort by dependency order (parents first)
|
// Sort by dependency order (parents first)
|
||||||
const sortedItems = topologicalSort(items);
|
const sortedItems = topologicalSort(items);
|
||||||
|
|
||||||
// Track entity ID mappings for dependency resolution
|
|
||||||
const entityIdMap = new Map<string, string>();
|
|
||||||
|
|
||||||
for (const item of sortedItems) {
|
for (const item of sortedItems) {
|
||||||
let entityId: string | null = null;
|
let entityId: string | null = null;
|
||||||
|
|
||||||
// Check if item is linked to existing entity (skip creation)
|
|
||||||
if (item.item_data._skip_creation && item.item_data._linked_entity_id) {
|
|
||||||
entityId = item.item_data._linked_entity_id;
|
|
||||||
entityIdMap.set(item.id, entityId);
|
|
||||||
} else {
|
|
||||||
// Resolve dependencies using entityIdMap
|
|
||||||
const resolvedItemData = resolveDependencies(item.item_data, entityIdMap);
|
|
||||||
|
|
||||||
// Create the entity based on type
|
// Create the entity based on type
|
||||||
switch (item.item_type) {
|
switch (item.item_type) {
|
||||||
case 'park':
|
case 'park':
|
||||||
entityId = await createPark(resolvedItemData);
|
entityId = await createPark(item.item_data);
|
||||||
break;
|
break;
|
||||||
case 'ride':
|
case 'ride':
|
||||||
entityId = await createRide(resolvedItemData);
|
entityId = await createRide(item.item_data);
|
||||||
break;
|
break;
|
||||||
case 'manufacturer':
|
case 'manufacturer':
|
||||||
case 'operator':
|
case 'operator':
|
||||||
case 'property_owner':
|
case 'property_owner':
|
||||||
case 'designer':
|
case 'designer':
|
||||||
entityId = await createCompany(resolvedItemData, item.item_type);
|
entityId = await createCompany(item.item_data, item.item_type);
|
||||||
break;
|
break;
|
||||||
case 'ride_model':
|
case 'ride_model':
|
||||||
entityId = await createRideModel(resolvedItemData);
|
entityId = await createRideModel(item.item_data);
|
||||||
break;
|
break;
|
||||||
case 'photo':
|
case 'photo':
|
||||||
entityId = await approvePhotos(resolvedItemData);
|
entityId = await approvePhotos(item.item_data);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (entityId) {
|
|
||||||
entityIdMap.set(item.id, entityId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update item status
|
// Update item status
|
||||||
await updateSubmissionItem(item.id, {
|
await updateSubmissionItem(item.id, {
|
||||||
status: 'approved',
|
status: 'approved',
|
||||||
@@ -531,31 +194,6 @@ export async function approveSubmissionItems(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Resolve dependencies in item data using entity ID mappings
|
|
||||||
*/
|
|
||||||
function resolveDependencies(itemData: any, entityIdMap: Map<string, string>): any {
|
|
||||||
const resolved = { ...itemData };
|
|
||||||
|
|
||||||
// Map common dependency fields
|
|
||||||
const dependencyFields = [
|
|
||||||
'park_id',
|
|
||||||
'manufacturer_id',
|
|
||||||
'operator_id',
|
|
||||||
'property_owner_id',
|
|
||||||
'designer_id',
|
|
||||||
'ride_model_id',
|
|
||||||
];
|
|
||||||
|
|
||||||
dependencyFields.forEach(field => {
|
|
||||||
if (resolved[field] && entityIdMap.has(resolved[field])) {
|
|
||||||
resolved[field] = entityIdMap.get(resolved[field]);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return resolved;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Topological sort for dependency-ordered processing
|
* Topological sort for dependency-ordered processing
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user