mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-20 12:31:26 -05:00
Fix seed-test-data edge function
This commit is contained in:
@@ -114,10 +114,10 @@ async function getExistingTestEntities(
|
|||||||
async function getPendingSubmissionItems(
|
async function getPendingSubmissionItems(
|
||||||
supabase: any,
|
supabase: any,
|
||||||
itemType: string
|
itemType: string
|
||||||
): Promise<Array<{ id: string; item_data: any }>> {
|
): Promise<Array<{ id: string; item_data_id: string }>> {
|
||||||
const { data, error } = await supabase
|
const { data, error } = await supabase
|
||||||
.from('submission_items')
|
.from('submission_items')
|
||||||
.select('id, item_data')
|
.select('id, item_data_id')
|
||||||
.eq('item_type', itemType)
|
.eq('item_type', itemType)
|
||||||
.eq('status', 'pending');
|
.eq('status', 'pending');
|
||||||
|
|
||||||
@@ -129,6 +129,39 @@ async function getPendingSubmissionItems(
|
|||||||
return data || [];
|
return data || [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get slug from relational submission table
|
||||||
|
async function getSubmissionSlug(
|
||||||
|
supabase: any,
|
||||||
|
itemType: string,
|
||||||
|
itemDataId: string
|
||||||
|
): Promise<string | null> {
|
||||||
|
const tableMap: Record<string, string> = {
|
||||||
|
park: 'park_submissions',
|
||||||
|
ride: 'ride_submissions',
|
||||||
|
manufacturer: 'company_submissions',
|
||||||
|
operator: 'company_submissions',
|
||||||
|
designer: 'company_submissions',
|
||||||
|
property_owner: 'company_submissions',
|
||||||
|
ride_model: 'ride_model_submissions'
|
||||||
|
};
|
||||||
|
|
||||||
|
const table = tableMap[itemType];
|
||||||
|
if (!table) return null;
|
||||||
|
|
||||||
|
const { data, error } = await supabase
|
||||||
|
.from(table)
|
||||||
|
.select('slug')
|
||||||
|
.eq('id', itemDataId)
|
||||||
|
.maybeSingle();
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
edgeLogger.error(`Error fetching slug from ${table}`, { error: error.message, itemDataId });
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return data?.slug || null;
|
||||||
|
}
|
||||||
|
|
||||||
Deno.serve(async (req) => {
|
Deno.serve(async (req) => {
|
||||||
const tracking = startRequest();
|
const tracking = startRequest();
|
||||||
|
|
||||||
@@ -200,10 +233,9 @@ Deno.serve(async (req) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const startTime = Date.now();
|
const startTime = Date.now();
|
||||||
const sessionId = crypto.randomUUID(); // Unique ID for this generation session
|
const sessionId = crypto.randomUUID();
|
||||||
const summary = { parks: 0, rides: 0, companies: 0, rideModels: 0, photos: 0, totalPhotoItems: 0, conflicts: 0, versionChains: 0 };
|
const summary = { parks: 0, rides: 0, companies: 0, rideModels: 0, photos: 0, totalPhotoItems: 0, conflicts: 0, versionChains: 0 };
|
||||||
|
|
||||||
// Track submission item IDs for dependency resolution
|
|
||||||
const createdSubmissionItems: Record<string, string[]> = {
|
const createdSubmissionItems: Record<string, string[]> = {
|
||||||
operator: [],
|
operator: [],
|
||||||
property_owner: [],
|
property_owner: [],
|
||||||
@@ -218,7 +250,7 @@ Deno.serve(async (req) => {
|
|||||||
const createdParkSlugs: string[] = [];
|
const createdParkSlugs: string[] = [];
|
||||||
const createdRideSlugs: string[] = [];
|
const createdRideSlugs: string[] = [];
|
||||||
|
|
||||||
// Load existing test entities from registry if includeDependencies is true
|
// Load existing test entities from registry
|
||||||
if (includeDependencies) {
|
if (includeDependencies) {
|
||||||
edgeLogger.info('Loading existing test entities from registry', { requestId: tracking.requestId });
|
edgeLogger.info('Loading existing test entities from registry', { requestId: tracking.requestId });
|
||||||
|
|
||||||
@@ -227,9 +259,7 @@ Deno.serve(async (req) => {
|
|||||||
const existingManufacturers = await getExistingTestEntities(supabase, 'manufacturer');
|
const existingManufacturers = await getExistingTestEntities(supabase, 'manufacturer');
|
||||||
const existingDesigners = await getExistingTestEntities(supabase, 'designer');
|
const existingDesigners = await getExistingTestEntities(supabase, 'designer');
|
||||||
const existingParks = await getExistingTestEntities(supabase, 'park');
|
const existingParks = await getExistingTestEntities(supabase, 'park');
|
||||||
const existingRideModels = await getExistingTestEntities(supabase, 'ride_model');
|
|
||||||
|
|
||||||
// Load pending submission items for dependency resolution
|
|
||||||
const pendingOperators = await getPendingSubmissionItems(supabase, 'operator');
|
const pendingOperators = await getPendingSubmissionItems(supabase, 'operator');
|
||||||
const pendingOwners = await getPendingSubmissionItems(supabase, 'property_owner');
|
const pendingOwners = await getPendingSubmissionItems(supabase, 'property_owner');
|
||||||
const pendingManufacturers = await getPendingSubmissionItems(supabase, 'manufacturer');
|
const pendingManufacturers = await getPendingSubmissionItems(supabase, 'manufacturer');
|
||||||
@@ -237,7 +267,7 @@ Deno.serve(async (req) => {
|
|||||||
const pendingParks = await getPendingSubmissionItems(supabase, 'park');
|
const pendingParks = await getPendingSubmissionItems(supabase, 'park');
|
||||||
const pendingRideModels = await getPendingSubmissionItems(supabase, 'ride_model');
|
const pendingRideModels = await getPendingSubmissionItems(supabase, 'ride_model');
|
||||||
|
|
||||||
// Track both approved and pending items
|
// Track approved entities
|
||||||
existingOperators.forEach(op => {
|
existingOperators.forEach(op => {
|
||||||
createdCompanies.operator.push(op.slug);
|
createdCompanies.operator.push(op.slug);
|
||||||
if (op.submission_item_id) createdSubmissionItems.operator.push(op.submission_item_id);
|
if (op.submission_item_id) createdSubmissionItems.operator.push(op.submission_item_id);
|
||||||
@@ -260,7 +290,7 @@ Deno.serve(async (req) => {
|
|||||||
if (park.submission_item_id) createdSubmissionItems.park.push(park.submission_item_id);
|
if (park.submission_item_id) createdSubmissionItems.park.push(park.submission_item_id);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Add pending items to tracking
|
// Track pending items
|
||||||
pendingOperators.forEach(item => createdSubmissionItems.operator.push(item.id));
|
pendingOperators.forEach(item => createdSubmissionItems.operator.push(item.id));
|
||||||
pendingOwners.forEach(item => createdSubmissionItems.property_owner.push(item.id));
|
pendingOwners.forEach(item => createdSubmissionItems.property_owner.push(item.id));
|
||||||
pendingManufacturers.forEach(item => createdSubmissionItems.manufacturer.push(item.id));
|
pendingManufacturers.forEach(item => createdSubmissionItems.manufacturer.push(item.id));
|
||||||
@@ -268,6 +298,15 @@ Deno.serve(async (req) => {
|
|||||||
pendingParks.forEach(item => createdSubmissionItems.park.push(item.id));
|
pendingParks.forEach(item => createdSubmissionItems.park.push(item.id));
|
||||||
pendingRideModels.forEach(item => createdSubmissionItems.ride_model.push(item.id));
|
pendingRideModels.forEach(item => createdSubmissionItems.ride_model.push(item.id));
|
||||||
|
|
||||||
|
// Get slugs from pending parks for ride generation
|
||||||
|
for (const item of pendingParks) {
|
||||||
|
const slug = await getSubmissionSlug(supabase, 'park', item.item_data_id);
|
||||||
|
if (slug) {
|
||||||
|
createdParks.push(slug);
|
||||||
|
createdParkSlugs.push(slug);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
edgeLogger.info('Loaded existing entities', {
|
edgeLogger.info('Loaded existing entities', {
|
||||||
requestId: tracking.requestId,
|
requestId: tracking.requestId,
|
||||||
operators: existingOperators.length,
|
operators: existingOperators.length,
|
||||||
@@ -275,25 +314,11 @@ Deno.serve(async (req) => {
|
|||||||
manufacturers: existingManufacturers.length,
|
manufacturers: existingManufacturers.length,
|
||||||
designers: existingDesigners.length,
|
designers: existingDesigners.length,
|
||||||
parks: existingParks.length,
|
parks: existingParks.length,
|
||||||
pendingOperators: pendingOperators.length,
|
parksForRides: createdParks.length
|
||||||
pendingOwners: pendingOwners.length,
|
|
||||||
pendingManufacturers: pendingManufacturers.length,
|
|
||||||
pendingDesigners: pendingDesigners.length,
|
|
||||||
pendingParks: pendingParks.length
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Add pending parks' slugs to createdParks for ride generation
|
|
||||||
pendingParks.forEach(item => {
|
|
||||||
if (item.item_data?.slug) {
|
|
||||||
createdParks.push(item.item_data.slug);
|
|
||||||
createdParkSlugs.push(item.item_data.slug);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
edgeLogger.info('Parks available for rides', { requestId: tracking.requestId, count: createdParks.length });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper to create submission
|
// Helper to create submission using relational tables
|
||||||
async function createSubmission(
|
async function createSubmission(
|
||||||
userId: string,
|
userId: string,
|
||||||
type: string,
|
type: string,
|
||||||
@@ -302,24 +327,13 @@ Deno.serve(async (req) => {
|
|||||||
) {
|
) {
|
||||||
const submissionId = crypto.randomUUID();
|
const submissionId = crypto.randomUUID();
|
||||||
const itemId = crypto.randomUUID();
|
const itemId = crypto.randomUUID();
|
||||||
|
|
||||||
const contentData = {
|
|
||||||
action: 'create',
|
|
||||||
metadata: {
|
|
||||||
is_test_data: true,
|
|
||||||
generated_at: new Date().toISOString(),
|
|
||||||
generator_version: '2.0.0',
|
|
||||||
preset,
|
|
||||||
fieldDensity
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
// Create content_submission (WITHOUT content field)
|
||||||
const submissionData: any = {
|
const submissionData: any = {
|
||||||
id: submissionId,
|
id: submissionId,
|
||||||
user_id: userId,
|
user_id: userId,
|
||||||
submission_type: type,
|
submission_type: type,
|
||||||
status: 'pending',
|
status: 'pending',
|
||||||
content: contentData,
|
|
||||||
submitted_at: new Date().toISOString()
|
submitted_at: new Date().toISOString()
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -339,17 +353,7 @@ Deno.serve(async (req) => {
|
|||||||
throw subError;
|
throw subError;
|
||||||
}
|
}
|
||||||
|
|
||||||
const { error: itemError } = await supabase.from('submission_items').insert({
|
// Insert into type-specific submission table
|
||||||
id: itemId,
|
|
||||||
submission_id: submissionId,
|
|
||||||
item_type: type,
|
|
||||||
item_data: itemData,
|
|
||||||
status: 'pending',
|
|
||||||
order_index: 0,
|
|
||||||
depends_on: options.dependsOn || null
|
|
||||||
});
|
|
||||||
if (itemError) throw itemError;
|
|
||||||
|
|
||||||
const typeTableMap: Record<string, string> = {
|
const typeTableMap: Record<string, string> = {
|
||||||
park: 'park_submissions',
|
park: 'park_submissions',
|
||||||
ride: 'ride_submissions',
|
ride: 'ride_submissions',
|
||||||
@@ -361,24 +365,66 @@ Deno.serve(async (req) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const table = typeTableMap[type];
|
const table = typeTableMap[type];
|
||||||
|
let typeDataId: string | null = null;
|
||||||
|
|
||||||
if (table) {
|
if (table) {
|
||||||
const typeData = { ...itemData, submission_id: submissionId };
|
const typeData = { ...itemData, submission_id: submissionId };
|
||||||
if (table === 'company_submissions') {
|
if (table === 'company_submissions') {
|
||||||
typeData.company_type = type;
|
typeData.company_type = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
const { data: insertedData, error: typeError } = await supabase.from(table).insert(typeData).select('id').single();
|
const { data: insertedData, error: typeError } = await supabase
|
||||||
|
.from(table)
|
||||||
|
.insert(typeData)
|
||||||
|
.select('id')
|
||||||
|
.single();
|
||||||
|
|
||||||
if (typeError) {
|
if (typeError) {
|
||||||
edgeLogger.error('Error inserting into type table', { table, type, error: typeError.message, data: typeData });
|
edgeLogger.error('Error inserting into type table', { table, type, error: typeError.message });
|
||||||
throw typeError;
|
throw typeError;
|
||||||
}
|
}
|
||||||
return { submissionId, itemId, typeId: insertedData?.id };
|
|
||||||
|
typeDataId = insertedData?.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
return { submissionId, itemId, typeId: null };
|
// Create submission_item (WITHOUT item_data, using item_data_id)
|
||||||
|
const { error: itemError } = await supabase.from('submission_items').insert({
|
||||||
|
id: itemId,
|
||||||
|
submission_id: submissionId,
|
||||||
|
item_type: type,
|
||||||
|
item_data_id: typeDataId,
|
||||||
|
status: 'pending',
|
||||||
|
order_index: 0,
|
||||||
|
depends_on: options.dependsOn || null
|
||||||
|
});
|
||||||
|
|
||||||
|
if (itemError) {
|
||||||
|
edgeLogger.error('Error inserting submission_item', { type, error: itemError.message });
|
||||||
|
throw itemError;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add submission metadata for test data marking
|
||||||
|
await supabase.from('submission_metadata').insert({
|
||||||
|
submission_id: submissionId,
|
||||||
|
metadata_key: 'is_test_data',
|
||||||
|
metadata_value: 'true'
|
||||||
|
});
|
||||||
|
|
||||||
|
await supabase.from('submission_metadata').insert({
|
||||||
|
submission_id: submissionId,
|
||||||
|
metadata_key: 'generator_version',
|
||||||
|
metadata_value: '2.0.0'
|
||||||
|
});
|
||||||
|
|
||||||
|
await supabase.from('submission_metadata').insert({
|
||||||
|
submission_id: submissionId,
|
||||||
|
metadata_key: 'preset',
|
||||||
|
metadata_value: preset
|
||||||
|
});
|
||||||
|
|
||||||
|
return { submissionId, itemId, typeDataId };
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper function to properly pluralize company types
|
|
||||||
const pluralizeCompanyType = (singular: string): string => {
|
const pluralizeCompanyType = (singular: string): string => {
|
||||||
const pluralMap: Record<string, string> = {
|
const pluralMap: Record<string, string> = {
|
||||||
'manufacturer': 'manufacturers',
|
'manufacturer': 'manufacturers',
|
||||||
@@ -389,11 +435,9 @@ Deno.serve(async (req) => {
|
|||||||
return pluralMap[singular] || singular + 's';
|
return pluralMap[singular] || singular + 's';
|
||||||
};
|
};
|
||||||
|
|
||||||
// Create companies FIRST so parks can reference operators/owners
|
// Create companies
|
||||||
if (!stage || stage === 'companies') {
|
if (!stage || stage === 'companies') {
|
||||||
const companyTypes = ['manufacturer', 'operator', 'designer', 'property_owner'];
|
const companyTypes = ['manufacturer', 'operator', 'designer', 'property_owner'];
|
||||||
|
|
||||||
// First, determine which company types are selected
|
|
||||||
const selectedCompanyTypes = companyTypes.filter(compType =>
|
const selectedCompanyTypes = companyTypes.filter(compType =>
|
||||||
entityTypes.includes(pluralizeCompanyType(compType))
|
entityTypes.includes(pluralizeCompanyType(compType))
|
||||||
);
|
);
|
||||||
@@ -405,79 +449,71 @@ Deno.serve(async (req) => {
|
|||||||
selectedCompanyTypes
|
selectedCompanyTypes
|
||||||
});
|
});
|
||||||
|
|
||||||
// Calculate fair distribution: base amount per type + extras
|
|
||||||
const basePerType = Math.floor(plan.companies / selectedCompanyTypes.length);
|
const basePerType = Math.floor(plan.companies / selectedCompanyTypes.length);
|
||||||
const extras = plan.companies % selectedCompanyTypes.length;
|
const extras = plan.companies % selectedCompanyTypes.length;
|
||||||
|
|
||||||
let companiesCreatedTotal = 0;
|
|
||||||
|
|
||||||
for (let typeIndex = 0; typeIndex < selectedCompanyTypes.length; typeIndex++) {
|
for (let typeIndex = 0; typeIndex < selectedCompanyTypes.length; typeIndex++) {
|
||||||
const compType = selectedCompanyTypes[typeIndex];
|
const compType = selectedCompanyTypes[typeIndex];
|
||||||
const pluralType = pluralizeCompanyType(compType);
|
const count = basePerType + (typeIndex < extras ? 1 : 0);
|
||||||
|
|
||||||
// Each type gets base amount, first N types get +1 extra
|
|
||||||
const count = basePerType + (typeIndex < extras ? 1 : 0);
|
|
||||||
|
|
||||||
edgeLogger.info('Creating companies', { requestId: tracking.requestId, compType, count });
|
|
||||||
|
|
||||||
for (let i = 0; i < count; i++) {
|
|
||||||
const level = getPopulationLevel(fieldDensity, i);
|
|
||||||
const companyData: any = {
|
|
||||||
name: `Test ${compType.replace('_', ' ')} ${i + 1}`,
|
|
||||||
slug: `test-${compType}-${i + 1}`,
|
|
||||||
company_type: compType
|
|
||||||
};
|
|
||||||
|
|
||||||
if (level >= 1) {
|
|
||||||
companyData.description = `Leading ${compType.replace('_', ' ')} in the amusement industry.`;
|
|
||||||
companyData.person_type = compType === 'designer' && Math.random() > 0.5 ? 'individual' : 'company';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (level >= 2) {
|
|
||||||
companyData.founded_year = randomInt(1950, 2020);
|
|
||||||
const location = randomItem(CITIES);
|
|
||||||
companyData.headquarters_location = `${location.city}, ${location.country}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (level >= 3) {
|
|
||||||
companyData.website_url = `https://test-${compType}-${i + 1}.example.com`;
|
|
||||||
companyData.logo_url = `https://cdn.thrillwiki.com/images/test-${compType}-${i + 1}-logo/logo`;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (level >= 4) {
|
|
||||||
companyData.card_image_id = `test-${compType}-card-${i + 1}`;
|
|
||||||
companyData.card_image_url = `https://cdn.thrillwiki.com/images/test-${compType}-card-${i + 1}/card`;
|
|
||||||
companyData.banner_image_id = `test-${compType}-banner-${i + 1}`;
|
|
||||||
companyData.banner_image_url = `https://cdn.thrillwiki.com/images/test-${compType}-banner-${i + 1}/banner`;
|
|
||||||
}
|
|
||||||
|
|
||||||
const { itemId } = await createSubmission(user.id, compType, companyData);
|
|
||||||
|
|
||||||
// Track created submission item
|
edgeLogger.info('Creating companies', { requestId: tracking.requestId, compType, count });
|
||||||
createdSubmissionItems[compType].push(itemId);
|
|
||||||
|
|
||||||
// Register newly created company in the registry
|
for (let i = 0; i < count; i++) {
|
||||||
const companySlug = `test-${compType}-${i + 1}`;
|
const level = getPopulationLevel(fieldDensity, i);
|
||||||
const { data: approvedCompany } = await supabase
|
const companyData: any = {
|
||||||
.from('companies')
|
name: `Test ${compType.replace('_', ' ')} ${i + 1}`,
|
||||||
.select('id')
|
slug: `test-${compType}-${i + 1}`,
|
||||||
.eq('slug', companySlug)
|
company_type: compType
|
||||||
.maybeSingle();
|
};
|
||||||
|
|
||||||
if (approvedCompany) {
|
if (level >= 1) {
|
||||||
await registerTestEntity(supabase, compType, companySlug, approvedCompany.id, itemId, sessionId);
|
companyData.description = `Leading ${compType.replace('_', ' ')} in the amusement industry.`;
|
||||||
|
companyData.person_type = compType === 'designer' && Math.random() > 0.5 ? 'individual' : 'company';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (level >= 2) {
|
||||||
|
companyData.founded_year = randomInt(1950, 2020);
|
||||||
|
const location = randomItem(CITIES);
|
||||||
|
companyData.headquarters_location = `${location.city}, ${location.country}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (level >= 3) {
|
||||||
|
companyData.website_url = `https://test-${compType}-${i + 1}.example.com`;
|
||||||
|
companyData.logo_url = `https://cdn.thrillwiki.com/images/test-${compType}-${i + 1}-logo/logo`;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (level >= 4) {
|
||||||
|
companyData.card_image_id = `test-${compType}-card-${i + 1}`;
|
||||||
|
companyData.card_image_url = `https://cdn.thrillwiki.com/images/test-${compType}-card-${i + 1}/card`;
|
||||||
|
companyData.banner_image_id = `test-${compType}-banner-${i + 1}`;
|
||||||
|
companyData.banner_image_url = `https://cdn.thrillwiki.com/images/test-${compType}-banner-${i + 1}/banner`;
|
||||||
|
}
|
||||||
|
|
||||||
|
const { itemId } = await createSubmission(user.id, compType, companyData);
|
||||||
|
|
||||||
|
createdSubmissionItems[compType].push(itemId);
|
||||||
|
|
||||||
|
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, itemId, sessionId);
|
||||||
|
}
|
||||||
|
|
||||||
|
createdCompanies[compType].push(companySlug);
|
||||||
|
summary.companies++;
|
||||||
}
|
}
|
||||||
|
|
||||||
createdCompanies[compType].push(companySlug);
|
|
||||||
summary.companies++;
|
|
||||||
companiesCreatedTotal++;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Create parks
|
// Create parks
|
||||||
if ((!stage || stage === 'parks') && entityTypes.includes('parks')) {
|
if ((!stage || stage === 'parks') && entityTypes.includes('parks')) {
|
||||||
edgeLogger.info('Creating parks', { requestId: tracking.requestId, count: plan.parks });
|
edgeLogger.info('Creating parks', { requestId: tracking.requestId, count: plan.parks });
|
||||||
|
|
||||||
for (let i = 0; i < plan.parks; i++) {
|
for (let i = 0; i < plan.parks; i++) {
|
||||||
const level = getPopulationLevel(fieldDensity, i);
|
const level = getPopulationLevel(fieldDensity, i);
|
||||||
const shouldConflict = includeConflicts && createdParkSlugs.length > 0 && Math.random() < 0.15;
|
const shouldConflict = includeConflicts && createdParkSlugs.length > 0 && Math.random() < 0.15;
|
||||||
@@ -513,9 +549,13 @@ Deno.serve(async (req) => {
|
|||||||
let ownerItemId: string | undefined;
|
let ownerItemId: string | undefined;
|
||||||
|
|
||||||
if (level >= 3 && createdCompanies.operator.length > 0) {
|
if (level >= 3 && createdCompanies.operator.length > 0) {
|
||||||
const { data: operatorData } = await supabase.from('companies').select('id').eq('slug', randomItem(createdCompanies.operator)).maybeSingle();
|
const { data: operatorData } = await supabase
|
||||||
|
.from('companies')
|
||||||
|
.select('id')
|
||||||
|
.eq('slug', randomItem(createdCompanies.operator))
|
||||||
|
.maybeSingle();
|
||||||
if (operatorData) parkData.operator_id = operatorData.id;
|
if (operatorData) parkData.operator_id = operatorData.id;
|
||||||
// Track operator dependency
|
|
||||||
if (createdSubmissionItems.operator.length > 0) {
|
if (createdSubmissionItems.operator.length > 0) {
|
||||||
operatorItemId = randomItem(createdSubmissionItems.operator);
|
operatorItemId = randomItem(createdSubmissionItems.operator);
|
||||||
}
|
}
|
||||||
@@ -526,9 +566,13 @@ Deno.serve(async (req) => {
|
|||||||
|
|
||||||
if (level >= 4) {
|
if (level >= 4) {
|
||||||
if (createdCompanies.property_owner.length > 0) {
|
if (createdCompanies.property_owner.length > 0) {
|
||||||
const { data: ownerData } = await supabase.from('companies').select('id').eq('slug', randomItem(createdCompanies.property_owner)).maybeSingle();
|
const { data: ownerData } = await supabase
|
||||||
|
.from('companies')
|
||||||
|
.select('id')
|
||||||
|
.eq('slug', randomItem(createdCompanies.property_owner))
|
||||||
|
.maybeSingle();
|
||||||
if (ownerData) parkData.property_owner_id = ownerData.id;
|
if (ownerData) parkData.property_owner_id = ownerData.id;
|
||||||
// Track owner dependency (prefer operator if both exist)
|
|
||||||
if (createdSubmissionItems.property_owner.length > 0 && !operatorItemId) {
|
if (createdSubmissionItems.property_owner.length > 0 && !operatorItemId) {
|
||||||
ownerItemId = randomItem(createdSubmissionItems.property_owner);
|
ownerItemId = randomItem(createdSubmissionItems.property_owner);
|
||||||
}
|
}
|
||||||
@@ -549,10 +593,8 @@ Deno.serve(async (req) => {
|
|||||||
|
|
||||||
const { itemId } = await createSubmission(user.id, 'park', parkData, options);
|
const { itemId } = await createSubmission(user.id, 'park', parkData, options);
|
||||||
|
|
||||||
// Track created submission item
|
|
||||||
createdSubmissionItems.park.push(itemId);
|
createdSubmissionItems.park.push(itemId);
|
||||||
|
|
||||||
// Register newly created park in the registry
|
|
||||||
const { data: approvedPark } = await supabase
|
const { data: approvedPark } = await supabase
|
||||||
.from('parks')
|
.from('parks')
|
||||||
.select('id')
|
.select('id')
|
||||||
@@ -571,9 +613,10 @@ Deno.serve(async (req) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Create rides
|
// Create rides
|
||||||
if ((!stage || stage === 'rides') && entityTypes.includes('rides') && includeDependencies && createdParks.length > 0) {
|
if ((!stage || stage === 'rides') && entityTypes.includes('rides') && includeDependencies && createdParks.length > 0) {
|
||||||
|
edgeLogger.info('Creating rides', { requestId: tracking.requestId, count: plan.rides });
|
||||||
|
|
||||||
for (let i = 0; i < plan.rides; i++) {
|
for (let i = 0; i < plan.rides; i++) {
|
||||||
const level = getPopulationLevel(fieldDensity, i);
|
const level = getPopulationLevel(fieldDensity, i);
|
||||||
const shouldConflict = includeConflicts && createdRideSlugs.length > 0 && Math.random() < 0.15;
|
const shouldConflict = includeConflicts && createdRideSlugs.length > 0 && Math.random() < 0.15;
|
||||||
@@ -589,9 +632,12 @@ Deno.serve(async (req) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const parkSlug = randomItem(createdParks);
|
const parkSlug = randomItem(createdParks);
|
||||||
const { data: parkData } = await supabase.from('parks').select('id').eq('slug', parkSlug).maybeSingle();
|
const { data: parkData } = await supabase
|
||||||
|
.from('parks')
|
||||||
|
.select('id')
|
||||||
|
.eq('slug', parkSlug)
|
||||||
|
.maybeSingle();
|
||||||
|
|
||||||
// Get park dependency for depends_on
|
|
||||||
const parkItemId = createdSubmissionItems.park.length > 0 ? randomItem(createdSubmissionItems.park) : undefined;
|
const parkItemId = createdSubmissionItems.park.length > 0 ? randomItem(createdSubmissionItems.park) : undefined;
|
||||||
|
|
||||||
const category = randomItem(['roller_coaster', 'flat_ride', 'water_ride', 'dark_ride']);
|
const category = randomItem(['roller_coaster', 'flat_ride', 'water_ride', 'dark_ride']);
|
||||||
@@ -619,7 +665,11 @@ Deno.serve(async (req) => {
|
|||||||
|
|
||||||
if (level >= 3) {
|
if (level >= 3) {
|
||||||
if (createdCompanies.manufacturer.length > 0) {
|
if (createdCompanies.manufacturer.length > 0) {
|
||||||
const { data: mfgData } = await supabase.from('companies').select('id').eq('slug', randomItem(createdCompanies.manufacturer)).maybeSingle();
|
const { data: mfgData } = await supabase
|
||||||
|
.from('companies')
|
||||||
|
.select('id')
|
||||||
|
.eq('slug', randomItem(createdCompanies.manufacturer))
|
||||||
|
.maybeSingle();
|
||||||
if (mfgData) rideData.manufacturer_id = mfgData.id;
|
if (mfgData) rideData.manufacturer_id = mfgData.id;
|
||||||
}
|
}
|
||||||
if (category === 'roller_coaster') {
|
if (category === 'roller_coaster') {
|
||||||
@@ -633,7 +683,11 @@ Deno.serve(async (req) => {
|
|||||||
|
|
||||||
if (level >= 4) {
|
if (level >= 4) {
|
||||||
if (createdCompanies.designer.length > 0) {
|
if (createdCompanies.designer.length > 0) {
|
||||||
const { data: designerData } = await supabase.from('companies').select('id').eq('slug', randomItem(createdCompanies.designer)).maybeSingle();
|
const { data: designerData } = await supabase
|
||||||
|
.from('companies')
|
||||||
|
.select('id')
|
||||||
|
.eq('slug', randomItem(createdCompanies.designer))
|
||||||
|
.maybeSingle();
|
||||||
if (designerData) rideData.designer_id = designerData.id;
|
if (designerData) rideData.designer_id = designerData.id;
|
||||||
}
|
}
|
||||||
rideData.length_meters = randomInt(500, 2000);
|
rideData.length_meters = randomInt(500, 2000);
|
||||||
@@ -647,14 +701,13 @@ Deno.serve(async (req) => {
|
|||||||
dependsOn: parkItemId
|
dependsOn: parkItemId
|
||||||
};
|
};
|
||||||
|
|
||||||
const { submissionId, itemId, typeId } = await createSubmission(user.id, 'ride', rideData, options);
|
const { submissionId, itemId, typeDataId } = await createSubmission(user.id, 'ride', rideData, options);
|
||||||
|
|
||||||
// Add technical specs and stats for level 4
|
// Add relational data for level 4 rides
|
||||||
if (level >= 4 && typeId && category === 'roller_coaster') {
|
if (level >= 4 && typeDataId && category === 'roller_coaster') {
|
||||||
// Add coaster stats
|
|
||||||
for (let s = 0; s < randomInt(2, 3); s++) {
|
for (let s = 0; s < randomInt(2, 3); s++) {
|
||||||
await supabase.from('ride_submission_coaster_statistics').insert({
|
await supabase.from('ride_submission_coaster_statistics').insert({
|
||||||
ride_submission_id: typeId,
|
ride_submission_id: typeDataId,
|
||||||
stat_name: randomItem(['Airtime Duration', 'Zero-G Time', 'Track Gauge']),
|
stat_name: randomItem(['Airtime Duration', 'Zero-G Time', 'Track Gauge']),
|
||||||
stat_value: Math.random() * 100,
|
stat_value: Math.random() * 100,
|
||||||
unit: randomItem(['seconds', 'mm']),
|
unit: randomItem(['seconds', 'mm']),
|
||||||
@@ -662,10 +715,9 @@ Deno.serve(async (req) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add technical specs
|
|
||||||
for (let t = 0; t < randomInt(3, 5); t++) {
|
for (let t = 0; t < randomInt(3, 5); t++) {
|
||||||
await supabase.from('ride_submission_technical_specifications').insert({
|
await supabase.from('ride_submission_technical_specifications').insert({
|
||||||
ride_submission_id: typeId,
|
ride_submission_id: typeDataId,
|
||||||
spec_name: randomItem(['Lift System', 'Brake System', 'Train Count', 'Track Material']),
|
spec_name: randomItem(['Lift System', 'Brake System', 'Train Count', 'Track Material']),
|
||||||
spec_value: randomItem(['Chain lift', 'Magnetic brakes', '3 trains', 'Steel tubular']),
|
spec_value: randomItem(['Chain lift', 'Magnetic brakes', '3 trains', 'Steel tubular']),
|
||||||
spec_type: 'system',
|
spec_type: 'system',
|
||||||
@@ -673,10 +725,9 @@ Deno.serve(async (req) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add former name
|
|
||||||
if (Math.random() > 0.7) {
|
if (Math.random() > 0.7) {
|
||||||
await supabase.from('ride_submission_name_history').insert({
|
await supabase.from('ride_submission_name_history').insert({
|
||||||
ride_submission_id: typeId,
|
ride_submission_id: typeDataId,
|
||||||
former_name: `Original Name ${i + 1}`,
|
former_name: `Original Name ${i + 1}`,
|
||||||
date_changed: randomDate(2010, 2020),
|
date_changed: randomDate(2010, 2020),
|
||||||
reason: 'Rebranding',
|
reason: 'Rebranding',
|
||||||
@@ -685,7 +736,6 @@ Deno.serve(async (req) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Register newly created ride in the registry
|
|
||||||
const { data: approvedRide } = await supabase
|
const { data: approvedRide } = await supabase
|
||||||
.from('rides')
|
.from('rides')
|
||||||
.select('id')
|
.select('id')
|
||||||
@@ -705,27 +755,32 @@ Deno.serve(async (req) => {
|
|||||||
|
|
||||||
// Create ride models
|
// Create ride models
|
||||||
if ((!stage || stage === 'rides') && entityTypes.includes('ride_models') && includeDependencies && createdSubmissionItems.manufacturer.length > 0) {
|
if ((!stage || stage === 'rides') && entityTypes.includes('ride_models') && includeDependencies && createdSubmissionItems.manufacturer.length > 0) {
|
||||||
|
edgeLogger.info('Creating ride models', { requestId: tracking.requestId, count: plan.rideModels });
|
||||||
|
|
||||||
for (let i = 0; i < plan.rideModels; i++) {
|
for (let i = 0; i < plan.rideModels; i++) {
|
||||||
const level = getPopulationLevel(fieldDensity, i);
|
const level = getPopulationLevel(fieldDensity, i);
|
||||||
const { data: mfgData } = await supabase.from('companies').select('id').eq('slug', randomItem(createdCompanies.manufacturer)).maybeSingle();
|
const { data: mfgData } = await supabase
|
||||||
|
.from('companies')
|
||||||
|
.select('id')
|
||||||
|
.eq('slug', randomItem(createdCompanies.manufacturer))
|
||||||
|
.maybeSingle();
|
||||||
|
|
||||||
// Get manufacturer dependency for depends_on
|
|
||||||
const mfgItemId = createdSubmissionItems.manufacturer.length > 0 ? randomItem(createdSubmissionItems.manufacturer) : undefined;
|
const mfgItemId = createdSubmissionItems.manufacturer.length > 0 ? randomItem(createdSubmissionItems.manufacturer) : undefined;
|
||||||
|
|
||||||
const category = randomItem(['roller_coaster', 'flat_ride', 'water_ride']);
|
const category = randomItem(['roller_coaster', 'flat_ride', 'water_ride']);
|
||||||
const rideType = randomItem(['spinning', 'launch', 'suspended', 'family', 'standard']);
|
const rideType = randomItem(['spinning', 'launch', 'suspended', 'family', 'standard']);
|
||||||
|
|
||||||
const modelData: any = {
|
const modelData: any = {
|
||||||
name: `Test Model ${i + 1}`,
|
name: `Test Model ${i + 1}`,
|
||||||
slug: `test-model-${i + 1}`,
|
slug: `test-model-${i + 1}`,
|
||||||
category: category,
|
category: category,
|
||||||
ride_type: rideType,
|
ride_type: rideType,
|
||||||
manufacturer_id: mfgData?.id || null
|
manufacturer_id: mfgData?.id || null
|
||||||
};
|
};
|
||||||
|
|
||||||
if (level >= 1) {
|
if (level >= 1) {
|
||||||
modelData.description = `Popular ${category.replace('_', ' ')} model.`;
|
modelData.description = `Popular ${category.replace('_', ' ')} model.`;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (level >= 2) {
|
if (level >= 2) {
|
||||||
modelData.card_image_id = `test-model-card-${i + 1}`;
|
modelData.card_image_id = `test-model-card-${i + 1}`;
|
||||||
@@ -743,10 +798,8 @@ Deno.serve(async (req) => {
|
|||||||
|
|
||||||
const { itemId } = await createSubmission(user.id, 'ride_model', modelData, options);
|
const { itemId } = await createSubmission(user.id, 'ride_model', modelData, options);
|
||||||
|
|
||||||
// Track created submission item
|
|
||||||
createdSubmissionItems.ride_model.push(itemId);
|
createdSubmissionItems.ride_model.push(itemId);
|
||||||
|
|
||||||
// Register newly created ride model in the registry
|
|
||||||
const modelSlug = `test-model-${i + 1}`;
|
const modelSlug = `test-model-${i + 1}`;
|
||||||
const { data: approvedModel } = await supabase
|
const { data: approvedModel } = await supabase
|
||||||
.from('ride_models')
|
.from('ride_models')
|
||||||
@@ -764,6 +817,8 @@ Deno.serve(async (req) => {
|
|||||||
|
|
||||||
// Create photo submissions
|
// Create photo submissions
|
||||||
if ((!stage || stage === 'photos') && entityTypes.includes('photos') && plan.photos > 0) {
|
if ((!stage || stage === 'photos') && entityTypes.includes('photos') && plan.photos > 0) {
|
||||||
|
edgeLogger.info('Creating photo submissions', { requestId: tracking.requestId, count: plan.photos });
|
||||||
|
|
||||||
const { data: approvedParks } = await supabase.from('parks').select('id').limit(Math.min(20, plan.photos));
|
const { data: approvedParks } = await supabase.from('parks').select('id').limit(Math.min(20, plan.photos));
|
||||||
const { data: approvedRides } = await supabase.from('rides').select('id, park_id').limit(Math.min(20, plan.photos));
|
const { data: approvedRides } = await supabase.from('rides').select('id, park_id').limit(Math.min(20, plan.photos));
|
||||||
|
|
||||||
@@ -788,24 +843,22 @@ Deno.serve(async (req) => {
|
|||||||
|
|
||||||
if (!entityId) continue;
|
if (!entityId) continue;
|
||||||
|
|
||||||
// Create content_submission
|
// Create content_submission (WITHOUT content field)
|
||||||
await supabase.from('content_submissions').insert({
|
await supabase.from('content_submissions').insert({
|
||||||
id: submissionId,
|
id: submissionId,
|
||||||
user_id: user.id,
|
user_id: user.id,
|
||||||
submission_type: 'photo',
|
submission_type: 'photo',
|
||||||
status: 'pending',
|
status: 'pending',
|
||||||
content: {
|
|
||||||
action: 'create',
|
|
||||||
metadata: {
|
|
||||||
is_test_data: true,
|
|
||||||
generated_at: new Date().toISOString(),
|
|
||||||
generator_version: '2.0.0',
|
|
||||||
preset
|
|
||||||
}
|
|
||||||
},
|
|
||||||
submitted_at: new Date().toISOString()
|
submitted_at: new Date().toISOString()
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Mark as test data
|
||||||
|
await supabase.from('submission_metadata').insert({
|
||||||
|
submission_id: submissionId,
|
||||||
|
metadata_key: 'is_test_data',
|
||||||
|
metadata_value: 'true'
|
||||||
|
});
|
||||||
|
|
||||||
// Create photo_submission
|
// Create photo_submission
|
||||||
await supabase.from('photo_submissions').insert({
|
await supabase.from('photo_submissions').insert({
|
||||||
id: photoSubmissionId,
|
id: photoSubmissionId,
|
||||||
|
|||||||
Reference in New Issue
Block a user