mirror of
https://github.com/jcreek/LivingDexTracker.git
synced 2026-07-13 02:53:45 +00:00
feat(*): Change how data is set up
This commit is contained in:
@@ -15,8 +15,7 @@ class CatchRecordRepository {
|
||||
return {
|
||||
_id: record.id,
|
||||
userId: record.userId,
|
||||
pokedexEntryId: record.pokedexEntryId.toString(),
|
||||
pokedexId: record.pokedexId,
|
||||
pokemonId: record.pokemonId.toString(),
|
||||
haveToEvolve: record.haveToEvolve,
|
||||
caught: record.caught,
|
||||
inHome: record.inHome,
|
||||
@@ -28,12 +27,12 @@ class CatchRecordRepository {
|
||||
// Transform frontend data to database format (minimal transformation)
|
||||
private transformToDatabase(data: Partial<CatchRecord>): Partial<CatchRecordDB> {
|
||||
const dbData: Partial<CatchRecordDB> = {};
|
||||
if (data.pokedexEntryId !== undefined && data.pokedexEntryId !== null) {
|
||||
const numericId = Number(data.pokedexEntryId);
|
||||
if (data.pokemonId !== undefined && data.pokemonId !== null) {
|
||||
const numericId = Number(data.pokemonId);
|
||||
if (isNaN(numericId)) {
|
||||
throw new Error(`Invalid pokedexEntryId: ${data.pokedexEntryId}`);
|
||||
throw new Error(`Invalid pokemonId: ${data.pokemonId}`);
|
||||
}
|
||||
dbData.pokedexEntryId = numericId;
|
||||
dbData.pokemonId = numericId;
|
||||
}
|
||||
if (data.pokedexId !== undefined) dbData.pokedexId = data.pokedexId;
|
||||
if (data.haveToEvolve !== undefined) dbData.haveToEvolve = data.haveToEvolve;
|
||||
@@ -53,14 +52,14 @@ class CatchRecordRepository {
|
||||
* catch the error and fall back to per-record upserts.
|
||||
*/
|
||||
async bulkUpsert(
|
||||
records: Array<PartialExcept<CatchRecord, 'pokedexEntryId'>>
|
||||
records: Array<PartialExcept<CatchRecord, 'pokemonId'>>
|
||||
): Promise<CatchRecord[]> {
|
||||
if (records.length === 0) return [];
|
||||
|
||||
const dbRows: Partial<CatchRecordDB>[] = records.map((r, index) => {
|
||||
if (r.pokedexEntryId === undefined || r.pokedexEntryId === null || r.pokedexEntryId === '') {
|
||||
if (r.pokemonId === undefined || r.pokemonId === null || r.pokemonId === '') {
|
||||
throw new Error(
|
||||
`CatchRecordRepository.bulkUpsert: record at index ${index} is missing required field "pokedexEntryId"`
|
||||
`CatchRecordRepository.bulkUpsert: record at index ${index} is missing required field "pokemonId"`
|
||||
);
|
||||
}
|
||||
|
||||
@@ -81,9 +80,9 @@ class CatchRecordRepository {
|
||||
mapped.userId = this.userId;
|
||||
mapped.pokedexId = this.pokedexId;
|
||||
|
||||
if (mapped.pokedexEntryId === undefined || mapped.pokedexEntryId === null) {
|
||||
if (mapped.pokemonId === undefined || mapped.pokemonId === null) {
|
||||
throw new Error(
|
||||
`CatchRecordRepository.bulkUpsert: record at index ${index} produced no pokedexEntryId after transform`
|
||||
`CatchRecordRepository.bulkUpsert: record at index ${index} produced no pokemonId after transform`
|
||||
);
|
||||
}
|
||||
|
||||
@@ -93,7 +92,7 @@ class CatchRecordRepository {
|
||||
const { data: result, error } = await this.supabase
|
||||
.from('catch_records')
|
||||
.upsert(dbRows, {
|
||||
onConflict: '"userId","pokedexId","pokedexEntryId"'
|
||||
onConflict: '"userId","pokedexId","pokemonId"'
|
||||
})
|
||||
.select();
|
||||
|
||||
@@ -198,13 +197,9 @@ class CatchRecordRepository {
|
||||
async upsert(data: Partial<CatchRecord>): Promise<CatchRecord | null> {
|
||||
if (data._id) {
|
||||
return this.update(data._id, data);
|
||||
} else if (data.pokedexEntryId) {
|
||||
} else if (data.pokemonId) {
|
||||
// Try to find existing record for this user, pokemon, and pokedex
|
||||
const existing = await this.findByUserAndPokemon(
|
||||
this.userId,
|
||||
data.pokedexEntryId,
|
||||
this.pokedexId
|
||||
);
|
||||
const existing = await this.findByUserAndPokemon(this.userId, data.pokemonId, this.pokedexId);
|
||||
if (existing) {
|
||||
return this.update(existing._id, data);
|
||||
} else {
|
||||
@@ -216,10 +211,10 @@ class CatchRecordRepository {
|
||||
|
||||
async findByUserAndPokemon(
|
||||
userId: string,
|
||||
pokedexEntryId: string,
|
||||
pokemonId: string,
|
||||
pokedexId: string
|
||||
): Promise<CatchRecord | null> {
|
||||
const numericId = Number(pokedexEntryId);
|
||||
const numericId = Number(pokemonId);
|
||||
if (isNaN(numericId)) {
|
||||
return null;
|
||||
}
|
||||
@@ -227,7 +222,7 @@ class CatchRecordRepository {
|
||||
.from('catch_records')
|
||||
.select('*')
|
||||
.eq('userId', userId)
|
||||
.eq('pokedexEntryId', numericId)
|
||||
.eq('pokemonId', numericId)
|
||||
.eq('pokedexId', pokedexId)
|
||||
.single();
|
||||
|
||||
|
||||
@@ -31,7 +31,8 @@ class CombinedDataRepository {
|
||||
return {
|
||||
_id: record.id,
|
||||
userId: record.userId,
|
||||
pokedexEntryId: record.pokedexEntryId.toString(),
|
||||
// Backwards-compatible field name; DB column is pokemonId
|
||||
pokedexEntryId: record.pokemonId.toString(),
|
||||
pokedexId: record.pokedexId,
|
||||
haveToEvolve: record.haveToEvolve,
|
||||
caught: record.caught,
|
||||
@@ -87,7 +88,7 @@ class CombinedDataRepository {
|
||||
.select('*')
|
||||
.eq('userId', userId)
|
||||
.eq('pokedexId', this.pokedexId)
|
||||
.in('pokedexEntryId', entryIds);
|
||||
.in('pokemonId', entryIds);
|
||||
|
||||
if (!recordsError && records) {
|
||||
catchRecords = records;
|
||||
@@ -96,8 +97,7 @@ class CombinedDataRepository {
|
||||
|
||||
// Combine the data exactly like master branch
|
||||
const combinedData = entries.map((entry) => {
|
||||
const userCatchRecord =
|
||||
catchRecords.find((record) => record.pokedexEntryId === entry.id) || null;
|
||||
const userCatchRecord = catchRecords.find((record) => record.pokemonId === entry.id) || null;
|
||||
|
||||
const transformedEntry = this.transformPokedexEntry(entry);
|
||||
const transformedCatchRecord = userCatchRecord
|
||||
@@ -165,7 +165,7 @@ class CombinedDataRepository {
|
||||
.select('*')
|
||||
.eq('userId', userId)
|
||||
.eq('pokedexId', this.pokedexId)
|
||||
.in('pokedexEntryId', entryIds);
|
||||
.in('pokemonId', entryIds);
|
||||
|
||||
if (!recordsError && records) {
|
||||
catchRecords = records;
|
||||
@@ -174,8 +174,7 @@ class CombinedDataRepository {
|
||||
|
||||
// Combine the data exactly like master branch
|
||||
const combinedData = entries.map((entry) => {
|
||||
const userCatchRecord =
|
||||
catchRecords.find((record) => record.pokedexEntryId === entry.id) || null;
|
||||
const userCatchRecord = catchRecords.find((record) => record.pokemonId === entry.id) || null;
|
||||
|
||||
const transformedEntry = this.transformPokedexEntry(entry);
|
||||
const transformedCatchRecord = userCatchRecord
|
||||
|
||||
Reference in New Issue
Block a user