mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-21 19:51:14 -05:00
Fix form photo upload to photos table
This commit is contained in:
@@ -68,6 +68,19 @@ serve(async (req) => {
|
|||||||
throw new Error(`Failed to fetch items: ${fetchError.message}`);
|
throw new Error(`Failed to fetch items: ${fetchError.message}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get the submitter's user_id from the submission
|
||||||
|
const { data: submission, error: submissionError } = await supabase
|
||||||
|
.from('content_submissions')
|
||||||
|
.select('user_id')
|
||||||
|
.eq('id', submissionId)
|
||||||
|
.single();
|
||||||
|
|
||||||
|
if (submissionError || !submission) {
|
||||||
|
throw new Error(`Failed to fetch submission: ${submissionError?.message}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const submitterId = submission.user_id;
|
||||||
|
|
||||||
// Topologically sort items by dependencies
|
// Topologically sort items by dependencies
|
||||||
const sortedItems = topologicalSort(items);
|
const sortedItems = topologicalSort(items);
|
||||||
const dependencyMap = new Map<string, string>();
|
const dependencyMap = new Map<string, string>();
|
||||||
@@ -81,6 +94,9 @@ serve(async (req) => {
|
|||||||
// Resolve dependencies in item data
|
// Resolve dependencies in item data
|
||||||
const resolvedData = resolveDependencies(item.item_data, dependencyMap);
|
const resolvedData = resolveDependencies(item.item_data, dependencyMap);
|
||||||
|
|
||||||
|
// Add submitter ID to the data for photo tracking
|
||||||
|
resolvedData._submitter_id = submitterId;
|
||||||
|
|
||||||
let entityId: string | null = null;
|
let entityId: string | null = null;
|
||||||
|
|
||||||
// Create entity based on type
|
// Create entity based on type
|
||||||
@@ -275,11 +291,17 @@ function normalizeStatusValue(data: any): any {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function createPark(supabase: any, data: any): Promise<string> {
|
async function createPark(supabase: any, data: any): Promise<string> {
|
||||||
|
const submitterId = data._submitter_id;
|
||||||
|
let uploadedPhotos: any[] = [];
|
||||||
|
|
||||||
// Transform images object if present
|
// Transform images object if present
|
||||||
if (data.images) {
|
if (data.images) {
|
||||||
const { uploaded, banner_assignment, card_assignment } = data.images;
|
const { uploaded, banner_assignment, card_assignment } = data.images;
|
||||||
|
|
||||||
if (uploaded && Array.isArray(uploaded)) {
|
if (uploaded && Array.isArray(uploaded)) {
|
||||||
|
// Store uploaded photos for later insertion into photos table
|
||||||
|
uploadedPhotos = uploaded;
|
||||||
|
|
||||||
// Assign banner image
|
// Assign banner image
|
||||||
if (banner_assignment !== undefined && uploaded[banner_assignment]) {
|
if (banner_assignment !== undefined && uploaded[banner_assignment]) {
|
||||||
data.banner_image_id = uploaded[banner_assignment].cloudflare_id;
|
data.banner_image_id = uploaded[banner_assignment].cloudflare_id;
|
||||||
@@ -297,10 +319,15 @@ async function createPark(supabase: any, data: any): Promise<string> {
|
|||||||
delete data.images;
|
delete data.images;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Remove internal fields
|
||||||
|
delete data._submitter_id;
|
||||||
|
|
||||||
|
let parkId: string;
|
||||||
|
|
||||||
// Check if this is an edit (has park_id) or a new creation
|
// Check if this is an edit (has park_id) or a new creation
|
||||||
if (data.park_id) {
|
if (data.park_id) {
|
||||||
console.log(`Updating existing park ${data.park_id}`);
|
console.log(`Updating existing park ${data.park_id}`);
|
||||||
const parkId = data.park_id;
|
parkId = data.park_id;
|
||||||
delete data.park_id; // Remove ID from update data
|
delete data.park_id; // Remove ID from update data
|
||||||
|
|
||||||
const normalizedData = normalizeStatusValue(data);
|
const normalizedData = normalizeStatusValue(data);
|
||||||
@@ -312,7 +339,6 @@ async function createPark(supabase: any, data: any): Promise<string> {
|
|||||||
.eq('id', parkId);
|
.eq('id', parkId);
|
||||||
|
|
||||||
if (error) throw new Error(`Failed to update park: ${error.message}`);
|
if (error) throw new Error(`Failed to update park: ${error.message}`);
|
||||||
return parkId;
|
|
||||||
} else {
|
} else {
|
||||||
console.log('Creating new park');
|
console.log('Creating new park');
|
||||||
const normalizedData = normalizeStatusValue(data);
|
const normalizedData = normalizeStatusValue(data);
|
||||||
@@ -325,16 +351,49 @@ async function createPark(supabase: any, data: any): Promise<string> {
|
|||||||
.single();
|
.single();
|
||||||
|
|
||||||
if (error) throw new Error(`Failed to create park: ${error.message}`);
|
if (error) throw new Error(`Failed to create park: ${error.message}`);
|
||||||
return park.id;
|
parkId = park.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Insert photos into photos table
|
||||||
|
if (uploadedPhotos.length > 0 && submitterId) {
|
||||||
|
console.log(`Inserting ${uploadedPhotos.length} photos for park ${parkId}`);
|
||||||
|
for (let i = 0; i < uploadedPhotos.length; i++) {
|
||||||
|
const photo = uploadedPhotos[i];
|
||||||
|
if (photo.cloudflare_id && photo.url) {
|
||||||
|
const { error: photoError } = await supabase.from('photos').insert({
|
||||||
|
entity_id: parkId,
|
||||||
|
entity_type: 'park',
|
||||||
|
cloudflare_image_id: photo.cloudflare_id,
|
||||||
|
cloudflare_image_url: photo.url,
|
||||||
|
caption: photo.caption || null,
|
||||||
|
title: null,
|
||||||
|
submitted_by: submitterId,
|
||||||
|
approved_at: new Date().toISOString(),
|
||||||
|
order_index: i,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (photoError) {
|
||||||
|
console.error(`Failed to insert photo ${i}:`, photoError);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return parkId;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function createRide(supabase: any, data: any): Promise<string> {
|
async function createRide(supabase: any, data: any): Promise<string> {
|
||||||
|
const submitterId = data._submitter_id;
|
||||||
|
let uploadedPhotos: any[] = [];
|
||||||
|
|
||||||
// Transform images object if present
|
// Transform images object if present
|
||||||
if (data.images) {
|
if (data.images) {
|
||||||
const { uploaded, banner_assignment, card_assignment } = data.images;
|
const { uploaded, banner_assignment, card_assignment } = data.images;
|
||||||
|
|
||||||
if (uploaded && Array.isArray(uploaded)) {
|
if (uploaded && Array.isArray(uploaded)) {
|
||||||
|
// Store uploaded photos for later insertion into photos table
|
||||||
|
uploadedPhotos = uploaded;
|
||||||
|
|
||||||
// Assign banner image
|
// Assign banner image
|
||||||
if (banner_assignment !== undefined && uploaded[banner_assignment]) {
|
if (banner_assignment !== undefined && uploaded[banner_assignment]) {
|
||||||
data.banner_image_id = uploaded[banner_assignment].cloudflare_id;
|
data.banner_image_id = uploaded[banner_assignment].cloudflare_id;
|
||||||
@@ -352,10 +411,16 @@ async function createRide(supabase: any, data: any): Promise<string> {
|
|||||||
delete data.images;
|
delete data.images;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Remove internal fields and store park_id before filtering
|
||||||
|
delete data._submitter_id;
|
||||||
|
const parkId = data.park_id;
|
||||||
|
|
||||||
|
let rideId: string;
|
||||||
|
|
||||||
// Check if this is an edit (has ride_id) or a new creation
|
// Check if this is an edit (has ride_id) or a new creation
|
||||||
if (data.ride_id) {
|
if (data.ride_id) {
|
||||||
console.log(`Updating existing ride ${data.ride_id}`);
|
console.log(`Updating existing ride ${data.ride_id}`);
|
||||||
const rideId = data.ride_id;
|
rideId = data.ride_id;
|
||||||
delete data.ride_id; // Remove ID from update data
|
delete data.ride_id; // Remove ID from update data
|
||||||
|
|
||||||
const normalizedData = normalizeStatusValue(data);
|
const normalizedData = normalizeStatusValue(data);
|
||||||
@@ -369,18 +434,16 @@ async function createRide(supabase: any, data: any): Promise<string> {
|
|||||||
if (error) throw new Error(`Failed to update ride: ${error.message}`);
|
if (error) throw new Error(`Failed to update ride: ${error.message}`);
|
||||||
|
|
||||||
// Update park ride counts after successful ride update
|
// Update park ride counts after successful ride update
|
||||||
if (data.park_id) {
|
if (parkId) {
|
||||||
console.log(`Updating ride counts for park ${data.park_id}`);
|
console.log(`Updating ride counts for park ${parkId}`);
|
||||||
const { error: countError } = await supabase.rpc('update_park_ride_counts', {
|
const { error: countError } = await supabase.rpc('update_park_ride_counts', {
|
||||||
target_park_id: data.park_id
|
target_park_id: parkId
|
||||||
});
|
});
|
||||||
|
|
||||||
if (countError) {
|
if (countError) {
|
||||||
console.error('Failed to update park counts:', countError);
|
console.error('Failed to update park counts:', countError);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return rideId;
|
|
||||||
} else {
|
} else {
|
||||||
console.log('Creating new ride');
|
console.log('Creating new ride');
|
||||||
const normalizedData = normalizeStatusValue(data);
|
const normalizedData = normalizeStatusValue(data);
|
||||||
@@ -393,21 +456,47 @@ async function createRide(supabase: any, data: any): Promise<string> {
|
|||||||
.single();
|
.single();
|
||||||
|
|
||||||
if (error) throw new Error(`Failed to create ride: ${error.message}`);
|
if (error) throw new Error(`Failed to create ride: ${error.message}`);
|
||||||
|
rideId = ride.id;
|
||||||
|
|
||||||
// Update park ride counts after successful ride creation
|
// Update park ride counts after successful ride creation
|
||||||
if (data.park_id) {
|
if (parkId) {
|
||||||
console.log(`Updating ride counts for park ${data.park_id}`);
|
console.log(`Updating ride counts for park ${parkId}`);
|
||||||
const { error: countError } = await supabase.rpc('update_park_ride_counts', {
|
const { error: countError } = await supabase.rpc('update_park_ride_counts', {
|
||||||
target_park_id: data.park_id
|
target_park_id: parkId
|
||||||
});
|
});
|
||||||
|
|
||||||
if (countError) {
|
if (countError) {
|
||||||
console.error('Failed to update park counts:', countError);
|
console.error('Failed to update park counts:', countError);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return ride.id;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Insert photos into photos table
|
||||||
|
if (uploadedPhotos.length > 0 && submitterId) {
|
||||||
|
console.log(`Inserting ${uploadedPhotos.length} photos for ride ${rideId}`);
|
||||||
|
for (let i = 0; i < uploadedPhotos.length; i++) {
|
||||||
|
const photo = uploadedPhotos[i];
|
||||||
|
if (photo.cloudflare_id && photo.url) {
|
||||||
|
const { error: photoError } = await supabase.from('photos').insert({
|
||||||
|
entity_id: rideId,
|
||||||
|
entity_type: 'ride',
|
||||||
|
cloudflare_image_id: photo.cloudflare_id,
|
||||||
|
cloudflare_image_url: photo.url,
|
||||||
|
caption: photo.caption || null,
|
||||||
|
title: null,
|
||||||
|
submitted_by: submitterId,
|
||||||
|
approved_at: new Date().toISOString(),
|
||||||
|
order_index: i,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (photoError) {
|
||||||
|
console.error(`Failed to insert photo ${i}:`, photoError);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return rideId;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function createCompany(supabase: any, data: any, companyType: string): Promise<string> {
|
async function createCompany(supabase: any, data: any, companyType: string): Promise<string> {
|
||||||
|
|||||||
Reference in New Issue
Block a user