fix(#64): Address PR feedback

This commit is contained in:
Josh Creek
2026-01-10 19:25:29 +00:00
parent 5657c2b2ab
commit 75f7161b7a
3 changed files with 31 additions and 5 deletions
+25 -1
View File
@@ -24,6 +24,8 @@ export const GET = async (event: RequestEvent) => {
// POST: Create new pokedex // POST: Create new pokedex
export const POST = async (event: RequestEvent) => { export const POST = async (event: RequestEvent) => {
let requestedName: string | undefined; let requestedName: string | undefined;
let createdPokedexId: string | undefined;
let createdPokedexName: string | undefined;
try { try {
const userId = await requireAuth(event); const userId = await requireAuth(event);
const data: Partial<Pokedex> = await event.request.json(); const data: Partial<Pokedex> = await event.request.json();
@@ -32,9 +34,31 @@ export const POST = async (event: RequestEvent) => {
const repo = new PokedexRepository(event.locals.supabase, userId); const repo = new PokedexRepository(event.locals.supabase, userId);
const pokedex = await repo.create(data); const pokedex = await repo.create(data);
createdPokedexId = pokedex._id;
createdPokedexName = pokedex.name;
// Populate pokedex_entries_mapping table with expected entries // Populate pokedex_entries_mapping table with expected entries
await populatePokedexMappings(event.locals.supabase, pokedex._id, pokedex); try {
await populatePokedexMappings(event.locals.supabase, pokedex._id, pokedex);
} catch (mappingError) {
// Rollback: delete the partially-initialised pokedex to prevent dangling records
console.error(
`Failed to populate pokedex mappings for pokedex id="${createdPokedexId}" name="${createdPokedexName}":`,
mappingError
);
try {
await repo.delete(pokedex._id);
console.log(
`Successfully rolled back pokedex id="${createdPokedexId}" name="${createdPokedexName}"`
);
} catch (deleteError) {
console.error(
`Failed to rollback pokedex id="${createdPokedexId}" name="${createdPokedexName}":`,
deleteError
);
}
return json({ error: 'Failed to initialise Pokédex. Please try again.' }, { status: 500 });
}
return json(pokedex); return json(pokedex);
} catch (err) { } catch (err) {
+3 -2
View File
@@ -73,9 +73,10 @@ export const PUT = async (event: RequestEvent) => {
// Recalculate pokedex_entries_mapping if configuration changed // 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 or gameScope actually changed (these are the only fields that affect mapping)
// Compare the persisted result (pokedex) to existingPokedex to catch changes that repo.update may normalize or default
const configChanged = const configChanged =
(data.isFormDex !== undefined && existingPokedex.isFormDex !== data.isFormDex) || pokedex.isFormDex !== existingPokedex.isFormDex ||
(data.gameScope !== undefined && existingPokedex.gameScope !== data.gameScope); pokedex.gameScope !== existingPokedex.gameScope;
if (configChanged) { if (configChanged) {
await recalculatePokedexMappings(event.locals.supabase, id, pokedex); await recalculatePokedexMappings(event.locals.supabase, id, pokedex);
@@ -28,7 +28,7 @@ BEGIN
RAISE EXCEPTION 'pokedex not found'; RAISE EXCEPTION 'pokedex not found';
END IF; END IF;
IF v_user_id != auth.uid() THEN IF auth.uid() IS NULL OR v_user_id IS DISTINCT FROM auth.uid() THEN
RAISE EXCEPTION 'not authorized: you do not own this pokedex'; RAISE EXCEPTION 'not authorized: you do not own this pokedex';
END IF; END IF;
@@ -38,6 +38,7 @@ BEGIN
WHERE "pokedexId" = p_pokedex_id; WHERE "pokedexId" = p_pokedex_id;
INSERT INTO pokedex_entries_mapping ("pokedexId", "pokedexEntryId") INSERT INTO pokedex_entries_mapping ("pokedexId", "pokedexEntryId")
SELECT p_pokedex_id, unnest(p_entry_ids); SELECT p_pokedex_id, unnest(p_entry_ids)
ON CONFLICT ("pokedexId", "pokedexEntryId") DO NOTHING;
END; END;
$$; $$;