mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-22 23:51:13 -05:00
Fix: Enable edit functionality for ride models and timeline events
This commit is contained in:
@@ -896,6 +896,24 @@ async function createCompany(supabase: any, data: any, companyType: string): Pro
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function createRideModel(supabase: any, data: any): Promise<string> {
|
async function createRideModel(supabase: any, data: any): Promise<string> {
|
||||||
|
let rideModelId: string;
|
||||||
|
|
||||||
|
// Check if this is an edit (has ride_model_id) or a new creation
|
||||||
|
if (data.ride_model_id) {
|
||||||
|
console.log(`Updating existing ride model ${data.ride_model_id}`);
|
||||||
|
rideModelId = data.ride_model_id;
|
||||||
|
delete data.ride_model_id; // Remove ID from update data
|
||||||
|
|
||||||
|
const sanitizedData = sanitizeDateFields(data);
|
||||||
|
const filteredData = filterDatabaseFields(sanitizedData, RIDE_MODEL_FIELDS);
|
||||||
|
const { error } = await supabase
|
||||||
|
.from('ride_models')
|
||||||
|
.update(filteredData)
|
||||||
|
.eq('id', rideModelId);
|
||||||
|
|
||||||
|
if (error) throw new Error(`Failed to update ride model: ${error.message}`);
|
||||||
|
} else {
|
||||||
|
console.log('Creating new ride model');
|
||||||
const sanitizedData = sanitizeDateFields(data);
|
const sanitizedData = sanitizeDateFields(data);
|
||||||
const filteredData = filterDatabaseFields(sanitizedData, RIDE_MODEL_FIELDS);
|
const filteredData = filterDatabaseFields(sanitizedData, RIDE_MODEL_FIELDS);
|
||||||
const { data: model, error } = await supabase
|
const { data: model, error } = await supabase
|
||||||
@@ -905,7 +923,10 @@ async function createRideModel(supabase: any, data: any): Promise<string> {
|
|||||||
.single();
|
.single();
|
||||||
|
|
||||||
if (error) throw new Error(`Failed to create ride model: ${error.message}`);
|
if (error) throw new Error(`Failed to create ride model: ${error.message}`);
|
||||||
return model.id;
|
rideModelId = model.id;
|
||||||
|
}
|
||||||
|
|
||||||
|
return rideModelId;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function approvePhotos(supabase: any, data: any, submissionItemId: string): Promise<void> {
|
async function approvePhotos(supabase: any, data: any, submissionItemId: string): Promise<void> {
|
||||||
@@ -966,7 +987,43 @@ async function createTimelineEvent(
|
|||||||
approvingUserId: string,
|
approvingUserId: string,
|
||||||
submissionId: string
|
submissionId: string
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
console.log('Creating timeline event');
|
// Determine if this is an edit based on presence of event_id in data
|
||||||
|
// Note: Timeline events from frontend use 'id' field, not 'event_id'
|
||||||
|
const eventId = data.id || data.event_id;
|
||||||
|
|
||||||
|
if (eventId) {
|
||||||
|
console.log(`Updating existing timeline event ${eventId}`);
|
||||||
|
|
||||||
|
// Prepare update data (exclude ID and audit fields)
|
||||||
|
const updateData: any = {
|
||||||
|
event_type: data.event_type,
|
||||||
|
event_date: data.event_date,
|
||||||
|
event_date_precision: data.event_date_precision,
|
||||||
|
title: data.title,
|
||||||
|
description: data.description,
|
||||||
|
from_value: data.from_value,
|
||||||
|
to_value: data.to_value,
|
||||||
|
from_entity_id: data.from_entity_id,
|
||||||
|
to_entity_id: data.to_entity_id,
|
||||||
|
from_location_id: data.from_location_id,
|
||||||
|
to_location_id: data.to_location_id,
|
||||||
|
is_public: true,
|
||||||
|
};
|
||||||
|
|
||||||
|
// Remove undefined/null values
|
||||||
|
Object.keys(updateData).forEach(key =>
|
||||||
|
updateData[key] === undefined && delete updateData[key]
|
||||||
|
);
|
||||||
|
|
||||||
|
const { error } = await supabase
|
||||||
|
.from('entity_timeline_events')
|
||||||
|
.update(updateData)
|
||||||
|
.eq('id', eventId);
|
||||||
|
|
||||||
|
if (error) throw new Error(`Failed to update timeline event: ${error.message}`);
|
||||||
|
return eventId;
|
||||||
|
} else {
|
||||||
|
console.log('Creating new timeline event');
|
||||||
|
|
||||||
const eventData = {
|
const eventData = {
|
||||||
entity_id: data.entity_id,
|
entity_id: data.entity_id,
|
||||||
@@ -982,7 +1039,7 @@ async function createTimelineEvent(
|
|||||||
to_entity_id: data.to_entity_id,
|
to_entity_id: data.to_entity_id,
|
||||||
from_location_id: data.from_location_id,
|
from_location_id: data.from_location_id,
|
||||||
to_location_id: data.to_location_id,
|
to_location_id: data.to_location_id,
|
||||||
is_public: true, // All timeline events are public
|
is_public: true,
|
||||||
created_by: submitterId,
|
created_by: submitterId,
|
||||||
approved_by: approvingUserId,
|
approved_by: approvingUserId,
|
||||||
submission_id: submissionId,
|
submission_id: submissionId,
|
||||||
@@ -997,3 +1054,4 @@ async function createTimelineEvent(
|
|||||||
if (error) throw new Error(`Failed to create timeline event: ${error.message}`);
|
if (error) throw new Error(`Failed to create timeline event: ${error.message}`);
|
||||||
return event.id;
|
return event.id;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user