mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-24 08:51:16 -05:00
Refactor: Implement type safety plan
This commit is contained in:
@@ -346,11 +346,39 @@ async function checkSlugUniqueness(
|
||||
try {
|
||||
console.log(`Checking slug uniqueness for "${slug}" in ${tableName}, excludeId: ${excludeId}`);
|
||||
|
||||
const { data, error } = await supabase
|
||||
.from(tableName as any)
|
||||
.select('id')
|
||||
.eq('slug', slug)
|
||||
.limit(1);
|
||||
// Type-safe queries for each table
|
||||
type ValidTableName = 'parks' | 'rides' | 'companies' | 'ride_models' | 'photos';
|
||||
|
||||
// Validate table name
|
||||
const validTables: ValidTableName[] = ['parks', 'rides', 'companies', 'ride_models', 'photos'];
|
||||
if (!validTables.includes(tableName as ValidTableName)) {
|
||||
console.error(`Invalid table name: ${tableName}`);
|
||||
return true; // Assume unique on invalid table
|
||||
}
|
||||
|
||||
// Query with explicit table name
|
||||
let data, error;
|
||||
if (tableName === 'parks') {
|
||||
const result = await supabase.from('parks').select('id').eq('slug', slug).limit(1);
|
||||
data = result.data;
|
||||
error = result.error;
|
||||
} else if (tableName === 'rides') {
|
||||
const result = await supabase.from('rides').select('id').eq('slug', slug).limit(1);
|
||||
data = result.data;
|
||||
error = result.error;
|
||||
} else if (tableName === 'companies') {
|
||||
const result = await supabase.from('companies').select('id').eq('slug', slug).limit(1);
|
||||
data = result.data;
|
||||
error = result.error;
|
||||
} else if (tableName === 'ride_models') {
|
||||
const result = await supabase.from('ride_models').select('id').eq('slug', slug).limit(1);
|
||||
data = result.data;
|
||||
error = result.error;
|
||||
} else {
|
||||
const result = await supabase.from('photos').select('id').eq('slug', slug).limit(1);
|
||||
data = result.data;
|
||||
error = result.error;
|
||||
}
|
||||
|
||||
if (error) {
|
||||
console.error(`Slug uniqueness check failed for ${entityType}:`, error);
|
||||
@@ -364,7 +392,7 @@ async function checkSlugUniqueness(
|
||||
}
|
||||
|
||||
// If excludeId provided and matches, it's the same entity (editing)
|
||||
if (excludeId && data[0] && ((data[0] as unknown) as { id: string }).id === excludeId) {
|
||||
if (excludeId && data[0] && data[0].id === excludeId) {
|
||||
console.log(`Slug "${slug}" matches current entity (editing mode)`);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -14,6 +14,23 @@ interface EntityCache {
|
||||
companies: Map<string, { id: string; name: string }>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generic submission content type
|
||||
*/
|
||||
interface GenericSubmissionContent {
|
||||
name?: string;
|
||||
entity_id?: string;
|
||||
entity_name?: string;
|
||||
park_id?: string;
|
||||
ride_id?: string;
|
||||
company_id?: string;
|
||||
manufacturer_id?: string;
|
||||
designer_id?: string;
|
||||
operator_id?: string;
|
||||
property_owner_id?: string;
|
||||
[key: string]: unknown;
|
||||
}
|
||||
|
||||
/**
|
||||
* Result of entity name resolution
|
||||
*/
|
||||
@@ -46,7 +63,7 @@ export interface ResolvedEntityNames {
|
||||
*/
|
||||
export function resolveEntityName(
|
||||
submissionType: string,
|
||||
content: any,
|
||||
content: GenericSubmissionContent | null | undefined,
|
||||
entityCache: EntityCache
|
||||
): ResolvedEntityNames {
|
||||
let entityName = content?.name || 'Unknown';
|
||||
@@ -134,7 +151,7 @@ export function getEntityDisplayName(
|
||||
* @param submissions - Array of submission objects
|
||||
* @returns Object containing Sets of IDs for each entity type
|
||||
*/
|
||||
export function extractEntityIds(submissions: any[]): {
|
||||
export function extractEntityIds(submissions: Array<{ content: unknown; submission_type: string }>): {
|
||||
rideIds: Set<string>;
|
||||
parkIds: Set<string>;
|
||||
companyIds: Set<string>;
|
||||
@@ -144,7 +161,7 @@ export function extractEntityIds(submissions: any[]): {
|
||||
const companyIds = new Set<string>();
|
||||
|
||||
submissions.forEach(submission => {
|
||||
const content = submission.content as any;
|
||||
const content = submission.content as GenericSubmissionContent | null | undefined;
|
||||
if (content && typeof content === 'object') {
|
||||
// Direct entity references
|
||||
if (content.ride_id) rideIds.add(content.ride_id);
|
||||
|
||||
@@ -229,6 +229,9 @@ class NotificationService {
|
||||
}
|
||||
|
||||
// Create audit log entry
|
||||
// DOCUMENTED EXCEPTION: profile_audit_log.changes column accepts JSONB
|
||||
// We validate the preferences structure with Zod before this point
|
||||
// Safe because the payload is constructed type-safely earlier in the function
|
||||
await supabase.from('profile_audit_log').insert([{
|
||||
user_id: userId,
|
||||
changed_by: userId,
|
||||
|
||||
96
src/lib/unitValidation.ts
Normal file
96
src/lib/unitValidation.ts
Normal file
@@ -0,0 +1,96 @@
|
||||
/**
|
||||
* Unit Validation Utilities
|
||||
* Ensures all stored units comply with metric-only storage rule
|
||||
*
|
||||
* Custom Knowledge Requirement:
|
||||
* "Unit Conversion Rules: Storage: Always metric in DB (km/h, m, cm, kg)"
|
||||
*/
|
||||
|
||||
export const METRIC_UNITS = [
|
||||
'km/h', // Speed
|
||||
'm', // Distance (large)
|
||||
'cm', // Distance (small)
|
||||
'kg', // Weight
|
||||
'g', // Weight (small)
|
||||
'G', // G-force
|
||||
'celsius', // Temperature
|
||||
'seconds', // Time
|
||||
'minutes', // Time
|
||||
'hours', // Time
|
||||
'count', // Dimensionless
|
||||
'%', // Percentage
|
||||
] as const;
|
||||
|
||||
export const IMPERIAL_UNITS = [
|
||||
'mph', // Speed
|
||||
'ft', // Distance
|
||||
'in', // Distance
|
||||
'lbs', // Weight
|
||||
'fahrenheit', // Temperature
|
||||
] as const;
|
||||
|
||||
export type MetricUnit = typeof METRIC_UNITS[number];
|
||||
export type ImperialUnit = typeof IMPERIAL_UNITS[number];
|
||||
|
||||
/**
|
||||
* Check if a unit is metric
|
||||
*/
|
||||
export function isMetricUnit(unit: string): unit is MetricUnit {
|
||||
return METRIC_UNITS.includes(unit as MetricUnit);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that a unit is metric (throws if not)
|
||||
*/
|
||||
export function validateMetricUnit(unit: string, fieldName: string = 'unit'): void {
|
||||
if (!isMetricUnit(unit)) {
|
||||
throw new Error(
|
||||
`${fieldName} must be metric. Received "${unit}", expected one of: ${METRIC_UNITS.join(', ')}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure value is in metric units, converting if necessary
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const { value, unit } = ensureMetricUnit(60, 'mph');
|
||||
* // Returns: { value: 96.56, unit: 'km/h' }
|
||||
* ```
|
||||
*/
|
||||
export function ensureMetricUnit(
|
||||
value: number,
|
||||
unit: string
|
||||
): { value: number; unit: MetricUnit } {
|
||||
if (isMetricUnit(unit)) {
|
||||
return { value, unit };
|
||||
}
|
||||
|
||||
// Convert imperial to metric
|
||||
const { convertValueToMetric, getMetricUnit } = require('./units');
|
||||
const metricValue = convertValueToMetric(value, unit);
|
||||
const metricUnit = getMetricUnit(unit) as MetricUnit;
|
||||
|
||||
return { value: metricValue, unit: metricUnit };
|
||||
}
|
||||
|
||||
/**
|
||||
* Batch validate an array of measurements
|
||||
*/
|
||||
export function validateMetricUnits(
|
||||
measurements: Array<{ value: number; unit: string; name: string }>
|
||||
): { valid: boolean; errors: string[] } {
|
||||
const errors: string[] = [];
|
||||
|
||||
measurements.forEach(({ unit, name }) => {
|
||||
if (!isMetricUnit(unit)) {
|
||||
errors.push(`${name}: "${unit}" is not a valid metric unit`);
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
valid: errors.length === 0,
|
||||
errors
|
||||
};
|
||||
}
|
||||
@@ -62,14 +62,39 @@ export async function getVersionStats(
|
||||
entityType: EntityType,
|
||||
entityId: string
|
||||
) {
|
||||
const versionTable = `${entityType}_versions`;
|
||||
const entityIdCol = `${entityType}_id`;
|
||||
|
||||
const { data, error } = await supabase
|
||||
.from(versionTable as any)
|
||||
.select('version_number, created_at, change_type', { count: 'exact' })
|
||||
.eq(entityIdCol, entityId)
|
||||
.order('version_number', { ascending: true });
|
||||
// Directly query the version table based on entity type
|
||||
// Use simpler type inference to avoid TypeScript deep instantiation issues
|
||||
let result;
|
||||
|
||||
if (entityType === 'park') {
|
||||
result = await supabase
|
||||
.from('park_versions')
|
||||
.select('version_number, created_at, change_type', { count: 'exact' })
|
||||
.eq('park_id', entityId)
|
||||
.order('version_number', { ascending: true });
|
||||
} else if (entityType === 'ride') {
|
||||
result = await supabase
|
||||
.from('ride_versions')
|
||||
.select('version_number, created_at, change_type', { count: 'exact' })
|
||||
.eq('ride_id', entityId)
|
||||
.order('version_number', { ascending: true });
|
||||
} else if (entityType === 'company') {
|
||||
result = await supabase
|
||||
.from('company_versions')
|
||||
.select('version_number, created_at, change_type', { count: 'exact' })
|
||||
.eq('company_id', entityId)
|
||||
.order('version_number', { ascending: true });
|
||||
} else {
|
||||
result = await supabase
|
||||
.from('ride_model_versions')
|
||||
.select('version_number, created_at, change_type', { count: 'exact' })
|
||||
.eq('ride_model_id', entityId)
|
||||
.order('version_number', { ascending: true });
|
||||
}
|
||||
|
||||
const { data, error } = result;
|
||||
|
||||
if (error || !data) {
|
||||
console.error('Failed to fetch version stats:', error);
|
||||
@@ -159,13 +184,30 @@ export async function hasVersions(
|
||||
entityType: EntityType,
|
||||
entityId: string
|
||||
): Promise<boolean> {
|
||||
const versionTable = `${entityType}_versions`;
|
||||
const entityIdCol = `${entityType}_id`;
|
||||
// Directly query the version table based on entity type with explicit column names
|
||||
let result;
|
||||
|
||||
const { count } = await supabase
|
||||
.from(versionTable as any)
|
||||
.select('*', { count: 'exact', head: true })
|
||||
.eq(entityIdCol, entityId);
|
||||
if (entityType === 'park') {
|
||||
result = await supabase
|
||||
.from('park_versions')
|
||||
.select('*', { count: 'exact', head: true })
|
||||
.eq('park_id', entityId);
|
||||
} else if (entityType === 'ride') {
|
||||
result = await supabase
|
||||
.from('ride_versions')
|
||||
.select('*', { count: 'exact', head: true })
|
||||
.eq('ride_id', entityId);
|
||||
} else if (entityType === 'company') {
|
||||
result = await supabase
|
||||
.from('company_versions')
|
||||
.select('*', { count: 'exact', head: true })
|
||||
.eq('company_id', entityId);
|
||||
} else {
|
||||
result = await supabase
|
||||
.from('ride_model_versions')
|
||||
.select('*', { count: 'exact', head: true })
|
||||
.eq('ride_model_id', entityId);
|
||||
}
|
||||
|
||||
return (count || 0) > 0;
|
||||
return (result.count || 0) > 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user