mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-21 22:51:12 -05:00
Fix photo submission approval logic
This commit is contained in:
@@ -502,7 +502,8 @@ export const ModerationQueue = forwardRef<ModerationQueueRef>((props, ref) => {
|
|||||||
|
|
||||||
// Handle photo submissions - create photos records when approved
|
// Handle photo submissions - create photos records when approved
|
||||||
if (action === 'approved' && item.type === 'content_submission' && item.submission_type === 'photo') {
|
if (action === 'approved' && item.type === 'content_submission' && item.submission_type === 'photo') {
|
||||||
console.log('Processing photo submission approval:', item.content);
|
console.log('🖼️ [PHOTO APPROVAL] Starting photo submission approval');
|
||||||
|
console.log('🖼️ [PHOTO APPROVAL] Full item content:', JSON.stringify(item.content, null, 2));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Extract photos array - handle both nested paths
|
// Extract photos array - handle both nested paths
|
||||||
@@ -513,45 +514,36 @@ export const ModerationQueue = forwardRef<ModerationQueueRef>((props, ref) => {
|
|||||||
item.content?.ride_id ? 'ride' :
|
item.content?.ride_id ? 'ride' :
|
||||||
item.content?.company_id ? 'company' : null);
|
item.content?.company_id ? 'company' : null);
|
||||||
|
|
||||||
|
console.log('🖼️ [PHOTO APPROVAL] Extracted data:', {
|
||||||
|
photosArray: photosArray,
|
||||||
|
entityId: entityId,
|
||||||
|
entityType: entityType,
|
||||||
|
hasPhotosArray: !!photosArray,
|
||||||
|
photosCount: photosArray?.length
|
||||||
|
});
|
||||||
|
|
||||||
if (!photosArray || !Array.isArray(photosArray) || photosArray.length === 0) {
|
if (!photosArray || !Array.isArray(photosArray) || photosArray.length === 0) {
|
||||||
|
console.error('🖼️ [PHOTO APPROVAL] ERROR: No photos found in submission');
|
||||||
throw new Error('No photos found in submission');
|
throw new Error('No photos found in submission');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!entityId || !entityType) {
|
if (!entityId || !entityType) {
|
||||||
|
console.error('🖼️ [PHOTO APPROVAL] ERROR: Invalid entity information', { entityId, entityType });
|
||||||
throw new Error('Invalid entity information in photo submission');
|
throw new Error('Invalid entity information in photo submission');
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('Creating photo records:', { photosArray, entityId, entityType });
|
// Check if photos already exist for this submission (in case of re-approval)
|
||||||
|
const { data: existingPhotos } = await supabase
|
||||||
// Create photo records in the photos table
|
|
||||||
const photoRecords = photosArray.map((photo, index) => ({
|
|
||||||
entity_id: entityId,
|
|
||||||
entity_type: entityType,
|
|
||||||
cloudflare_image_id: photo.imageId || photo.cloudflare_image_id,
|
|
||||||
cloudflare_image_url: photo.url || photo.cloudflare_image_url,
|
|
||||||
title: photo.title,
|
|
||||||
caption: photo.caption,
|
|
||||||
date_taken: photo.date,
|
|
||||||
order_index: photo.order ?? index,
|
|
||||||
submission_id: item.id,
|
|
||||||
submitted_by: item.user_id,
|
|
||||||
approved_by: user?.id,
|
|
||||||
approved_at: new Date().toISOString(),
|
|
||||||
}));
|
|
||||||
|
|
||||||
const { data: createdPhotos, error: photoError } = await supabase
|
|
||||||
.from('photos')
|
.from('photos')
|
||||||
.insert(photoRecords)
|
.select('id')
|
||||||
.select();
|
.eq('submission_id', item.id);
|
||||||
|
|
||||||
if (photoError) {
|
console.log('🖼️ [PHOTO APPROVAL] Existing photos check:', existingPhotos);
|
||||||
console.error('Failed to create photo records:', photoError);
|
|
||||||
throw new Error(`Failed to create photos: ${photoError.message}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log('Created photo records:', createdPhotos);
|
if (existingPhotos && existingPhotos.length > 0) {
|
||||||
|
console.log('🖼️ [PHOTO APPROVAL] Photos already exist for this submission, skipping creation');
|
||||||
|
|
||||||
// Update submission status
|
// Just update submission status
|
||||||
const { error: updateError } = await supabase
|
const { error: updateError } = await supabase
|
||||||
.from('content_submissions')
|
.from('content_submissions')
|
||||||
.update({
|
.update({
|
||||||
@@ -564,6 +556,67 @@ export const ModerationQueue = forwardRef<ModerationQueueRef>((props, ref) => {
|
|||||||
|
|
||||||
if (updateError) throw updateError;
|
if (updateError) throw updateError;
|
||||||
|
|
||||||
|
toast({
|
||||||
|
title: "Photos Re-Approved",
|
||||||
|
description: `Photo submission re-approved (${existingPhotos.length} existing photo(s))`,
|
||||||
|
});
|
||||||
|
|
||||||
|
fetchItems(activeEntityFilter, activeStatusFilter);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create photo records in the photos table
|
||||||
|
const photoRecords = photosArray.map((photo, index) => {
|
||||||
|
const record = {
|
||||||
|
entity_id: entityId,
|
||||||
|
entity_type: entityType,
|
||||||
|
cloudflare_image_id: photo.imageId || photo.cloudflare_image_id,
|
||||||
|
cloudflare_image_url: photo.url || photo.cloudflare_image_url,
|
||||||
|
title: photo.title,
|
||||||
|
caption: photo.caption,
|
||||||
|
date_taken: photo.date,
|
||||||
|
order_index: photo.order ?? index,
|
||||||
|
submission_id: item.id,
|
||||||
|
submitted_by: item.user_id,
|
||||||
|
approved_by: user?.id,
|
||||||
|
approved_at: new Date().toISOString(),
|
||||||
|
};
|
||||||
|
console.log('🖼️ [PHOTO APPROVAL] Photo record to insert:', record);
|
||||||
|
return record;
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log('🖼️ [PHOTO APPROVAL] Attempting to insert photo records:', photoRecords);
|
||||||
|
|
||||||
|
const { data: createdPhotos, error: photoError } = await supabase
|
||||||
|
.from('photos')
|
||||||
|
.insert(photoRecords)
|
||||||
|
.select();
|
||||||
|
|
||||||
|
if (photoError) {
|
||||||
|
console.error('🖼️ [PHOTO APPROVAL] Database error creating photos:', photoError);
|
||||||
|
throw new Error(`Failed to create photos: ${photoError.message}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('🖼️ [PHOTO APPROVAL] Successfully created photo records:', createdPhotos);
|
||||||
|
|
||||||
|
// Update submission status
|
||||||
|
const { error: updateError } = await supabase
|
||||||
|
.from('content_submissions')
|
||||||
|
.update({
|
||||||
|
status: 'approved',
|
||||||
|
reviewer_id: user?.id,
|
||||||
|
reviewed_at: new Date().toISOString(),
|
||||||
|
reviewer_notes: moderatorNotes
|
||||||
|
})
|
||||||
|
.eq('id', item.id);
|
||||||
|
|
||||||
|
if (updateError) {
|
||||||
|
console.error('🖼️ [PHOTO APPROVAL] Error updating submission:', updateError);
|
||||||
|
throw updateError;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('🖼️ [PHOTO APPROVAL] ✅ Complete! Created', createdPhotos.length, 'photos');
|
||||||
|
|
||||||
toast({
|
toast({
|
||||||
title: "Photos Approved",
|
title: "Photos Approved",
|
||||||
description: `Successfully approved ${createdPhotos.length} photo(s)`,
|
description: `Successfully approved ${createdPhotos.length} photo(s)`,
|
||||||
@@ -574,7 +627,8 @@ export const ModerationQueue = forwardRef<ModerationQueueRef>((props, ref) => {
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
console.error('Error processing photo approval:', error);
|
console.error('🖼️ [PHOTO APPROVAL] ❌ FATAL ERROR:', error);
|
||||||
|
console.error('🖼️ [PHOTO APPROVAL] Error details:', error.message, error.code, error.details);
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,6 +39,8 @@ export function EntityPhotoGallery({
|
|||||||
|
|
||||||
const fetchPhotos = async () => {
|
const fetchPhotos = async () => {
|
||||||
try {
|
try {
|
||||||
|
console.log('📷 [FETCH PHOTOS] Starting fetch for:', { entityId, entityType });
|
||||||
|
|
||||||
// Fetch photos directly from the photos table
|
// Fetch photos directly from the photos table
|
||||||
const { data: photoData, error } = await supabase
|
const { data: photoData, error } = await supabase
|
||||||
.from('photos')
|
.from('photos')
|
||||||
@@ -48,6 +50,8 @@ export function EntityPhotoGallery({
|
|||||||
.order('order_index', { ascending: true })
|
.order('order_index', { ascending: true })
|
||||||
.order('created_at', { ascending: false });
|
.order('created_at', { ascending: false });
|
||||||
|
|
||||||
|
console.log('📷 [FETCH PHOTOS] Query result:', { photoData, error, count: photoData?.length });
|
||||||
|
|
||||||
if (error) throw error;
|
if (error) throw error;
|
||||||
|
|
||||||
// Map to Photo interface
|
// Map to Photo interface
|
||||||
@@ -60,9 +64,10 @@ export function EntityPhotoGallery({
|
|||||||
created_at: photo.created_at,
|
created_at: photo.created_at,
|
||||||
})) || [];
|
})) || [];
|
||||||
|
|
||||||
|
console.log('📷 [FETCH PHOTOS] Mapped photos:', mappedPhotos);
|
||||||
setPhotos(mappedPhotos);
|
setPhotos(mappedPhotos);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error fetching photos:', error);
|
console.error('📷 [FETCH PHOTOS] Error:', error);
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user