mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-23 05:11:14 -05:00
Refactor: Complete Photo System Refactor
This commit is contained in:
@@ -183,51 +183,67 @@ export function UppyPhotoSubmissionUpload({
|
||||
|
||||
setUploadProgress(null);
|
||||
|
||||
// Submit to database with Cloudflare URLs
|
||||
const submissionData = {
|
||||
user_id: user.id,
|
||||
submission_type: 'photo',
|
||||
content: {
|
||||
title: title.trim() || undefined,
|
||||
photos: photos.map((photo, index) => ({
|
||||
url: photo.uploadStatus === 'uploaded' ? photo.url : uploadedPhotos.find(p => p.order === photo.order)?.url || photo.url,
|
||||
caption: photo.caption.trim(),
|
||||
title: photo.title?.trim(),
|
||||
date: photo.date?.toISOString(),
|
||||
order: index,
|
||||
// Include file metadata for moderation queue
|
||||
filename: photo.file?.name,
|
||||
size: photo.file?.size,
|
||||
type: photo.file?.type,
|
||||
})),
|
||||
// NEW STRUCTURE: Generic entity references
|
||||
context: finalEntityType,
|
||||
entity_id: finalEntityId,
|
||||
// Legacy structure for backwards compatibility
|
||||
...(finalEntityType === 'ride' && { ride_id: finalEntityId }),
|
||||
...(finalEntityType === 'park' && { park_id: finalEntityId }),
|
||||
...(finalParentId && finalEntityType === 'ride' && { park_id: finalParentId }),
|
||||
...(['manufacturer', 'operator', 'designer', 'property_owner'].includes(finalEntityType) && { company_id: finalEntityId }),
|
||||
},
|
||||
};
|
||||
|
||||
// Debug logging for verification
|
||||
console.log('Photo Submission Data:', {
|
||||
entity_id: finalEntityId,
|
||||
context: finalEntityType,
|
||||
parent_id: finalParentId,
|
||||
photo_count: photos.length,
|
||||
submission_data: submissionData
|
||||
});
|
||||
|
||||
const { error } = await supabase
|
||||
// Create content_submission record first
|
||||
const { data: submissionData, error: submissionError } = await supabase
|
||||
.from('content_submissions')
|
||||
.insert(submissionData);
|
||||
.insert({
|
||||
user_id: user.id,
|
||||
submission_type: 'photo',
|
||||
content: {}, // Empty content, all data is in relational tables
|
||||
})
|
||||
.select()
|
||||
.single();
|
||||
|
||||
if (error) {
|
||||
throw error;
|
||||
if (submissionError || !submissionData) {
|
||||
throw submissionError || new Error('Failed to create submission record');
|
||||
}
|
||||
|
||||
// Create photo_submission record
|
||||
const { data: photoSubmissionData, error: photoSubmissionError } = await supabase
|
||||
.from('photo_submissions')
|
||||
.insert({
|
||||
submission_id: submissionData.id,
|
||||
entity_type: finalEntityType,
|
||||
entity_id: finalEntityId,
|
||||
parent_id: finalParentId || null,
|
||||
title: title.trim() || null,
|
||||
})
|
||||
.select()
|
||||
.single();
|
||||
|
||||
if (photoSubmissionError || !photoSubmissionData) {
|
||||
throw photoSubmissionError || new Error('Failed to create photo submission');
|
||||
}
|
||||
|
||||
// Insert all photo items
|
||||
const photoItems = photos.map((photo, index) => ({
|
||||
photo_submission_id: photoSubmissionData.id,
|
||||
cloudflare_image_id: photo.url.split('/').slice(-2, -1)[0] || '', // Extract ID from URL
|
||||
cloudflare_image_url: photo.uploadStatus === 'uploaded' ? photo.url : uploadedPhotos.find(p => p.order === photo.order)?.url || photo.url,
|
||||
caption: photo.caption.trim() || null,
|
||||
title: photo.title?.trim() || null,
|
||||
filename: photo.file?.name || null,
|
||||
order_index: index,
|
||||
file_size: photo.file?.size || null,
|
||||
mime_type: photo.file?.type || null,
|
||||
}));
|
||||
|
||||
const { error: itemsError } = await supabase
|
||||
.from('photo_submission_items')
|
||||
.insert(photoItems);
|
||||
|
||||
if (itemsError) {
|
||||
throw itemsError;
|
||||
}
|
||||
|
||||
console.log('✅ Photo submission created:', {
|
||||
submission_id: submissionData.id,
|
||||
photo_submission_id: photoSubmissionData.id,
|
||||
entity_type: finalEntityType,
|
||||
entity_id: finalEntityId,
|
||||
photo_count: photoItems.length,
|
||||
});
|
||||
|
||||
toast({
|
||||
title: 'Submission Successful',
|
||||
description: 'Your photos have been submitted for review. Thank you for contributing!',
|
||||
|
||||
Reference in New Issue
Block a user