mirror of
https://github.com/jcreek/LivingDexTracker.git
synced 2026-07-13 11:03:44 +00:00
fix(#64): Address PR comments
This commit is contained in:
@@ -3,6 +3,80 @@ import type { Pokedex } from '$lib/models/Pokedex';
|
||||
import PokedexRepository from '$lib/repositories/PokedexRepository';
|
||||
import { requireAuth } from '$lib/utils/auth';
|
||||
import type { RequestEvent } from '@sveltejs/kit';
|
||||
import type { SupabaseClient } from '@supabase/supabase-js';
|
||||
|
||||
/**
|
||||
* Calculate expected pokedex entries based on pokedex configuration
|
||||
* Applies the same filtering logic as CombinedDataRepository
|
||||
*/
|
||||
async function calculateExpectedEntries(
|
||||
supabase: SupabaseClient,
|
||||
pokedex: Pokedex
|
||||
): Promise<number[]> {
|
||||
let query = supabase.from('pokedex_entries').select('id');
|
||||
|
||||
// Apply form filter: if isFormDex is false, only include base forms
|
||||
// Base forms have form IS NULL, except Unown which has no base form (use 'A')
|
||||
if (!pokedex.isFormDex) {
|
||||
query = query.or('form.is.null,and(pokemon.eq.Unown,form.eq.A)');
|
||||
}
|
||||
|
||||
// Apply region filter: if gameScope is specified, filter by region
|
||||
if (pokedex.gameScope) {
|
||||
// Determine region from gameScope using region_game_mappings
|
||||
const { data: regionData } = await supabase
|
||||
.from('region_game_mappings')
|
||||
.select('region')
|
||||
.eq('game', pokedex.gameScope)
|
||||
.single();
|
||||
|
||||
if (regionData?.region) {
|
||||
query = query.eq('regionToCatchIn', regionData.region);
|
||||
}
|
||||
}
|
||||
|
||||
// Apply game filter: if gameScope is specified, filter by gamesToCatchIn array
|
||||
if (pokedex.gameScope) {
|
||||
query = query.contains('gamesToCatchIn', [pokedex.gameScope]);
|
||||
}
|
||||
|
||||
const { data, error } = await query;
|
||||
|
||||
if (error) {
|
||||
console.error('Error calculating expected entries:', error);
|
||||
return [];
|
||||
}
|
||||
|
||||
return data?.map((entry) => entry.id) || [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Populate pokedex_entries_mapping table for a pokedex
|
||||
*/
|
||||
async function populatePokedexMappings(
|
||||
supabase: SupabaseClient,
|
||||
pokedexId: string,
|
||||
pokedex: Pokedex
|
||||
): Promise<void> {
|
||||
const expectedEntryIds = await calculateExpectedEntries(supabase, pokedex);
|
||||
|
||||
if (expectedEntryIds.length === 0) {
|
||||
console.warn(`No expected entries calculated for pokedex ${pokedexId}`);
|
||||
return;
|
||||
}
|
||||
|
||||
const mappings = expectedEntryIds.map((pokedexEntryId) => ({
|
||||
pokedexId,
|
||||
pokedexEntryId
|
||||
}));
|
||||
|
||||
const { error } = await supabase.from('pokedex_entries_mapping').insert(mappings);
|
||||
|
||||
if (error) {
|
||||
console.error('Error populating pokedex mappings:', error);
|
||||
throw new Error(`Failed to populate pokedex mappings: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
// GET: List all pokedexes for user
|
||||
export const GET = async (event: RequestEvent) => {
|
||||
@@ -31,6 +105,10 @@ export const POST = async (event: RequestEvent) => {
|
||||
|
||||
const repo = new PokedexRepository(event.locals.supabase, userId);
|
||||
const pokedex = await repo.create(data);
|
||||
|
||||
// Populate pokedex_entries_mapping table with expected entries
|
||||
await populatePokedexMappings(event.locals.supabase, pokedex._id, pokedex);
|
||||
|
||||
return json(pokedex);
|
||||
} catch (err) {
|
||||
console.error('Error in POST /api/pokedexes:', err);
|
||||
|
||||
@@ -3,6 +3,103 @@ import type { Pokedex } from '$lib/models/Pokedex';
|
||||
import PokedexRepository from '$lib/repositories/PokedexRepository';
|
||||
import { requireAuth } from '$lib/utils/auth';
|
||||
import type { RequestEvent } from '@sveltejs/kit';
|
||||
import type { SupabaseClient } from '@supabase/supabase-js';
|
||||
|
||||
/**
|
||||
* Calculate expected pokedex entries based on pokedex configuration
|
||||
* Applies the same filtering logic as CombinedDataRepository
|
||||
*/
|
||||
async function calculateExpectedEntries(
|
||||
supabase: SupabaseClient,
|
||||
pokedex: Pokedex
|
||||
): Promise<number[]> {
|
||||
let query = supabase.from('pokedex_entries').select('id');
|
||||
|
||||
// Apply form filter: if isFormDex is false, only include base forms
|
||||
// Base forms have form IS NULL, except Unown which has no base form (use 'A')
|
||||
if (!pokedex.isFormDex) {
|
||||
query = query.or('form.is.null,and(pokemon.eq.Unown,form.eq.A)');
|
||||
}
|
||||
|
||||
// Apply region filter: if gameScope is specified, filter by region
|
||||
if (pokedex.gameScope) {
|
||||
// Determine region from gameScope using region_game_mappings
|
||||
const { data: regionData } = await supabase
|
||||
.from('region_game_mappings')
|
||||
.select('region')
|
||||
.eq('game', pokedex.gameScope)
|
||||
.single();
|
||||
|
||||
if (regionData?.region) {
|
||||
query = query.eq('regionToCatchIn', regionData.region);
|
||||
}
|
||||
}
|
||||
|
||||
// Apply game filter: if gameScope is specified, filter by gamesToCatchIn array
|
||||
if (pokedex.gameScope) {
|
||||
query = query.contains('gamesToCatchIn', [pokedex.gameScope]);
|
||||
}
|
||||
|
||||
const { data, error } = await query;
|
||||
|
||||
if (error) {
|
||||
console.error('Error calculating expected entries:', error);
|
||||
return [];
|
||||
}
|
||||
|
||||
return data?.map((entry) => entry.id) || [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Populate pokedex_entries_mapping table for a pokedex
|
||||
*/
|
||||
async function populatePokedexMappings(
|
||||
supabase: SupabaseClient,
|
||||
pokedexId: string,
|
||||
pokedex: Pokedex
|
||||
): Promise<void> {
|
||||
const expectedEntryIds = await calculateExpectedEntries(supabase, pokedex);
|
||||
|
||||
if (expectedEntryIds.length === 0) {
|
||||
console.warn(`No expected entries calculated for pokedex ${pokedexId}`);
|
||||
return;
|
||||
}
|
||||
|
||||
const mappings = expectedEntryIds.map((pokedexEntryId) => ({
|
||||
pokedexId,
|
||||
pokedexEntryId
|
||||
}));
|
||||
|
||||
const { error } = await supabase.from('pokedex_entries_mapping').insert(mappings);
|
||||
|
||||
if (error) {
|
||||
console.error('Error populating pokedex mappings:', error);
|
||||
throw new Error(`Failed to populate pokedex mappings: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Recalculate pokedex_entries_mapping table for a pokedex (used on update)
|
||||
*/
|
||||
async function recalculatePokedexMappings(
|
||||
supabase: SupabaseClient,
|
||||
pokedexId: string,
|
||||
pokedex: Pokedex
|
||||
): Promise<void> {
|
||||
// Delete old mappings
|
||||
const { error: deleteError } = await supabase
|
||||
.from('pokedex_entries_mapping')
|
||||
.delete()
|
||||
.eq('pokedexId', pokedexId);
|
||||
|
||||
if (deleteError) {
|
||||
console.error('Error deleting old pokedex mappings:', deleteError);
|
||||
throw new Error(`Failed to delete old pokedex mappings: ${deleteError.message}`);
|
||||
}
|
||||
|
||||
// Populate new mappings
|
||||
await populatePokedexMappings(supabase, pokedexId, pokedex);
|
||||
}
|
||||
|
||||
// GET: Get single pokedex by ID
|
||||
export const GET = async (event: RequestEvent) => {
|
||||
@@ -63,6 +160,19 @@ export const PUT = async (event: RequestEvent) => {
|
||||
return json({ error: 'Pokedex not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
// Recalculate pokedex_entries_mapping if configuration changed
|
||||
// Only recalculate if type flags or gameScope changed
|
||||
const configChanged =
|
||||
data.isFormDex !== undefined ||
|
||||
data.gameScope !== undefined ||
|
||||
(data.isLivingDex !== undefined && pokedex.isLivingDex !== data.isLivingDex) ||
|
||||
(data.isShinyDex !== undefined && pokedex.isShinyDex !== data.isShinyDex) ||
|
||||
(data.isOriginDex !== undefined && pokedex.isOriginDex !== data.isOriginDex);
|
||||
|
||||
if (configChanged) {
|
||||
await recalculatePokedexMappings(event.locals.supabase, id, { ...pokedex, ...data });
|
||||
}
|
||||
|
||||
return json(pokedex);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
|
||||
Reference in New Issue
Block a user