mirror of
https://github.com/jcreek/LivingDexTracker.git
synced 2026-07-13 11:03:44 +00:00
data(*): Add final data changes
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
import { json } from '@sveltejs/kit';
|
||||
import type { RequestHandler } from './$types';
|
||||
import { listGameDexes } from '$lib/services/PokedexDexScopeService';
|
||||
|
||||
/**
|
||||
* GET /api/game-dexes
|
||||
* Returns game dexes grouped by game display name.
|
||||
*/
|
||||
export const GET: RequestHandler = async ({ locals }) => {
|
||||
try {
|
||||
const { gameDexes, gameOrder, games } = await listGameDexes(locals.supabase);
|
||||
return json({ gameDexes, gameOrder, games });
|
||||
} catch (err) {
|
||||
console.error('Unexpected error fetching game dexes:', err);
|
||||
return json({ error: 'Internal server error' }, { status: 500 });
|
||||
}
|
||||
};
|
||||
@@ -4,6 +4,7 @@ import PokedexRepository from '$lib/repositories/PokedexRepository';
|
||||
import { requireAuth } from '$lib/utils/auth';
|
||||
import type { RequestEvent } from '@sveltejs/kit';
|
||||
import { populatePokedexMappings } from '$lib/services/PokedexMappingService';
|
||||
import { resolveDexScopes, setPokedexDexScopes } from '$lib/services/PokedexDexScopeService';
|
||||
|
||||
// GET: List all pokedexes for user
|
||||
export const GET = async (event: RequestEvent) => {
|
||||
@@ -31,12 +32,20 @@ export const POST = async (event: RequestEvent) => {
|
||||
const data: Partial<Pokedex> = await event.request.json();
|
||||
requestedName = typeof data.name === 'string' ? data.name : undefined;
|
||||
data.userId = userId;
|
||||
const requestedDexScopes = Array.isArray(data.dexScopes) ? data.dexScopes : [];
|
||||
|
||||
const repo = new PokedexRepository(event.locals.supabase, userId);
|
||||
const pokedex = await repo.create(data);
|
||||
createdPokedexId = pokedex._id;
|
||||
createdPokedexName = pokedex.name;
|
||||
|
||||
const resolvedDexScopes = await resolveDexScopes(event.locals.supabase, {
|
||||
...pokedex,
|
||||
dexScopes: requestedDexScopes
|
||||
});
|
||||
await setPokedexDexScopes(event.locals.supabase, pokedex._id, resolvedDexScopes);
|
||||
pokedex.dexScopes = resolvedDexScopes;
|
||||
|
||||
// Populate pokedex_entries_mapping table with expected entries
|
||||
try {
|
||||
await populatePokedexMappings(event.locals.supabase, pokedex._id, pokedex);
|
||||
|
||||
@@ -4,6 +4,7 @@ import PokedexRepository from '$lib/repositories/PokedexRepository';
|
||||
import { requireAuth } from '$lib/utils/auth';
|
||||
import type { RequestEvent } from '@sveltejs/kit';
|
||||
import { recalculatePokedexMappings } from '$lib/services/PokedexMappingService';
|
||||
import { resolveDexScopes, setPokedexDexScopes } from '$lib/services/PokedexDexScopeService';
|
||||
|
||||
// GET: Get single pokedex by ID
|
||||
export const GET = async (event: RequestEvent) => {
|
||||
@@ -71,12 +72,35 @@ export const PUT = async (event: RequestEvent) => {
|
||||
return json({ error: 'Pokedex not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
const dexScopesProvided = Object.prototype.hasOwnProperty.call(data, 'dexScopes');
|
||||
const requestedDexScopes =
|
||||
dexScopesProvided && Array.isArray(data.dexScopes)
|
||||
? data.dexScopes
|
||||
: existingPokedex.dexScopes || [];
|
||||
|
||||
const resolvedDexScopes = await resolveDexScopes(event.locals.supabase, {
|
||||
...pokedex,
|
||||
dexScopes: requestedDexScopes
|
||||
});
|
||||
|
||||
const existingDexScopes = new Set(existingPokedex.dexScopes || []);
|
||||
const nextDexScopes = new Set(resolvedDexScopes);
|
||||
const dexScopesChanged =
|
||||
existingDexScopes.size !== nextDexScopes.size ||
|
||||
[...existingDexScopes].some((dexId) => !nextDexScopes.has(dexId));
|
||||
|
||||
if (dexScopesChanged) {
|
||||
await setPokedexDexScopes(event.locals.supabase, id, resolvedDexScopes);
|
||||
pokedex.dexScopes = resolvedDexScopes;
|
||||
}
|
||||
|
||||
// Recalculate pokedex_entries_mapping if configuration changed
|
||||
// Only recalculate if isFormDex or gameScope actually changed (these are the only fields that affect mapping)
|
||||
// Only recalculate if isFormDex, gameScope, or dexScopes changed
|
||||
// Compare the persisted result (pokedex) to existingPokedex to catch changes that repo.update may normalize or default
|
||||
const configChanged =
|
||||
pokedex.isFormDex !== existingPokedex.isFormDex ||
|
||||
pokedex.gameScope !== existingPokedex.gameScope;
|
||||
pokedex.gameScope !== existingPokedex.gameScope ||
|
||||
dexScopesChanged;
|
||||
|
||||
if (configChanged) {
|
||||
await recalculatePokedexMappings(event.locals.supabase, id, pokedex);
|
||||
|
||||
@@ -3,6 +3,7 @@ import CombinedDataRepository from '$lib/repositories/CombinedDataRepository';
|
||||
import PokedexRepository from '$lib/repositories/PokedexRepository';
|
||||
import { getOptionalUserId } from '$lib/utils/auth';
|
||||
import type { RequestEvent } from '@sveltejs/kit';
|
||||
import { resolveDexScopes } from '$lib/services/PokedexDexScopeService';
|
||||
|
||||
// GET: Get combined data (pokédex entries + catch records) for specific pokédex
|
||||
export const GET = async (event: RequestEvent) => {
|
||||
@@ -39,6 +40,7 @@ export const GET = async (event: RequestEvent) => {
|
||||
|
||||
// Use pokédex's gameScope as default filter if no manual game filter is set
|
||||
const effectiveGame = game || pokedex.gameScope || '';
|
||||
const dexScopes = await resolveDexScopes(event.locals.supabase, pokedex);
|
||||
|
||||
const repo = new CombinedDataRepository(event.locals.supabase, userId, pokedexId);
|
||||
|
||||
@@ -49,11 +51,12 @@ export const GET = async (event: RequestEvent) => {
|
||||
limit,
|
||||
enableForms,
|
||||
region,
|
||||
effectiveGame
|
||||
effectiveGame,
|
||||
dexScopes
|
||||
);
|
||||
|
||||
// Get total count for pagination
|
||||
const totalCount = await repo.countCombinedData(enableForms, region, effectiveGame);
|
||||
const totalCount = await repo.countCombinedData(enableForms, region, effectiveGame, dexScopes);
|
||||
const totalPages = Math.ceil(totalCount / limit);
|
||||
|
||||
return json({
|
||||
|
||||
Reference in New Issue
Block a user