mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-28 17:26:58 -05:00
Compare commits
2 Commits
0d6d3fb2cc
...
d29e873e14
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d29e873e14 | ||
|
|
882959bce6 |
@@ -1,5 +1,5 @@
|
|||||||
import { useState } from 'react';
|
import { useState } from 'react';
|
||||||
import { AlertTriangle } from 'lucide-react';
|
import { AlertTriangle, AlertCircle } from 'lucide-react';
|
||||||
import {
|
import {
|
||||||
Dialog,
|
Dialog,
|
||||||
DialogContent,
|
DialogContent,
|
||||||
@@ -18,12 +18,14 @@ import {
|
|||||||
SelectTrigger,
|
SelectTrigger,
|
||||||
SelectValue,
|
SelectValue,
|
||||||
} from '@/components/ui/select';
|
} from '@/components/ui/select';
|
||||||
|
import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert';
|
||||||
|
|
||||||
interface EscalationDialogProps {
|
interface EscalationDialogProps {
|
||||||
open: boolean;
|
open: boolean;
|
||||||
onOpenChange: (open: boolean) => void;
|
onOpenChange: (open: boolean) => void;
|
||||||
onEscalate: (reason: string) => Promise<void>;
|
onEscalate: (reason: string) => Promise<void>;
|
||||||
submissionType: string;
|
submissionType: string;
|
||||||
|
error?: { message: string; errorId?: string } | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const escalationReasons = [
|
const escalationReasons = [
|
||||||
@@ -40,6 +42,7 @@ export function EscalationDialog({
|
|||||||
onOpenChange,
|
onOpenChange,
|
||||||
onEscalate,
|
onEscalate,
|
||||||
submissionType,
|
submissionType,
|
||||||
|
error,
|
||||||
}: EscalationDialogProps) {
|
}: EscalationDialogProps) {
|
||||||
const [selectedReason, setSelectedReason] = useState('');
|
const [selectedReason, setSelectedReason] = useState('');
|
||||||
const [additionalNotes, setAdditionalNotes] = useState('');
|
const [additionalNotes, setAdditionalNotes] = useState('');
|
||||||
@@ -76,6 +79,23 @@ export function EscalationDialog({
|
|||||||
</DialogDescription>
|
</DialogDescription>
|
||||||
</DialogHeader>
|
</DialogHeader>
|
||||||
|
|
||||||
|
{error && (
|
||||||
|
<Alert variant="destructive" className="mt-4">
|
||||||
|
<AlertCircle className="h-4 w-4" />
|
||||||
|
<AlertTitle>Escalation Failed</AlertTitle>
|
||||||
|
<AlertDescription>
|
||||||
|
<div className="space-y-2">
|
||||||
|
<p className="text-sm">{error.message}</p>
|
||||||
|
{error.errorId && (
|
||||||
|
<p className="text-xs font-mono bg-destructive/10 px-2 py-1 rounded">
|
||||||
|
Reference: {error.errorId.slice(0, 8)}
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</AlertDescription>
|
||||||
|
</Alert>
|
||||||
|
)}
|
||||||
|
|
||||||
<div className="space-y-4 py-4">
|
<div className="space-y-4 py-4">
|
||||||
<div className="space-y-2">
|
<div className="space-y-2">
|
||||||
<Label>Escalation Reason</Label>
|
<Label>Escalation Reason</Label>
|
||||||
|
|||||||
@@ -12,12 +12,12 @@ import {
|
|||||||
detectDependencyConflicts,
|
detectDependencyConflicts,
|
||||||
approveSubmissionItems,
|
approveSubmissionItems,
|
||||||
rejectSubmissionItems,
|
rejectSubmissionItems,
|
||||||
escalateSubmission,
|
|
||||||
checkSubmissionConflict,
|
checkSubmissionConflict,
|
||||||
type SubmissionItemWithDeps,
|
type SubmissionItemWithDeps,
|
||||||
type DependencyConflict,
|
type DependencyConflict,
|
||||||
type ConflictCheckResult
|
type ConflictCheckResult
|
||||||
} from '@/lib/submissionItemsService';
|
} from '@/lib/submissionItemsService';
|
||||||
|
import { useModerationActions } from '@/hooks/moderation/useModerationActions';
|
||||||
import { Sheet, SheetContent, SheetHeader, SheetTitle, SheetDescription } from '@/components/ui/sheet';
|
import { Sheet, SheetContent, SheetHeader, SheetTitle, SheetDescription } from '@/components/ui/sheet';
|
||||||
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription } from '@/components/ui/dialog';
|
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription } from '@/components/ui/dialog';
|
||||||
import { Button } from '@/components/ui/button';
|
import { Button } from '@/components/ui/button';
|
||||||
@@ -77,6 +77,10 @@ export function SubmissionReviewManager({
|
|||||||
const [conflictData, setConflictData] = useState<ConflictCheckResult | null>(null);
|
const [conflictData, setConflictData] = useState<ConflictCheckResult | null>(null);
|
||||||
const [showConflictResolutionModal, setShowConflictResolutionModal] = useState(false);
|
const [showConflictResolutionModal, setShowConflictResolutionModal] = useState(false);
|
||||||
const [lastModifiedTimestamp, setLastModifiedTimestamp] = useState<string | null>(null);
|
const [lastModifiedTimestamp, setLastModifiedTimestamp] = useState<string | null>(null);
|
||||||
|
const [escalationError, setEscalationError] = useState<{
|
||||||
|
message: string;
|
||||||
|
errorId?: string;
|
||||||
|
} | null>(null);
|
||||||
|
|
||||||
const { toast } = useToast();
|
const { toast } = useToast();
|
||||||
const { isAdmin, isSuperuser } = useUserRole();
|
const { isAdmin, isSuperuser } = useUserRole();
|
||||||
@@ -87,6 +91,17 @@ export function SubmissionReviewManager({
|
|||||||
// Lock monitoring integration
|
// Lock monitoring integration
|
||||||
const { extendLock } = useLockMonitor(state, dispatch, submissionId);
|
const { extendLock } = useLockMonitor(state, dispatch, submissionId);
|
||||||
|
|
||||||
|
// Moderation actions
|
||||||
|
const { escalateSubmission } = useModerationActions({
|
||||||
|
user,
|
||||||
|
onActionStart: (itemId: string) => {
|
||||||
|
logger.log(`Starting escalation for ${itemId}`);
|
||||||
|
},
|
||||||
|
onActionComplete: () => {
|
||||||
|
logger.log('Escalation complete');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// Auto-claim on mount
|
// Auto-claim on mount
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (open && submissionId && state.status === 'idle') {
|
if (open && submissionId && state.status === 'idle') {
|
||||||
@@ -232,28 +247,68 @@ export function SubmissionReviewManager({
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Run validation on all selected items
|
// Run validation on all selected items
|
||||||
const validationResultsMap = await validateMultipleItems(
|
let validationResultsMap: Map<string, any>;
|
||||||
selectedItems.map(item => ({
|
|
||||||
item_type: item.item_type,
|
|
||||||
item_data: item.item_data,
|
|
||||||
id: item.id
|
|
||||||
}))
|
|
||||||
);
|
|
||||||
|
|
||||||
setValidationResults(validationResultsMap);
|
try {
|
||||||
|
validationResultsMap = await validateMultipleItems(
|
||||||
|
selectedItems.map(item => ({
|
||||||
|
item_type: item.item_type,
|
||||||
|
item_data: item.item_data,
|
||||||
|
id: item.id
|
||||||
|
}))
|
||||||
|
);
|
||||||
|
|
||||||
// Check for blocking errors
|
setValidationResults(validationResultsMap);
|
||||||
const itemsWithBlockingErrors = selectedItems.filter(item => {
|
|
||||||
const result = validationResultsMap.get(item.id);
|
|
||||||
return result && result.blockingErrors.length > 0;
|
|
||||||
});
|
|
||||||
|
|
||||||
// CRITICAL: Blocking errors can NEVER be bypassed, regardless of warnings
|
// Check for blocking errors
|
||||||
if (itemsWithBlockingErrors.length > 0) {
|
const itemsWithBlockingErrors = selectedItems.filter(item => {
|
||||||
setHasBlockingErrors(true);
|
const result = validationResultsMap.get(item.id);
|
||||||
setShowValidationBlockerDialog(true);
|
return result && result.blockingErrors.length > 0;
|
||||||
dispatch({ type: 'ERROR', payload: { error: 'Validation failed' } });
|
});
|
||||||
return; // Block approval
|
|
||||||
|
// CRITICAL: Blocking errors can NEVER be bypassed, regardless of warnings
|
||||||
|
if (itemsWithBlockingErrors.length > 0) {
|
||||||
|
// Log which items have blocking errors
|
||||||
|
itemsWithBlockingErrors.forEach(item => {
|
||||||
|
const result = validationResultsMap.get(item.id);
|
||||||
|
logger.error('Blocking validation errors prevent approval', {
|
||||||
|
submissionId,
|
||||||
|
itemId: item.id,
|
||||||
|
itemType: item.item_type,
|
||||||
|
errors: result?.blockingErrors
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
setHasBlockingErrors(true);
|
||||||
|
setShowValidationBlockerDialog(true);
|
||||||
|
dispatch({ type: 'ERROR', payload: { error: 'Validation failed' } });
|
||||||
|
return; // Block approval
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
// Validation itself failed (network error, bug, etc.)
|
||||||
|
const errorId = handleError(error, {
|
||||||
|
action: 'Validation System Error',
|
||||||
|
userId: user?.id,
|
||||||
|
metadata: {
|
||||||
|
submissionId,
|
||||||
|
selectedItemCount: selectedItems.length,
|
||||||
|
itemTypes: selectedItems.map(i => i.item_type)
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
toast({
|
||||||
|
title: 'Validation System Error',
|
||||||
|
description: (
|
||||||
|
<div className="space-y-2">
|
||||||
|
<p>Unable to validate submission. Please try again.</p>
|
||||||
|
<p className="text-xs font-mono">Ref: {errorId.slice(0, 8)}</p>
|
||||||
|
</div>
|
||||||
|
),
|
||||||
|
variant: 'destructive'
|
||||||
|
});
|
||||||
|
|
||||||
|
dispatch({ type: 'ERROR', payload: { error: 'Validation system error' } });
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for warnings
|
// Check for warnings
|
||||||
@@ -425,50 +480,35 @@ export function SubmissionReviewManager({
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const { supabase } = await import('@/integrations/supabase/client');
|
setEscalationError(null);
|
||||||
|
|
||||||
// Call the escalation notification edge function
|
// Use consolidated action from useModerationActions
|
||||||
const { data, error, requestId } = await invokeWithTracking(
|
// This handles: edge function call, fallback, error logging, cache invalidation
|
||||||
'send-escalation-notification',
|
await escalateSubmission(
|
||||||
{
|
{
|
||||||
submissionId,
|
id: submissionId,
|
||||||
escalationReason: reason,
|
submission_type: submissionType,
|
||||||
escalatedBy: user.id
|
type: 'submission'
|
||||||
},
|
} as any,
|
||||||
user.id
|
reason
|
||||||
);
|
);
|
||||||
|
|
||||||
if (error) {
|
// Success - close dialog
|
||||||
handleError(error, {
|
|
||||||
action: 'Send escalation notification',
|
|
||||||
userId: user.id,
|
|
||||||
metadata: { submissionId }
|
|
||||||
});
|
|
||||||
// Fallback to direct database update if email fails
|
|
||||||
await escalateSubmission(submissionId, reason, user.id);
|
|
||||||
toast({
|
|
||||||
title: 'Escalated (Email Failed)',
|
|
||||||
description: 'Submission escalated but notification email failed to send',
|
|
||||||
variant: 'default',
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
toast({
|
|
||||||
title: 'Escalated Successfully',
|
|
||||||
description: 'Submission escalated and admin notified via email',
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
onComplete();
|
onComplete();
|
||||||
onOpenChange(false);
|
onOpenChange(false);
|
||||||
} catch (error: unknown) {
|
} catch (error: any) {
|
||||||
handleError(error, {
|
// Track error for retry UI
|
||||||
action: 'Escalate Submission',
|
setEscalationError({
|
||||||
userId: user?.id,
|
message: getErrorMessage(error),
|
||||||
metadata: {
|
errorId: error.errorId
|
||||||
submissionId,
|
|
||||||
reason: reason.substring(0, 100)
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
logger.error('Escalation failed in SubmissionReviewManager', {
|
||||||
|
submissionId,
|
||||||
|
error: getErrorMessage(error)
|
||||||
|
});
|
||||||
|
|
||||||
|
// Don't close dialog on error - let user retry
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -587,6 +627,7 @@ export function SubmissionReviewManager({
|
|||||||
onOpenChange={setShowEscalationDialog}
|
onOpenChange={setShowEscalationDialog}
|
||||||
onEscalate={handleEscalate}
|
onEscalate={handleEscalate}
|
||||||
submissionType={submissionType}
|
submissionType={submissionType}
|
||||||
|
error={escalationError}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<RejectionDialog
|
<RejectionDialog
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { AlertCircle } from 'lucide-react';
|
import { useState } from 'react';
|
||||||
|
import { AlertCircle, ChevronDown } from 'lucide-react';
|
||||||
import {
|
import {
|
||||||
AlertDialog,
|
AlertDialog,
|
||||||
AlertDialogAction,
|
AlertDialogAction,
|
||||||
@@ -9,6 +10,9 @@ import {
|
|||||||
AlertDialogTitle,
|
AlertDialogTitle,
|
||||||
} from '@/components/ui/alert-dialog';
|
} from '@/components/ui/alert-dialog';
|
||||||
import { Alert, AlertDescription } from '@/components/ui/alert';
|
import { Alert, AlertDescription } from '@/components/ui/alert';
|
||||||
|
import { Badge } from '@/components/ui/badge';
|
||||||
|
import { Button } from '@/components/ui/button';
|
||||||
|
import { Collapsible, CollapsibleContent, CollapsibleTrigger } from '@/components/ui/collapsible';
|
||||||
import { ValidationError } from '@/lib/entityValidationSchemas';
|
import { ValidationError } from '@/lib/entityValidationSchemas';
|
||||||
|
|
||||||
interface ValidationBlockerDialogProps {
|
interface ValidationBlockerDialogProps {
|
||||||
@@ -24,9 +28,11 @@ export function ValidationBlockerDialog({
|
|||||||
blockingErrors,
|
blockingErrors,
|
||||||
itemNames,
|
itemNames,
|
||||||
}: ValidationBlockerDialogProps) {
|
}: ValidationBlockerDialogProps) {
|
||||||
|
const [showDetails, setShowDetails] = useState(false);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<AlertDialog open={open} onOpenChange={onClose}>
|
<AlertDialog open={open} onOpenChange={onClose}>
|
||||||
<AlertDialogContent>
|
<AlertDialogContent className="max-w-2xl">
|
||||||
<AlertDialogHeader>
|
<AlertDialogHeader>
|
||||||
<AlertDialogTitle className="flex items-center gap-2 text-destructive">
|
<AlertDialogTitle className="flex items-center gap-2 text-destructive">
|
||||||
<AlertCircle className="w-5 h-5" />
|
<AlertCircle className="w-5 h-5" />
|
||||||
@@ -34,29 +40,52 @@ export function ValidationBlockerDialog({
|
|||||||
</AlertDialogTitle>
|
</AlertDialogTitle>
|
||||||
<AlertDialogDescription>
|
<AlertDialogDescription>
|
||||||
The following items have blocking validation errors that MUST be fixed before approval.
|
The following items have blocking validation errors that MUST be fixed before approval.
|
||||||
These items cannot be approved until the errors are resolved. Please edit or reject them.
|
Edit the items to fix the errors, or reject them.
|
||||||
</AlertDialogDescription>
|
</AlertDialogDescription>
|
||||||
</AlertDialogHeader>
|
</AlertDialogHeader>
|
||||||
|
|
||||||
<div className="space-y-3 my-4">
|
<div className="space-y-3 my-4">
|
||||||
{itemNames.map((name, index) => (
|
{itemNames.map((name, index) => {
|
||||||
<div key={index} className="space-y-2">
|
const itemErrors = blockingErrors.filter((_, i) =>
|
||||||
<div className="font-medium text-sm">{name}</div>
|
itemNames.length === 1 || i === index
|
||||||
<Alert variant="destructive">
|
);
|
||||||
<AlertDescription className="space-y-1">
|
|
||||||
{blockingErrors
|
return (
|
||||||
.filter((_, i) => i === index || itemNames.length === 1)
|
<div key={index} className="space-y-2">
|
||||||
.map((error, errIndex) => (
|
<div className="font-medium text-sm flex items-center justify-between">
|
||||||
|
<span>{name}</span>
|
||||||
|
<Badge variant="destructive">
|
||||||
|
{itemErrors.length} error{itemErrors.length > 1 ? 's' : ''}
|
||||||
|
</Badge>
|
||||||
|
</div>
|
||||||
|
<Alert variant="destructive">
|
||||||
|
<AlertDescription className="space-y-1">
|
||||||
|
{itemErrors.map((error, errIndex) => (
|
||||||
<div key={errIndex} className="text-sm">
|
<div key={errIndex} className="text-sm">
|
||||||
• <span className="font-medium">{error.field}:</span> {error.message}
|
• <span className="font-medium">{error.field}:</span> {error.message}
|
||||||
</div>
|
</div>
|
||||||
))}
|
))}
|
||||||
</AlertDescription>
|
</AlertDescription>
|
||||||
</Alert>
|
</Alert>
|
||||||
</div>
|
</div>
|
||||||
))}
|
);
|
||||||
|
})}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<Collapsible open={showDetails} onOpenChange={setShowDetails}>
|
||||||
|
<CollapsibleTrigger asChild>
|
||||||
|
<Button variant="ghost" size="sm" className="w-full">
|
||||||
|
{showDetails ? 'Hide' : 'Show'} Technical Details
|
||||||
|
<ChevronDown className={`ml-2 h-4 w-4 transition-transform ${showDetails ? 'rotate-180' : ''}`} />
|
||||||
|
</Button>
|
||||||
|
</CollapsibleTrigger>
|
||||||
|
<CollapsibleContent className="mt-2">
|
||||||
|
<div className="bg-muted p-3 rounded text-xs font-mono max-h-60 overflow-auto">
|
||||||
|
<pre>{JSON.stringify(blockingErrors, null, 2)}</pre>
|
||||||
|
</div>
|
||||||
|
</CollapsibleContent>
|
||||||
|
</Collapsible>
|
||||||
|
|
||||||
<AlertDialogFooter>
|
<AlertDialogFooter>
|
||||||
<AlertDialogAction onClick={onClose}>
|
<AlertDialogAction onClick={onClose}>
|
||||||
Close
|
Close
|
||||||
|
|||||||
@@ -171,42 +171,93 @@ export function useModerationActions(config: ModerationActionsConfig): Moderatio
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Run validation on all items
|
// Run validation on all items
|
||||||
const validationResults = await validateMultipleItems(itemsWithData);
|
try {
|
||||||
|
const validationResults = await validateMultipleItems(itemsWithData);
|
||||||
|
|
||||||
// Check for blocking errors
|
// Check for blocking errors
|
||||||
const itemsWithBlockingErrors = itemsWithData.filter(item => {
|
const itemsWithBlockingErrors = itemsWithData.filter(item => {
|
||||||
const result = validationResults.get(item.id);
|
|
||||||
return result && result.blockingErrors.length > 0;
|
|
||||||
});
|
|
||||||
|
|
||||||
// CRITICAL: Block approval if any item has blocking errors
|
|
||||||
if (itemsWithBlockingErrors.length > 0) {
|
|
||||||
const errorDetails = itemsWithBlockingErrors.map(item => {
|
|
||||||
const result = validationResults.get(item.id);
|
const result = validationResults.get(item.id);
|
||||||
return `${item.item_type}: ${result?.blockingErrors[0]?.message || 'Unknown error'}`;
|
return result && result.blockingErrors.length > 0;
|
||||||
}).join(', ');
|
|
||||||
|
|
||||||
toast({
|
|
||||||
title: 'Cannot Approve - Validation Errors',
|
|
||||||
description: `${itemsWithBlockingErrors.length} item(s) have blocking errors that must be fixed first. ${errorDetails}`,
|
|
||||||
variant: 'destructive',
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Return early - do NOT proceed with approval
|
// CRITICAL: Block approval if any item has blocking errors
|
||||||
return;
|
if (itemsWithBlockingErrors.length > 0) {
|
||||||
}
|
// Log detailed blocking errors
|
||||||
|
itemsWithBlockingErrors.forEach(item => {
|
||||||
|
const result = validationResults.get(item.id);
|
||||||
|
logger.error('Validation blocking approval', {
|
||||||
|
submissionId: item.id,
|
||||||
|
itemId: item.id,
|
||||||
|
itemType: item.item_type,
|
||||||
|
blockingErrors: result?.blockingErrors
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
// Check for warnings (optional - can proceed but inform user)
|
const errorDetails = itemsWithBlockingErrors.map(item => {
|
||||||
const itemsWithWarnings = itemsWithData.filter(item => {
|
const result = validationResults.get(item.id);
|
||||||
const result = validationResults.get(item.id);
|
const itemName = (item.item_data as any)?.name || item.item_type;
|
||||||
return result && result.warnings.length > 0;
|
const errors = result?.blockingErrors.map(e => `${e.field}: ${e.message}`).join(', ');
|
||||||
});
|
return `${itemName} - ${errors}`;
|
||||||
|
}).join('; ');
|
||||||
|
|
||||||
if (itemsWithWarnings.length > 0) {
|
throw new Error(`Validation failed: ${errorDetails}`);
|
||||||
logger.info('Approval proceeding with warnings', {
|
}
|
||||||
submissionId: item.id,
|
|
||||||
warningCount: itemsWithWarnings.length
|
// Check for warnings (optional - can proceed but inform user)
|
||||||
|
const itemsWithWarnings = itemsWithData.filter(item => {
|
||||||
|
const result = validationResults.get(item.id);
|
||||||
|
return result && result.warnings.length > 0;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (itemsWithWarnings.length > 0) {
|
||||||
|
logger.info('Approval proceeding with warnings', {
|
||||||
|
submissionId: item.id,
|
||||||
|
warningCount: itemsWithWarnings.length
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
// Check if this is a validation error or system error
|
||||||
|
if (getErrorMessage(error).includes('Validation failed:')) {
|
||||||
|
// This is expected - validation rules preventing approval
|
||||||
|
handleError(error, {
|
||||||
|
action: 'Validation Blocked Approval',
|
||||||
|
userId: user?.id,
|
||||||
|
metadata: {
|
||||||
|
submissionId: item.id,
|
||||||
|
submissionType: item.submission_type,
|
||||||
|
selectedItemCount: itemsWithData.length
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
toast({
|
||||||
|
title: 'Cannot Approve - Validation Errors',
|
||||||
|
description: getErrorMessage(error),
|
||||||
|
variant: 'destructive',
|
||||||
|
});
|
||||||
|
|
||||||
|
// Return early - do NOT proceed with approval
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
// Unexpected validation system error
|
||||||
|
const errorId = handleError(error, {
|
||||||
|
action: 'Validation System Failure',
|
||||||
|
userId: user?.id,
|
||||||
|
metadata: {
|
||||||
|
submissionId: item.id,
|
||||||
|
submissionType: item.submission_type,
|
||||||
|
phase: 'validation'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
toast({
|
||||||
|
title: 'Validation System Error',
|
||||||
|
description: `Unable to validate submission (ref: ${errorId.slice(0, 8)})`,
|
||||||
|
variant: 'destructive',
|
||||||
|
});
|
||||||
|
|
||||||
|
// Return early - do NOT proceed with approval
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -372,7 +372,10 @@ export const useModerationQueue = (config?: UseModerationQueueConfig) => {
|
|||||||
return Math.max(0, currentLock.expiresAt.getTime() - Date.now());
|
return Math.max(0, currentLock.expiresAt.getTime() - Date.now());
|
||||||
}, [currentLock]);
|
}, [currentLock]);
|
||||||
|
|
||||||
// Escalate submission
|
/**
|
||||||
|
* @deprecated Use escalateSubmission from useModerationActions instead
|
||||||
|
* This method only updates the database and doesn't send email notifications
|
||||||
|
*/
|
||||||
const escalateSubmission = useCallback(async (submissionId: string, reason: string): Promise<boolean> => {
|
const escalateSubmission = useCallback(async (submissionId: string, reason: string): Promise<boolean> => {
|
||||||
if (!user?.id) return false;
|
if (!user?.id) return false;
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import { supabase } from '@/lib/supabaseClient';
|
import { supabase } from '@/lib/supabaseClient';
|
||||||
|
import { handleNonCriticalError, getErrorMessage } from '@/lib/errorHandler';
|
||||||
|
import { logger } from '@/lib/logger';
|
||||||
|
|
||||||
// ============================================
|
// ============================================
|
||||||
// CENTRALIZED VALIDATION SCHEMAS
|
// CENTRALIZED VALIDATION SCHEMAS
|
||||||
@@ -452,35 +454,71 @@ export async function validateEntityData(
|
|||||||
entityType: keyof typeof entitySchemas,
|
entityType: keyof typeof entitySchemas,
|
||||||
data: unknown
|
data: unknown
|
||||||
): Promise<ValidationResult> {
|
): Promise<ValidationResult> {
|
||||||
const schema = entitySchemas[entityType];
|
try {
|
||||||
|
// Debug logging for operator entity
|
||||||
if (!schema) {
|
if (entityType === 'operator') {
|
||||||
return {
|
logger.log('Validating operator entity', {
|
||||||
isValid: false,
|
dataKeys: data ? Object.keys(data as object) : [],
|
||||||
blockingErrors: [{ field: 'entity_type', message: `Unknown entity type: ${entityType}`, severity: 'blocking' }],
|
dataTypes: data ? Object.entries(data as object).reduce((acc, [key, val]) => {
|
||||||
warnings: [],
|
acc[key] = typeof val;
|
||||||
suggestions: [],
|
return acc;
|
||||||
allErrors: [{ field: 'entity_type', message: `Unknown entity type: ${entityType}`, severity: 'blocking' }],
|
}, {} as Record<string, string>) : {},
|
||||||
};
|
rawData: JSON.stringify(data).substring(0, 500)
|
||||||
}
|
|
||||||
|
|
||||||
const result = schema.safeParse(data);
|
|
||||||
const blockingErrors: ValidationError[] = [];
|
|
||||||
const warnings: ValidationError[] = [];
|
|
||||||
const suggestions: ValidationError[] = [];
|
|
||||||
|
|
||||||
// Process Zod errors
|
|
||||||
if (!result.success) {
|
|
||||||
const zodError = result.error as z.ZodError;
|
|
||||||
zodError.issues.forEach((issue) => {
|
|
||||||
const field = issue.path.join('.');
|
|
||||||
blockingErrors.push({
|
|
||||||
field: field || 'unknown',
|
|
||||||
message: issue.message,
|
|
||||||
severity: 'blocking',
|
|
||||||
});
|
});
|
||||||
});
|
}
|
||||||
}
|
|
||||||
|
const schema = entitySchemas[entityType];
|
||||||
|
|
||||||
|
if (!schema) {
|
||||||
|
const error = {
|
||||||
|
field: 'entity_type',
|
||||||
|
message: `Unknown entity type: ${entityType}`,
|
||||||
|
severity: 'blocking' as const
|
||||||
|
};
|
||||||
|
|
||||||
|
handleNonCriticalError(new Error(`Unknown entity type: ${entityType}`), {
|
||||||
|
action: 'Entity Validation',
|
||||||
|
metadata: { entityType, providedData: data }
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
isValid: false,
|
||||||
|
blockingErrors: [error],
|
||||||
|
warnings: [],
|
||||||
|
suggestions: [],
|
||||||
|
allErrors: [error],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = schema.safeParse(data);
|
||||||
|
const blockingErrors: ValidationError[] = [];
|
||||||
|
const warnings: ValidationError[] = [];
|
||||||
|
const suggestions: ValidationError[] = [];
|
||||||
|
|
||||||
|
// Process Zod errors
|
||||||
|
if (!result.success) {
|
||||||
|
const zodError = result.error as z.ZodError;
|
||||||
|
|
||||||
|
// Log detailed validation failure
|
||||||
|
handleNonCriticalError(zodError, {
|
||||||
|
action: 'Zod Validation Failed',
|
||||||
|
metadata: {
|
||||||
|
entityType,
|
||||||
|
issues: zodError.issues,
|
||||||
|
providedData: JSON.stringify(data).substring(0, 500),
|
||||||
|
issueCount: zodError.issues.length
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
zodError.issues.forEach((issue) => {
|
||||||
|
const field = issue.path.join('.') || entityType;
|
||||||
|
blockingErrors.push({
|
||||||
|
field,
|
||||||
|
message: `${issue.message} (code: ${issue.code})`,
|
||||||
|
severity: 'blocking',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// Add warnings for optional but recommended fields
|
// Add warnings for optional but recommended fields
|
||||||
const validData = data as Record<string, unknown>;
|
const validData = data as Record<string, unknown>;
|
||||||
@@ -585,16 +623,43 @@ export async function validateEntityData(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const allErrors = [...blockingErrors, ...warnings, ...suggestions];
|
const allErrors = [...blockingErrors, ...warnings, ...suggestions];
|
||||||
const isValid = blockingErrors.length === 0;
|
const isValid = blockingErrors.length === 0;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
isValid,
|
isValid,
|
||||||
blockingErrors,
|
blockingErrors,
|
||||||
warnings,
|
warnings,
|
||||||
suggestions,
|
suggestions,
|
||||||
allErrors,
|
allErrors,
|
||||||
};
|
};
|
||||||
|
} catch (error) {
|
||||||
|
// Catch any unexpected errors during validation
|
||||||
|
const errorId = handleNonCriticalError(error, {
|
||||||
|
action: 'Entity Validation Unexpected Error',
|
||||||
|
metadata: {
|
||||||
|
entityType,
|
||||||
|
dataType: typeof data,
|
||||||
|
hasData: !!data
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
isValid: false,
|
||||||
|
blockingErrors: [{
|
||||||
|
field: entityType,
|
||||||
|
message: `Validation error: ${getErrorMessage(error)} (ref: ${errorId.slice(0, 8)})`,
|
||||||
|
severity: 'blocking'
|
||||||
|
}],
|
||||||
|
warnings: [],
|
||||||
|
suggestions: [],
|
||||||
|
allErrors: [{
|
||||||
|
field: entityType,
|
||||||
|
message: `Validation error: ${getErrorMessage(error)} (ref: ${errorId.slice(0, 8)})`,
|
||||||
|
severity: 'blocking'
|
||||||
|
}],
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user