mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-26 19:26:59 -05:00
Refactor: Approve lovable tool use
This commit is contained in:
@@ -70,6 +70,43 @@ function getPopulationLevel(fieldDensity: string, index: number): number {
|
||||
return 2;
|
||||
}
|
||||
|
||||
// Registry helper functions for cascading test data
|
||||
async function registerTestEntity(
|
||||
supabase: any,
|
||||
entityType: string,
|
||||
slug: string,
|
||||
entityId: string,
|
||||
sessionId: string
|
||||
) {
|
||||
const { error } = await supabase.from('test_data_registry').insert({
|
||||
entity_type: entityType,
|
||||
entity_slug: slug,
|
||||
entity_id: entityId,
|
||||
test_session_id: sessionId
|
||||
});
|
||||
|
||||
if (error && error.code !== '23505') { // Ignore unique constraint violations
|
||||
console.error(`Error registering ${entityType} ${slug}:`, error);
|
||||
}
|
||||
}
|
||||
|
||||
async function getExistingTestEntities(
|
||||
supabase: any,
|
||||
entityType: string
|
||||
): Promise<Array<{ slug: string; entity_id: string }>> {
|
||||
const { data, error } = await supabase
|
||||
.from('test_data_registry')
|
||||
.select('entity_slug, entity_id')
|
||||
.eq('entity_type', entityType);
|
||||
|
||||
if (error) {
|
||||
console.error(`Error fetching existing ${entityType}:`, error);
|
||||
return [];
|
||||
}
|
||||
|
||||
return data || [];
|
||||
}
|
||||
|
||||
Deno.serve(async (req) => {
|
||||
if (req.method === 'OPTIONS') {
|
||||
return new Response(null, { headers: corsHeaders });
|
||||
@@ -126,12 +163,36 @@ Deno.serve(async (req) => {
|
||||
}
|
||||
|
||||
const startTime = Date.now();
|
||||
const sessionId = crypto.randomUUID(); // Unique ID for this generation session
|
||||
const summary = { parks: 0, rides: 0, companies: 0, rideModels: 0, photos: 0, totalPhotoItems: 0, conflicts: 0, versionChains: 0 };
|
||||
const createdParks: string[] = [];
|
||||
const createdCompanies: Record<string, string[]> = { manufacturer: [], operator: [], designer: [], property_owner: [] };
|
||||
const createdParkSlugs: string[] = [];
|
||||
const createdRideSlugs: string[] = [];
|
||||
|
||||
// Load existing test entities from registry if includeDependencies is true
|
||||
if (includeDependencies) {
|
||||
console.log('Loading existing test entities from registry...');
|
||||
|
||||
const existingOperators = await getExistingTestEntities(supabase, 'operator');
|
||||
const existingOwners = await getExistingTestEntities(supabase, 'property_owner');
|
||||
const existingManufacturers = await getExistingTestEntities(supabase, 'manufacturer');
|
||||
const existingDesigners = await getExistingTestEntities(supabase, 'designer');
|
||||
const existingParks = await getExistingTestEntities(supabase, 'park');
|
||||
const existingRideModels = await getExistingTestEntities(supabase, 'ride_model');
|
||||
|
||||
existingOperators.forEach(op => createdCompanies.operator.push(op.slug));
|
||||
existingOwners.forEach(own => createdCompanies.property_owner.push(own.slug));
|
||||
existingManufacturers.forEach(mfg => createdCompanies.manufacturer.push(mfg.slug));
|
||||
existingDesigners.forEach(des => createdCompanies.designer.push(des.slug));
|
||||
existingParks.forEach(park => {
|
||||
createdParks.push(park.slug);
|
||||
createdParkSlugs.push(park.slug);
|
||||
});
|
||||
|
||||
console.log(`Loaded ${existingOperators.length} operators, ${existingOwners.length} owners, ${existingManufacturers.length} manufacturers, ${existingDesigners.length} designers, ${existingParks.length} parks`);
|
||||
}
|
||||
|
||||
// Helper to create submission
|
||||
async function createSubmission(userId: string, type: string, itemData: any, options: { escalated?: boolean; expiredLock?: boolean } = {}) {
|
||||
const submissionId = crypto.randomUUID();
|
||||
@@ -265,6 +326,18 @@ Deno.serve(async (req) => {
|
||||
};
|
||||
|
||||
await createSubmission(user.id, 'park', parkData, options);
|
||||
|
||||
// Register newly created park in the registry
|
||||
const { data: approvedPark } = await supabase
|
||||
.from('parks')
|
||||
.select('id')
|
||||
.eq('slug', slug)
|
||||
.maybeSingle();
|
||||
|
||||
if (approvedPark) {
|
||||
await registerTestEntity(supabase, 'park', slug, approvedPark.id, sessionId);
|
||||
}
|
||||
|
||||
createdParks.push(slug);
|
||||
if (!shouldConflict && !shouldVersionChain) {
|
||||
createdParkSlugs.push(slug);
|
||||
@@ -310,7 +383,20 @@ Deno.serve(async (req) => {
|
||||
}
|
||||
|
||||
await createSubmission(user.id, compType, companyData);
|
||||
createdCompanies[compType].push(`test-${compType}-${i + 1}`);
|
||||
|
||||
// Register newly created company in the registry
|
||||
const companySlug = `test-${compType}-${i + 1}`;
|
||||
const { data: approvedCompany } = await supabase
|
||||
.from('companies')
|
||||
.select('id')
|
||||
.eq('slug', companySlug)
|
||||
.maybeSingle();
|
||||
|
||||
if (approvedCompany) {
|
||||
await registerTestEntity(supabase, compType, companySlug, approvedCompany.id, sessionId);
|
||||
}
|
||||
|
||||
createdCompanies[compType].push(companySlug);
|
||||
summary.companies++;
|
||||
}
|
||||
}
|
||||
@@ -422,6 +508,17 @@ Deno.serve(async (req) => {
|
||||
}
|
||||
}
|
||||
|
||||
// Register newly created ride in the registry
|
||||
const { data: approvedRide } = await supabase
|
||||
.from('rides')
|
||||
.select('id')
|
||||
.eq('slug', slug)
|
||||
.maybeSingle();
|
||||
|
||||
if (approvedRide) {
|
||||
await registerTestEntity(supabase, 'ride', slug, approvedRide.id, sessionId);
|
||||
}
|
||||
|
||||
if (!shouldConflict && !shouldVersionChain) {
|
||||
createdRideSlugs.push(slug);
|
||||
}
|
||||
@@ -459,6 +556,19 @@ Deno.serve(async (req) => {
|
||||
}
|
||||
|
||||
await createSubmission(user.id, 'ride_model', modelData);
|
||||
|
||||
// Register newly created ride model in the registry
|
||||
const modelSlug = `test-model-${i + 1}`;
|
||||
const { data: approvedModel } = await supabase
|
||||
.from('ride_models')
|
||||
.select('id')
|
||||
.eq('slug', modelSlug)
|
||||
.maybeSingle();
|
||||
|
||||
if (approvedModel) {
|
||||
await registerTestEntity(supabase, 'ride_model', modelSlug, approvedModel.id, sessionId);
|
||||
}
|
||||
|
||||
summary.rideModels++;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
-- Create test data registry table for tracking test entities across runs
|
||||
CREATE TABLE IF NOT EXISTS test_data_registry (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
entity_type TEXT NOT NULL,
|
||||
entity_slug TEXT NOT NULL,
|
||||
entity_id UUID NOT NULL,
|
||||
test_session_id TEXT,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
metadata JSONB DEFAULT '{}'::jsonb
|
||||
);
|
||||
|
||||
-- Create indexes for efficient queries
|
||||
CREATE INDEX IF NOT EXISTS idx_test_data_registry_type ON test_data_registry(entity_type);
|
||||
CREATE INDEX IF NOT EXISTS idx_test_data_registry_slug ON test_data_registry(entity_slug);
|
||||
CREATE INDEX IF NOT EXISTS idx_test_data_registry_session ON test_data_registry(test_session_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_test_data_registry_entity_id ON test_data_registry(entity_id);
|
||||
|
||||
-- Add unique constraint to prevent duplicate entries
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_test_data_registry_unique ON test_data_registry(entity_type, entity_slug);
|
||||
|
||||
-- Enable RLS
|
||||
ALTER TABLE test_data_registry ENABLE ROW LEVEL SECURITY;
|
||||
|
||||
-- Create RLS policies
|
||||
CREATE POLICY "Moderators can manage test data registry"
|
||||
ON test_data_registry
|
||||
FOR ALL
|
||||
TO authenticated
|
||||
USING (is_moderator(auth.uid()));
|
||||
|
||||
CREATE POLICY "Moderators can view test data registry"
|
||||
ON test_data_registry
|
||||
FOR SELECT
|
||||
TO authenticated
|
||||
USING (is_moderator(auth.uid()));
|
||||
Reference in New Issue
Block a user