feat: Add retry logic to updates

This commit is contained in:
gpt-engineer-app[bot]
2025-11-05 13:56:08 +00:00
parent 80826a83a8
commit dcc9e2af8f

View File

@@ -698,18 +698,28 @@ export async function submitParkUpdate(
data: ParkFormData,
userId: string
): Promise<{ submitted: boolean; submissionId: string }> {
// Check if user is banned
const { withRetry, isRetryableError } = await import('./retryHelpers');
// Check if user is banned - with retry for transient failures
const profile = await withRetry(
async () => {
const { data: profile } = await supabase
.from('profiles')
.select('banned')
.eq('user_id', userId)
.single();
return profile;
},
{ maxAttempts: 2 }
);
if (profile?.banned) {
throw new Error('Account suspended. Contact support for assistance.');
}
// Fetch existing park data first
// Fetch existing park data first - with retry for transient failures
const existingPark = await withRetry(
async () => {
const { data: existingPark, error: fetchError } = await supabase
.from('parks')
.select('id, name, slug, description, park_type, status, opening_date, opening_date_precision, closing_date, closing_date_precision, website_url, phone, email, location_id, operator_id, property_owner_id, banner_image_url, banner_image_id, card_image_url, card_image_id')
@@ -719,6 +729,11 @@ export async function submitParkUpdate(
if (fetchError) throw new Error(`Failed to fetch park: ${fetchError.message}`);
if (!existingPark) throw new Error('Park not found');
return existingPark;
},
{ maxAttempts: 2 }
);
// CRITICAL: Block new photo uploads on edits
// Photos can only be submitted during creation or via the photo gallery
if (data.images?.uploaded && data.images.uploaded.some(img => img.isLocal)) {
@@ -728,6 +743,9 @@ export async function submitParkUpdate(
// Only allow banner/card reassignments from existing photos
let processedImages = data.images;
// Main submission logic with retry and error handling
const result = await withRetry(
async () => {
// Create the main submission record
const { data: submissionData, error: submissionError } = await supabase
.from('content_submissions')
@@ -765,6 +783,47 @@ export async function submitParkUpdate(
if (itemError) throw itemError;
return { submitted: true, submissionId: submissionData.id };
},
{
maxAttempts: 3,
onRetry: (attempt, error, delay) => {
logger.warn('Retrying park update submission', {
attempt,
delay,
parkId,
error: error instanceof Error ? error.message : String(error)
});
// Emit event for UI retry indicator
window.dispatchEvent(new CustomEvent('submission-retry', {
detail: { attempt, maxAttempts: 3, delay, type: 'park update' }
}));
},
shouldRetry: (error) => {
// Don't retry validation/business logic errors
if (error instanceof Error) {
const message = error.message.toLowerCase();
if (message.includes('required')) return false;
if (message.includes('banned')) return false;
if (message.includes('slug')) return false;
if (message.includes('permission')) return false;
if (message.includes('not found')) return false;
if (message.includes('not allowed')) return false;
}
return isRetryableError(error);
}
}
).catch((error) => {
handleError(error, {
action: 'Park update submission',
userId,
metadata: { retriesExhausted: true, parkId },
});
throw error;
});
return result;
}
/**
@@ -1073,18 +1132,28 @@ export async function submitRideUpdate(
data: RideFormData,
userId: string
): Promise<{ submitted: boolean; submissionId: string }> {
// Check if user is banned
const { withRetry, isRetryableError } = await import('./retryHelpers');
// Check if user is banned - with retry for transient failures
const profile = await withRetry(
async () => {
const { data: profile } = await supabase
.from('profiles')
.select('banned')
.eq('user_id', userId)
.single();
return profile;
},
{ maxAttempts: 2 }
);
if (profile?.banned) {
throw new Error('Account suspended. Contact support for assistance.');
}
// Fetch existing ride data first
// Fetch existing ride data first - with retry for transient failures
const existingRide = await withRetry(
async () => {
const { data: existingRide, error: fetchError } = await supabase
.from('rides')
.select('*')
@@ -1094,6 +1163,11 @@ export async function submitRideUpdate(
if (fetchError) throw new Error(`Failed to fetch ride: ${fetchError.message}`);
if (!existingRide) throw new Error('Ride not found');
return existingRide;
},
{ maxAttempts: 2 }
);
// CRITICAL: Block new photo uploads on edits
// Photos can only be submitted during creation or via the photo gallery
if (data.images?.uploaded && data.images.uploaded.some(img => img.isLocal)) {
@@ -1103,6 +1177,9 @@ export async function submitRideUpdate(
// Only allow banner/card reassignments from existing photos
let processedImages = data.images;
// Main submission logic with retry and error handling
const result = await withRetry(
async () => {
// Create the main submission record
const { data: submissionData, error: submissionError } = await supabase
.from('content_submissions')
@@ -1140,6 +1217,47 @@ export async function submitRideUpdate(
if (itemError) throw itemError;
return { submitted: true, submissionId: submissionData.id };
},
{
maxAttempts: 3,
onRetry: (attempt, error, delay) => {
logger.warn('Retrying ride update submission', {
attempt,
delay,
rideId,
error: error instanceof Error ? error.message : String(error)
});
// Emit event for UI retry indicator
window.dispatchEvent(new CustomEvent('submission-retry', {
detail: { attempt, maxAttempts: 3, delay, type: 'ride update' }
}));
},
shouldRetry: (error) => {
// Don't retry validation/business logic errors
if (error instanceof Error) {
const message = error.message.toLowerCase();
if (message.includes('required')) return false;
if (message.includes('banned')) return false;
if (message.includes('slug')) return false;
if (message.includes('permission')) return false;
if (message.includes('not found')) return false;
if (message.includes('not allowed')) return false;
}
return isRetryableError(error);
}
}
).catch((error) => {
handleError(error, {
action: 'Ride update submission',
userId,
metadata: { retriesExhausted: true, rideId },
});
throw error;
});
return result;
}
/**