diff --git a/src/routes/api/catch-records/+server.ts b/src/routes/api/catch-records/+server.ts index 0ff29b3..280a949 100644 --- a/src/routes/api/catch-records/+server.ts +++ b/src/routes/api/catch-records/+server.ts @@ -32,21 +32,16 @@ export const GET = async (event: RequestEvent) => { export const PUT = async (event: RequestEvent) => { try { - console.log('PUT request received for catch-records'); - // Check if we can get a session first const { session, user } = await event.locals.safeGetSession(); - console.log('Session check:', { hasSession: !!session, hasUser: !!user, userId: user?.id }); const userId = await requireAuth(event); - console.log('Auth successful, userId:', userId); const data: Partial = await event.request.json(); // Get the user's session and set it on the Supabase client if (session) { await event.locals.supabase.auth.setSession(session); - console.log('Session set on Supabase client'); } // Ensure the userId is set to the authenticated user diff --git a/src/routes/api/seed/+server.ts b/src/routes/api/seed/+server.ts new file mode 100644 index 0000000..166bd39 --- /dev/null +++ b/src/routes/api/seed/+server.ts @@ -0,0 +1,98 @@ +import { json } from '@sveltejs/kit'; +import { createClient } from '@supabase/supabase-js'; +import { PUBLIC_SUPABASE_URL, PUBLIC_SUPABASE_ANON_KEY } from '$env/static/public'; +import dex from '$lib/helpers/pokedex.json'; +import RegionGameMappingJson from '$lib/helpers/region-game-mapping.json'; + +export const GET = async () => { + // Only allow seeding in development + if (process.env.NODE_ENV === 'production') { + return json({ message: 'Seeding not allowed in production' }, { status: 403 }); + } + + const supabase = createClient(PUBLIC_SUPABASE_URL, PUBLIC_SUPABASE_ANON_KEY); + + try { + // Seed Pokédex entries + const { data: existingEntries, error: countError } = await supabase + .from('pokedex_entries') + .select('id', { count: 'exact', head: true }); + + if (countError) { + throw new Error(`Failed to check existing entries: ${countError.message}`); + } + + // Only seed if table is empty + if (!existingEntries || existingEntries.length === 0) { + console.log('Seeding Pokédex entries...'); + + // Transform data to match database schema + const pokemonData = dex.map((pokemon: any) => ({ + pokedexNumber: pokemon.pokedexNumber, + pokemon: pokemon.pokemon, + form: pokemon.form || null, + canGigantamax: pokemon.canGigantamax || false, + regionToCatchIn: pokemon.regionToCatchIn || null, + gamesToCatchIn: pokemon.gamesToCatchIn || null, + regionToEvolveIn: pokemon.regionToEvolveIn || null, + evolutionInformation: pokemon.evolutionInformation || null, + catchInformation: pokemon.catchInformation || null, + boxPlacementFormsBox: pokemon.boxPlacementForms?.box || null, + boxPlacementFormsRow: pokemon.boxPlacementForms?.row || null, + boxPlacementFormsColumn: pokemon.boxPlacementForms?.column || null, + boxPlacementBox: pokemon.boxPlacement?.box || null, + boxPlacementRow: pokemon.boxPlacement?.row || null, + boxPlacementColumn: pokemon.boxPlacement?.column || null + })); + + const { error: insertError } = await supabase.from('pokedex_entries').insert(pokemonData); + + if (insertError) { + throw new Error(`Failed to seed Pokédex entries: ${insertError.message}`); + } + + console.log(`Seeded ${pokemonData.length} Pokédex entries`); + } else { + console.log('Pokédex entries already exist, skipping seeding'); + } + + // Seed region-game mappings (check if table exists first) + const { data: existingMappings, error: mappingCountError } = await supabase + .from('region_game_mappings') + .select('id', { count: 'exact', head: true }); + + if (!mappingCountError && (!existingMappings || existingMappings.length === 0)) { + console.log('Seeding region-game mappings...'); + + const { error: mappingInsertError } = await supabase + .from('region_game_mappings') + .insert(RegionGameMappingJson); + + if (mappingInsertError) { + throw new Error(`Failed to seed region-game mappings: ${mappingInsertError.message}`); + } + + console.log(`Seeded ${RegionGameMappingJson.length} region-game mappings`); + } else if (!mappingCountError) { + console.log('Region-game mappings already exist, skipping seeding'); + } + + return json({ + message: 'Seeding completed successfully', + seeded: { + pokemonEntries: !existingEntries || existingEntries.length === 0, + regionGameMappings: + !mappingCountError && (!existingMappings || existingMappings.length === 0) + } + }); + } catch (error) { + console.error('Seeding failed:', error); + return json( + { + message: 'Seeding failed', + error: error instanceof Error ? error.message : 'Unknown error' + }, + { status: 500 } + ); + } +}; diff --git a/tsconfig.json b/tsconfig.json index c0d3990..949222a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -17,12 +17,7 @@ "virtual:pwa-assets" ] }, - "include": [ - "src/**/*.ts", - "src/**/*.svelte", - "node_modules/vite-plugin-pwa/client.d.ts", - "node_modules/**/*.d.ts" - ], + "include": ["src/**/*.ts", "src/**/*.svelte", "node_modules/vite-plugin-pwa/client.d.ts"], "exclude": [".svelte-kit/**/*", "node_modules/**/*"] // Path aliases are handled by https://kit.svelte.dev/docs/configuration#alias //