mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-20 08:11:13 -05:00
Fix slug uniqueness validation
This commit is contained in:
@@ -345,19 +345,86 @@ export async function validateEntityData(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check slug uniqueness (async)
|
// Check slug uniqueness (async) - only if slug has changed
|
||||||
if (validData.slug && typeof validData.slug === 'string') {
|
if (validData.slug && typeof validData.slug === 'string') {
|
||||||
const isSlugUnique = await checkSlugUniqueness(
|
// Extract the correct ID field based on entity type
|
||||||
entityType,
|
let entityId: string | undefined;
|
||||||
validData.slug,
|
|
||||||
typeof validData.id === 'string' ? validData.id : undefined
|
switch (entityType) {
|
||||||
);
|
case 'park':
|
||||||
if (!isSlugUnique) {
|
entityId = typeof validData.park_id === 'string' ? validData.park_id : undefined;
|
||||||
blockingErrors.push({
|
break;
|
||||||
field: 'slug',
|
case 'ride':
|
||||||
message: 'This slug is already in use. Manually check if this entity already exists, and reject if so. Otherwise, escalate to an admin for manual editing of the slug.',
|
entityId = typeof validData.ride_id === 'string' ? validData.ride_id : undefined;
|
||||||
severity: 'blocking',
|
break;
|
||||||
});
|
case 'manufacturer':
|
||||||
|
case 'designer':
|
||||||
|
case 'operator':
|
||||||
|
case 'property_owner':
|
||||||
|
entityId = typeof validData.company_id === 'string'
|
||||||
|
? validData.company_id
|
||||||
|
: (typeof validData.id === 'string' ? validData.id : undefined);
|
||||||
|
break;
|
||||||
|
case 'ride_model':
|
||||||
|
entityId = typeof validData.ride_model_id === 'string'
|
||||||
|
? validData.ride_model_id
|
||||||
|
: (typeof validData.id === 'string' ? validData.id : undefined);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
entityId = typeof validData.id === 'string' ? validData.id : undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we have an entity ID, check if slug has actually changed
|
||||||
|
let shouldCheckUniqueness = true;
|
||||||
|
if (entityId) {
|
||||||
|
const tableName = getTableNameFromEntityType(entityType);
|
||||||
|
|
||||||
|
// Use switch to avoid TypeScript type instantiation issues
|
||||||
|
let originalSlug: string | null = null;
|
||||||
|
switch (tableName) {
|
||||||
|
case 'parks': {
|
||||||
|
const { data } = await supabase.from('parks').select('slug').eq('id', entityId).single();
|
||||||
|
originalSlug = data?.slug || null;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'rides': {
|
||||||
|
const { data } = await supabase.from('rides').select('slug').eq('id', entityId).single();
|
||||||
|
originalSlug = data?.slug || null;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'companies': {
|
||||||
|
const { data } = await supabase.from('companies').select('slug').eq('id', entityId).single();
|
||||||
|
originalSlug = data?.slug || null;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'ride_models': {
|
||||||
|
const { data } = await supabase.from('ride_models').select('slug').eq('id', entityId).single();
|
||||||
|
originalSlug = data?.slug || null;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If slug hasn't changed, skip uniqueness check
|
||||||
|
if (originalSlug && originalSlug === validData.slug) {
|
||||||
|
shouldCheckUniqueness = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only check uniqueness if this is a new entity or slug has changed
|
||||||
|
if (shouldCheckUniqueness) {
|
||||||
|
const isSlugUnique = await checkSlugUniqueness(
|
||||||
|
entityType,
|
||||||
|
validData.slug,
|
||||||
|
entityId
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!isSlugUnique) {
|
||||||
|
blockingErrors.push({
|
||||||
|
field: 'slug',
|
||||||
|
message: 'This slug is already in use. Manually check if this entity already exists, and reject if so. Otherwise, escalate to an admin for manual editing of the slug.',
|
||||||
|
severity: 'blocking',
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user